示例#1
0
def _write_yearly_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])]
    :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()
    years = util.years(eparam.start_date, eparam.end_date)
    for sidx, sheet_name in enumerate(sheet_names):
        ws = wb.add_worksheet('yearly %s' % sheet_name)
        field_name = fields[sidx]
        list_by_oc, year_list, oc_names = _extract_indices(results, operating_conditions, field_name, missing_value)
        ws.write_row(0, 0, ['Year'] + oc_names)
        ws.write_column(1, 0, years)
        col = 1
        for oidx, list_for_a_oc in enumerate(list_by_oc):
            data = []
            for y in years:
                tidx = -1
                for midx, y2 in enumerate(year_list[oidx]):
                    if y == y2:
                        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
示例#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))
示例#3
0
def _write_yearly_index_variations_oc(eparam, operating_conditions,
                                      results_yearly, output_dir, **kwargs):
    """
    :type eparam: pyticas_tetres.ttypes.EstimationRequestInfo
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ftypes.ExtFilterGroup]
    :type results_yearly: list[(list[dict], list[int])]
    :type output_dir: str
    """
    yearly_output_dir = os.path.join(output_dir, OUTPUT_DIR_NAME,
                                     'yearly-variations')

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

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

    all_years = util.years(eparam.start_date, eparam.end_date)
    xaxis = list(range(len(all_years)))
    _xaxis, _xlabels = _get_xaxis(all_years)
    _xaxis = np.array(_xaxis)
    sw = 0.5

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

        tts, ttrs, ttis, bi95s, bi85s, pt95s, pt85s = _get_indices(
            results, years, all_years)

        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(list(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 + sw, _xlabels, rotation=70)
            else:
                indice = [v if v >= 0 else None for v in indice]
                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('Yearly %s Variations [%s]' % (indice_name, oc.label))
            plt.xlabel('Year')
            ax1.set_ylim(ymin=0,
                         ymax=max(default_maxs[idx],
                                  np.nanmax([bi95s, pt95s, ttis])))
            ax1.set_ylabel('Index Value')
            output_file = _output_path(
                yearly_output_dir,
                'Yearly %s Variations (%s).png' % (indice_name, oc.label))
            plt.tight_layout()
            plt.savefig(output_file)
            plt.clf()
            plt.close(fig)

            del indice
            gc.collect()
示例#4
0
def _write_yearly_index_variations(eparam, operating_conditions,
                                   results_yearly, output_dir, **kwargs):
    """
    :type eparam: pyticas_tetres.ttypes.EstimationRequestInfo
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ftypes.ExtFilterGroup]
    :type results_yearly: list[(list[dict], list[int])]
    :type output_dir: str
    """
    yearly_output_dir = os.path.join(output_dir, OUTPUT_DIR_NAME,
                                     'yearly-variations')

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

    all_years = util.years(eparam.start_date, eparam.end_date)
    xaxis = np.array(list(range(len(all_years))))
    _xaxis, _xlabels = _get_xaxis(all_years)
    _xaxis = np.array(_xaxis)

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

        ch = ColorHatches()
        tts, ttrs, ttis, bi95s, bi85s, pt95s, pt85s = _get_indices(
            results, years, all_years, 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='BufferIndex(95th-%ile)')
        h, c = ch.next()
        ax1.bar(xaxis + 1 * w,
                pt95s,
                width=w,
                color=c,
                hatch=h,
                label='PlanningTimeIndex(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()

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

        plt.title('Yearly Index Variations [%s]' % oc.label)
        plt.xlabel('Year')
        ax1.set_ylim(ymin=0,
                     ymax=max(DEFAULT_MAX_INDEX,
                              np.nanmax([bi95s, pt95s, ttis])))
        ax1.set_ylabel('Index Value')
        output_file = _output_path(yearly_output_dir,
                                   'Yearly-Variations (%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()