Spaces:
Sleeping
Sleeping
Jon Solow
commited on
Commit
·
fb7f2c1
1
Parent(s):
727d494
Get league simulation working
Browse files
src/pages/50_League_Simulation.py
CHANGED
@@ -1,9 +1,49 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
from config import DEFAULT_ICON
|
|
|
4 |
from shared_page import common_page_config
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
def get_page():
|
@@ -19,7 +59,37 @@ def get_page():
|
|
19 |
)
|
20 |
|
21 |
else:
|
22 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
|
25 |
if __name__ == "__main__":
|
|
|
1 |
+
import pandas as pd
|
2 |
import streamlit as st
|
3 |
|
4 |
+
from config import DEFAULT_ICON, SEASON
|
5 |
+
from login_component import is_token_in_session
|
6 |
from shared_page import common_page_config
|
7 |
+
from simulate import create_simulate_summary, run_simulations
|
8 |
|
9 |
+
|
10 |
+
@st.cache_resource(ttl=60 * 60 * 24)
|
11 |
+
def get_all_league_settings_with_cache(season: int):
|
12 |
+
return st.session_state.yahoo_client.get_all_logged_in_user_league_settings(season=season)
|
13 |
+
|
14 |
+
|
15 |
+
@st.cache_resource(ttl=60 * 60 * 24)
|
16 |
+
def get_schedule_with_cache(league_key):
|
17 |
+
return st.session_state.yahoo_client.full_schedule_dataframe(league_key)
|
18 |
+
|
19 |
+
|
20 |
+
def clear_all():
|
21 |
+
for session_key in st.session_state.keys():
|
22 |
+
if session_key[:13] == "match-filter-":
|
23 |
+
st.session_state[session_key] = None
|
24 |
+
return
|
25 |
+
|
26 |
+
|
27 |
+
def show_week_matches(schedule: pd.DataFrame, week: int | str):
|
28 |
+
df_week = schedule[schedule.week == week]
|
29 |
+
columns = st.columns(df_week.match_index.nunique())
|
30 |
+
match_filter_map = {}
|
31 |
+
for match_col, (match_index, df) in zip(columns, df_week.groupby("match_index")):
|
32 |
+
with match_col:
|
33 |
+
match_winner = st.radio(
|
34 |
+
"Set winner",
|
35 |
+
key=f"match-filter-{match_index}",
|
36 |
+
options=df.team_name.tolist(),
|
37 |
+
index=None,
|
38 |
+
)
|
39 |
+
if match_winner:
|
40 |
+
match_filter_map[match_index] = match_winner
|
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():
|
|
|
59 |
)
|
60 |
|
61 |
else:
|
62 |
+
selected_season = st.selectbox("Select Season", list(range(SEASON, 2012, -1)))
|
63 |
+
user_leagues = get_all_league_settings_with_cache(season=selected_season)
|
64 |
+
selected_league = st.selectbox("Select league", user_leagues, format_func=lambda x: x.name)
|
65 |
+
st.header(f"{selected_league.name} - {selected_league.season}")
|
66 |
+
|
67 |
+
df_schedule = get_schedule_with_cache(selected_league.league_key)
|
68 |
+
# st.dataframe(df_schedule)
|
69 |
+
st.write(f"Current Week: {selected_league.current_week}")
|
70 |
+
sim_result = pd.DataFrame()
|
71 |
+
n_sims = 10000
|
72 |
+
|
73 |
+
st.header("Match Scenarios")
|
74 |
+
st.write("Select match winners to filter simulation scenarios")
|
75 |
+
all_match_filters = {}
|
76 |
+
st.button("Reset", on_click=clear_all)
|
77 |
+
for week in range(selected_league.current_week, selected_league.playoff_start_week):
|
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):
|
86 |
+
filtered_sims = sim_result.copy()
|
87 |
+
for week, filtered_match_dict in all_match_filters.items():
|
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 |
|
95 |
if __name__ == "__main__":
|