Jon Solow
commited on
Commit
·
8aa2333
1
Parent(s):
e6aafd7
Implement return td stats mapped both to player and team def
Browse files- src/stats.py +36 -16
src/stats.py
CHANGED
|
@@ -227,6 +227,10 @@ YAHOO_TO_STAT_MAP: dict[str, dict[str, str]] = {
|
|
| 227 |
"FUMBLE_RETURN_TOUCHDOWNS": DEF_TD.key,
|
| 228 |
"SAFETIES": SAFETY.key,
|
| 229 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
}
|
| 231 |
|
| 232 |
|
|
@@ -266,33 +270,49 @@ def add_yahoo_stat_type_to_stat_map(
|
|
| 266 |
elif yahoo_stat_type == "DEFENSE":
|
| 267 |
week_leaders = week_dict["POSTSEASON"][""]["TOTAL_TACKLES"]["leagues"][0]["leagueWeeks"][0]["leaders"]
|
| 268 |
short_team_names_to_player_id = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
else:
|
| 270 |
week_leaders = week_dict["POSTSEASON"][""][f"{yahoo_stat_type}_YARDS"]["leagues"][0]["leagueWeeks"][0][
|
| 271 |
"leaders"
|
| 272 |
]
|
| 273 |
|
| 274 |
for player in week_leaders:
|
|
|
|
|
|
|
| 275 |
if yahoo_stat_type == "DEFENSE":
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
else:
|
| 278 |
raw_player_id = player["player"]["playerId"].split(".")[-1]
|
| 279 |
player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
|
|
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
def get_yahoo_stat_json_obj():
|
|
@@ -378,7 +398,7 @@ def get_yahoo_stats() -> dict[int, dict[str, dict[str, float]]]:
|
|
| 378 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballRushing"], "RUSHING", stat_map)
|
| 379 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballReceiving"], "RECEIVING", stat_map)
|
| 380 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballKicking"], "KICKING", stat_map)
|
| 381 |
-
|
| 382 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballDefense"], "DEFENSE", stat_map)
|
| 383 |
|
| 384 |
return stat_map
|
|
|
|
| 227 |
"FUMBLE_RETURN_TOUCHDOWNS": DEF_TD.key,
|
| 228 |
"SAFETIES": SAFETY.key,
|
| 229 |
},
|
| 230 |
+
"RETURNING": {
|
| 231 |
+
"KICKOFF_RETURN_TOUCHDOWNS": ST_TD.key,
|
| 232 |
+
"PUNT_RETURN_TOUCHDOWNS": ST_TD.key,
|
| 233 |
+
},
|
| 234 |
}
|
| 235 |
|
| 236 |
|
|
|
|
| 270 |
elif yahoo_stat_type == "DEFENSE":
|
| 271 |
week_leaders = week_dict["POSTSEASON"][""]["TOTAL_TACKLES"]["leagues"][0]["leagueWeeks"][0]["leaders"]
|
| 272 |
short_team_names_to_player_id = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
|
| 273 |
+
elif yahoo_stat_type == "RETURNING":
|
| 274 |
+
week_leaders = week_dict["POSTSEASON"][""]["RETURN_YARDS_PER_KICKOFF"]["leagues"][0]["leagueWeeks"][0][
|
| 275 |
+
"leaders"
|
| 276 |
+
]
|
| 277 |
+
short_team_names_to_player_id = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
|
| 278 |
else:
|
| 279 |
week_leaders = week_dict["POSTSEASON"][""][f"{yahoo_stat_type}_YARDS"]["leagues"][0]["leagueWeeks"][0][
|
| 280 |
"leaders"
|
| 281 |
]
|
| 282 |
|
| 283 |
for player in week_leaders:
|
| 284 |
+
def_player_id = ""
|
| 285 |
+
player_id = ""
|
| 286 |
if yahoo_stat_type == "DEFENSE":
|
| 287 |
+
def_player_id = short_team_names_to_player_id[player["player"]["team"]["abbreviation"]]
|
| 288 |
+
if yahoo_stat_type == "RETURNING":
|
| 289 |
+
raw_player_id = player["player"]["playerId"].split(".")[-1]
|
| 290 |
+
player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
|
| 291 |
+
def_player_id = short_team_names_to_player_id[player["player"]["team"]["abbreviation"]]
|
| 292 |
else:
|
| 293 |
raw_player_id = player["player"]["playerId"].split(".")[-1]
|
| 294 |
player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
|
| 295 |
|
| 296 |
+
map_stats_to_week_player_id(player_id, week, player, stat_map, yahoo_stat_type)
|
| 297 |
+
map_stats_to_week_player_id(def_player_id, week, player, stat_map, yahoo_stat_type)
|
| 298 |
+
|
| 299 |
|
| 300 |
+
def map_stats_to_week_player_id(player_id: str, week: int, player, stat_map, yahoo_stat_type):
|
| 301 |
+
if not player_id:
|
| 302 |
+
return
|
| 303 |
+
|
| 304 |
+
if player_id not in stat_map[week]:
|
| 305 |
+
stat_map[week][player_id] = {}
|
| 306 |
+
stats = player["stats"]
|
| 307 |
+
for stat in stats:
|
| 308 |
+
if stat_key := YAHOO_TO_STAT_MAP[yahoo_stat_type].get(stat["statId"]):
|
| 309 |
+
if stat_key in stat_map[week][player_id]:
|
| 310 |
+
stat_map[week][player_id][stat_key] += float(stat["value"] or 0.0)
|
| 311 |
+
else:
|
| 312 |
+
stat_map[week][player_id][stat_key] = float(stat["value"] or 0.0)
|
| 313 |
+
# else:
|
| 314 |
+
# # remove after mapping all intended
|
| 315 |
+
# stat_map[week][player_id][stat["statId"]] = stat["value"]
|
| 316 |
|
| 317 |
|
| 318 |
def get_yahoo_stat_json_obj():
|
|
|
|
| 398 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballRushing"], "RUSHING", stat_map)
|
| 399 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballReceiving"], "RECEIVING", stat_map)
|
| 400 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballKicking"], "KICKING", stat_map)
|
| 401 |
+
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballReturns"], "RETURNING", stat_map)
|
| 402 |
add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballDefense"], "DEFENSE", stat_map)
|
| 403 |
|
| 404 |
return stat_map
|