Spaces:
Running
Running
import numpy as np | |
import pandas as pd | |
import streamlit as st | |
import gspread | |
import plotly.figure_factory as ff | |
from itertools import combinations | |
scope = ['https://www.googleapis.com/auth/spreadsheets', | |
"https://www.googleapis.com/auth/drive"] | |
credentials = { | |
"type": "service_account", | |
"project_id": "sheets-api-connect-378620", | |
"private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9", | |
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKa01beXwc88R\nnPZVQTNPVQuBnbwoOfc66gW3547ja/UEyIGAF112dt/VqHprRafkKGmlg55jqJNt\na4zceLKV+wTm7vBu7lDISTJfGzCf2TrxQYNqwMKE2LOjI69dBM8u4Dcb4k0wcp9v\ntW1ZzLVVuwTvmrg7JBHjiSaB+x5wxm/r3FOiJDXdlAgFlytzqgcyeZMJVKKBQHyJ\njEGg/1720A0numuOCt71w/2G0bDmijuj1e6tH32MwRWcvRNZ19K9ssyDz2S9p68s\nYDhIxX69OWxwScTIHLY6J2t8txf/XMivL/636fPlDADvBEVTdlT606n8CcKUVQeq\npUVdG+lfAgMBAAECggEAP38SUA7B69eTfRpo658ycOs3Amr0JW4H/bb1rNeAul0K\nZhwd/HnU4E07y81xQmey5kN5ZeNrD5EvqkZvSyMJHV0EEahZStwhjCfnDB/cxyix\nZ+kFhv4y9eK+kFpUAhBy5nX6T0O+2T6WvzAwbmbVsZ+X8kJyPuF9m8ldcPlD0sce\ntj8NwVq1ys52eosqs7zi2vjt+eMcaY393l4ls+vNq8Yf27cfyFw45W45CH/97/Nu\n5AmuzlCOAfFF+z4OC5g4rei4E/Qgpxa7/uom+BVfv9G0DIGW/tU6Sne0+37uoGKt\nW6DzhgtebUtoYkG7ZJ05BTXGp2lwgVcNRoPwnKJDxQKBgQDT5wYPUBDW+FHbvZSp\nd1m1UQuXyerqOTA9smFaM8sr/UraeH85DJPEIEk8qsntMBVMhvD3Pw8uIUeFNMYj\naLmZFObsL+WctepXrVo5NB6RtLB/jZYxiKMatMLUJIYtcKIp+2z/YtKiWcLnwotB\nWdCjVnPTxpkurmF2fWP/eewZ+wKBgQDRMtJg7etjvKyjYNQ5fARnCc+XsI3gkBe1\nX9oeXfhyfZFeBXWnZzN1ITgFHplDznmBdxAyYGiQdbbkdKQSghviUQ0igBvoDMYy\n1rWcy+a17Mj98uyNEfmb3X2cC6WpvOZaGHwg9+GY67BThwI3FqHIbyk6Ko09WlTX\nQpRQjMzU7QKBgAfi1iflu+q0LR+3a3vvFCiaToskmZiD7latd9AKk2ocsBd3Woy9\n+hXXecJHPOKV4oUJlJgvAZqe5HGBqEoTEK0wyPNLSQlO/9ypd+0fEnArwFHO7CMF\nycQprAKHJXM1eOOFFuZeQCaInqdPZy1UcV5Szla4UmUZWkk1m24blHzXAoGBAMcA\nyH4qdbxX9AYrC1dvsSRvgcnzytMvX05LU0uF6tzGtG0zVlub4ahvpEHCfNuy44UT\nxRWW/oFFaWjjyFxO5sWggpUqNuHEnRopg3QXx22SRRTGbN45li/+QAocTkgsiRh1\nqEcYZsO4mPCsQqAy6E2p6RcK+Xa+omxvSnVhq0x1AoGAKr8GdkCl4CF6rieLMAQ7\nLNBuuoYGaHoh8l5E2uOQpzwxVy/nMBcAv+2+KqHEzHryUv1owOi6pMLv7A9mTFoS\n18B0QRLuz5fSOsVnmldfC9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n", | |
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com", | |
"client_id": "106625872877651920064", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com" | |
} | |
gc = gspread.service_account_from_dict(credentials) | |
st.set_page_config(layout="wide") | |
master_hold = 'https://docs.google.com/spreadsheets/d/15flX6E7lPxu_HC7IOHpB3VEg2Am1AmtxTo9c2y_I-Mw/edit?gid=676575006#gid=676575006' | |
def init_baselines(): | |
sh = gc.open_by_url(master_hold) | |
worksheet = sh.worksheet('ADPs (model)') | |
adp_hold = pd.DataFrame(worksheet.get_all_records()) | |
adp_hold = adp_hold[['Player', 'Team', 'Bye', 'Position', 'Position Rank', 'Underdog', 'MFL10', 'RTSPORTS', 'AVG', 'Projection', 'Proj ADP', 'Diff']] | |
adp_table = adp_hold.drop_duplicates(subset='Player') | |
worksheet = sh.worksheet('Stacks (model)') | |
stacks_hold = pd.DataFrame(worksheet.get_all_records()) | |
stacks_table = stacks_hold.drop_duplicates(subset='Team') | |
worksheet = sh.worksheet('Player Level Projections') | |
proj_hold = pd.DataFrame(worksheet.get_all_records()) | |
proj_table = proj_hold[['Player', 'Team', 'Pos', 'Pass Yards', 'PassTD', 'Rush Yards', 'RushTD', 'Receptions', 'Rec Yards', 'RecTD', 'Proj']] | |
return adp_table, stacks_table, proj_table | |
adp_table, stacks_table, proj_table = init_baselines() | |
tab1, tab2, tab3, tab4 = st.tabs(["ADPs and Ranks", "Team Projections", 'Player Projections', "Stack Finder"]) | |
def convert_df_to_csv(df): | |
return df.to_csv().encode('utf-8') | |
with tab1: | |
col1, col2 = st.columns([1, 5]) | |
with col1: | |
if st.button("Load/Reset Data", key='reset1'): | |
st.cache_data.clear() | |
adp_table, stacks_table, proj_table = init_baselines() | |
site_var1 = st.radio("What site are you playing?", ('Underdog', 'MFL10'), key='site_var1') | |
split_var1 = st.radio("Would you like to run stack analysis for the full slate or individual teams?", ('All Teams', 'Specific Teams'), key='split_var1') | |
if split_var1 == 'Specific Teams': | |
team_var1 = st.multiselect('Which teams would you like to include in the analysis?', options = adp_table['Team'].unique(), key='team_var1') | |
elif split_var1 == 'All Teams': | |
team_var1 = adp_table.Team.unique().tolist() | |
pos_split1 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1') | |
if pos_split1 == 'Specific Positions': | |
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['QB', 'RB', 'WR', 'TE']) | |
elif pos_split1 == 'All Positions': | |
pos_var1 = adp_table.Position.unique().tolist() | |
with col2: | |
stack_hold_container = st.empty() | |
working_baselines = adp_table.copy() | |
if pos_split1 == 'All Positions': | |
raw_baselines = working_baselines | |
elif pos_split1 != 'All Positions': | |
raw_baselines = working_baselines[working_baselines['Position'].str.contains('|'.join(pos_var1))] | |
if split_var1 == 'All Teams': | |
raw_baselines = raw_baselines | |
elif split_var1 != 'All Teams': | |
raw_baselines = raw_baselines[raw_baselines['Team'].str.contains('|'.join(team_var1))] | |
display_frame = raw_baselines.copy() | |
display_frame = display_frame.sort_values(by='Proj ADP', ascending=False) | |
with stack_hold_container: | |
stack_hold_container = st.empty() | |
st.dataframe(display_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Position Rank', 'Proj ADP']).format(precision=2), height=750, use_container_width = True) | |
st.download_button( | |
label="Export Tables", | |
data=convert_df_to_csv(display_frame), | |
file_name='NFL_Stack_Options_export.csv', | |
mime='text/csv', | |
) | |
with tab2: | |
st.write('working on it') | |
with tab3: | |
st.write('working on it') | |
with tab4: | |
col1, col2 = st.columns([1, 5]) | |
with col1: | |
if st.button("Load/Reset Data", key='reset4'): | |
st.cache_data.clear() | |
adp_table, stacks_table, proj_table = init_baselines() | |
site_var4 = st.radio("What site are you playing?", ('Underdog', 'MFL10'), key='site_var2') | |
split_var4 = st.radio("Would you like to run stack analysis for the full slate or individual teams?", ('All Teams', 'Specific Teams'), key='split_var4') | |
if split_var4 == 'Specific Teams': | |
team_var4 = st.multiselect('Which teams would you like to include in the analysis?', options = adp_table['Team'].unique(), key='team_var4') | |
elif split_var4 == 'All Teams': | |
team_var4 = adp_table.Team.unique().tolist() | |
pos_split4 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split4') | |
if pos_split4 == 'Specific Positions': | |
pos_var4 = st.multiselect('What Positions would you like to view?', options = ['QB', 'RB', 'WR', 'TE'], key='pos_var4') | |
elif pos_split4 == 'All Positions': | |
pos_var4 = adp_table.Position.unique().tolist() | |
if site_var4 == 'Underdog': | |
adp_dict = dict(zip(adp_table.Player, adp_table.Underdog)) | |
elif site_var4 == 'MFL10': | |
adp_dict = dict(zip(adp_table.Player, adp_table.MFL10)) | |
size_var4 = st.number_input('What size of stacks are you analyzing?', min_value = 3, max_value = 6, step=1) | |
stack_size = size_var4 | |
cut_var4 = st.radio("Do you want to remove stacks with a negative average value?", ('Yes', 'No'), key='cut_var4') | |
if cut_var4 == "Yes": | |
cut_sequence = 1 | |
elif cut_var4 == "No": | |
cut_sequence = 0 | |
team_dict = dict(zip(adp_table.Player, adp_table.Team)) | |
proj_dict = dict(zip(adp_table.Player, adp_table.Projection)) | |
diff_dict = dict(zip(adp_table.Player, adp_table.Diff)) | |
with col2: | |
stack_hold_container = st.empty() | |
if st.button('Run stack analysis'): | |
comb_list = [] | |
if pos_split4 == 'All Positions': | |
raw_baselines = adp_table.copy() | |
elif pos_split4 != 'All Positions': | |
raw_baselines = adp_table[adp_table['Position'].str.contains('|'.join(pos_var4))] | |
for cur_team in team_var4: | |
working_baselines = raw_baselines.copy() | |
working_baselines = working_baselines[working_baselines['Team'] == cur_team] | |
order_list = working_baselines['Player'] | |
comb = combinations(order_list, stack_size) | |
for i in list(comb): | |
comb_list.append(i) | |
comb_DF = pd.DataFrame(comb_list) | |
if stack_size == 3: | |
comb_DF['Team'] = comb_DF[0].map(team_dict) | |
comb_DF['Proj'] = comb_DF.apply(lambda row: pd.Series([proj_dict.get(row[i], None) for i in range(3)]).sum(), axis=1) | |
comb_DF['ADP_1'] = comb_DF[0].map(adp_dict) | |
comb_DF['ADP_2'] = comb_DF[1].map(adp_dict) | |
comb_DF['ADP_3'] = comb_DF[2].map(adp_dict) | |
comb_DF['Value'] = comb_DF.apply(lambda row: pd.Series([diff_dict.get(row[i], None) for i in range(3)]).mean(), axis=1) | |
elif stack_size == 4: | |
comb_DF['Team'] = comb_DF[0].map(team_dict) | |
comb_DF['Proj'] = comb_DF.apply(lambda row: pd.Series([proj_dict.get(row[i], None) for i in range(4)]).sum(), axis=1) | |
comb_DF['ADP_1'] = comb_DF[0].map(adp_dict) | |
comb_DF['ADP_2'] = comb_DF[1].map(adp_dict) | |
comb_DF['ADP_3'] = comb_DF[2].map(adp_dict) | |
comb_DF['ADP_4'] = comb_DF[3].map(adp_dict) | |
comb_DF['Value'] = comb_DF.apply(lambda row: pd.Series([diff_dict.get(row[i], None) for i in range(4)]).mean(), axis=1) | |
elif stack_size == 5: | |
comb_DF['Team'] = comb_DF[0].map(team_dict) | |
comb_DF['Proj'] = comb_DF.apply(lambda row: pd.Series([proj_dict.get(row[i], None) for i in range(5)]).sum(), axis=1) | |
comb_DF['ADP_1'] = comb_DF[0].map(adp_dict) | |
comb_DF['ADP_2'] = comb_DF[1].map(adp_dict) | |
comb_DF['ADP_3'] = comb_DF[2].map(adp_dict) | |
comb_DF['ADP_4'] = comb_DF[3].map(adp_dict) | |
comb_DF['ADP_5'] = comb_DF[4].map(adp_dict) | |
comb_DF['Value'] = comb_DF.apply(lambda row: pd.Series([diff_dict.get(row[i], None) for i in range(5)]).mean(), axis=1) | |
elif stack_size == 6: | |
comb_DF['Team'] = comb_DF[0].map(team_dict) | |
comb_DF['Proj'] = comb_DF.apply(lambda row: pd.Series([proj_dict.get(row[i], None) for i in range(6)]).sum(), axis=1) | |
comb_DF['ADP_1'] = comb_DF[0].map(adp_dict) | |
comb_DF['ADP_2'] = comb_DF[1].map(adp_dict) | |
comb_DF['ADP_3'] = comb_DF[2].map(adp_dict) | |
comb_DF['ADP_4'] = comb_DF[3].map(adp_dict) | |
comb_DF['ADP_5'] = comb_DF[4].map(adp_dict) | |
comb_DF['ADP_6'] = comb_DF[5].map(adp_dict) | |
comb_DF['Value'] = comb_DF.apply(lambda row: pd.Series([diff_dict.get(row[i], None) for i in range(6)]).mean(), axis=1) | |
comb_DF = comb_DF.sort_values(by='Proj', ascending=False) | |
if cut_sequence == 1: | |
cut_var = 0 | |
if stack_size == 3: | |
while cut_var <= int(len(comb_DF)): | |
try: | |
if int(cut_var) == 0: | |
cur_proj = float(comb_DF.iat[cut_var,4]) | |
cur_own = 0 | |
elif int(cut_var) >= 1: | |
check_own = float(comb_DF.iat[cut_var,8]) | |
if check_own < cur_own: | |
comb_DF = comb_DF.drop([cut_var]) | |
cur_own = cur_own | |
cut_var = cut_var - 1 | |
comb_DF = comb_DF.reset_index() | |
comb_DF = comb_DF.drop(['index'], axis=1) | |
elif check_own >= cur_own: | |
cur_own = float(comb_DF.iat[cut_var,8]) | |
cut_var = cut_var | |
cut_var += 1 | |
except: | |
cut_var += 1 | |
elif stack_size == 4: | |
while cut_var <= int(len(comb_DF)): | |
try: | |
if int(cut_var) == 0: | |
cur_proj = float(comb_DF.iat[cut_var,5]) | |
cur_own = 0 | |
elif int(cut_var) >= 1: | |
check_own = float(comb_DF.iat[cut_var,10]) | |
if check_own < cur_own: | |
comb_DF = comb_DF.drop([cut_var]) | |
cur_own = cur_own | |
cut_var = cut_var - 1 | |
comb_DF = comb_DF.reset_index() | |
comb_DF = comb_DF.drop(['index'], axis=1) | |
elif check_own >= cur_own: | |
cur_own = float(comb_DF.iat[cut_var,10]) | |
cut_var = cut_var | |
cut_var += 1 | |
except: | |
cut_var += 1 | |
elif stack_size == 5: | |
while cut_var <= int(len(comb_DF)): | |
try: | |
if int(cut_var) == 0: | |
cur_proj = float(comb_DF.iat[cut_var,6]) | |
cur_own = 0 | |
elif int(cut_var) >= 1: | |
check_own = float(comb_DF.iat[cut_var,12]) | |
if check_own < cur_own: | |
comb_DF = comb_DF.drop([cut_var]) | |
cur_own = cur_own | |
cut_var = cut_var - 1 | |
comb_DF = comb_DF.reset_index() | |
comb_DF = comb_DF.drop(['index'], axis=1) | |
elif check_own >= cur_own: | |
cur_own = float(comb_DF.iat[cut_var,12]) | |
cut_var = cut_var | |
cut_var += 1 | |
except: | |
cut_var += 1 | |
elif stack_size == 6: | |
while cut_var <= int(len(comb_DF)): | |
try: | |
if int(cut_var) == 0: | |
cur_proj = float(comb_DF.iat[cut_var,7]) | |
cur_own = 0 | |
elif int(cut_var) >= 1: | |
check_own = float(comb_DF.iat[cut_var,14]) | |
if check_own < cur_own: | |
comb_DF = comb_DF.drop([cut_var]) | |
cur_own = cur_own | |
cut_var = cut_var - 1 | |
comb_DF = comb_DF.reset_index() | |
comb_DF = comb_DF.drop(['index'], axis=1) | |
elif check_own >= cur_own: | |
cur_own = float(comb_DF.iat[cut_var,14]) | |
cut_var = cut_var | |
cut_var += 1 | |
except: | |
cut_var += 1 | |
with stack_hold_container: | |
stack_hold_container = st.empty() | |
st.dataframe(comb_DF.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True) | |
st.download_button( | |
label="Export Tables", | |
data=convert_df_to_csv(comb_DF), | |
file_name='NFL_Stack_Options_export.csv', | |
mime='text/csv', | |
) |