Exemplo n.º 1
0
def add_schedule_adherence_stats_for_route(all_stats, timestamp_intervals, route_config, history_df, timetable_df):
    route_id = route_config.id

    timetable_df = timetable_df.sort_values('DEPARTURE_TIME', axis=0)
    history_df = history_df.sort_values('DEPARTURE_TIME', axis=0)

    history_sid_values = history_df['SID'].values
    history_did_values = history_df['DID'].values

    timetable_sid_values = timetable_df['SID'].values
    timetable_did_values = timetable_df['DID'].values

    for dir_info in route_config.get_direction_infos():

        dir_id = dir_info.id
        stop_ids = dir_info.get_stop_ids()

        for i, stop_id in enumerate(stop_ids):
            stop_history_df = history_df[(history_sid_values == stop_id) & (history_did_values == dir_id)]
            stop_timetable_df = timetable_df[(timetable_sid_values == stop_id) & (timetable_did_values == dir_id)]

            stop_history_departure_times = stop_history_df['DEPARTURE_TIME'].values
            stop_timetable_departure_times = stop_timetable_df['DEPARTURE_TIME'].values

            comparison_df = timetables.match_schedule_to_actual_times(
                stop_timetable_departure_times,
                stop_history_departure_times,
                #late_sec=180
            )
            comparison_df['DEPARTURE_TIME'] = stop_timetable_departure_times

            for interval_index, (start_time, end_time) in enumerate(timestamp_intervals):

                interval_comparison_df = comparison_df

                if start_time is not None:
                    interval_comparison_df = interval_comparison_df[interval_comparison_df['DEPARTURE_TIME'] >= start_time]

                if end_time is not None:
                    interval_comparison_df = interval_comparison_df[interval_comparison_df['DEPARTURE_TIME'] < end_time]

                num_scheduled_departures = len(interval_comparison_df)

                if num_scheduled_departures > 0:
                    on_time_rate = round(np.sum(interval_comparison_df['on_time']) / num_scheduled_departures, 3)
                    dir_stats = all_stats['combined'][interval_index][route_id]['directions'][dir_id]
                    dir_stats['onTimeRates'][stop_id] = on_time_rate

        for interval_index, _ in enumerate(timestamp_intervals):
            add_median_schedule_adherence_stats_for_direction(
                all_stats['combined'][interval_index][route_id]['directions'][dir_id]
            )
Exemplo n.º 2
0
    early_sec = early_min * 60
    late_sec = late_min * 60

    timetable_df['scheduled_headway'] = np.r_[
        np.nan,
        metrics.compute_headway_minutes(timetable_df['TIME'].values)]

    if comparison:
        history = arrival_history.get_by_date(agency_id, route_id, d)
        arrivals_df = history.get_data_frame(stop_id=stop_id,
                                             direction_id=direction_id)

        comparison_df = timetables.match_schedule_to_actual_times(
            timetable_df['TIME'].values,
            arrivals_df['TIME'].values,
            early_sec=early_sec,
            late_sec=late_sec)

        timetable_df = pd.concat([timetable_df, comparison_df], axis=1)

    timetable_df['DATE_TIME'] = timetable_df['TIME'].apply(
        lambda t: datetime.fromtimestamp(t, tz))

    for row in timetable_df.itertuples():
        did = row.DID
        dwell_time = util.render_dwell_time(row.DEPARTURE_TIME - row.TIME)

        scheduled_headway = f'{round(row.scheduled_headway, 1)}'.rjust(4)

        if args.comparison: