Jon Solow commited on
Commit
f413cfe
·
1 Parent(s): 8ef2226

Use local file cache to make faster

Browse files
Files changed (1) hide show
  1. src/pages/50_League_Simulation.py +15 -6
src/pages/50_League_Simulation.py CHANGED
@@ -41,9 +41,14 @@ def show_week_matches(schedule: pd.DataFrame, week: int | str):
41
  return match_filter_map
42
 
43
 
44
- @st.cache_resource(ttl=60 * 60 * 24)
45
- def get_sim_with_cache(schedule: pd.DataFrame, complete_weeks: int, n_sims: int, n_playoff: int):
46
- return run_simulations(schedule, complete_weeks, n_sims, n_playoff)
 
 
 
 
 
47
 
48
 
49
  def get_page():
@@ -78,8 +83,12 @@ def get_page():
78
  with st.expander(f"Week {week}"):
79
  all_match_filters[week] = show_week_matches(df_schedule, week)
80
 
81
- sim_result = get_sim_with_cache(
82
- df_schedule, selected_league.current_week - 1, n_sims, selected_league.num_playoff_teams
 
 
 
 
83
  )
84
 
85
  if len(sim_result):
@@ -88,7 +97,7 @@ def get_page():
88
  for match_index, match_winner in filtered_match_dict.items():
89
  filtered_sims = filtered_sims[filtered_sims[int(week)][match_index] == match_winner]
90
 
91
- st.write(f"Number of Scenarios in filter: {len(filtered_sims)=} / {n_sims}")
92
  st.dataframe(create_simulate_summary(filtered_sims))
93
 
94
 
 
41
  return match_filter_map
42
 
43
 
44
+ def get_sim_with_file_cache(league_key, schedule: pd.DataFrame, complete_weeks: int, n_sims: int, n_playoff: int):
45
+ file_name = f"sims_{league_key}_{complete_weeks}.parquet"
46
+ try:
47
+ return pd.read_parquet(file_name)
48
+ except Exception:
49
+ df = run_simulations(schedule, complete_weeks, n_sims, n_playoff)
50
+ df.to_parquet(file_name)
51
+ return df
52
 
53
 
54
  def get_page():
 
83
  with st.expander(f"Week {week}"):
84
  all_match_filters[week] = show_week_matches(df_schedule, week)
85
 
86
+ sim_result = get_sim_with_file_cache(
87
+ selected_league.league_key,
88
+ df_schedule,
89
+ selected_league.current_week - 1,
90
+ n_sims,
91
+ selected_league.num_playoff_teams,
92
  )
93
 
94
  if len(sim_result):
 
97
  for match_index, match_winner in filtered_match_dict.items():
98
  filtered_sims = filtered_sims[filtered_sims[int(week)][match_index] == match_winner]
99
 
100
+ st.write(f"Number of Scenarios included in filter: {len(filtered_sims)} / {n_sims}")
101
  st.dataframe(create_simulate_summary(filtered_sims))
102
 
103