Spaces:
Running
Running
File size: 17,509 Bytes
a671bf1 8c0f770 a671bf1 8c0f770 a671bf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
import streamlit as st
st.set_page_config(layout="wide")
for name in dir():
if not name.startswith('_'):
del globals()[name]
import numpy as np
import pandas as pd
import streamlit as st
import os
from database import init_conn
gcservice_account = init_conn()
master_hold = os.getenv('MASTER_HOLD')
sim_format = {'Top_finish': '{:.2%}', 'Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}'}
st.markdown("""
<style>
/* Tab styling */
.stElementContainer [data-baseweb="button-group"] {
gap: 2.000rem;
padding: 4px;
}
.stElementContainer [kind="segmented_control"] {
height: 2.000rem;
white-space: pre-wrap;
background-color: #DAA520;
color: white;
border-radius: 20px;
gap: 1px;
padding: 10px 20px;
font-weight: bold;
transition: all 0.3s ease;
}
.stElementContainer [kind="segmented_controlActive"] {
height: 3.000rem;
background-color: #DAA520;
border: 3px solid #FFD700;
border-radius: 10px;
color: black;
}
.stElementContainer [kind="segmented_control"]:hover {
background-color: #FFD700;
cursor: pointer;
}
div[data-baseweb="select"] > div {
background-color: #DAA520;
color: white;
}
</style>""", unsafe_allow_html=True)
@st.cache_resource(ttl = 600)
def init_baselines():
sh = gcservice_account.open_by_url(master_hold)
worksheet = sh.worksheet('Pitcher_Proj')
raw_display = pd.DataFrame(worksheet.get_all_records())
raw_display.replace("", np.nan, inplace=True)
pitcher_proj = raw_display.dropna()
sh = gcservice_account.open_by_url(master_hold)
worksheet = sh.worksheet('Hitter_Proj')
raw_display = pd.DataFrame(worksheet.get_all_records())
raw_display.replace("", np.nan, inplace=True)
hitter_proj = raw_display.dropna()
sh = gcservice_account.open_by_url(master_hold)
worksheet = sh.worksheet('Display')
raw_display = pd.DataFrame(worksheet.get_all_records())
wins_proj = raw_display.dropna()
return pitcher_proj, hitter_proj, wins_proj
def convert_df_to_csv(df):
return df.to_csv().encode('utf-8')
pitcher_proj, hitter_proj, wins_proj = init_baselines()
total_teams = pitcher_proj['Team'].values.tolist()
selected_tab = st.segmented_control(
"Select Tab",
options=["Team Win Projections", "Pitcher Projections", "Hitter Projections", "Pitcher Simulations", "Hitter Simulations"],
selection_mode='single',
default='Team Win Projections',
width='stretch',
label_visibility='collapsed',
key='tab_selector'
)
if selected_tab == 'Team Win Projections':
if st.button("Reset Data", key='reset1'):
st.cache_data.clear()
pitcher_proj, hitter_proj, wins_proj = init_baselines()
total_teams = pitcher_proj['Team'].values.tolist()
raw_frame = wins_proj.copy()
export_frame_team = raw_frame[['Team', '2B', 'HR', 'SB', 'P_SO', 'P_H', 'P_R', 'P_HR', 'P_BB', 'LY Added', 'Added', 'LY Adj Wins', 'Adj Wins', 'Vegas', 'Proj wins', 'Diff']]
export_frame_team = export_frame_team.sort_values(by='Proj wins', ascending=False)
disp_frame = raw_frame[['Team', '2B', 'HR', 'SB', 'P_SO', 'P_H', 'P_R', 'P_HR', 'P_BB', 'LY Added', 'Added', 'LY Adj Wins', 'Adj Wins', 'Vegas', 'Proj wins', 'Diff']]
disp_frame = disp_frame.sort_values(by='Proj wins', ascending=False)
st.dataframe(disp_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height = 1000, use_container_width = True)
st.download_button(
label="Export Team Win Projections",
data=convert_df_to_csv(export_frame_team),
file_name='MLB_team_win_export.csv',
mime='text/csv',
key='team_win_export',
)
elif selected_tab == 'Pitcher Projections':
if st.button("Reset Data", key='reset2'):
st.cache_data.clear()
pitcher_proj, hitter_proj, wins_proj = init_baselines()
total_teams = pitcher_proj['Team'].values.tolist()
raw_frame = pitcher_proj.copy()
split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1')
if split_var1 == 'Specific Teams':
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = total_teams, key='team_var1')
elif split_var1 == 'All':
team_var1 = total_teams
working_data = raw_frame[raw_frame['Team'].isin(team_var1)]
export_frame_sp = raw_frame[['Name', 'Team', 'TBF', 'Ceiling_var', 'True_AVG', 'Hits', 'Singles%', 'Singles', 'Doubles%', 'Doubles', 'xHR%', 'Homeruns', 'Strikeout%', 'Strikeouts',
'Walk%', 'Walks', 'Runs%', 'Runs', 'ERA', 'Wins', 'Quality_starts', 'ADP', 'UD_fpts', 'DK_fpts']]
disp_frame_sp = working_data[['Name', 'Team', 'TBF', 'True_AVG', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeouts',
'Walks', 'Runs', 'ERA', 'Wins', 'Quality_starts', 'ADP', 'UD_fpts', 'DK_fpts']]
disp_frame_sp = disp_frame_sp.sort_values(by='UD_fpts', ascending=False)
st.dataframe(disp_frame_sp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').background_gradient(cmap='RdYlGn', subset=['TBF', 'Strikeouts', 'Wins', 'Quality_starts', 'UD_fpts', 'DK_fpts']).format(precision=2), height = 1000, use_container_width = True)
st.download_button(
label="Export Pitcher Projections",
data=convert_df_to_csv(export_frame_sp),
file_name='MLB_pitcher_proj_export.csv',
mime='text/csv',
key='pitcher_proj_export',
)
elif selected_tab == 'Hitter Projections':
if st.button("Reset Data", key='reset3'):
st.cache_data.clear()
pitcher_proj, hitter_proj, wins_proj = init_baselines()
total_teams = pitcher_proj['Team'].values.tolist()
raw_frame = hitter_proj.copy()
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
if split_var2 == 'Specific Teams':
team_var2 = st.multiselect('Which teams would you like to include in the tables?', options = total_teams, key='team_var2')
elif split_var2 == 'All':
team_var2 = total_teams
working_data = raw_frame[raw_frame['Team'].isin(team_var2)]
export_frame_h = raw_frame[['Name', 'Team', 'PA', 'Ceiling_var', 'Walk%', 'Walks', 'xHits', 'Singles%', 'Singles', 'Doubles%', 'Doubles',
'xHR%', 'Homeruns', 'Runs%', 'Runs', 'RBI%', 'RBI', 'Steal%', 'Stolen_bases', 'ADP', 'UD_fpts', 'DK_fpts']]
disp_frame_h = working_data[['Name', 'Team', 'PA', 'Walks', 'xHits', 'Singles', 'Doubles',
'Homeruns', 'Runs', 'RBI', 'Stolen_bases', 'ADP', 'UD_fpts', 'DK_fpts']]
disp_frame_h = disp_frame_h.sort_values(by='UD_fpts', ascending=False)
st.dataframe(disp_frame_h.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['ADP']).format(precision=2), height = 1000, use_container_width = True)
st.download_button(
label="Export Hitter Projections",
data=convert_df_to_csv(export_frame_h),
file_name='MLB_hitter_proj_export.csv',
mime='text/csv',
key='hitter_proj_export',
)
elif selected_tab == 'Pitcher Simulations':
if st.button("Reset Data", key='reset4'):
st.cache_data.clear()
pitcher_proj, hitter_proj, wins_proj = init_baselines()
total_teams = pitcher_proj['Team'].values.tolist()
col1, col2 = st.columns([1, 5])
with col2:
df_hold_container = st.empty()
with col1:
prop_type_var_sp = st.selectbox('Select type of prop to simulate', options = ['Strikeouts', 'Wins', 'Quality_starts'], key='prop_type_var_sp')
if st.button('Simulate Stat', key='sim_sp'):
with col2:
with df_hold_container.container():
df = pitcher_proj.copy()
total_sims = 5000
df.replace("", 0, inplace=True)
if prop_type_var_sp == 'Strikeouts':
df['Median'] = df['Strikeouts']
stat_cap = 300
elif prop_type_var_sp == 'Wins':
df['Median'] = df['Wins']
stat_cap = 25
elif prop_type_var_sp == 'Quality_starts':
df['Median'] = df['Quality_starts']
stat_cap = 30
flex_file = df.copy()
flex_file.rename(columns={"Name": "Player"}, inplace = True)
flex_file['Floor'] = (flex_file['Median'] * .25)
flex_file['Ceiling'] = np.where((flex_file['Median'] + (flex_file['Median'] * flex_file['Ceiling_var'])) > stat_cap, stat_cap + (flex_file['Median']/10), (flex_file['Median'] + (flex_file['Median'] * flex_file['Ceiling_var'])))
flex_file['STD'] = (flex_file['Median']/3)
flex_file = flex_file[['Player', 'Floor', 'Median', 'Ceiling', 'STD']]
hold_file = flex_file.copy()
hold_file = hold_file.sort_values(by='Median', ascending=False)
overall_file = flex_file.copy()
overall_file = overall_file.sort_values(by='Median', ascending=False)
overall_players = overall_file[['Player']]
for x in range(0,total_sims):
overall_file['g'] = np.random.gumbel(overall_file['Median'] * .75,overall_file['STD'])
overall_file[x] = np.where((overall_file['g']<=overall_file['Ceiling']),overall_file['g'],overall_file['Ceiling'])
check_file = overall_file.copy()
overall_file=overall_file.drop(['Player', 'Floor', 'Median', 'Ceiling', 'STD', 'g'], axis=1)
overall_file.astype('int').dtypes
players_only = hold_file[['Player']]
raw_lineups_file = players_only.copy()
for x in range(0,total_sims):
maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
players_only[x] = raw_lineups_file[x].rank(ascending=False)
players_only=players_only.drop(['Player'], axis=1)
players_only.astype('int').dtypes
players_only['Average_Rank'] = players_only.mean(axis=1)
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
players_only['10%'] = overall_file.quantile(0.1, axis=1)
players_only['90%'] = overall_file.quantile(0.9, axis=1)
players_only['Player'] = hold_file[['Player']]
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '10%', '90%']]
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
final_Proj = final_Proj[['Player', '10%', 'Median', '90%', 'Top_finish', 'Top_5_finish', 'Top_10_finish']]
final_Proj.rename(columns={"Median": "Projection"}, inplace = True)
with df_hold_container.container():
df_hold_container = st.empty()
st.dataframe(final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(sim_format, precision=2), use_container_width = True)
elif selected_tab == 'Hitter Simulations':
if st.button("Reset Data", key='reset5'):
st.cache_data.clear()
pitcher_proj, hitter_proj, wins_proj = init_baselines()
total_teams = pitcher_proj['Team'].values.tolist()
col1, col2 = st.columns([1, 5])
with col2:
df_hold_container = st.empty()
with col1:
prop_type_var_h = st.selectbox('Select type of prop to simulate', options = ['Hits', 'Doubles', 'Home Runs', 'RBI', 'Stolen Bases'], key='prop_type_var_h')
if st.button('Simulate Stat', key='sim_h'):
with col2:
with df_hold_container.container():
df = hitter_proj.copy()
total_sims = 5000
df.replace("", 0, inplace=True)
if prop_type_var_h == 'Hits':
df['Median'] = df['xHits']
stat_cap = 250
elif prop_type_var_h == 'Doubles':
df['Median'] = df['Doubles']
stat_cap = 65
elif prop_type_var_h == 'Home Runs':
df['Median'] = df['Homeruns']
stat_cap = 75
elif prop_type_var_h == 'RBI':
df['Median'] = df['RBI']
stat_cap = 150
elif prop_type_var_h == 'Stolen Bases':
df['Median'] = df['Stolen_bases']
stat_cap = 80
flex_file = df.copy()
flex_file.rename(columns={"Name": "Player"}, inplace = True)
flex_file['Floor'] = (flex_file['Median'] * .15)
flex_file['Ceiling'] = np.where((flex_file['Median'] + (flex_file['Median'] * flex_file['Ceiling_var'])) > stat_cap, stat_cap + (flex_file['Median']/20), (flex_file['Median'] + (flex_file['Median'] * flex_file['Ceiling_var'])))
flex_file['STD'] = (flex_file['Median']/2)
flex_file = flex_file[['Player', 'Floor', 'Median', 'Ceiling', 'STD']]
hold_file = flex_file.copy()
hold_file = hold_file.sort_values(by='Median', ascending=False)
overall_file = flex_file.copy()
overall_file = overall_file.sort_values(by='Median', ascending=False)
overall_players = overall_file[['Player']]
for x in range(0,total_sims):
overall_file['g'] = np.random.gumbel(overall_file['Median'] * .5,overall_file['STD'])
overall_file[x] = np.where((overall_file['g']<=overall_file['Ceiling']),overall_file['g'],overall_file['Ceiling'])
check_file = overall_file.copy()
overall_file=overall_file.drop(['Player', 'Floor', 'Median', 'Ceiling', 'STD', 'g'], axis=1)
overall_file.astype('int').dtypes
players_only = hold_file[['Player']]
raw_lineups_file = players_only.copy()
for x in range(0,total_sims):
maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
players_only[x] = raw_lineups_file[x].rank(ascending=False)
players_only=players_only.drop(['Player'], axis=1)
players_only.astype('int').dtypes
players_only['Average_Rank'] = players_only.mean(axis=1)
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
players_only['10%'] = overall_file.quantile(0.1, axis=1)
players_only['90%'] = overall_file.quantile(0.9, axis=1)
players_only['Player'] = hold_file[['Player']]
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '10%', '90%']]
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
final_Proj = final_Proj[['Player', '10%', 'Median', '90%', 'Top_finish', 'Top_5_finish', 'Top_10_finish']]
final_Proj.rename(columns={"Median": "Projection"}, inplace = True)
with df_hold_container.container():
df_hold_container = st.empty()
st.dataframe(final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(sim_format, precision=2), use_container_width = True) |