Spaces:
Sleeping
Sleeping
File size: 19,687 Bytes
b41ab08 9f83d6b b41ab08 ff43a69 b41ab08 ff43a69 b41ab08 ff43a69 b41ab08 ca1122e 4a6242b b41ab08 ff43a69 b41ab08 ff43a69 b41ab08 ca1122e b41ab08 ca1122e 9f83d6b ca1122e 9f83d6b ca1122e b41ab08 ca1122e |
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 |
import numpy as np
from numpy import random
import pandas as pd
import streamlit as st
import pymongo
st.set_page_config(layout="wide")
@st.cache_resource
def init_conn():
uri = st.secrets['mongo_uri']
client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
db = client["NHL_Database"]
return db
db = init_conn()
wrong_acro = ['WSH', 'AZ']
right_acro = ['WAS', 'ARI']
game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
team_roo_format = {'Top Score%': '{:.2%}','0 Runs': '{:.2%}', '1 Run': '{:.2%}', '2 Runs': '{:.2%}', '3 Runs': '{:.2%}', '4 Runs': '{:.2%}',
'5 Runs': '{:.2%}','6 Runs': '{:.2%}', '7 Runs': '{:.2%}', '8 Runs': '{:.2%}', '9 Runs': '{:.2%}', '10 Runs': '{:.2%}'}
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
'4x%': '{:.2%}','GPP%': '{:.2%}'}
st.markdown("""
<style>
/* Tab styling */
.stTabs [data-baseweb="tab-list"] {
gap: 8px;
padding: 4px;
}
.stTabs [data-baseweb="tab"] {
height: 50px;
white-space: pre-wrap;
background-color: #FFD700;
color: white;
border-radius: 10px;
gap: 1px;
padding: 10px 20px;
font-weight: bold;
transition: all 0.3s ease;
}
.stTabs [aria-selected="true"] {
background-color: #DAA520;
color: white;
}
.stTabs [data-baseweb="tab"]:hover {
background-color: #DAA520;
cursor: pointer;
}
</style>""", unsafe_allow_html=True)
@st.cache_resource(ttl = 599)
def player_stat_table():
collection = db["Player_Level_ROO"]
cursor = collection.find()
load_display = pd.DataFrame(cursor)
load_display.replace('', np.nan, inplace=True)
player_stats = load_display.copy()
dk_load_display = load_display[load_display['Site'] == 'Draftkings']
fd_load_display = load_display[load_display['Site'] == 'Fanduel']
dk_load_display = dk_load_display.sort_values(by='Own', ascending=False)
fd_load_display = fd_load_display.sort_values(by='Own', ascending=False)
dk_load_display = dk_load_display.dropna(subset=['Own'])
fd_load_display = fd_load_display.dropna(subset=['Own'])
dk_roo_raw = dk_load_display
fd_roo_raw = fd_load_display
return player_stats, dk_roo_raw, fd_roo_raw
@st.cache_data
def convert_df_to_csv(df):
return df.to_csv().encode('utf-8')
player_stats, dk_roo_raw, fd_roo_raw = player_stat_table()
opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
st.header("NHL Pivot Finder Tool")
with st.expander("Info and Filters"):
st.info(t_stamp)
if st.button("Load/Reset Data", key='reset1'):
st.cache_data.clear()
for key in st.session_state.keys():
del st.session_state[key]
player_stats, dk_roo_raw, fd_roo_raw = player_stat_table()
opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
if site_var1 == 'Draftkings':
raw_baselines = dk_roo_raw[dk_roo_raw['Slate'] == 'Main Slate']
raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
elif site_var1 == 'Fanduel':
raw_baselines = fd_roo_raw[fd_roo_raw['Slate'] == 'Main Slate']
raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
check_seq = st.radio("Do you want to check a single player or the top 10 in ownership?", ('Single Player', 'Top X Owned'), key='check_seq')
if check_seq == 'Single Player':
player_check = st.selectbox('Select player to create comps', options = raw_baselines['Player'].unique(), key='dk_player')
elif check_seq == 'Top X Owned':
top_x_var = st.number_input('How many players would you like to check?', min_value = 1, max_value = 10, value = 5, step = 1)
Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
if pos_var1 == 'Specific Positions':
pos_var_list = st.multiselect('Which positions would you like to include?', options = raw_baselines['Position'].unique(), key='pos_var_list')
elif pos_var1 == 'All Positions':
pos_var_list = raw_baselines.Position.values.tolist()
split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
if split_var1 == 'Specific Games':
team_var1 = st.multiselect('Which teams would you like to include?', options = raw_baselines['Team'].unique(), key='team_var1')
elif split_var1 == 'Full Slate Run':
team_var1 = raw_baselines.Team.values.tolist()
placeholder = st.empty()
displayholder = st.empty()
if st.button('Simulate appropriate pivots'):
with placeholder:
if site_var1 == 'Draftkings':
working_roo = raw_baselines
working_roo.replace('', 0, inplace=True)
if site_var1 == 'Fanduel':
working_roo = raw_baselines
working_roo.replace('', 0, inplace=True)
own_dict = dict(zip(working_roo.Player, working_roo.Own))
team_dict = dict(zip(working_roo.Player, working_roo.Team))
opp_dict = dict(zip(working_roo.Player, working_roo.Opp))
pos_dict = dict(zip(working_roo.Player, working_roo.Position))
total_sims = 1000
if check_seq == 'Single Player':
player_var = working_roo.loc[working_roo['Player'] == player_check]
player_var = player_var.reset_index()
working_roo = working_roo[working_roo['Position'].isin(pos_var_list)]
working_roo = working_roo[working_roo['Team'].isin(team_var1)]
working_roo = working_roo.loc[(working_roo['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo['Salary'] <= player_var['Salary'][0] + Salary_var)]
working_roo = working_roo.loc[(working_roo['Median'] >= player_var['Median'][0] - Median_var) & (working_roo['Median'] <= player_var['Median'][0] + Median_var)]
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median']]
flex_file['Floor_raw'] = flex_file['Median'] * .25
flex_file['Ceiling_raw'] = flex_file['Median'] * 2
flex_file['Floor'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
flex_file['Floor'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
flex_file['STD'] = flex_file['Median'] / 3
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
hold_file = flex_file.copy()
overall_file = flex_file.copy()
salary_file = flex_file.copy()
overall_players = overall_file[['Player']]
for x in range(0,total_sims):
salary_file[x] = salary_file['Salary']
overall_file[x] = random.normal(overall_file['Median'],overall_file['STD'])
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
salary_file = salary_file.div(1000)
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
players_only = hold_file[['Player']]
raw_lineups_file = players_only
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)
salary_2x_check = (overall_file - (salary_file*2))
salary_3x_check = (overall_file - (salary_file*3))
salary_4x_check = (overall_file - (salary_file*4))
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['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
players_only['Player'] = hold_file[['Player']]
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own']]
final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
final_Proj['LevX'] = 0
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'C', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'W', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'D', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'G', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['CPT_Own'] = final_Proj['Own'] / 4
final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', 'LevX']]
final_Proj = final_Proj.set_index('Player')
st.session_state.final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
elif check_seq == 'Top X Owned':
if pos_var1 == 'Specific Positions':
raw_baselines = raw_baselines[raw_baselines['Position'].isin(pos_var_list)]
player_check = raw_baselines['Player'].head(top_x_var).tolist()
final_proj_list = []
for players in player_check:
players_pos = pos_dict[players]
player_var = working_roo.loc[working_roo['Player'] == players]
player_var = player_var.reset_index()
working_roo_temp = working_roo[working_roo['Position'] == players_pos]
working_roo_temp = working_roo_temp[working_roo_temp['Team'].isin(team_var1)]
working_roo_temp = working_roo_temp.loc[(working_roo_temp['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo_temp['Salary'] <= player_var['Salary'][0] + Salary_var)]
working_roo_temp = working_roo_temp.loc[(working_roo_temp['Median'] >= player_var['Median'][0] - Median_var) & (working_roo_temp['Median'] <= player_var['Median'][0] + Median_var)]
flex_file = working_roo_temp[['Player', 'Position', 'Salary', 'Median']]
flex_file['Floor_raw'] = flex_file['Median'] * .25
flex_file['Ceiling_raw'] = flex_file['Median'] * 2
flex_file['Floor'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
flex_file['Floor'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
flex_file['STD'] = flex_file['Median'] / 3
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
hold_file = flex_file.copy()
overall_file = flex_file.copy()
salary_file = flex_file.copy()
overall_players = overall_file[['Player']]
for x in range(0,total_sims):
salary_file[x] = salary_file['Salary']
overall_file[x] = random.normal(overall_file['Median'],overall_file['STD'])
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
salary_file = salary_file.div(1000)
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
players_only = hold_file[['Player']]
raw_lineups_file = players_only
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)
salary_2x_check = (overall_file - (salary_file*2))
salary_3x_check = (overall_file - (salary_file*3))
salary_4x_check = (overall_file - (salary_file*4))
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['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
players_only['Player'] = hold_file[['Player']]
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own']]
final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
final_Proj['LevX'] = 0
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'C', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'W', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'D', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['LevX'] = np.where(final_Proj['Position'] == 'G', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
final_Proj['CPT_Own'] = final_Proj['Own'] / 4
final_Proj['Pivot_source'] = players
final_Proj = final_Proj[['Player', 'Pivot_source', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', 'LevX']]
final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
final_proj_list.append(final_Proj)
st.write(f'finished run for {players}')
# Concatenate all the final_Proj dataframes
final_Proj_combined = pd.concat(final_proj_list)
final_Proj_combined = final_Proj_combined.sort_values(by='LevX', ascending=False)
final_Proj_combined = final_Proj_combined[final_Proj_combined['Player'] != final_Proj_combined['Pivot_source']]
st.session_state.final_Proj = final_Proj_combined.reset_index(drop=True) # Assign the combined dataframe back to final_Proj
placeholder.empty()
with displayholder.container():
if 'final_Proj' in st.session_state:
st.dataframe(st.session_state.final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), use_container_width = True)
st.download_button(
label="Export Tables",
data=convert_df_to_csv(st.session_state.final_Proj),
file_name='NHL_pivot_export.csv',
mime='text/csv',
)
else:
st.write("Run some pivots my dude/dudette")
|