Jon Solow
commited on
Commit
·
183562b
1
Parent(s):
662f01a
Implement ST TD for defenses
Browse files- src/stats.py +28 -0
src/stats.py
CHANGED
|
@@ -137,6 +137,33 @@ def add_stats_from_team_def_df_to_stat_map(df: pd.DataFrame, stat_map):
|
|
| 137 |
stat_map[week][player_id].update(player_stats)
|
| 138 |
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# 24 hour cache
|
| 141 |
@st.cache_data(ttl=60 * 60 * 24)
|
| 142 |
def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
|
|
@@ -150,6 +177,7 @@ def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
|
|
| 150 |
add_stats_from_player_df_to_stat_map(df_player_stats, stat_map)
|
| 151 |
add_stats_from_player_df_to_stat_map(df_kicking_stats, stat_map)
|
| 152 |
add_stats_from_team_def_df_to_stat_map(df_def_stats, stat_map)
|
|
|
|
| 153 |
|
| 154 |
return stat_map
|
| 155 |
|
|
|
|
| 137 |
stat_map[week][player_id].update(player_stats)
|
| 138 |
|
| 139 |
|
| 140 |
+
def add_st_stats_to_defense(df: pd.DataFrame, stat_map):
|
| 141 |
+
short_team_names_to_player_id = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
|
| 142 |
+
df_playoffs = df[
|
| 143 |
+
(df.week.isin(NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.keys()) & df.team.isin(short_team_names_to_player_id.keys()))
|
| 144 |
+
]
|
| 145 |
+
df_playoffs.week = df_playoffs.week.apply(lambda x: NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK[x])
|
| 146 |
+
|
| 147 |
+
for week_team_tuple, row in df_playoffs.set_index(["week", "team"]).iterrows():
|
| 148 |
+
if isinstance(week_team_tuple, tuple):
|
| 149 |
+
week, team = week_team_tuple
|
| 150 |
+
else:
|
| 151 |
+
# this won't happen but makes mypy happy
|
| 152 |
+
continue
|
| 153 |
+
player_id = short_team_names_to_player_id[team]
|
| 154 |
+
player_stats: dict[str, float] = stat_map[week].get(player_id, {})
|
| 155 |
+
|
| 156 |
+
# special teams td update only
|
| 157 |
+
for k, v in row.to_dict().items():
|
| 158 |
+
if k == "special_teams_tds":
|
| 159 |
+
if (mapped_k := NFLVERSE_STAT_COL_TO_ID[k]) in player_stats:
|
| 160 |
+
player_stats[mapped_k] += v
|
| 161 |
+
else:
|
| 162 |
+
player_stats[mapped_k] = v
|
| 163 |
+
|
| 164 |
+
stat_map[week][player_id] = player_stats
|
| 165 |
+
|
| 166 |
+
|
| 167 |
# 24 hour cache
|
| 168 |
@st.cache_data(ttl=60 * 60 * 24)
|
| 169 |
def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
|
|
|
|
| 177 |
add_stats_from_player_df_to_stat_map(df_player_stats, stat_map)
|
| 178 |
add_stats_from_player_df_to_stat_map(df_kicking_stats, stat_map)
|
| 179 |
add_stats_from_team_def_df_to_stat_map(df_def_stats, stat_map)
|
| 180 |
+
add_st_stats_to_defense(df_player_stats, stat_map)
|
| 181 |
|
| 182 |
return stat_map
|
| 183 |
|