Skip to content

Commit 31e807c

Browse files
ankouredevinmatte
andauthored
927 automatically determine historic maximum for ridership (#1075)
* Scaffold out update_max_ridership (#927) * Formatting (#927) * Polished Script output formatting (#927) --------- Co-authored-by: Devin Matté <devinmatte@gmail.com>
1 parent b652474 commit 31e807c

2 files changed

Lines changed: 199 additions & 0 deletions

File tree

server/chalicelib/dynamo.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ def query_extended_trip_metrics(
160160
responses = ddb_json.loads(response["Items"])
161161
response_dicts.extend(responses)
162162
return response_dicts
163+
164+
165+
def query_historic_max_ridership(start_date: date, end_date: date, line_id: str = None):
166+
potential_vals = query_ridership(start_date, end_date, line_id)
167+
ridership = {val["count"] for val in potential_vals}
168+
max_ridership = max(ridership)
169+
170+
return max_ridership
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#!/usr/bin/env python3
2+
from chalicelib.dynamo import query_historic_max_ridership
3+
from datetime import date, datetime
4+
from dateutil.relativedelta import relativedelta
5+
import sys
6+
import os
7+
8+
# To run cd /home/andrew/transitmatters/t-performance-dash/server && PYTHONPATH=. poetry run python scripts/update_max_ridership.py
9+
10+
11+
def get_max_ridership_by_lineId(line_id: str, start_date: date, end_date: date):
12+
"""
13+
Get the maximum ridership for a given line within a date range.
14+
15+
Args:
16+
line_id: Line identifier (e.g., 'line-Red', 'line-Blue', 'line-Green', 'line-Orange', 'line-Fairmount','line-1')
17+
start_date: Start date for the query
18+
end_date: End date for the query
19+
20+
Returns:
21+
Maximum ridership count for the specified line and date range
22+
"""
23+
max_ridership = query_historic_max_ridership(start_date, end_date, line_id)
24+
print(f"Maximum ridership for {line_id} from {start_date} to {end_date}: {max_ridership}")
25+
return max_ridership
26+
27+
28+
def get_line_id(line_id: str) -> str:
29+
if line_id.startswith("CR-"):
30+
line_id = line_id.replace("CR-", "line-")
31+
return line_id
32+
# Check if first character is digit, if so then it is a bus
33+
if line_id[0].isdigit():
34+
if line_id == "57":
35+
line_id = "5757A"
36+
line_id = "line-" + line_id
37+
return line_id
38+
# This is meant for Red, Blue, Orange, Green, and Mattapan
39+
if line_id in [
40+
"line-red",
41+
"line-orange",
42+
"line-blue",
43+
"line-green",
44+
"line-mattapan",
45+
]:
46+
line_id = "line-" + line_id.replace("line-", "").capitalize()
47+
return line_id
48+
49+
50+
if __name__ == "__main__":
51+
# Example: Get max ridership for all lines
52+
lines = [
53+
"line-red",
54+
"line-orange",
55+
"line-blue",
56+
"line-green",
57+
"line-mattapan",
58+
"1",
59+
"4",
60+
"7",
61+
"8",
62+
"9",
63+
"10",
64+
"11",
65+
"14",
66+
"15",
67+
"16",
68+
"1719",
69+
"18",
70+
"21",
71+
"22",
72+
"23",
73+
"26",
74+
"28",
75+
"29",
76+
"30",
77+
"31",
78+
"32",
79+
"34",
80+
"35",
81+
"36",
82+
"37",
83+
"38",
84+
"39",
85+
"41",
86+
"42",
87+
"43",
88+
"44",
89+
"45",
90+
"47",
91+
"51",
92+
"55",
93+
"57",
94+
"6170170",
95+
"66",
96+
"69",
97+
"71",
98+
"73",
99+
"77",
100+
"80",
101+
"83",
102+
"85",
103+
"86-legacy",
104+
"86",
105+
"87",
106+
"88",
107+
"89",
108+
"90",
109+
"91",
110+
"92",
111+
"93",
112+
"94",
113+
"95",
114+
"96",
115+
"97",
116+
"99",
117+
"104",
118+
"109",
119+
"104109",
120+
"110",
121+
"111",
122+
"116",
123+
"114116117",
124+
"220221222",
125+
"CR-Fitchburg",
126+
"CR-Franklin",
127+
"CR-Greenbush",
128+
"CR-Haverhill",
129+
"CR-Lowell",
130+
"CR-Worcester",
131+
"CR-Fairmount",
132+
"CR-Kingston",
133+
"CR-Middleborough",
134+
"CR-Needham",
135+
"CR-Newburyport",
136+
"CR-NewBedford",
137+
"CR-Providence",
138+
"line-commuter-rail",
139+
]
140+
# Example usage with command line arguments
141+
if len(sys.argv) >= 4:
142+
line_id = sys.argv[1]
143+
start_date_str = sys.argv[2] # Format: YYYY-MM-DD
144+
end_date_str = sys.argv[3] # Format: YYYY-MM-DD
145+
146+
start_date = datetime.strptime(start_date_str, "%Y-%m-%d").date()
147+
end_date = datetime.strptime(end_date_str, "%Y-%m-%d").date()
148+
peak_ridership = {}
149+
150+
for line in lines:
151+
if line and line != "line-commuter-rail":
152+
line_id = get_line_id(line)
153+
try:
154+
max_ridership = get_max_ridership_by_lineId(line_id, start_date, end_date)
155+
peak_ridership[line] = max_ridership
156+
157+
except Exception as e:
158+
print(f"Error getting ridership for {line}: {e}")
159+
160+
if os.path.isdir("data"):
161+
pass
162+
else:
163+
os.mkdir("data")
164+
165+
with open("data/peakridership.txt", "w") as f:
166+
f.write(str(peak_ridership))
167+
else:
168+
# Default value is 1 year before today's date -> 2016
169+
print("Usage: python update_max_ridership.py <line_id> <start_date> <end_date>")
170+
print("\nRunning with default values...")
171+
start_date = date(2016, 1, 1)
172+
end_date = datetime.now() - relativedelta(years=1)
173+
174+
peak_ridership = {}
175+
for line in lines:
176+
if line and line != "line-commuter-rail":
177+
line_id = get_line_id(line)
178+
try:
179+
max_ridership = get_max_ridership_by_lineId(line_id, start_date, end_date)
180+
peak_ridership[line] = max_ridership
181+
182+
except Exception as e:
183+
print(f"Error getting ridership for {line}: {e}")
184+
185+
if os.path.isdir("data"):
186+
pass
187+
else:
188+
os.mkdir("data")
189+
190+
with open("data/peakridership.txt", "w") as f:
191+
f.write(str(peak_ridership))

0 commit comments

Comments
 (0)