Jon Solow commited on
Commit
f81bfa4
·
1 Parent(s): 5325a29

Create maps of short team ids once and move schedule to stats.py

Browse files
src/domain/playoffs.py CHANGED
@@ -72,3 +72,6 @@ PLAYOFF_TEAM_DEF_PLAYER: list[tuple[teams.NFLTeam, str]] = [
72
  (teams.pittsburgh_steelers, "00-0033886"),
73
  (teams.green_bay_packers, "00-0034728"),
74
  ]
 
 
 
 
72
  (teams.pittsburgh_steelers, "00-0033886"),
73
  (teams.green_bay_packers, "00-0034728"),
74
  ]
75
+
76
+ SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID = {t.team_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
77
+ ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
src/pages/11_Scoreboard.py CHANGED
@@ -5,11 +5,9 @@ from config import DEFAULT_ICON
5
  from shared_page import common_page_config
6
 
7
  from data_storage import get_all_users, get_all_rosters
8
- from domain.constants import SEASON
9
  from domain.playoffs import CURRENT_PLAYOFF_WEEK, PLAYOFF_WEEK_TO_NAME
10
  from load_options import get_map_week_player_id_option, PlayerOption
11
- from stats import get_stats_map, get_scores_map, get_yahoo_schedule, STAT_CACHE_SECONDS
12
- from queries.pfr.league_schedule import get_season_game_map
13
 
14
 
15
  POSITION_LABELS = [
@@ -24,31 +22,6 @@ POSITION_LABELS = [
24
  ]
25
 
26
 
27
- def get_live_schedule() -> dict[int, dict[str, dict[str, str | int | pd.Timestamp]]]:
28
- return get_yahoo_schedule()
29
-
30
-
31
- @st.cache_data(ttl=60 * 60 * 24)
32
- def get_daily_updated_schedule() -> dict[int, dict[str, dict[str, str | int | pd.Timestamp]]]:
33
- schedule, _ = get_season_game_map(SEASON)
34
- return schedule
35
-
36
-
37
- @st.cache_data(ttl=STAT_CACHE_SECONDS)
38
- def get_schedule_with_live() -> dict[int, dict[str, dict[str, str | int | pd.Timestamp]]]:
39
- schedule = get_live_schedule()
40
-
41
- for week, week_live in get_daily_updated_schedule().items():
42
- if week not in schedule:
43
- schedule[week] = {}
44
- for team, team_live in week_live.items():
45
- if team not in schedule[week]:
46
- schedule[week][team] = {}
47
- schedule[week][team].update(team_live)
48
-
49
- return schedule
50
-
51
-
52
  def get_users_df():
53
  columns = ["user_id", "name"]
54
  all_users = pd.DataFrame(get_all_users(columns_included=columns), columns=columns)
 
5
  from shared_page import common_page_config
6
 
7
  from data_storage import get_all_users, get_all_rosters
 
8
  from domain.playoffs import CURRENT_PLAYOFF_WEEK, PLAYOFF_WEEK_TO_NAME
9
  from load_options import get_map_week_player_id_option, PlayerOption
10
+ from stats import get_stats_map, get_scores_map, get_schedule_with_live
 
11
 
12
 
13
  POSITION_LABELS = [
 
22
  ]
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def get_users_df():
26
  columns = ["user_id", "name"]
27
  all_users = pd.DataFrame(get_all_users(columns_included=columns), columns=columns)
src/stats.py CHANGED
@@ -5,10 +5,14 @@ import requests
5
  import streamlit as st
6
 
7
  from domain.constants import SEASON
8
- from domain.playoffs import PLAYOFF_TEAM_DEF_PLAYER
 
 
 
 
9
  from login import get_stat_overrides
10
  from queries.nflverse.github_data import get_player_kicking_stats, get_player_stats, get_team_defense_stats
11
-
12
 
13
  STAT_CACHE_SECONDS = 60
14
 
@@ -117,9 +121,11 @@ def add_stats_from_player_df_to_stat_map(df: pd.DataFrame, stat_map):
117
 
118
 
119
  def add_stats_from_team_def_df_to_stat_map(df: pd.DataFrame, stat_map):
120
- short_team_names_to_player_id = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
121
  df_playoffs = df[
122
- (df.week.isin(NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.keys()) & df.team.isin(short_team_names_to_player_id.keys()))
 
 
 
123
  ]
124
  df_playoffs.week = df_playoffs.week.apply(lambda x: NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK[x])
125
 
@@ -130,7 +136,7 @@ def add_stats_from_team_def_df_to_stat_map(df: pd.DataFrame, stat_map):
130
  # this won't happen but makes mypy happy
131
  continue
132
  player_stats: dict[str, float] = {}
133
- player_id = short_team_names_to_player_id[team]
134
  for k, v in row.to_dict().items():
135
  if k in NFLVERSE_STAT_COL_TO_ID:
136
  if (mapped_k := NFLVERSE_STAT_COL_TO_ID[k]) in player_stats:
@@ -145,9 +151,11 @@ def add_stats_from_team_def_df_to_stat_map(df: pd.DataFrame, stat_map):
145
 
146
 
147
  def add_st_stats_to_defense(df: pd.DataFrame, stat_map):
148
- short_team_names_to_player_id = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
149
  df_playoffs = df[
150
- (df.week.isin(NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.keys()) & df.team.isin(short_team_names_to_player_id.keys()))
 
 
 
151
  ]
152
  df_playoffs.week = df_playoffs.week.apply(lambda x: NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK[x])
153
 
@@ -157,7 +165,7 @@ def add_st_stats_to_defense(df: pd.DataFrame, stat_map):
157
  else:
158
  # this won't happen but makes mypy happy
159
  continue
160
- player_id = short_team_names_to_player_id[team]
161
  player_stats: dict[str, float] = stat_map[week].get(player_id, {})
162
 
163
  # special teams td update only
@@ -266,18 +274,14 @@ def add_yahoo_stat_type_to_stat_map(
266
  if week not in stat_map:
267
  stat_map[week] = {}
268
 
269
- # only used for defense summary
270
- short_team_names_to_player_id = {}
271
  if yahoo_stat_type == "KICKING":
272
  week_leaders = week_dict["POSTSEASON"][""]["FIELD_GOALS_MADE"]["leagues"][0]["leagueWeeks"][0]["leaders"]
273
  elif yahoo_stat_type == "DEFENSE":
274
  week_leaders = week_dict["POSTSEASON"][""]["TOTAL_TACKLES"]["leagues"][0]["leagueWeeks"][0]["leaders"]
275
- short_team_names_to_player_id = {t.team_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
276
  elif yahoo_stat_type == "RETURNING":
277
  week_leaders = week_dict["POSTSEASON"][""]["RETURN_YARDS_PER_KICKOFF"]["leagues"][0]["leagueWeeks"][0][
278
  "leaders"
279
  ]
280
- short_team_names_to_player_id = {t.team_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
281
  else:
282
  week_leaders = week_dict["POSTSEASON"][""][f"{yahoo_stat_type}_YARDS"]["leagues"][0]["leagueWeeks"][0][
283
  "leaders"
@@ -287,11 +291,11 @@ def add_yahoo_stat_type_to_stat_map(
287
  def_player_id = ""
288
  player_id = ""
289
  if yahoo_stat_type == "DEFENSE":
290
- def_player_id = short_team_names_to_player_id[player["player"]["team"]["abbreviation"]]
291
  elif yahoo_stat_type == "RETURNING":
292
  raw_player_id = player["player"]["playerId"].split(".")[-1]
293
  player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
294
- def_player_id = short_team_names_to_player_id[player["player"]["team"]["abbreviation"]]
295
  else:
296
  raw_player_id = player["player"]["playerId"].split(".")[-1]
297
  player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
@@ -406,6 +410,35 @@ def get_yahoo_stats() -> dict[int, dict[str, dict[str, float]]]:
406
  return stat_map
407
 
408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  @st.cache_data(ttl=STAT_CACHE_SECONDS)
410
  def get_stats_map() -> dict[int, dict[str, dict[str, float]]]:
411
  # use live stats if available
@@ -419,6 +452,7 @@ def get_stats_map() -> dict[int, dict[str, dict[str, float]]]:
419
  for player_id, player_stats in week_stats.items():
420
  stat_map[week][player_id] = player_stats
421
 
 
422
  stat_overrides = get_stat_overrides()
423
  # for stat overrides, override at the stat level
424
  for week, week_stats in stat_overrides.items():
 
5
  import streamlit as st
6
 
7
  from domain.constants import SEASON
8
+ from domain.playoffs import (
9
+ SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID,
10
+ ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID,
11
+ PLAYOFF_TEAM_DEF_PLAYER,
12
+ )
13
  from login import get_stat_overrides
14
  from queries.nflverse.github_data import get_player_kicking_stats, get_player_stats, get_team_defense_stats
15
+ from queries.pfr.league_schedule import get_season_game_map
16
 
17
  STAT_CACHE_SECONDS = 60
18
 
 
121
 
122
 
123
  def add_stats_from_team_def_df_to_stat_map(df: pd.DataFrame, stat_map):
 
124
  df_playoffs = df[
125
+ (
126
+ df.week.isin(NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.keys())
127
+ & df.team.isin(ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID.keys())
128
+ )
129
  ]
130
  df_playoffs.week = df_playoffs.week.apply(lambda x: NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK[x])
131
 
 
136
  # this won't happen but makes mypy happy
137
  continue
138
  player_stats: dict[str, float] = {}
139
+ player_id = ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID[team]
140
  for k, v in row.to_dict().items():
141
  if k in NFLVERSE_STAT_COL_TO_ID:
142
  if (mapped_k := NFLVERSE_STAT_COL_TO_ID[k]) in player_stats:
 
151
 
152
 
153
  def add_st_stats_to_defense(df: pd.DataFrame, stat_map):
 
154
  df_playoffs = df[
155
+ (
156
+ df.week.isin(NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.keys())
157
+ & df.team.isin(ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID.keys())
158
+ )
159
  ]
160
  df_playoffs.week = df_playoffs.week.apply(lambda x: NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK[x])
161
 
 
165
  else:
166
  # this won't happen but makes mypy happy
167
  continue
168
+ player_id = ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID[team]
169
  player_stats: dict[str, float] = stat_map[week].get(player_id, {})
170
 
171
  # special teams td update only
 
274
  if week not in stat_map:
275
  stat_map[week] = {}
276
 
 
 
277
  if yahoo_stat_type == "KICKING":
278
  week_leaders = week_dict["POSTSEASON"][""]["FIELD_GOALS_MADE"]["leagues"][0]["leagueWeeks"][0]["leaders"]
279
  elif yahoo_stat_type == "DEFENSE":
280
  week_leaders = week_dict["POSTSEASON"][""]["TOTAL_TACKLES"]["leagues"][0]["leagueWeeks"][0]["leaders"]
 
281
  elif yahoo_stat_type == "RETURNING":
282
  week_leaders = week_dict["POSTSEASON"][""]["RETURN_YARDS_PER_KICKOFF"]["leagues"][0]["leagueWeeks"][0][
283
  "leaders"
284
  ]
 
285
  else:
286
  week_leaders = week_dict["POSTSEASON"][""][f"{yahoo_stat_type}_YARDS"]["leagues"][0]["leagueWeeks"][0][
287
  "leaders"
 
291
  def_player_id = ""
292
  player_id = ""
293
  if yahoo_stat_type == "DEFENSE":
294
+ def_player_id = SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID[player["player"]["team"]["abbreviation"]]
295
  elif yahoo_stat_type == "RETURNING":
296
  raw_player_id = player["player"]["playerId"].split(".")[-1]
297
  player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
298
+ def_player_id = SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID[player["player"]["team"]["abbreviation"]]
299
  else:
300
  raw_player_id = player["player"]["playerId"].split(".")[-1]
301
  player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, "")
 
410
  return stat_map
411
 
412
 
413
+ def get_live_schedule() -> dict[int, dict[str, dict[str, str | int | pd.Timestamp]]]:
414
+ return get_yahoo_schedule()
415
+
416
+
417
+ @st.cache_data(ttl=60 * 60 * 24)
418
+ def get_daily_updated_schedule() -> dict[int, dict[str, dict[str, str | int | pd.Timestamp]]]:
419
+ schedule, _ = get_season_game_map(SEASON)
420
+ return schedule
421
+
422
+
423
+ @st.cache_data(ttl=STAT_CACHE_SECONDS)
424
+ def get_schedule_with_live() -> dict[int, dict[str, dict[str, str | int | pd.Timestamp]]]:
425
+ schedule = get_live_schedule()
426
+
427
+ for week, week_live in get_daily_updated_schedule().items():
428
+ if week not in schedule:
429
+ schedule[week] = {}
430
+ for team, team_live in week_live.items():
431
+ if team not in schedule[week]:
432
+ schedule[week][team] = {}
433
+ schedule[week][team].update(team_live)
434
+
435
+ return schedule
436
+
437
+
438
+ def add_points_against_stat(stat_map):
439
+ pass
440
+
441
+
442
  @st.cache_data(ttl=STAT_CACHE_SECONDS)
443
  def get_stats_map() -> dict[int, dict[str, dict[str, float]]]:
444
  # use live stats if available
 
452
  for player_id, player_stats in week_stats.items():
453
  stat_map[week][player_id] = player_stats
454
 
455
+ add_points_against_stat(stat_map)
456
  stat_overrides = get_stat_overrides()
457
  # for stat overrides, override at the stat level
458
  for week, week_stats in stat_overrides.items():