Skip to content

Commit c75557c

Browse files
authored
Merge pull request #1023 from cal-itp/sched-speed-charts
Route average speed charts
2 parents 2eae3ed + 0d72311 commit c75557c

File tree

173 files changed

+1740
-704
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+1740
-704
lines changed

Makefile

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
# Run this in data-analyses
22
# To specify different Makefile: make build_parallel_corridors -f Makefile
33

4+
site = my_site_name
5+
6+
build_portfolio_site:
7+
git rm portfolio/$(site)/ -rf
8+
python portfolio/portfolio.py clean $(site)
9+
python portfolio/portfolio.py build $(site) --deploy
10+
git add portfolio/$(site)/*.yml portfolio/$(site)/*.md
11+
git add portfolio/$(site)/*.ipynb
12+
git add portfolio/sites/$(site).yml
13+
14+
415
build_competitive_corridors:
5-
#cd bus_service_increase/ && make setup_bus_service_utils && cd ..
6-
git rm portfolio/competitive_corridors/ -rf
16+
$(eval override site = competitive_corridors)
17+
cd bus_service_increase/ && make setup_bus_service_utils && cd ..
718
#need git rm because otherwise, just local removal, but git change is untracked
8-
python portfolio/portfolio.py clean competitive_corridors
919
python bus_service_increase/deploy_portfolio_yaml.py
10-
python portfolio/portfolio.py build competitive_corridors --deploy
11-
git add portfolio/competitive_corridors/district_*/ portfolio/competitive_corridors/*.yml portfolio/competitive_corridors/*.md
12-
git add portfolio/sites/competitive_corridors.yml
20+
make build_portfolio_site
1321
#--config=./portfolio/test-analyses.yml
1422

1523
build_dla_reports:
24+
$(eval override site = dla)
1625
cd dla/ && pip install -r requirements.txt && cd ..
17-
git rm portfolio/dla/ -rf
18-
python portfolio/portfolio.py build dla --deploy
19-
git add portfolio/dla/district_*/ portfolio/dla/*.yml portfolio/dla/*.md
20-
git add portfolio/sites/dla.yml
26+
make build_portfolio_site
27+
git add portfolio/dla/district_*/
2128

2229
build_quarterly_performance_metrics:
30+
$(eval override site = quarterly_performance_metrics)
2331
cd bus_service_increase/ && make setup_bus_service_utils && cd ..
24-
git rm portfolio/quarterly_performance_metrics/ -rf
25-
python portfolio/portfolio.py clean quarterly_performance_metrics
26-
python portfolio/portfolio.py build quarterly_performance_metrics --deploy
27-
git add portfolio/quarterly_performance_metrics/*.ipynb portfolio/quarterly_performance_metrics/*.yml portfolio/quarterly_performance_metrics/*.md
28-
git add portfolio/sites/quarterly_performance_metrics.yml
32+
make build_portfolio_site
2933

3034
build_ntd_report:
31-
#cd bus_service_increase/ && make setup_bus_service_utils && cd ..
32-
git rm portfolio/ntd_monthly_ridership/ -rf
33-
python portfolio/portfolio.py clean ntd_monthly_ridership
35+
$(eval override site = ntd_monthly_ridership)
36+
cd bus_service_increase/ && make setup_bus_service_utils && cd ..
3437
cd ntd/ && python deploy_portfolio_yaml.py && cd ..
35-
python portfolio/portfolio.py build ntd_monthly_ridership --deploy
36-
git add portfolio/ntd_monthly_ridership/*.ipynb portfolio/ntd_monthly_ridership/*.yml portfolio/ntd_monthly_ridership/*.md
37-
git add portfolio/sites/ntd_monthly_ridership.yml
38+
make build_portfolio_site
39+
40+
build_route_speeds:
41+
$(eval override site = route_speeds)
42+
cd rt_segment_speeds / && make pip install -r requirements.txt && cd ..
43+
cd rt_segment_speeds/ && python deploy_portfolio_yaml.py && cd ..
44+
make build_portfolio_site
3845

3946
add_precommit:
4047
pip install pre-commit

gtfs_digest/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assemble_data:
2+
python merge_data.py

gtfs_digest/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GTFS Digest
2+
3+
Performance metrics from GTFS schedule and vehicle positions data for all transit operators by route.

gtfs_digest/merge_data.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import geopandas as gpd
2+
import pandas as pd
3+
4+
from segment_speed_utils.project_vars import SEGMENT_GCS, RT_SCHED_GCS, SCHED_GCS
5+
6+
route_time_cols = ["schedule_gtfs_dataset_key",
7+
"route_id", "direction_id", "time_period"]
8+
9+
def concatenate_schedule_by_route_direction(
10+
date_list: list
11+
) -> pd.DataFrame:
12+
"""
13+
Concatenate schedule data that's been
14+
aggregated to route-direction-time_period.
15+
"""
16+
df = pd.concat([
17+
pd.read_parquet(
18+
f"{RT_SCHED_GCS}schedule_route_dir/"
19+
f"schedule_route_direction_metrics_{d}.parquet",
20+
columns = route_time_cols + [
21+
"avg_sched_service_min",
22+
"avg_stop_meters",
23+
"n_trips", "frequency",
24+
]
25+
).assign(
26+
service_date = pd.to_datetime(d)
27+
).astype({"direction_id": "Int64"})
28+
for d in date_list
29+
], axis=0, ignore_index=True)
30+
31+
return df
32+
33+
34+
def concatenate_segment_speeds_by_route_direction(
35+
date_list: list
36+
) -> gpd.GeoDataFrame:
37+
"""
38+
Concatenate segment speeds data that's been
39+
aggregated to route-direction-time_period.
40+
"""
41+
df = pd.concat([
42+
gpd.read_parquet(
43+
f"{SEGMENT_GCS}rollup_singleday/"
44+
f"speeds_route_dir_segments_{d}.parquet",
45+
columns = route_time_cols + ["p20_mph", "p50_mph", "p80_mph"]
46+
).assign(
47+
service_date = pd.to_datetime(d)
48+
).astype({"direction_id": "Int64"})
49+
for d in date_list],
50+
axis=0, ignore_index=True
51+
)
52+
53+
return df
54+
55+
56+
def concatenate_speeds_by_route_direction(
57+
date_list: list
58+
) -> pd.DataFrame:
59+
df = pd.concat([
60+
pd.read_parquet(
61+
f"{SEGMENT_GCS}rollup_singleday/"
62+
f"speeds_route_dir_{d}.parquet",
63+
columns = route_time_cols + ["speed_mph"]
64+
).assign(
65+
service_date = pd.to_datetime(d)
66+
).astype({"direction_id": "Int64"})
67+
for d in date_list],
68+
axis=0, ignore_index=True
69+
)
70+
71+
return df
72+
73+
74+
def merge_in_standardized_route_names(df: pd.DataFrame) -> pd.DataFrame:
75+
standardized_route_names = pd.read_parquet(
76+
f"{SCHED_GCS}standardized_route_ids.parquet",
77+
columns = ["schedule_gtfs_dataset_key", "name",
78+
"route_id", "service_date",
79+
"recent_route_id2", "recent_combined_name"
80+
]
81+
)
82+
83+
df = pd.merge(
84+
df,
85+
standardized_route_names,
86+
on = ["schedule_gtfs_dataset_key", "route_id", "service_date"],
87+
how = "left",
88+
)
89+
90+
df = df.assign(
91+
route_short_name = (df.recent_combined_name
92+
.str.split("__", expand=True)[0]),
93+
route_long_name = (df.recent_combined_name
94+
.str.split("__", expand=True)[1]),
95+
).drop(
96+
columns = ["route_id", "recent_combined_name"]
97+
).rename(
98+
columns = {"recent_route_id2": "route_id"}
99+
)
100+
101+
return df
102+
103+
104+
def clean_up_for_charts(df: pd.DataFrame) -> pd.DataFrame:
105+
# Clean up, round columns, get it as close to ready for charts
106+
df = df.assign(
107+
direction_id = df.direction_id.astype("int"),
108+
avg_sched_service_min = df.avg_sched_service_min.round(1),
109+
avg_stop_meters = df.avg_stop_meters.round(1),
110+
)
111+
112+
return df
113+
114+
115+
if __name__ == "__main__":
116+
117+
from shared_utils.rt_dates import y2023_dates, y2024_dates
118+
119+
analysis_date_list = y2024_dates + y2023_dates
120+
121+
df_schedule = concatenate_schedule_by_route_direction(analysis_date_list)
122+
df_avg_speeds = concatenate_speeds_by_route_direction(analysis_date_list)
123+
124+
df_sched_speeds = pd.merge(
125+
df_schedule,
126+
df_avg_speeds,
127+
on = route_time_cols + ["service_date"],
128+
how = "outer",
129+
indicator = "sched_rt_category"
130+
)
131+
132+
category_dict = {
133+
"left_only": "schedule_only",
134+
"both": "schedule_and_vp",
135+
"right_only": "vp_only"
136+
}
137+
138+
df_sched_speeds= df_sched_speeds.assign(
139+
sched_rt_category = df_sched_speeds.sched_rt_category.map(category_dict)
140+
).pipe(merge_in_standardized_route_names)
141+
142+
143+
df_sched_speeds.to_parquet(
144+
f"{RT_SCHED_GCS}digest/schedule_vp_metrics.parquet"
145+
)

0 commit comments

Comments
 (0)