route_id = args.route stop_id = args.stop d = date.fromisoformat(args.date) comparison = args.comparison early_min = args.early_min late_min = args.late_min agency = config.get_agency(args.agency) agency_id = agency.id start_time = datetime.now() print(f"Start: {start_time}") timetable = timetables.get_by_date(agency_id, route_id, d) route_config = agency.get_route_config(route_id) tz = agency.tz direction_id = args.dir timetable_df = timetable.get_data_frame( stop_id=stop_id, direction_id=direction_id).sort_values('TIME') 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)]
dates = util.get_dates_in_range(args.start_date, args.end_date) else: raise Exception('missing date, start-date, or end-date') print(f"Date: {', '.join([str(date) for date in dates])}") print(f"Local Time Range: [{start_time_str}, {end_time_str})") print(f"Route: {route_id} ({route_config.title})") print(f"From: {s1} ({s1_info.title})") print(f"To: {s2} ({s2_info.title})") print(f"Direction: {','.join(common_dirs)} ({dir_info.title})") completed_trips_arr = [] for d in dates: if show_scheduled: history = timetables.get_by_date(agency.id, route_id, d, version) else: history = arrival_history.get_by_date(agency.id, route_id, d, version) start_time = util.get_timestamp_or_none(d, start_time_str, tz) end_time = util.get_timestamp_or_none(d, end_time_str, tz) s1_df = history.get_data_frame(stop_id=s1, start_time=start_time, end_time=end_time) s2_df = history.get_data_frame(stop_id=s2, start_time=start_time) s1_df['trip_min'], s1_df[ 'dest_arrival_time'] = trip_times.get_matching_trips_and_arrival_times( s1_df['TRIP'].values, s1_df['DEPARTURE_TIME'].values,
def compute_stats(d: date, agency: config.Agency, routes, save_to_s3=True): tz = agency.tz stat_ids = all_stat_ids print(d) time_str_intervals = constants.DEFAULT_TIME_STR_INTERVALS.copy() time_str_intervals.append(('07:00','19:00')) timestamp_intervals = [( int(util.get_localized_datetime(d, start_time_str, tz).timestamp()), int(util.get_localized_datetime(d, end_time_str, tz).timestamp()) ) for start_time_str, end_time_str in time_str_intervals ] timestamp_intervals.append((None, None)) time_str_intervals.append((None, None)) all_stats = {} for stat_id in stat_ids: all_stats[stat_id] = {} for interval_index, _ in enumerate(timestamp_intervals): all_stats[stat_id][interval_index] = {} for route in routes: route_id = route.id print(route_id) t1 = time.time() route_config = agency.get_route_config(route_id) try: history = arrival_history.get_by_date(agency.id, route_id, d) except FileNotFoundError as ex: print(ex) continue try: timetable = timetables.get_by_date(agency.id, route_id, d) except FileNotFoundError as ex: print(ex) continue timetable_df = timetable.get_data_frame() history_df = history.get_data_frame() for stat_id in stat_ids: for interval_index, _ in enumerate(timestamp_intervals): all_stats[stat_id][interval_index][route_id] = {'directions':{}} for dir_info in route_config.get_direction_infos(): dir_id = dir_info.id all_stats[stat_id][interval_index][route_id]['directions'][dir_id] = collections.defaultdict(dict) add_trip_time_stats_for_route(all_stats, timestamp_intervals, route_config, history_df) add_wait_time_stats_for_route(all_stats, timestamp_intervals, route_config, history_df) add_schedule_adherence_stats_for_route(all_stats, timestamp_intervals, route_config, history_df, timetable_df) t2 = time.time() print(f' {round(t2-t1, 2)} sec') for stat_id in stat_ids: for interval_index, (start_time, end_time) in enumerate(timestamp_intervals): start_time_str, end_time_str = time_str_intervals[interval_index] data = { 'routes': all_stats[stat_id][interval_index], } precomputed_stats.save_stats(agency.id, stat_id, d, start_time_str, end_time_str, data, save_to_s3)
elif args.start_date is not None and args.end_date is not None: dates = util.get_dates_in_range(args.start_date, args.end_date) else: raise Exception('missing date, start-date, or end-date') # print results for each direction for stop_dir in stop_dirs: dir_info = route_config.get_direction_info(stop_dir) interval_stats_arr = [] first_bus_date_times = [] last_bus_date_times = [] for d in dates: if show_scheduled: hist = timetables.get_by_date(agency.id, route_id, d) else: hist = arrival_history.get_by_date(agency.id, route_id, d) arrivals = hist.get_data_frame(stop_id=stop, direction_id=stop_dir) start_time = util.get_timestamp_or_none(d, start_time_str, tz) end_time = util.get_timestamp_or_none(d, end_time_str, tz) departure_times = np.sort(arrivals['DEPARTURE_TIME'].values) if len(departure_times) == 0: continue first_bus_date_times.append( datetime.fromtimestamp(departure_times[0], tz)) last_bus_date_times.append(