Exemplo n.º 1
0
def convert_predictions_json_to_sorted_ucdays(predictions):
    ucdays = []
    days = predictions.keys()
    sorted_days = sorted(days)
    for day in sorted_days:
        ucday = UCDay(day)
        for i in range(0, 24):
            hour_json = predictions[day][i]
            ucday.hours[i] = UCHour.buildv2(hour_json)
        ucdays.append(ucday)
    return ucdays
Exemplo n.º 2
0
def convert_records_map_to_list(records):
    sorted_records_list = []

    sorted_indices = sorted(records)
    first_date_str = sorted_indices[0]
    last_date_str = sorted_indices[-1]

    # days_str contains all the days from first date to last day
    days_str = []
    days_str.append(first_date_str)

    _date = get_date(first_date_str)
    last_date = get_date(last_date_str)
    while (True):
        if (_date >= last_date):
            break
        _date += datetime.timedelta(days=1)
        _date_str = _date.strftime('%Y-%m-%d')
        days_str.append(_date_str)

    for day_str in days_str:
        if day_str in sorted_indices:
            sorted_records_list.append(records[day_str])
        else:
            sorted_records_list.append(UCDay(day_str))

    return sorted_records_list
Exemplo n.º 3
0
    def dl_forecast(self, serving_url, model_stats, ucdoc, ucday_list):

        records_hour_price_map = {}
        day_list = []
        duration = model_stats['model']['duration']
        predict_window = model_stats['model']['predict_window']

        # ucday_list is sorted from past to present [2018-01-01, 2018-01-02, ...]
        empthy_list = []
        if len(ucday_list) >= duration:
            ucday_list = ucday_list[len(ucday_list)-duration:]
        else:
            empthy_list = [0 for _ in range(0, duration-len(ucday_list))]
        for ucday in ucday_list:
            day_list.append(ucday.date)
            for hour in range(24):
                for price_cat in range(4):
                    key = (hour, price_cat)
                    if key not in records_hour_price_map:
                        records_hour_price_map[key] = empthy_list[:]
                    records_hour_price_map[key].append(
                        ucday.hours[hour].histogram_value(price_cat))

        records_hour_price_list = []
        for key, value in records_hour_price_map.items():
            # Replace 0 in the value with its median
            median = statistics.median(value)
            value = [i if i!=0 else median for i in value]
            records_hour_price_list.append((value, key[0], key[1]))
 
        # URL = "http://10.193.217.105:8501/v1/models/faezeh:predict"
        response, predict_day_list = predict(serving_url, model_stats=model_stats, day_list=day_list, uckey=ucdoc.uckey, age=ucdoc.a, si=ucdoc.si, network=ucdoc.t, gender=ucdoc.g,
                           media=ucdoc.m, ip_location=ucdoc.ipl, records_hour_price_list=records_hour_price_list)
 
        ucdayMap = {}
        date = day_list[-1]
        prediction_records = response
        i = -1
        for date in predict_day_list:
            i += 1
            ucday = UCDay(date)
            j = -1
            for _, hour, price_cat in records_hour_price_list:
                j += 1
                count = prediction_records[j][i]
                if math.isnan(count):
                    count = 0
                if price_cat == 0:
                    ucday.hours[hour].h0 = count
                elif price_cat == 1:
                    ucday.hours[hour].h1 = count
                if price_cat == 2:
                    ucday.hours[hour].h2 = count
                if price_cat == 3:
                    ucday.hours[hour].h3 = count
            ucdayMap[date] = ucday.hours

        return ucdayMap
Exemplo n.º 4
0
def convert_day_hour_counts_to_ucdoc(uckey, day_hour_counts):
    ucdoc = UCDoc.build_from_concat_string(uckey)
    for day_map in day_hour_counts:
        if len(day_map) > 0:
            # There is only one key here.
            day = next(iter(day_map.keys()))
            hour_count_map = day_map[day]
            for hour_map in hour_count_map:
                if len(hour_map) > 0:
                    hour = next(iter(hour_map.keys()))
                    h = hour_map[hour]
                    records = ucdoc.records
                    if day not in records:
                        records[day] = UCDay(str(day))
                    dict = build_dict_from_counts(h, hour)
                    uchour = UCHour.buildv1(dict)
                    records[day].hours[hour] = uchour
    return ucdoc
Exemplo n.º 5
0
    def build_from_es_doc(dict_ucdoc):
        ucdoc = UCDoc(None)
        ucdoc.uckey = dict_ucdoc.get('uckey')
        ucdoc.m = dict_ucdoc.get('m')
        ucdoc.si = dict_ucdoc.get('si')
        ucdoc.t = dict_ucdoc.get('t')
        ucdoc.a = dict_ucdoc.get('a')
        ucdoc.g = dict_ucdoc.get('g')
        #ucdoc.dpc = dict_ucdoc['dpc']
        ucdoc.pm = dict_ucdoc.get('pm')
        ucdoc.r = dict_ucdoc.get('r')
        ucdoc.ipl = dict_ucdoc.get('ip_city_code')
        ucdoc.records = {}
        # ucdoc.lastUpdate = None

        if dict_ucdoc.get('records') is not None:
            for date, ucday_doc in dict_ucdoc['records'].items():
                ucday = UCDay.build(ucday_doc)
                ucdoc.records[date] = ucday

        return ucdoc