Exemple #1
0
def _write_monthly_result_sheet(eparam, operating_conditions, wb, results, **kwargs):
    """
    :type eparam: pyticas_tetres.ttypes.EstimationRequestInfo
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ftypes.ExtFilterGroup]
    :type wb:xlsxwriter.Workbook
    :type results: list[(list[dict], list[[int, int])]
    :type dates: list[datetime.date]
    """
    missing_value = kwargs.get('missing_value', MISSING_VALUE)
    fields, col_names, sheet_names = report_helper.get_indice_names_for_byindices()
    months = util.months(eparam.start_date, eparam.end_date)
    for sidx, sheet_name in enumerate(sheet_names):
        ws = wb.add_worksheet('monthly %s' % sheet_name)
        field_name = fields[sidx]
        list_by_oc, months_list, oc_names = _extract_indices(results, operating_conditions, field_name, missing_value)
        ws.write_row(0, 0, ['Month'] + oc_names)
        ws.write_column(1, 0, ['%04d-%02d' % (month[0], month[1]) for month in months])
        col = 1
        for oidx, list_for_a_oc in enumerate(list_by_oc):
            data = []
            for (y, m) in months:
                tidx = -1
                for midx, (y2, m2) in enumerate(months_list[oidx]):
                    if y == y2 and m == m2:
                        tidx = midx
                        break
                if tidx >= 0:
                    data.append(list_for_a_oc[tidx] if list_for_a_oc[tidx] else missing_value)
                else:
                    data.append(missing_value)
            ws.write_column(1, col, data)
            col += 1
Exemple #2
0
def _prepare_yearly_monthly_daily_data(eparam, operating_conditions):
    """
    :type eparam: pyticas_tetres.ttypes.EstimationRequestInfo
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ftypes.ExtFilterGroup]
    """
    sdate, edate = util.get_date(eparam.start_date), util.get_date(eparam.end_date)
    all_years = util.years(sdate, edate)
    all_months = util.months(sdate, edate)
    all_dates = util.dates(sdate, edate, weekdays=eparam.weekdays.get_weekdays(), except_holiday=eparam.except_holiday)
    all_times = [ dt.time() for (h, m, dt) in _time_of_day_generator(eparam) ]

    for oc in operating_conditions:
        oc.all_years = all_years
        oc.all_months = all_months
        oc.all_dates = all_dates
        oc.all_times = all_times
        if not oc.whole_data:
            continue
        prev_day = util.get_datetime(oc.whole_data[0].tti.time).date()
        years, results, one_year_data, one_month_data, one_day_data = [], [], [], [], []

        for ext_data in oc.whole_data:
            cur_day = util.get_datetime(ext_data.tti.time).date()
            if cur_day.year != prev_day.year:
                oc.yearly_data.append((prev_day.year, one_year_data))
                one_year_data = []
            if cur_day.month != prev_day.month:
                oc.monthly_data.append(([prev_day.year, prev_day.month], one_month_data))
                one_month_data = []
            if cur_day != prev_day:
                oc.daily_data.append((prev_day, one_day_data))
                one_day_data = []

            prev_day = cur_day
            one_year_data.append(ext_data)
            one_month_data.append(ext_data)
            one_day_data.append(ext_data)

        if one_year_data:
            oc.yearly_data.append((prev_day.year, one_year_data))
        if one_month_data:
            oc.monthly_data.append(([prev_day.year, prev_day.month], one_month_data))
        if one_day_data:
            oc.daily_data.append((prev_day, one_day_data))
Exemple #3
0
def _write_monthly_index_variations_by_oc(eparam, operating_conditions,
                                          results_monthly, output_dir,
                                          **kwargs):
    """
    :type eparam: pyticas_tetres.ttypes.EstimationRequestInfo
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ftypes.ExtFilterGroup]
    :type results_monthly: list[(list[dict], list[str])]
    :type output_dir: str
    """
    monthly_output_dir = os.path.join(output_dir, OUTPUT_DIR_NAME,
                                      'monthly-variations')

    if not os.path.exists(monthly_output_dir):
        os.makedirs(monthly_output_dir)

    marker, color = kwargs.get('marker', 'o'), kwargs.get('color', 'b')
    chart_type = kwargs.get('chart_type', 'bar')

    all_months = util.months(eparam.start_date, eparam.end_date)
    xaxis = np.array(list(range(len(all_months))))
    _xaxis, _xlabels = _get_xaxis(
        ['%04d-%02d' % (m[0], m[1]) for m in all_months])
    _xaxis = np.array(_xaxis)
    sw = 0.5

    for gidx, oc in enumerate(operating_conditions):
        results, months = results_monthly[gidx]
        if not results:
            continue

        tts, ttrs, ttis, bi95s, bi85s, pt95s, pt85s = _get_indices(
            results, months, all_months, 0)

        indices = [ttis, bi95s, bi85s, pt95s, pt85s]
        default_maxs = [
            DEFAULT_TTI_MAX, DEFAULT_BI_MAX, DEFAULT_BI_MAX, DEFAULT_PTI_MAX,
            DEFAULT_PTI_MAX
        ]
        names = [
            'Travel Time Index', 'Buffer Index (95th-%ile)',
            'Buffer Index (85th-%ile)', 'Planning Time Index (95th-%ile)',
            'Planning Time Index (85th-%ile)'
        ]

        for idx, (indice, indice_name) in enumerate(zip(indices, names)):
            if not indice:
                continue
            fig = plt.figure(figsize=(16, 8), facecolor='white')
            ax1 = plt.subplot(111)

            if chart_type == 'bar':
                ax1.bar(xaxis, indice, color=color, label=indice_name)
                plt.xticks(_xaxis + 0.5, _xlabels, rotation=70)
            else:
                ax1.plot(xaxis,
                         indice,
                         marker=marker,
                         c=color,
                         label=indice_name)
                plt.xticks(_xaxis, _xlabels, rotation=70)

            plt.grid()
            plt.xlim(xmin=0)
            ax1.legend(prop={'size': 12})
            plt.title('Monthly %s Variations [%s]' % (indice_name, oc.label))
            plt.xlabel('Month')
            ax1.set_ylim(ymin=0,
                         ymax=max(default_maxs[idx],
                                  np.nanmax([bi95s, pt95s, ttis])))
            ax1.set_ylabel('Index Value')
            output_file = _output_path(
                monthly_output_dir,
                'Monthly %s Variations (%s).png' % (indice_name, oc.label))
            plt.tight_layout()
            plt.savefig(output_file)
            plt.clf()
            plt.close(fig)

            del indice
            gc.collect()
Exemple #4
0
def _write_monthly_index_variations_bar(eparam, operating_conditions,
                                        results_monthly, output_dir):
    """
    :type eparam: pyticas_tetres.ttypes.EstimationRequestInfo
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ftypes.ExtFilterGroup]
    :type results_monthly: list[(list[dict], list[str])]
    :type output_dir: str
    """
    monthly_output_dir = os.path.join(output_dir, OUTPUT_DIR_NAME,
                                      'monthly-variations')

    if not os.path.exists(monthly_output_dir):
        os.makedirs(monthly_output_dir)

    all_months = util.months(eparam.start_date, eparam.end_date)
    xaxis = np.array(list(range(len(all_months))))
    _xaxis, _xlabels = _get_xaxis(
        ['%04d-%02d' % (m[0], m[1]) for m in all_months])
    _xaxis = np.array(_xaxis)

    for gidx, oc in enumerate(operating_conditions):
        results, months = results_monthly[gidx]
        if not results:
            continue

        ch = ColorHatches()
        tts, ttrs, ttis, bi95s, bi85s, pt95s, pt85s = _get_indices(
            results, months, all_months, 0)

        fig = plt.figure(figsize=(16, 8), facecolor='white')
        ax1 = plt.subplot(111)

        space = 0.4
        n = 3
        w = (1 - space) / n
        sw = w * n / 2
        h, c = ch.next()
        ax1.bar(xaxis,
                bi95s,
                width=w,
                color=c,
                hatch=h,
                label='Buffer Index(95th-%ile)')
        h, c = ch.next()
        ax1.bar(xaxis + 1 * w,
                pt95s,
                width=w,
                color=c,
                hatch=h,
                label='Planning Time Index(95th-%ile)')
        h, c = ch.next()
        ax1.bar(xaxis + 2 * w,
                ttis,
                width=w,
                color=c,
                hatch=h,
                label='Travel Time Index')

        plt.xticks(_xaxis + sw, _xlabels, rotation=70)

        plt.grid()
        plt.xlim(xmin=0)

        ax1.legend(prop={'size': 12})

        plt.title('Monthly Variations of BI, PTI and TTI [%s]' % oc.label)
        plt.xlabel('Month')

        ax1.set_ylim(ymin=0,
                     ymax=max(DEFAULT_MAX_INDEX,
                              np.nanmax([bi95s, pt95s, ttis])))
        ax1.set_ylabel('Index Value')
        output_file = _output_path(
            monthly_output_dir,
            'Monthly-Variations of BI, PTI and TTI (%s).png' % oc.label)
        plt.tight_layout()
        plt.savefig(output_file)
        plt.clf()
        plt.close(fig)

        del tts, ttrs, ttis, bi95s, bi85s, pt95s, pt85s
        gc.collect()