Skip to content

Commit f1ee6a4

Browse files
committed
Consolidate code from notebooks to utils.py and constants.py modules.
1 parent 54cc01a commit f1ee6a4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/constants.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
""" Project constants, parameters, and settings."""
2+
3+
DAY_OF_WEEK_MAP = {
4+
0: "Monday",
5+
1: "Tuesday",
6+
2: "Wednesday",
7+
3: "Thursday",
8+
4: "Friday",
9+
5: "Saturday",
10+
6: "Sunday",
11+
}
12+
13+
MONTHS_MAP = {
14+
1: "Jan",
15+
2: "Feb",
16+
3: "Mar",
17+
4: "Apr",
18+
5: "May",
19+
6: "Jun",
20+
7: "Jul",
21+
8: "Aug",
22+
9: "Sep",
23+
10: "Oct",
24+
11: "Nov",
25+
12: "Dec",
26+
}

src/utils.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Project helper functions."""
2+
3+
import pandas as pd
4+
from .constants import DAY_OF_WEEK_MAP
5+
6+
7+
def make_week_crosstab(
8+
df, divisor, day_of_week_map=DAY_OF_WEEK_MAP.copy(), values=None, aggfunc=None
9+
):
10+
"""Returns an hour / day-of-week crosstab scaled by a divisor."""
11+
ct = pd.crosstab(
12+
index=df["datetime"].dt.dayofweek,
13+
columns=df["datetime"].dt.hour,
14+
values=values,
15+
aggfunc=aggfunc,
16+
)
17+
if day_of_week_map:
18+
ct.rename(index=day_of_week_map)
19+
ct /= divisor # scale crosstab by divisor
20+
return ct
21+
22+
23+
def make_heatmap_labels(
24+
title, x_label="Hour of Day", y_label="", cbar_label="Number of Collisions per Hour"
25+
):
26+
"""Returns a dictionary of labels for a 2D heatmap."""
27+
ct_labels = {
28+
"title": title,
29+
"x_label": x_label,
30+
"y_label": y_label,
31+
"cbar_label": cbar_label,
32+
}
33+
return ct_labels

0 commit comments

Comments
 (0)