Spaces:
Sleeping
Sleeping
James McCool
commited on
Commit
·
629b5f7
1
Parent(s):
5898ebf
Add Streamlit app for NHL pivot analysis with MongoDB integration and deployment configuration
Browse files- app.py +328 -0
- app.yaml +10 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,328 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy import random
|
3 |
+
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
+
import pymongo
|
6 |
+
|
7 |
+
st.set_page_config(layout="wide")
|
8 |
+
|
9 |
+
@st.cache_resource
|
10 |
+
def init_conn():
|
11 |
+
uri = st.secrets['mongo_uri']
|
12 |
+
client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
|
13 |
+
db = client["NHL_Database"]
|
14 |
+
|
15 |
+
return db
|
16 |
+
|
17 |
+
db = init_conn()
|
18 |
+
|
19 |
+
wrong_acro = ['WSH', 'AZ']
|
20 |
+
right_acro = ['WAS', 'ARI']
|
21 |
+
|
22 |
+
game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
|
23 |
+
'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
|
24 |
+
|
25 |
+
team_roo_format = {'Top Score%': '{:.2%}','0 Runs': '{:.2%}', '1 Run': '{:.2%}', '2 Runs': '{:.2%}', '3 Runs': '{:.2%}', '4 Runs': '{:.2%}',
|
26 |
+
'5 Runs': '{:.2%}','6 Runs': '{:.2%}', '7 Runs': '{:.2%}', '8 Runs': '{:.2%}', '9 Runs': '{:.2%}', '10 Runs': '{:.2%}'}
|
27 |
+
|
28 |
+
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
|
29 |
+
'4x%': '{:.2%}','GPP%': '{:.2%}'}
|
30 |
+
|
31 |
+
@st.cache_resource(ttl = 599)
|
32 |
+
def player_stat_table():
|
33 |
+
collection = db["Player_Level_ROO"]
|
34 |
+
cursor = collection.find()
|
35 |
+
load_display = pd.DataFrame(cursor)
|
36 |
+
|
37 |
+
load_display.replace('', np.nan, inplace=True)
|
38 |
+
player_stats = load_display.copy()
|
39 |
+
|
40 |
+
dk_load_display = load_display[load_display['Site'] == 'Draftkings']
|
41 |
+
fd_load_display = load_display[load_display['Site'] == 'Fanduel']
|
42 |
+
|
43 |
+
dk_load_display = dk_load_display.sort_values(by='Own', ascending=False)
|
44 |
+
fd_load_display = fd_load_display.sort_values(by='Own', ascending=False)
|
45 |
+
|
46 |
+
dk_load_display = dk_load_display.dropna(subset=['Own'])
|
47 |
+
fd_load_display = fd_load_display.dropna(subset=['Own'])
|
48 |
+
|
49 |
+
dk_roo_raw = dk_load_display
|
50 |
+
fd_roo_raw = fd_load_display
|
51 |
+
|
52 |
+
return player_stats, dk_roo_raw, fd_roo_raw
|
53 |
+
|
54 |
+
@st.cache_data
|
55 |
+
def convert_df_to_csv(df):
|
56 |
+
return df.to_csv().encode('utf-8')
|
57 |
+
|
58 |
+
player_stats, dk_roo_raw, fd_roo_raw = player_stat_table()
|
59 |
+
opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
|
60 |
+
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
61 |
+
|
62 |
+
tab1, tab2 = st.tabs(['Pivot Finder', 'Uploads and Info'])
|
63 |
+
|
64 |
+
with tab1:
|
65 |
+
col1, col2 = st.columns([1, 5])
|
66 |
+
with col1:
|
67 |
+
st.info(t_stamp)
|
68 |
+
if st.button("Load/Reset Data", key='reset1'):
|
69 |
+
st.cache_data.clear()
|
70 |
+
for key in st.session_state.keys():
|
71 |
+
del st.session_state[key]
|
72 |
+
player_stats, dk_roo_raw, fd_roo_raw = player_stat_table()
|
73 |
+
opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
|
74 |
+
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
75 |
+
data_var1 = st.radio("Which data are you loading?", ('Paydirt', 'User'), key='data_var1')
|
76 |
+
site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
|
77 |
+
if site_var1 == 'Draftkings':
|
78 |
+
if data_var1 == 'User':
|
79 |
+
raw_baselines = proj_dataframe
|
80 |
+
elif data_var1 != 'User':
|
81 |
+
raw_baselines = dk_roo_raw[dk_roo_raw['Slate'] == 'Main Slate']
|
82 |
+
raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
|
83 |
+
elif site_var1 == 'Fanduel':
|
84 |
+
if data_var1 == 'User':
|
85 |
+
raw_baselines = proj_dataframe
|
86 |
+
elif data_var1 != 'User':
|
87 |
+
raw_baselines = fd_roo_raw[fd_roo_raw['Slate'] == 'Main Slate']
|
88 |
+
raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
|
89 |
+
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')
|
90 |
+
if check_seq == 'Single Player':
|
91 |
+
player_check = st.selectbox('Select player to create comps', options = raw_baselines['Player'].unique(), key='dk_player')
|
92 |
+
elif check_seq == 'Top X Owned':
|
93 |
+
top_x_var = st.number_input('How many players would you like to check?', min_value = 1, max_value = 10, value = 5, step = 1)
|
94 |
+
Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
|
95 |
+
Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
|
96 |
+
pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
|
97 |
+
if pos_var1 == 'Specific Positions':
|
98 |
+
pos_var_list = st.multiselect('Which positions would you like to include?', options = raw_baselines['Position'].unique(), key='pos_var_list')
|
99 |
+
elif pos_var1 == 'All Positions':
|
100 |
+
pos_var_list = raw_baselines.Position.values.tolist()
|
101 |
+
split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
|
102 |
+
if split_var1 == 'Specific Games':
|
103 |
+
team_var1 = st.multiselect('Which teams would you like to include?', options = raw_baselines['Team'].unique(), key='team_var1')
|
104 |
+
elif split_var1 == 'Full Slate Run':
|
105 |
+
team_var1 = raw_baselines.Team.values.tolist()
|
106 |
+
|
107 |
+
with col2:
|
108 |
+
placeholder = st.empty()
|
109 |
+
displayholder = st.empty()
|
110 |
+
|
111 |
+
if st.button('Simulate appropriate pivots'):
|
112 |
+
with placeholder:
|
113 |
+
if site_var1 == 'Draftkings':
|
114 |
+
working_roo = raw_baselines
|
115 |
+
working_roo.replace('', 0, inplace=True)
|
116 |
+
if site_var1 == 'Fanduel':
|
117 |
+
working_roo = raw_baselines
|
118 |
+
working_roo.replace('', 0, inplace=True)
|
119 |
+
|
120 |
+
own_dict = dict(zip(working_roo.Player, working_roo.Own))
|
121 |
+
team_dict = dict(zip(working_roo.Player, working_roo.Team))
|
122 |
+
opp_dict = dict(zip(working_roo.Player, working_roo.Opp))
|
123 |
+
pos_dict = dict(zip(working_roo.Player, working_roo.Position))
|
124 |
+
total_sims = 1000
|
125 |
+
|
126 |
+
if check_seq == 'Single Player':
|
127 |
+
player_var = working_roo.loc[working_roo['Player'] == player_check]
|
128 |
+
player_var = player_var.reset_index()
|
129 |
+
|
130 |
+
working_roo = working_roo[working_roo['Position'].isin(pos_var_list)]
|
131 |
+
working_roo = working_roo[working_roo['Team'].isin(team_var1)]
|
132 |
+
working_roo = working_roo.loc[(working_roo['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo['Salary'] <= player_var['Salary'][0] + Salary_var)]
|
133 |
+
working_roo = working_roo.loc[(working_roo['Median'] >= player_var['Median'][0] - Median_var) & (working_roo['Median'] <= player_var['Median'][0] + Median_var)]
|
134 |
+
|
135 |
+
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median']]
|
136 |
+
flex_file['Floor_raw'] = flex_file['Median'] * .25
|
137 |
+
flex_file['Ceiling_raw'] = flex_file['Median'] * 2
|
138 |
+
flex_file['Floor'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
|
139 |
+
flex_file['Floor'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
|
140 |
+
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
|
141 |
+
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
|
142 |
+
flex_file['STD'] = flex_file['Median'] / 3
|
143 |
+
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
144 |
+
hold_file = flex_file.copy()
|
145 |
+
overall_file = flex_file.copy()
|
146 |
+
salary_file = flex_file.copy()
|
147 |
+
|
148 |
+
overall_players = overall_file[['Player']]
|
149 |
+
|
150 |
+
for x in range(0,total_sims):
|
151 |
+
salary_file[x] = salary_file['Salary']
|
152 |
+
overall_file[x] = random.normal(overall_file['Median'],overall_file['STD'])
|
153 |
+
|
154 |
+
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
155 |
+
|
156 |
+
salary_file = salary_file.div(1000)
|
157 |
+
|
158 |
+
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
159 |
+
|
160 |
+
players_only = hold_file[['Player']]
|
161 |
+
raw_lineups_file = players_only
|
162 |
+
|
163 |
+
for x in range(0,total_sims):
|
164 |
+
maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
|
165 |
+
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
166 |
+
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
167 |
+
|
168 |
+
players_only=players_only.drop(['Player'], axis=1)
|
169 |
+
|
170 |
+
salary_2x_check = (overall_file - (salary_file*2))
|
171 |
+
salary_3x_check = (overall_file - (salary_file*3))
|
172 |
+
salary_4x_check = (overall_file - (salary_file*4))
|
173 |
+
|
174 |
+
players_only['Average_Rank'] = players_only.mean(axis=1)
|
175 |
+
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
176 |
+
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
177 |
+
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
178 |
+
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
179 |
+
players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
180 |
+
players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
181 |
+
players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
182 |
+
|
183 |
+
players_only['Player'] = hold_file[['Player']]
|
184 |
+
|
185 |
+
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
|
186 |
+
|
187 |
+
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
|
188 |
+
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
|
189 |
+
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
|
190 |
+
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
|
191 |
+
final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
|
192 |
+
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']]
|
193 |
+
final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
|
194 |
+
final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
|
195 |
+
final_Proj['LevX'] = 0
|
196 |
+
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'])
|
197 |
+
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'])
|
198 |
+
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'])
|
199 |
+
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'])
|
200 |
+
final_Proj['CPT_Own'] = final_Proj['Own'] / 4
|
201 |
+
|
202 |
+
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']]
|
203 |
+
final_Proj = final_Proj.set_index('Player')
|
204 |
+
st.session_state.final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
|
205 |
+
|
206 |
+
elif check_seq == 'Top X Owned':
|
207 |
+
if pos_var1 == 'Specific Positions':
|
208 |
+
raw_baselines = raw_baselines[raw_baselines['Position'].isin(pos_var_list)]
|
209 |
+
player_check = raw_baselines['Player'].head(top_x_var).tolist()
|
210 |
+
final_proj_list = []
|
211 |
+
for players in player_check:
|
212 |
+
players_pos = pos_dict[players]
|
213 |
+
player_var = working_roo.loc[working_roo['Player'] == players]
|
214 |
+
player_var = player_var.reset_index()
|
215 |
+
working_roo_temp = working_roo[working_roo['Position'] == players_pos]
|
216 |
+
working_roo_temp = working_roo_temp[working_roo_temp['Team'].isin(team_var1)]
|
217 |
+
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)]
|
218 |
+
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)]
|
219 |
+
|
220 |
+
flex_file = working_roo_temp[['Player', 'Position', 'Salary', 'Median']]
|
221 |
+
flex_file['Floor_raw'] = flex_file['Median'] * .25
|
222 |
+
flex_file['Ceiling_raw'] = flex_file['Median'] * 2
|
223 |
+
flex_file['Floor'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
|
224 |
+
flex_file['Floor'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
|
225 |
+
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
|
226 |
+
flex_file['Ceiling'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
|
227 |
+
flex_file['STD'] = flex_file['Median'] / 3
|
228 |
+
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
229 |
+
hold_file = flex_file.copy()
|
230 |
+
overall_file = flex_file.copy()
|
231 |
+
salary_file = flex_file.copy()
|
232 |
+
|
233 |
+
overall_players = overall_file[['Player']]
|
234 |
+
|
235 |
+
for x in range(0,total_sims):
|
236 |
+
salary_file[x] = salary_file['Salary']
|
237 |
+
overall_file[x] = random.normal(overall_file['Median'],overall_file['STD'])
|
238 |
+
|
239 |
+
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
240 |
+
|
241 |
+
salary_file = salary_file.div(1000)
|
242 |
+
|
243 |
+
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
244 |
+
|
245 |
+
players_only = hold_file[['Player']]
|
246 |
+
raw_lineups_file = players_only
|
247 |
+
|
248 |
+
for x in range(0,total_sims):
|
249 |
+
maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
|
250 |
+
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
251 |
+
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
252 |
+
|
253 |
+
players_only=players_only.drop(['Player'], axis=1)
|
254 |
+
|
255 |
+
salary_2x_check = (overall_file - (salary_file*2))
|
256 |
+
salary_3x_check = (overall_file - (salary_file*3))
|
257 |
+
salary_4x_check = (overall_file - (salary_file*4))
|
258 |
+
|
259 |
+
players_only['Average_Rank'] = players_only.mean(axis=1)
|
260 |
+
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
261 |
+
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
262 |
+
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
263 |
+
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
264 |
+
players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
265 |
+
players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
266 |
+
players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
267 |
+
|
268 |
+
players_only['Player'] = hold_file[['Player']]
|
269 |
+
|
270 |
+
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
|
271 |
+
|
272 |
+
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
|
273 |
+
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
|
274 |
+
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
|
275 |
+
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
|
276 |
+
final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
|
277 |
+
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']]
|
278 |
+
final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
|
279 |
+
final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
|
280 |
+
final_Proj['LevX'] = 0
|
281 |
+
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'])
|
282 |
+
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'])
|
283 |
+
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'])
|
284 |
+
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'])
|
285 |
+
final_Proj['CPT_Own'] = final_Proj['Own'] / 4
|
286 |
+
final_Proj['Pivot_source'] = players
|
287 |
+
|
288 |
+
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']]
|
289 |
+
|
290 |
+
final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
|
291 |
+
final_proj_list.append(final_Proj)
|
292 |
+
st.write(f'finished run for {players}')
|
293 |
+
|
294 |
+
# Concatenate all the final_Proj dataframes
|
295 |
+
final_Proj_combined = pd.concat(final_proj_list)
|
296 |
+
final_Proj_combined = final_Proj_combined.sort_values(by='LevX', ascending=False)
|
297 |
+
final_Proj_combined = final_Proj_combined[final_Proj_combined['Player'] != final_Proj_combined['Pivot_source']]
|
298 |
+
st.session_state.final_Proj = final_Proj_combined.reset_index(drop=True) # Assign the combined dataframe back to final_Proj
|
299 |
+
placeholder.empty()
|
300 |
+
|
301 |
+
with displayholder.container():
|
302 |
+
if 'final_Proj' in st.session_state:
|
303 |
+
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)
|
304 |
+
|
305 |
+
st.download_button(
|
306 |
+
label="Export Tables",
|
307 |
+
data=convert_df_to_csv(st.session_state.final_Proj),
|
308 |
+
file_name='NHL_pivot_export.csv',
|
309 |
+
mime='text/csv',
|
310 |
+
)
|
311 |
+
else:
|
312 |
+
st.write("Run some pivots my dude/dudette")
|
313 |
+
|
314 |
+
with tab2:
|
315 |
+
st.info("The Projections file can have any columns in any order, but must contain columns explicitly named: 'Player', 'Salary', 'Position', 'Team', 'Opp', 'Median', and 'Own'.")
|
316 |
+
col1, col2 = st.columns([1, 5])
|
317 |
+
|
318 |
+
with col1:
|
319 |
+
proj_file = st.file_uploader("Upload Projections File", key = 'proj_uploader')
|
320 |
+
|
321 |
+
if proj_file is not None:
|
322 |
+
try:
|
323 |
+
proj_dataframe = pd.read_csv(proj_file)
|
324 |
+
except:
|
325 |
+
proj_dataframe = pd.read_excel(proj_file)
|
326 |
+
with col2:
|
327 |
+
if proj_file is not None:
|
328 |
+
st.dataframe(proj_dataframe.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
app.yaml
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
runtime: python
|
2 |
+
env: flex
|
3 |
+
|
4 |
+
runtime_config:
|
5 |
+
python_version: 3
|
6 |
+
|
7 |
+
entrypoint: streamlit run streamlit-app.py --server.port $PORT
|
8 |
+
|
9 |
+
automatic_scaling:
|
10 |
+
max_num_instances: 200
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
gspread
|
3 |
+
openpyxl
|
4 |
+
matplotlib
|
5 |
+
pymongo
|
6 |
+
pulp
|
7 |
+
docker
|
8 |
+
plotly
|
9 |
+
scipy
|