Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,15 +5,11 @@ for name in dir():
|
|
5 |
if not name.startswith('_'):
|
6 |
del globals()[name]
|
7 |
|
8 |
-
import pulp
|
9 |
import numpy as np
|
10 |
import pandas as pd
|
11 |
import streamlit as st
|
12 |
import gspread
|
13 |
-
import time
|
14 |
import random
|
15 |
-
import scipy.stats
|
16 |
-
import os
|
17 |
|
18 |
@st.cache_resource
|
19 |
def init_conn():
|
@@ -38,30 +34,8 @@ def init_conn():
|
|
38 |
|
39 |
gc = init_conn()
|
40 |
|
41 |
-
game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
|
42 |
-
'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
|
43 |
-
|
44 |
-
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
|
45 |
-
'4x%': '{:.2%}','GPP%': '{:.2%}'}
|
46 |
-
|
47 |
freq_format = {'Proj Own': '{:.2%}', 'Exposure': '{:.2%}', 'Edge': '{:.2%}'}
|
48 |
|
49 |
-
@st.cache_resource(ttl = 300)
|
50 |
-
def set_slate_teams():
|
51 |
-
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
52 |
-
worksheet = sh.worksheet('Site_Info')
|
53 |
-
raw_display = pd.DataFrame(worksheet.get_all_records())
|
54 |
-
|
55 |
-
return raw_display
|
56 |
-
|
57 |
-
@st.cache_resource(ttl = 300)
|
58 |
-
def player_stat_table():
|
59 |
-
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
60 |
-
worksheet = sh.worksheet('Player_Projections')
|
61 |
-
raw_display = pd.DataFrame(worksheet.get_all_records())
|
62 |
-
|
63 |
-
return raw_display
|
64 |
-
|
65 |
@st.cache_resource(ttl = 300)
|
66 |
def load_dk_player_projections():
|
67 |
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
@@ -251,6 +225,8 @@ def create_random_portfolio(Total_Sample_Size, raw_baselines):
|
|
251 |
RandomPortfolio = pd.DataFrame(np.hstack(all_choices), columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST'])
|
252 |
RandomPortfolio['User/Field'] = 0
|
253 |
|
|
|
|
|
254 |
del O_merge
|
255 |
|
256 |
return RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict
|
@@ -263,28 +239,6 @@ def get_correlated_portfolio_for_sim(Total_Sample_Size):
|
|
263 |
stack_num = random.randint(1, 3)
|
264 |
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
265 |
|
266 |
-
# # Create a dictionary for mapping positions to their corresponding dictionaries
|
267 |
-
# dict_map = {
|
268 |
-
# 'QB': qb_dict,
|
269 |
-
# 'RB1': full_pos_player_dict['pos_dicts'][0],
|
270 |
-
# 'RB2': full_pos_player_dict['pos_dicts'][0],
|
271 |
-
# 'WR1': full_pos_player_dict['pos_dicts'][1],
|
272 |
-
# 'WR2': full_pos_player_dict['pos_dicts'][1],
|
273 |
-
# 'WR3': full_pos_player_dict['pos_dicts'][1],
|
274 |
-
# 'TE': full_pos_player_dict['pos_dicts'][2],
|
275 |
-
# 'FLEX': full_pos_player_dict['pos_dicts'][3],
|
276 |
-
# 'DST': def_dict
|
277 |
-
# }
|
278 |
-
|
279 |
-
# # Apply mapping for each position
|
280 |
-
# for pos, mapping in dict_map.items():
|
281 |
-
# RandomPortfolio[pos] = RandomPortfolio[pos].map(mapping).astype("string[pyarrow]")
|
282 |
-
|
283 |
-
# # This part appears to be for filtering. Consider if it can be optimized depending on the data characteristics
|
284 |
-
# RandomPortfolio['plyr_list'] = RandomPortfolio.values.tolist()
|
285 |
-
# RandomPortfolio['plyr_count'] = RandomPortfolio['plyr_list'].apply(lambda x: len(set(x)))
|
286 |
-
# RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).reset_index(drop=True)
|
287 |
-
|
288 |
RandomPortfolio['QB'] = pd.Series(list(RandomPortfolio['QB'].map(qb_dict)), dtype="string[pyarrow]")
|
289 |
RandomPortfolio['RB1'] = pd.Series(list(RandomPortfolio['RB1'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
290 |
RandomPortfolio['RB2'] = pd.Series(list(RandomPortfolio['RB2'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
@@ -305,8 +259,6 @@ def get_correlated_portfolio_for_sim(Total_Sample_Size):
|
|
305 |
del stack_num
|
306 |
del stacking_dict
|
307 |
|
308 |
-
|
309 |
-
|
310 |
RandomPortfolio['QBs'] = RandomPortfolio['QB'].map(maps_dict['Salary_map']).astype(np.int32)
|
311 |
RandomPortfolio['RB1s'] = RandomPortfolio['RB1'].map(maps_dict['Salary_map']).astype(np.int32)
|
312 |
RandomPortfolio['RB2s'] = RandomPortfolio['RB2'].map(maps_dict['Salary_map']).astype(np.int32)
|
@@ -512,11 +464,10 @@ def get_uncorrelated_portfolio_for_sim(Total_Sample_Size):
|
|
512 |
|
513 |
return RandomPortfolio, maps_dict
|
514 |
|
515 |
-
|
516 |
dk_roo_raw = load_dk_player_projections()
|
517 |
fd_roo_raw = load_fd_player_projections()
|
518 |
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
519 |
-
site_slates = set_slate_teams()
|
520 |
dkid_dict, fdid_dict = set_export_ids()
|
521 |
|
522 |
static_exposure = pd.DataFrame(columns=['Player', 'count'])
|
@@ -815,6 +766,8 @@ with tab1:
|
|
815 |
overall_exposure = overall_exposure.sort_values(by='Exposure', ascending=False)
|
816 |
overall_exposure['Exposure'] = overall_exposure['Exposure'].astype(float).map(lambda n: '{:.2%}'.format(n))
|
817 |
|
|
|
|
|
818 |
with st.container():
|
819 |
col1, col2 = st.columns([1, 6])
|
820 |
|
@@ -845,7 +798,6 @@ with tab2:
|
|
845 |
dk_roo_raw = load_dk_player_projections()
|
846 |
fd_roo_raw = load_fd_player_projections()
|
847 |
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
848 |
-
site_slates = set_slate_teams()
|
849 |
dkid_dict, fdid_dict = set_export_ids()
|
850 |
|
851 |
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Secondary Slate', 'Thurs-Mon Slate', 'User'))
|
@@ -862,6 +814,8 @@ with tab2:
|
|
862 |
elif slate_var1 != 'User':
|
863 |
raw_baselines = fd_roo_raw[fd_roo_raw['slate'] == str(slate_var1)]
|
864 |
raw_baselines = raw_baselines[raw_baselines['version'] == 'overall']
|
|
|
|
|
865 |
st.info("If you are uploading a portfolio, note that there is an adjustments to projections and deviation mapping to prevent 'Projection Bias' and create a fair simulation")
|
866 |
insert_port1 = st.selectbox("Are you uploading a portfolio?", ('No', 'Yes'), key='insert_port1')
|
867 |
if insert_port1 == 'Yes':
|
@@ -889,26 +843,7 @@ with tab2:
|
|
889 |
sharp_split = .75
|
890 |
Strength_var = .01
|
891 |
scaling_var = 15
|
892 |
-
|
893 |
-
st.session_state.Sim_Winner_Display = pd.DataFrame(columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own', 'Fantasy', 'GPP_Proj'])
|
894 |
-
if 'Sim_Winner_Frame' not in st.session_state:
|
895 |
-
st.session_state.Sim_Winner_Frame = pd.DataFrame(columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own', 'Fantasy', 'GPP_Proj'])
|
896 |
-
if 'Sim_Winner_Export' not in st.session_state:
|
897 |
-
st.session_state.Sim_Winner_Export = pd.DataFrame(columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own', 'Fantasy', 'GPP_Proj'])
|
898 |
-
if 'player_freq' not in st.session_state:
|
899 |
-
st.session_state.player_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
900 |
-
if 'qb_freq' not in st.session_state:
|
901 |
-
st.session_state.qb_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
902 |
-
if 'rb_freq' not in st.session_state:
|
903 |
-
st.session_state.rb_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
904 |
-
if 'wr_freq' not in st.session_state:
|
905 |
-
st.session_state.wr_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
906 |
-
if 'te_freq' not in st.session_state:
|
907 |
-
st.session_state.te_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
908 |
-
if 'flex_freq' not in st.session_state:
|
909 |
-
st.session_state.flex_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
910 |
-
if 'dst_freq' not in st.session_state:
|
911 |
-
st.session_state.dst_freq = pd.DataFrame(columns=['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge'])
|
912 |
with col2:
|
913 |
with st.container():
|
914 |
if st.button("Simulate Contest"):
|
@@ -926,6 +861,8 @@ with tab2:
|
|
926 |
pass
|
927 |
with st.container():
|
928 |
st.write('Contest Simulation Starting')
|
|
|
|
|
929 |
seed_depth1 = 10
|
930 |
Total_Runs = 1000000
|
931 |
if Contest_Size <= 1000:
|
@@ -966,10 +903,11 @@ with tab2:
|
|
966 |
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
967 |
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
968 |
|
|
|
969 |
del OwnFrame
|
970 |
|
971 |
elif slate_var1 != 'User':
|
972 |
-
initial_proj = raw_baselines
|
973 |
drop_frame = initial_proj.drop_duplicates(subset = 'Player',keep = 'first')
|
974 |
OwnFrame = drop_frame[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
975 |
if contest_var1 == 'Small':
|
@@ -989,6 +927,7 @@ with tab2:
|
|
989 |
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
990 |
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
991 |
|
|
|
992 |
del initial_proj
|
993 |
del drop_frame
|
994 |
del OwnFrame
|
@@ -1225,16 +1164,19 @@ with tab2:
|
|
1225 |
SimVar += 1
|
1226 |
|
1227 |
|
1228 |
-
|
1229 |
-
|
1230 |
-
|
1231 |
-
|
1232 |
-
|
|
|
1233 |
st.write('Contest simulation complete')
|
1234 |
# Initial setup
|
1235 |
Sim_Winner_Frame = pd.DataFrame(np.concatenate(Sim_Winners), columns=FinalPortfolio.columns.tolist() + ['Fantasy'])
|
1236 |
Sim_Winner_Frame['GPP_Proj'] = (Sim_Winner_Frame['Projection'] + Sim_Winner_Frame['Fantasy']) / 2
|
1237 |
|
|
|
|
|
1238 |
# Type Casting
|
1239 |
type_cast_dict = {'Salary': int, 'Projection': np.float16, 'Fantasy': np.float16, 'GPP_Proj': np.float16}
|
1240 |
Sim_Winner_Frame = Sim_Winner_Frame.astype(type_cast_dict)
|
@@ -1245,6 +1187,8 @@ with tab2:
|
|
1245 |
# Data Copying
|
1246 |
st.session_state.Sim_Winner_Export = Sim_Winner_Frame.copy()
|
1247 |
|
|
|
|
|
1248 |
# Conditional Replacement
|
1249 |
columns_to_replace = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
1250 |
|
@@ -1253,11 +1197,14 @@ with tab2:
|
|
1253 |
elif site_var1 == 'Fanduel':
|
1254 |
replace_dict = fdid_dict
|
1255 |
|
|
|
|
|
|
|
1256 |
for col in columns_to_replace:
|
1257 |
st.session_state.Sim_Winner_Export[col].replace(replace_dict, inplace=True)
|
1258 |
|
1259 |
|
1260 |
-
player_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,0:9].values, return_counts=True)),
|
1261 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1262 |
player_freq['Freq'] = player_freq['Freq'].astype(int)
|
1263 |
player_freq['Position'] = player_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1270,8 +1217,9 @@ with tab2:
|
|
1270 |
player_freq['Team'] = player_freq['Team'].replace(item_list, team_list)
|
1271 |
|
1272 |
st.session_state.player_freq = player_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
1273 |
|
1274 |
-
qb_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,0:1].values, return_counts=True)),
|
1275 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1276 |
qb_freq['Freq'] = qb_freq['Freq'].astype(int)
|
1277 |
qb_freq['Position'] = qb_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1284,8 +1232,9 @@ with tab2:
|
|
1284 |
qb_freq['Team'] = qb_freq['Team'].replace(item_list, team_list)
|
1285 |
|
1286 |
st.session_state.qb_freq = qb_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
1287 |
|
1288 |
-
rb_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,[1, 2]].values, return_counts=True)),
|
1289 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1290 |
rb_freq['Freq'] = rb_freq['Freq'].astype(int)
|
1291 |
rb_freq['Position'] = rb_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1298,8 +1247,9 @@ with tab2:
|
|
1298 |
rb_freq['Team'] = rb_freq['Team'].replace(item_list, team_list)
|
1299 |
|
1300 |
st.session_state.rb_freq = rb_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
1301 |
|
1302 |
-
wr_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,[3, 4, 5]].values, return_counts=True)),
|
1303 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1304 |
wr_freq['Freq'] = wr_freq['Freq'].astype(int)
|
1305 |
wr_freq['Position'] = wr_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1312,8 +1262,9 @@ with tab2:
|
|
1312 |
wr_freq['Team'] = wr_freq['Team'].replace(item_list, team_list)
|
1313 |
|
1314 |
st.session_state.wr_freq = wr_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
1315 |
|
1316 |
-
te_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,[6]].values, return_counts=True)),
|
1317 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1318 |
te_freq['Freq'] = te_freq['Freq'].astype(int)
|
1319 |
te_freq['Position'] = te_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1326,8 +1277,9 @@ with tab2:
|
|
1326 |
te_freq['Team'] = te_freq['Team'].replace(item_list, team_list)
|
1327 |
|
1328 |
st.session_state.te_freq = te_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
1329 |
|
1330 |
-
flex_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,[7]].values, return_counts=True)),
|
1331 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1332 |
flex_freq['Freq'] = flex_freq['Freq'].astype(int)
|
1333 |
flex_freq['Position'] = flex_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1340,8 +1292,9 @@ with tab2:
|
|
1340 |
flex_freq['Team'] = flex_freq['Team'].replace(item_list, team_list)
|
1341 |
|
1342 |
st.session_state.flex_freq = flex_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
1343 |
|
1344 |
-
dst_freq = pd.DataFrame(np.column_stack(np.unique(Sim_Winner_Frame.iloc[:,8:9].values, return_counts=True)),
|
1345 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1346 |
dst_freq['Freq'] = dst_freq['Freq'].astype(int)
|
1347 |
dst_freq['Position'] = dst_freq['Player'].map(maps_dict['Pos_map'])
|
@@ -1354,84 +1307,95 @@ with tab2:
|
|
1354 |
dst_freq['Team'] = dst_freq['Team'].replace(item_list, team_list)
|
1355 |
|
1356 |
st.session_state.dst_freq = dst_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
|
|
|
|
1357 |
|
1358 |
with st.container():
|
1359 |
simulate_container = st.empty()
|
1360 |
-
|
1361 |
-
|
1362 |
-
|
1363 |
-
|
1364 |
-
|
1365 |
-
|
1366 |
-
|
1367 |
-
|
1368 |
-
|
1369 |
-
|
1370 |
-
|
1371 |
-
|
1372 |
-
|
1373 |
-
|
1374 |
-
|
1375 |
-
|
1376 |
-
|
1377 |
-
|
|
|
|
|
1378 |
|
1379 |
with st.container():
|
1380 |
freq_container = st.empty()
|
1381 |
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(['Overall Exposures', 'QB Exposures', 'RB Exposures', 'WR Exposures', 'TE Exposures', 'FLEX Exposures', 'DST Exposures'])
|
1382 |
with tab1:
|
1383 |
-
|
1384 |
-
|
1385 |
-
|
1386 |
-
|
1387 |
-
|
1388 |
-
|
1389 |
-
|
|
|
1390 |
with tab2:
|
1391 |
-
|
1392 |
-
|
1393 |
-
|
1394 |
-
|
1395 |
-
|
1396 |
-
|
1397 |
-
|
|
|
1398 |
with tab3:
|
1399 |
-
|
1400 |
-
|
1401 |
-
|
1402 |
-
|
1403 |
-
|
1404 |
-
|
1405 |
-
|
|
|
1406 |
with tab4:
|
1407 |
-
|
1408 |
-
|
1409 |
-
|
1410 |
-
|
1411 |
-
|
1412 |
-
|
1413 |
-
|
|
|
1414 |
with tab5:
|
1415 |
-
|
1416 |
-
|
1417 |
-
|
1418 |
-
|
1419 |
-
|
1420 |
-
|
1421 |
-
|
|
|
1422 |
with tab6:
|
1423 |
-
|
1424 |
-
|
1425 |
-
|
1426 |
-
|
1427 |
-
|
1428 |
-
|
1429 |
-
|
|
|
1430 |
with tab7:
|
1431 |
-
|
1432 |
-
|
1433 |
-
|
1434 |
-
|
1435 |
-
|
1436 |
-
|
1437 |
-
|
|
|
|
5 |
if not name.startswith('_'):
|
6 |
del globals()[name]
|
7 |
|
|
|
8 |
import numpy as np
|
9 |
import pandas as pd
|
10 |
import streamlit as st
|
11 |
import gspread
|
|
|
12 |
import random
|
|
|
|
|
13 |
|
14 |
@st.cache_resource
|
15 |
def init_conn():
|
|
|
34 |
|
35 |
gc = init_conn()
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
freq_format = {'Proj Own': '{:.2%}', 'Exposure': '{:.2%}', 'Edge': '{:.2%}'}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
@st.cache_resource(ttl = 300)
|
40 |
def load_dk_player_projections():
|
41 |
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
|
|
225 |
RandomPortfolio = pd.DataFrame(np.hstack(all_choices), columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST'])
|
226 |
RandomPortfolio['User/Field'] = 0
|
227 |
|
228 |
+
del total_elements
|
229 |
+
del all_choices
|
230 |
del O_merge
|
231 |
|
232 |
return RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict
|
|
|
239 |
stack_num = random.randint(1, 3)
|
240 |
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
RandomPortfolio['QB'] = pd.Series(list(RandomPortfolio['QB'].map(qb_dict)), dtype="string[pyarrow]")
|
243 |
RandomPortfolio['RB1'] = pd.Series(list(RandomPortfolio['RB1'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
244 |
RandomPortfolio['RB2'] = pd.Series(list(RandomPortfolio['RB2'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
|
|
259 |
del stack_num
|
260 |
del stacking_dict
|
261 |
|
|
|
|
|
262 |
RandomPortfolio['QBs'] = RandomPortfolio['QB'].map(maps_dict['Salary_map']).astype(np.int32)
|
263 |
RandomPortfolio['RB1s'] = RandomPortfolio['RB1'].map(maps_dict['Salary_map']).astype(np.int32)
|
264 |
RandomPortfolio['RB2s'] = RandomPortfolio['RB2'].map(maps_dict['Salary_map']).astype(np.int32)
|
|
|
464 |
|
465 |
return RandomPortfolio, maps_dict
|
466 |
|
467 |
+
|
468 |
dk_roo_raw = load_dk_player_projections()
|
469 |
fd_roo_raw = load_fd_player_projections()
|
470 |
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
|
|
471 |
dkid_dict, fdid_dict = set_export_ids()
|
472 |
|
473 |
static_exposure = pd.DataFrame(columns=['Player', 'count'])
|
|
|
766 |
overall_exposure = overall_exposure.sort_values(by='Exposure', ascending=False)
|
767 |
overall_exposure['Exposure'] = overall_exposure['Exposure'].astype(float).map(lambda n: '{:.2%}'.format(n))
|
768 |
|
769 |
+
del static_exposure
|
770 |
+
|
771 |
with st.container():
|
772 |
col1, col2 = st.columns([1, 6])
|
773 |
|
|
|
798 |
dk_roo_raw = load_dk_player_projections()
|
799 |
fd_roo_raw = load_fd_player_projections()
|
800 |
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
|
|
801 |
dkid_dict, fdid_dict = set_export_ids()
|
802 |
|
803 |
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Secondary Slate', 'Thurs-Mon Slate', 'User'))
|
|
|
814 |
elif slate_var1 != 'User':
|
815 |
raw_baselines = fd_roo_raw[fd_roo_raw['slate'] == str(slate_var1)]
|
816 |
raw_baselines = raw_baselines[raw_baselines['version'] == 'overall']
|
817 |
+
del dk_roo_raw
|
818 |
+
del fd_roo_raw
|
819 |
st.info("If you are uploading a portfolio, note that there is an adjustments to projections and deviation mapping to prevent 'Projection Bias' and create a fair simulation")
|
820 |
insert_port1 = st.selectbox("Are you uploading a portfolio?", ('No', 'Yes'), key='insert_port1')
|
821 |
if insert_port1 == 'Yes':
|
|
|
843 |
sharp_split = .75
|
844 |
Strength_var = .01
|
845 |
scaling_var = 15
|
846 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
847 |
with col2:
|
848 |
with st.container():
|
849 |
if st.button("Simulate Contest"):
|
|
|
861 |
pass
|
862 |
with st.container():
|
863 |
st.write('Contest Simulation Starting')
|
864 |
+
for key in st.session_state.keys():
|
865 |
+
del st.session_state[key]
|
866 |
seed_depth1 = 10
|
867 |
Total_Runs = 1000000
|
868 |
if Contest_Size <= 1000:
|
|
|
903 |
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
904 |
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
905 |
|
906 |
+
del proj_dataframe
|
907 |
del OwnFrame
|
908 |
|
909 |
elif slate_var1 != 'User':
|
910 |
+
initial_proj = raw_baselines.copy()
|
911 |
drop_frame = initial_proj.drop_duplicates(subset = 'Player',keep = 'first')
|
912 |
OwnFrame = drop_frame[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
913 |
if contest_var1 == 'Small':
|
|
|
927 |
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
928 |
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
929 |
|
930 |
+
del raw_baselines
|
931 |
del initial_proj
|
932 |
del drop_frame
|
933 |
del OwnFrame
|
|
|
1164 |
SimVar += 1
|
1165 |
|
1166 |
|
1167 |
+
del vec_projection_map
|
1168 |
+
del vec_stdev_map
|
1169 |
+
del sample_arrays
|
1170 |
+
del final_array
|
1171 |
+
del fp_array
|
1172 |
+
del fp_random
|
1173 |
st.write('Contest simulation complete')
|
1174 |
# Initial setup
|
1175 |
Sim_Winner_Frame = pd.DataFrame(np.concatenate(Sim_Winners), columns=FinalPortfolio.columns.tolist() + ['Fantasy'])
|
1176 |
Sim_Winner_Frame['GPP_Proj'] = (Sim_Winner_Frame['Projection'] + Sim_Winner_Frame['Fantasy']) / 2
|
1177 |
|
1178 |
+
del FinalPortfolio
|
1179 |
+
|
1180 |
# Type Casting
|
1181 |
type_cast_dict = {'Salary': int, 'Projection': np.float16, 'Fantasy': np.float16, 'GPP_Proj': np.float16}
|
1182 |
Sim_Winner_Frame = Sim_Winner_Frame.astype(type_cast_dict)
|
|
|
1187 |
# Data Copying
|
1188 |
st.session_state.Sim_Winner_Export = Sim_Winner_Frame.copy()
|
1189 |
|
1190 |
+
del Sim_Winner_Frame
|
1191 |
+
|
1192 |
# Conditional Replacement
|
1193 |
columns_to_replace = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
1194 |
|
|
|
1197 |
elif site_var1 == 'Fanduel':
|
1198 |
replace_dict = fdid_dict
|
1199 |
|
1200 |
+
del dkid_dict
|
1201 |
+
del fdid_dict
|
1202 |
+
|
1203 |
for col in columns_to_replace:
|
1204 |
st.session_state.Sim_Winner_Export[col].replace(replace_dict, inplace=True)
|
1205 |
|
1206 |
|
1207 |
+
player_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:9].values, return_counts=True)),
|
1208 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1209 |
player_freq['Freq'] = player_freq['Freq'].astype(int)
|
1210 |
player_freq['Position'] = player_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1217 |
player_freq['Team'] = player_freq['Team'].replace(item_list, team_list)
|
1218 |
|
1219 |
st.session_state.player_freq = player_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1220 |
+
del player_freq
|
1221 |
|
1222 |
+
qb_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:1].values, return_counts=True)),
|
1223 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1224 |
qb_freq['Freq'] = qb_freq['Freq'].astype(int)
|
1225 |
qb_freq['Position'] = qb_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1232 |
qb_freq['Team'] = qb_freq['Team'].replace(item_list, team_list)
|
1233 |
|
1234 |
st.session_state.qb_freq = qb_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1235 |
+
del qb_freq
|
1236 |
|
1237 |
+
rb_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[1, 2]].values, return_counts=True)),
|
1238 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1239 |
rb_freq['Freq'] = rb_freq['Freq'].astype(int)
|
1240 |
rb_freq['Position'] = rb_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1247 |
rb_freq['Team'] = rb_freq['Team'].replace(item_list, team_list)
|
1248 |
|
1249 |
st.session_state.rb_freq = rb_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1250 |
+
del rb_freq
|
1251 |
|
1252 |
+
wr_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[3, 4, 5]].values, return_counts=True)),
|
1253 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1254 |
wr_freq['Freq'] = wr_freq['Freq'].astype(int)
|
1255 |
wr_freq['Position'] = wr_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1262 |
wr_freq['Team'] = wr_freq['Team'].replace(item_list, team_list)
|
1263 |
|
1264 |
st.session_state.wr_freq = wr_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1265 |
+
del wr_freq
|
1266 |
|
1267 |
+
te_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[6]].values, return_counts=True)),
|
1268 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1269 |
te_freq['Freq'] = te_freq['Freq'].astype(int)
|
1270 |
te_freq['Position'] = te_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1277 |
te_freq['Team'] = te_freq['Team'].replace(item_list, team_list)
|
1278 |
|
1279 |
st.session_state.te_freq = te_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1280 |
+
del te_freq
|
1281 |
|
1282 |
+
flex_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[7]].values, return_counts=True)),
|
1283 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1284 |
flex_freq['Freq'] = flex_freq['Freq'].astype(int)
|
1285 |
flex_freq['Position'] = flex_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1292 |
flex_freq['Team'] = flex_freq['Team'].replace(item_list, team_list)
|
1293 |
|
1294 |
st.session_state.flex_freq = flex_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1295 |
+
del flex_freq
|
1296 |
|
1297 |
+
dst_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,8:9].values, return_counts=True)),
|
1298 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1299 |
dst_freq['Freq'] = dst_freq['Freq'].astype(int)
|
1300 |
dst_freq['Position'] = dst_freq['Player'].map(maps_dict['Pos_map'])
|
|
|
1307 |
dst_freq['Team'] = dst_freq['Team'].replace(item_list, team_list)
|
1308 |
|
1309 |
st.session_state.dst_freq = dst_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1310 |
+
del dst_freq
|
1311 |
+
del maps_dict
|
1312 |
|
1313 |
with st.container():
|
1314 |
simulate_container = st.empty()
|
1315 |
+
if 'player_freq' in st.session_state:
|
1316 |
+
player_split_var2 = st.radio("Are you wanting to isolate any lineups with specific players?", ('Full Players', 'Specific Players'), key='player_split_var2')
|
1317 |
+
if player_split_var2 == 'Specific Players':
|
1318 |
+
find_var2 = st.multiselect('Which players must be included in the lineups?', options = st.session_state.player_freq['Player'].unique())
|
1319 |
+
elif player_split_var2 == 'Full Players':
|
1320 |
+
find_var2 = st.session_state.player_freq.Player.values.tolist()
|
1321 |
+
|
1322 |
+
if player_split_var2 == 'Specific Players':
|
1323 |
+
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame[np.equal.outer(st.session_state.Sim_Winner_Frame.to_numpy(copy=False), find_var2).any(axis=1).all(axis=1)]
|
1324 |
+
if player_split_var2 == 'Full Players':
|
1325 |
+
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame
|
1326 |
+
if 'Sim_Winner_Display' in st.session_state:
|
1327 |
+
st.dataframe(st.session_state.Sim_Winner_Display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Own']).format(precision=2), use_container_width = True)
|
1328 |
+
if 'Sim_Winner_Export' in st.session_state:
|
1329 |
+
st.download_button(
|
1330 |
+
label="Export Tables",
|
1331 |
+
data=convert_df_to_csv(st.session_state.Sim_Winner_Export),
|
1332 |
+
file_name='NFL_consim_export.csv',
|
1333 |
+
mime='text/csv',
|
1334 |
+
)
|
1335 |
|
1336 |
with st.container():
|
1337 |
freq_container = st.empty()
|
1338 |
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(['Overall Exposures', 'QB Exposures', 'RB Exposures', 'WR Exposures', 'TE Exposures', 'FLEX Exposures', 'DST Exposures'])
|
1339 |
with tab1:
|
1340 |
+
if 'player_freq' in st.session_state:
|
1341 |
+
st.dataframe(st.session_state.player_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1342 |
+
st.download_button(
|
1343 |
+
label="Export Exposures",
|
1344 |
+
data=convert_df_to_csv(st.session_state.player_freq),
|
1345 |
+
file_name='player_freq_export.csv',
|
1346 |
+
mime='text/csv',
|
1347 |
+
)
|
1348 |
with tab2:
|
1349 |
+
if 'qb_freq' in st.session_state:
|
1350 |
+
st.dataframe(st.session_state.qb_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1351 |
+
st.download_button(
|
1352 |
+
label="Export Exposures",
|
1353 |
+
data=convert_df_to_csv(st.session_state.qb_freq),
|
1354 |
+
file_name='qb_freq_export.csv',
|
1355 |
+
mime='text/csv',
|
1356 |
+
)
|
1357 |
with tab3:
|
1358 |
+
if 'rb_freq' in st.session_state:
|
1359 |
+
st.dataframe(st.session_state.rb_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1360 |
+
st.download_button(
|
1361 |
+
label="Export Exposures",
|
1362 |
+
data=convert_df_to_csv(st.session_state.rb_freq),
|
1363 |
+
file_name='rb_freq_export.csv',
|
1364 |
+
mime='text/csv',
|
1365 |
+
)
|
1366 |
with tab4:
|
1367 |
+
if 'wr_freq' in st.session_state:
|
1368 |
+
st.dataframe(st.session_state.wr_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1369 |
+
st.download_button(
|
1370 |
+
label="Export Exposures",
|
1371 |
+
data=convert_df_to_csv(st.session_state.wr_freq),
|
1372 |
+
file_name='wr_freq_export.csv',
|
1373 |
+
mime='text/csv',
|
1374 |
+
)
|
1375 |
with tab5:
|
1376 |
+
if 'te_freq' in st.session_state:
|
1377 |
+
st.dataframe(st.session_state.te_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1378 |
+
st.download_button(
|
1379 |
+
label="Export Exposures",
|
1380 |
+
data=convert_df_to_csv(st.session_state.te_freq),
|
1381 |
+
file_name='te_freq_export.csv',
|
1382 |
+
mime='text/csv',
|
1383 |
+
)
|
1384 |
with tab6:
|
1385 |
+
if 'flex_freq' in st.session_state:
|
1386 |
+
st.dataframe(st.session_state.flex_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1387 |
+
st.download_button(
|
1388 |
+
label="Export Exposures",
|
1389 |
+
data=convert_df_to_csv(st.session_state.flex_freq),
|
1390 |
+
file_name='flex_freq_export.csv',
|
1391 |
+
mime='text/csv',
|
1392 |
+
)
|
1393 |
with tab7:
|
1394 |
+
if 'dst_freq' in st.session_state:
|
1395 |
+
st.dataframe(st.session_state.dst_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1396 |
+
st.download_button(
|
1397 |
+
label="Export Exposures",
|
1398 |
+
data=convert_df_to_csv(st.session_state.dst_freq),
|
1399 |
+
file_name='dst_freq_export.csv',
|
1400 |
+
mime='text/csv',
|
1401 |
+
)
|