Esempio n. 1
0
def accumulated_distances(results, route, **kwargs):
    """
    :type results: list[RNodeData]
    :type route: pyticas.ttypes.Route
    :rtype: list[float]
    """
    return moe_helper.accumulated_distances(results, route, **kwargs)
Esempio n. 2
0
def sheet_writer(wb, sheet, results, r, n_data, rows_before_data,
                 rows_after_data, **kwargs):
    """
    :type wb: Workbook
    :type sheet: xlsxwriter.workbook.Worksheet
    :type results: list[pyticas.ttypes.RNodeData]
    :type r: pyticas.ttypes.Route
    :type n_data: int
    :type rows_before_data: int
    :type rows_after_data: int
    """
    n_nodes = len(results)

    tcm_row = rows_before_data + n_data + rows_after_data
    tcmp_row = tcm_row + 1
    sheet.write_string(tcm_row, 0, 'TCMH')
    sheet.write_string(tcmp_row, 0, 'TCMH(%)')
    start_col = 1
    for ridx, rnd in enumerate(results):
        # last rnode must be excepted because TCMP can exceed 100% if the last station is included
        if ridx == n_nodes - 1:
            break
        congested = [
            v for tidx, v in enumerate(rnd.data)
            if v and not isinstance(v, str) and v > 0
        ]
        tcm = sum(congested)
        tcmp = len(congested) * 100 / n_data
        sheet.write_number(tcm_row, ridx + start_col, tcm)
        sheet.write_number(tcmp_row, ridx + start_col, tcmp)

    tcm_col = n_nodes + 1
    tcmp_col = tcm_col + 1
    sheet.write_string(0, tcm_col, 'TCMH')
    sheet.write_string(0, tcmp_col, 'TCMH(%)')

    acc_distances = moe_helper.accumulated_distances(results, r)

    for tidx in range(n_data):
        # accumulative distances can be same when distance between two stations is less than 0.1 mile
        congested = [
            acc_distances[ridx] for ridx, rnd in enumerate(results)
            if rnd.data[tidx] and not isinstance(rnd.data[tidx], str)
            and rnd.data[tidx] > 0
        ]
        congested = list(set(congested))
        n_congested = len(congested)
        tcm = n_congested * 0.1
        tcmp = n_congested * 100.0 / (n_nodes - 1)
        sheet.write_number(rows_before_data + tidx, tcm_col, tcm)
        sheet.write_number(rows_before_data + tidx, tcmp_col, tcmp)
Esempio n. 3
0
def _add_sheet(wb, results, _route, **kwargs):
    """
    :type wb: Workbook
    :type results: list[RNodeData]
    :type _route: pyticas.ttypes.Route
    :type kwargs: dict
    """
    missing_data = kwargs.get('missing_data', cfg.MISSING_VALUE)
    prd = results[0].prd
    timeline = prd.get_timeline()
    n_data = len(timeline)
    sheet = wb.add_worksheet(prd.get_date_string())

    # print rnode names
    sheet.write_row(row=0, col=1, data=[rnd.get_title(**kwargs) for rnd in results])

    data_start_row = 1
    rows_before_data = 1
    rows_after_data = 0

    # print used lane information
    if kwargs.get('print_lanes', True):
        rows_before_data += 1
        wrap = wb.add_format({'text_wrap': True})
        wrap.set_align('top')
        sheet.write_string(data_start_row, 0, 'Lane:Detector', wrap)
        sheet.write_row(row=data_start_row, col=1, data=moe_helper.used_lanes(results, _route), cell_format=wrap)
        data_start_row += 1

    # print accumulated distance from upstream
    if kwargs.get('print_distance', True):
        rows_before_data += 1
        sheet.write_string(data_start_row, 0, 'Distance')
        sheet.write_row(row=data_start_row, col=1, data=moe_helper.accumulated_distances(results, _route))
        data_start_row += 1

    # print confidence level (%)
    if kwargs.get('print_confidence', True):
        rows_before_data += 1
        sheet.write_string(data_start_row, 0, 'Confidence')
        sheet.write_row(row=data_start_row, col=1, data=moe_helper.confidences(results))
        data_start_row += 1

    # print timeline
    sheet.write_column(row=data_start_row, col=0, data=timeline)

    # print data
    for ridx, rnd in enumerate(results):
        sheet.write_column(row=data_start_row, col=ridx + 1, data=rnd.data)

    data_start_row = len(results[0].data) + data_start_row

    # print average
    if kwargs.get('print_average', False):
        rows_after_data += 1
        sheet.write_string(data_start_row, 0, 'Average')
        for ridx, rnd in enumerate(results):
            rnode_data = [v for v in rnd.data if v and not isinstance(v, str) and v > 0]
            if rnode_data:
                avg = sum(rnode_data) / len(rnode_data)
            else:
                avg = missing_data
            sheet.write_number(data_start_row, ridx + 1, avg)
        data_start_row += 1

    # call additional sheet_writer
    sheet_writer = kwargs.get('sheet_writer', None)
    if sheet_writer:
        assert callable(sheet_writer)
        sheet_writer(wb, sheet, results, _route, n_data, rows_before_data, rows_after_data, **kwargs)

    sheets = wb.worksheets()
    if len(sheets) >= 2:
        sheets[1].activate()