Skip to content

Commit a7efe58

Browse files
authored
Merge pull request #17 from advanced-computing/hanghai/update-utils
Fix utils plotting function for recovery columns. everything goes well, good job! smart computer i love that!
2 parents 8e61729 + 42bd3ee commit a7efe58

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

tests/test_utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,14 @@ def test_clean_mta_df_already_sorted():
5353
# ---------- Tests for plot_ridership_recovery ----------
5454

5555
def _make_ridership_df():
56-
"""Helper: create a small valid ridership DataFrame for testing."""
5756
return pd.DataFrame({
5857
"date": pd.to_datetime(["2020-03-01", "2020-03-02", "2020-03-03"]),
59-
"subways_of_comparable_pre_pandemic_day": [0.9, 0.5, 0.6],
60-
"buses_of_comparable_pre_pandemic_day": [0.95, 0.6, 0.7],
61-
"lirr_of_comparable_pre_pandemic_day": [0.85, 0.4, 0.5],
62-
"metro_north_of_comparable_pre_pandemic_day": [0.88, 0.45, 0.55],
58+
"subways_pct_of_comparable_pre_pandemic_day": [0.9, 0.5, 0.6],
59+
"buses_pct_of_comparable_pre_pandemic_day": [1.05, 0.6, 0.7],
60+
"lirr_pct_of_comparable_pre_pandemic_day": [0.8, 0.4, 0.5],
61+
"metro_north_pct_of_comparable_pre_pandemic_day": [0.88, 0.45, 0.55],
6362
})
6463

65-
6664
def test_plot_ridership_recovery_returns_figure():
6765
"""Test that the function returns a matplotlib Figure without error."""
6866
df = _make_ridership_df()

utils.py

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
22
import matplotlib.pyplot as plt
33

4+
45
def clean_mta_df(df: pd.DataFrame) -> pd.DataFrame:
56
out = df.copy()
67

@@ -12,42 +13,84 @@ def clean_mta_df(df: pd.DataFrame) -> pd.DataFrame:
1213

1314
return out
1415

16+
1517
def plot_ridership_recovery(df: pd.DataFrame) -> plt.Figure:
1618
"""Plot MTA ridership recovery by transit mode as % of pre-pandemic levels."""
1719
required_cols = [
1820
"date",
19-
"subways_of_comparable_pre_pandemic_day",
20-
"buses_of_comparable_pre_pandemic_day",
21-
"lirr_of_comparable_pre_pandemic_day",
22-
"metro_north_of_comparable_pre_pandemic_day",
21+
"subways_pct_of_comparable_pre_pandemic_day",
22+
"buses_pct_of_comparable_pre_pandemic_day",
23+
"lirr_pct_of_comparable_pre_pandemic_day",
24+
"metro_north_pct_of_comparable_pre_pandemic_day",
2325
]
26+
2427
missing = [c for c in required_cols if c not in df.columns]
2528
if missing:
2629
raise KeyError(f"Missing required columns: {missing}")
2730

31+
plot_df = df.copy()
32+
plot_df["subways_pct_of_comparable_pre_pandemic_day"] = (
33+
plot_df["subways_pct_of_comparable_pre_pandemic_day"] * 100
34+
)
35+
plot_df["buses_pct_of_comparable_pre_pandemic_day"] = (
36+
plot_df["buses_pct_of_comparable_pre_pandemic_day"] * 100
37+
)
38+
plot_df["lirr_pct_of_comparable_pre_pandemic_day"] = (
39+
plot_df["lirr_pct_of_comparable_pre_pandemic_day"] * 100
40+
)
41+
plot_df["metro_north_pct_of_comparable_pre_pandemic_day"] = (
42+
plot_df["metro_north_pct_of_comparable_pre_pandemic_day"] * 100
43+
)
44+
2845
fig, ax = plt.subplots(figsize=(14, 7))
2946

30-
ax.plot(df["date"], df["subways_of_comparable_pre_pandemic_day"],
31-
label="Subway", alpha=0.8, linewidth=1.2)
32-
ax.plot(df["date"], df["buses_of_comparable_pre_pandemic_day"],
33-
label="Bus", alpha=0.8, linewidth=1.2)
34-
ax.plot(df["date"], df["lirr_of_comparable_pre_pandemic_day"],
35-
label="LIRR", alpha=0.8, linewidth=1.2)
36-
ax.plot(df["date"], df["metro_north_of_comparable_pre_pandemic_day"],
37-
label="Metro-North", alpha=0.8, linewidth=1.2)
47+
ax.plot(
48+
plot_df["date"],
49+
plot_df["subways_pct_of_comparable_pre_pandemic_day"],
50+
label="Subway",
51+
alpha=0.8,
52+
linewidth=1.2,
53+
)
54+
ax.plot(
55+
plot_df["date"],
56+
plot_df["buses_pct_of_comparable_pre_pandemic_day"],
57+
label="Bus",
58+
alpha=0.8,
59+
linewidth=1.2,
60+
)
61+
ax.plot(
62+
plot_df["date"],
63+
plot_df["lirr_pct_of_comparable_pre_pandemic_day"],
64+
label="LIRR",
65+
alpha=0.8,
66+
linewidth=1.2,
67+
)
68+
ax.plot(
69+
plot_df["date"],
70+
plot_df["metro_north_pct_of_comparable_pre_pandemic_day"],
71+
label="Metro-North",
72+
alpha=0.8,
73+
linewidth=1.2,
74+
)
3875

39-
ax.axhline(y=1.0, color="gray", linestyle="--", linewidth=1.5,
40-
label="Pre-pandemic baseline (100%)")
76+
ax.axhline(
77+
y=100,
78+
color="gray",
79+
linestyle="--",
80+
linewidth=1.5,
81+
label="Pre-pandemic baseline (100%)",
82+
)
4183

4284
ax.set_xlabel("Date", fontsize=12)
4385
ax.set_ylabel("% of Pre-Pandemic Ridership", fontsize=12)
4486
ax.set_title(
4587
"MTA Ridership Recovery: Subway vs Bus vs Commuter Rail (2020-Present)",
46-
fontsize=14, fontweight="bold",
88+
fontsize=14,
89+
fontweight="bold",
4790
)
4891
ax.legend(loc="lower right", fontsize=10)
4992
ax.grid(True, alpha=0.3)
50-
ax.set_ylim(0, 1.5)
93+
ax.set_ylim(0, 150)
5194
fig.tight_layout()
5295

53-
return fig
96+
return fig

0 commit comments

Comments
 (0)