Spaces:
Running
Running
James McCool
commited on
Commit
·
c4089de
1
Parent(s):
9d454de
Lots of changes to structure. Added looping function to pivot structure.
Browse files
app.py
CHANGED
@@ -1,112 +1,92 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
st.set_page_config(layout="wide")
|
3 |
-
|
4 |
-
for name in dir():
|
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 |
|
|
|
|
|
13 |
@st.cache_resource
|
14 |
def init_conn():
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
@st.cache_resource(ttl = 300)
|
39 |
def init_stat_load():
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
raw_display = pd.DataFrame(worksheet.get_all_records())
|
53 |
-
raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
|
54 |
-
raw_display = raw_display[['Player', 'Salary', 'Position', 'Team', 'Opp', 'Minutes', 'Points', 'Rebounds', 'Assists', 'Steals', 'Blocks', 'Turnovers', 'Median', 'Own']]
|
55 |
raw_display.replace("", 'Welp', inplace=True)
|
56 |
raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
|
57 |
raw_display = raw_display.loc[raw_display['Median'] > 0]
|
58 |
raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
timestamp = worksheet.acell('A1').value
|
63 |
|
64 |
-
return
|
65 |
|
66 |
@st.cache_data
|
67 |
def convert_df_to_csv(df):
|
68 |
return df.to_csv().encode('utf-8')
|
69 |
|
70 |
-
|
71 |
-
opp_dict = dict(zip(dk_raw.Team, dk_raw.Opp))
|
72 |
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
73 |
|
74 |
-
tab1, tab2 = st.tabs(['
|
75 |
|
76 |
with tab1:
|
77 |
-
st.info("The Projections file can have any columns in any order, but must contain columns explicitly named: 'Player', 'Salary', 'Position', 'Team', 'Opp', 'Minutes', 'Median', 'Own'.")
|
78 |
-
col1, col2 = st.columns([1, 5])
|
79 |
-
|
80 |
-
with col1:
|
81 |
-
proj_file = st.file_uploader("Upload Projections File", key = 'proj_uploader')
|
82 |
-
|
83 |
-
if proj_file is not None:
|
84 |
-
try:
|
85 |
-
proj_dataframe = pd.read_csv(proj_file)
|
86 |
-
try:
|
87 |
-
proj_dataframe = proj_dataframe.replace(',','', regex=True)
|
88 |
-
proj_dataframe['Salary'] = proj_dataframe['Salary'].astype(int)
|
89 |
-
except:
|
90 |
-
pass
|
91 |
-
except:
|
92 |
-
proj_dataframe = pd.read_excel(proj_file)
|
93 |
-
try:
|
94 |
-
proj_dataframe = proj_dataframe.replace(',','', regex=True)
|
95 |
-
proj_dataframe['Salary'] = proj_dataframe['Salary'].astype(int)
|
96 |
-
except:
|
97 |
-
pass
|
98 |
-
with col2:
|
99 |
-
if proj_file is not None:
|
100 |
-
st.dataframe(proj_dataframe.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
101 |
-
|
102 |
-
with tab2:
|
103 |
col1, col2 = st.columns([1, 9])
|
104 |
with col1:
|
105 |
st.info(t_stamp)
|
106 |
if st.button("Load/Reset Data", key='reset1'):
|
107 |
st.cache_data.clear()
|
108 |
-
|
109 |
-
opp_dict = dict(zip(dk_raw.Team, dk_raw.Opp))
|
110 |
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
111 |
for key in st.session_state.keys():
|
112 |
del st.session_state[key]
|
@@ -116,13 +96,19 @@ with tab2:
|
|
116 |
if data_var1 == 'User':
|
117 |
raw_baselines = proj_dataframe
|
118 |
elif data_var1 != 'User':
|
119 |
-
raw_baselines =
|
|
|
120 |
elif site_var1 == 'Fanduel':
|
121 |
if data_var1 == 'User':
|
122 |
raw_baselines = proj_dataframe
|
123 |
elif data_var1 != 'User':
|
124 |
-
raw_baselines =
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
126 |
Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
|
127 |
Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
|
128 |
pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
|
@@ -137,138 +123,234 @@ with tab2:
|
|
137 |
team_var1 = raw_baselines.Team.values.tolist()
|
138 |
|
139 |
with col2:
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
hold_container = st.empty()
|
144 |
-
if site_var1 == 'Draftkings':
|
145 |
-
if data_var1 == 'User':
|
146 |
-
raw_baselines = proj_dataframe
|
147 |
-
elif data_var1 != 'User':
|
148 |
-
raw_baselines = dk_raw
|
149 |
-
elif site_var1 == 'Fanduel':
|
150 |
-
if data_var1 == 'User':
|
151 |
-
raw_baselines = proj_dataframe
|
152 |
-
elif data_var1 != 'User':
|
153 |
-
raw_baselines = fd_raw
|
154 |
-
if proj_file is not None:
|
155 |
-
st.session_state.proj_display = proj_dataframe.copy()
|
156 |
-
elif proj_file is None:
|
157 |
-
st.session_state.proj_display = raw_baselines.copy()
|
158 |
if st.button('Simulate appropriate pivots'):
|
159 |
-
with
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
own_dict = dict(zip(working_roo.Player, working_roo.Own))
|
168 |
-
min_dict = dict(zip(working_roo.Player, working_roo.Minutes))
|
169 |
team_dict = dict(zip(working_roo.Player, working_roo.Team))
|
|
|
|
|
170 |
total_sims = 1000
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median', 'Minutes']]
|
180 |
-
flex_file['Floor'] = (flex_file['Median'] * .25) + (flex_file['Minutes'] * .25)
|
181 |
-
flex_file['Ceiling'] = flex_file['Median'] + 10 + (flex_file['Minutes'] * .25)
|
182 |
-
flex_file['STD'] = (flex_file['Median']/4)
|
183 |
-
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
184 |
-
hold_file = flex_file
|
185 |
-
overall_file = flex_file
|
186 |
-
salary_file = flex_file
|
187 |
-
|
188 |
-
overall_players = overall_file[['Player']]
|
189 |
-
|
190 |
-
for x in range(0,total_sims):
|
191 |
-
salary_file[x] = salary_file['Salary']
|
192 |
-
|
193 |
-
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
194 |
-
salary_file.astype('int').dtypes
|
195 |
-
|
196 |
-
salary_file = salary_file.div(1000)
|
197 |
-
|
198 |
-
for x in range(0,total_sims):
|
199 |
-
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
200 |
-
|
201 |
-
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
202 |
-
overall_file.astype('int').dtypes
|
203 |
-
|
204 |
-
players_only = hold_file[['Player']]
|
205 |
-
raw_lineups_file = players_only
|
206 |
-
|
207 |
-
for x in range(0,total_sims):
|
208 |
-
maps_dict = {'proj_map':dict(zip(hold_file.Player,hold_file[x]))}
|
209 |
-
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
210 |
-
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
211 |
-
|
212 |
-
players_only=players_only.drop(['Player'], axis=1)
|
213 |
-
players_only.astype('int').dtypes
|
214 |
-
|
215 |
-
salary_2x_check = (overall_file - (salary_file*4))
|
216 |
-
salary_3x_check = (overall_file - (salary_file*5))
|
217 |
-
salary_4x_check = (overall_file - (salary_file*6))
|
218 |
-
gpp_check = (overall_file - ((salary_file*5)+10))
|
219 |
-
|
220 |
-
players_only['Average_Rank'] = players_only.mean(axis=1)
|
221 |
-
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
222 |
-
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
223 |
-
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
224 |
-
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
225 |
-
players_only['3x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
226 |
-
players_only['4x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
227 |
-
players_only['5x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
228 |
-
players_only['GPP%'] = salary_4x_check[gpp_check >= 1].count(axis=1)/float(total_sims)
|
229 |
-
|
230 |
-
players_only['Player'] = hold_file[['Player']]
|
231 |
-
|
232 |
-
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
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 |
-
proj_container = st.empty()
|
258 |
-
if 'proj_display' in st.session_state:
|
259 |
-
st.dataframe(st.session_state.proj_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
import pandas as pd
|
3 |
import streamlit as st
|
4 |
import gspread
|
5 |
|
6 |
+
st.set_page_config(layout="wide")
|
7 |
+
|
8 |
@st.cache_resource
|
9 |
def init_conn():
|
10 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
11 |
+
|
12 |
+
credentials = {
|
13 |
+
"type": "service_account",
|
14 |
+
"project_id": "model-sheets-connect",
|
15 |
+
"private_key_id": st.secrets['model_sheets_connect_pk'],
|
16 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\ncx0KQ23nZK3ZVvADYy8u/RUn/EDI82QKxTd/DizRLIV81JiNQxDJXSzgkbwKYEDm\n48E8zGvupU8+Nk76xNPakrQKy2Y8+VJlq5psBtGchJTuUSHcXU5Mg2JhQsB376PJ\nsCw552K6Pw8fpeMDJDZuxpKSkaJR6k9G5Dhf5q8HDXnC5Rh/PRFuKJ2GGRpX7n+2\nhT/sCax0J8jfdTy/MDGiDfJqfQrOPrMKELtsGHR9Iv6F4vKiDqXpKfqH+02E9ptz\nBk+MNcbZ3m90M8ShfRu28ebebsASfarNMzc3dk7tb3utHOGXKCf4tF8yYKo7x8BZ\noO9X4gSfAgMBAAECggEAU8ByyMpSKlTCF32TJhXnVJi/kS+IhC/Qn5JUDMuk4LXr\naAEWsWO6kV/ZRVXArjmuSzuUVrXumISapM9Ps5Ytbl95CJmGDiLDwRL815nvv6k3\nUyAS8EGKjz74RpoIoH6E7EWCAzxlnUgTn+5oP9Flije97epYk3H+e2f1f5e1Nn1d\nYNe8U+1HqJgILcxA1TAUsARBfoD7+K3z/8DVPHI8IpzAh6kTHqhqC23Rram4XoQ6\nzj/ZdVBjvnKuazETfsD+Vl3jGLQA8cKQVV70xdz3xwLcNeHsbPbpGBpZUoF73c65\nkAXOrjYl0JD5yAk+hmYhXr6H9c6z5AieuZGDrhmlFQKBgQDzV6LRXmjn4854DP/J\nI82oX2GcI4eioDZPRukhiQLzYerMQBmyqZIRC+/LTCAhYQSjNgMa+ZKyvLqv48M0\n/x398op/+n3xTs+8L49SPI48/iV+mnH7k0WI/ycd4OOKh8rrmhl/0EWb9iitwJYe\nMjTV/QxNEpPBEXfR1/mvrN/lVQKBgQDuhomOxUhWVRVH6x03slmyRBn0Oiw4MW+r\nrt1hlNgtVmTc5Mu+4G0USMZwYuOB7F8xG4Foc7rIlwS7Ic83jMJxemtqAelwOLdV\nXRLrLWJfX8+O1z/UE15l2q3SUEnQ4esPHbQnZowHLm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
|
17 |
+
"client_email": "[email protected]",
|
18 |
+
"client_id": "100369174533302798535",
|
19 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
20 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
21 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
22 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
|
23 |
+
}
|
24 |
+
|
25 |
+
credentials2 = {
|
26 |
+
"type": "service_account",
|
27 |
+
"project_id": "sheets-api-connect-378620",
|
28 |
+
"private_key_id": st.secrets['sheets_api_connect_pk'],
|
29 |
+
"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",
|
30 |
+
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
|
31 |
+
"client_id": "106625872877651920064",
|
32 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
33 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
34 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
35 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
36 |
+
}
|
37 |
+
|
38 |
+
NBA_Data = st.secrets['NBA_Data']
|
39 |
+
|
40 |
+
gc = gspread.service_account_from_dict(credentials)
|
41 |
+
gc2 = gspread.service_account_from_dict(credentials2)
|
42 |
+
|
43 |
+
return gc, gc2, NBA_Data
|
44 |
+
|
45 |
+
gcservice_account, gcservice_account2, NBA_Data = init_conn()
|
46 |
+
|
47 |
+
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '4x%': '{:.2%}', '5x%': '{:.2%}',
|
48 |
+
'6x%': '{:.2%}','GPP%': '{:.2%}'}
|
49 |
|
50 |
@st.cache_resource(ttl = 300)
|
51 |
def init_stat_load():
|
52 |
+
try:
|
53 |
+
sh = gcservice_account.open_by_url(NBA_Data)
|
54 |
+
worksheet = sh.worksheet('Player_Level_ROO')
|
55 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
56 |
+
raw_display = raw_display.rename(columns={"Minutes Proj": "Minutes"})
|
57 |
+
except:
|
58 |
+
sh = gcservice_account2.open_by_url(NBA_Data)
|
59 |
+
worksheet = sh.worksheet('Player_Level_ROO')
|
60 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
61 |
+
raw_display = raw_display.rename(columns={"Minutes Proj": "Minutes"})
|
62 |
+
|
63 |
+
raw_display = raw_display[['Player', 'Salary', 'Position', 'Team', 'Opp', 'Minutes', 'Median', 'Own']]
|
|
|
|
|
|
|
64 |
raw_display.replace("", 'Welp', inplace=True)
|
65 |
raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
|
66 |
raw_display = raw_display.loc[raw_display['Median'] > 0]
|
67 |
raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
|
68 |
+
proj_raw = raw_display.sort_values(by='Median', ascending=False)
|
69 |
|
70 |
+
timestamp = proj_raw['timestamp'].iloc[0]
|
|
|
71 |
|
72 |
+
return proj_raw, timestamp
|
73 |
|
74 |
@st.cache_data
|
75 |
def convert_df_to_csv(df):
|
76 |
return df.to_csv().encode('utf-8')
|
77 |
|
78 |
+
proj_raw, timestamp = init_stat_load()
|
|
|
79 |
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
80 |
|
81 |
+
tab1, tab2 = st.tabs(['Pivot Finder', 'Uploads and Info'])
|
82 |
|
83 |
with tab1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
col1, col2 = st.columns([1, 9])
|
85 |
with col1:
|
86 |
st.info(t_stamp)
|
87 |
if st.button("Load/Reset Data", key='reset1'):
|
88 |
st.cache_data.clear()
|
89 |
+
proj_raw, timestamp = init_stat_load()
|
|
|
90 |
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
91 |
for key in st.session_state.keys():
|
92 |
del st.session_state[key]
|
|
|
96 |
if data_var1 == 'User':
|
97 |
raw_baselines = proj_dataframe
|
98 |
elif data_var1 != 'User':
|
99 |
+
raw_baselines = proj_raw[proj_raw['site'] == 'Draftkings']
|
100 |
+
raw_baselines = raw_baselines[raw_baselines['slate'] == 'Main Slate']
|
101 |
elif site_var1 == 'Fanduel':
|
102 |
if data_var1 == 'User':
|
103 |
raw_baselines = proj_dataframe
|
104 |
elif data_var1 != 'User':
|
105 |
+
raw_baselines = proj_raw[proj_raw['site'] == 'Fanduel']
|
106 |
+
raw_baselines = raw_baselines[raw_baselines['slate'] == 'Main Slate']
|
107 |
+
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')
|
108 |
+
if check_seq == 'Single Player':
|
109 |
+
player_check = st.selectbox('Select player to create comps', options = raw_baselines['Player'].unique(), key='dk_player')
|
110 |
+
elif check_seq == 'Top X Owned':
|
111 |
+
top_x_var = st.number_input('How many players would you like to check?', min_value = 1, max_value = 10, value = 5, step = 1)
|
112 |
Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
|
113 |
Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
|
114 |
pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
|
|
|
123 |
team_var1 = raw_baselines.Team.values.tolist()
|
124 |
|
125 |
with col2:
|
126 |
+
placeholder = st.empty()
|
127 |
+
displayholder = st.empty()
|
128 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
if st.button('Simulate appropriate pivots'):
|
130 |
+
with placeholder:
|
131 |
+
if site_var1 == 'Draftkings':
|
132 |
+
working_roo = raw_baselines
|
133 |
+
working_roo.replace('', 0, inplace=True)
|
134 |
+
if site_var1 == 'Fanduel':
|
135 |
+
working_roo = raw_baselines
|
136 |
+
working_roo.replace('', 0, inplace=True)
|
137 |
+
|
138 |
own_dict = dict(zip(working_roo.Player, working_roo.Own))
|
|
|
139 |
team_dict = dict(zip(working_roo.Player, working_roo.Team))
|
140 |
+
pos_dict = dict(zip(working_roo.Player, working_roo.Position))
|
141 |
+
min_dict = dict(zip(working_roo.Player, working_roo.Minutes))
|
142 |
total_sims = 1000
|
143 |
+
|
144 |
+
if check_seq == 'Single Player':
|
145 |
+
player_var = working_roo.loc[working_roo['Player'] == player_check]
|
146 |
+
player_var = player_var.reset_index()
|
147 |
+
working_roo = working_roo[working_roo['Position'].isin(pos_var_list)]
|
148 |
+
working_roo = working_roo[working_roo['Team'].isin(team_var1)]
|
149 |
+
working_roo = working_roo.loc[(working_roo['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo['Salary'] <= player_var['Salary'][0] + Salary_var)]
|
150 |
+
working_roo = working_roo.loc[(working_roo['Median'] >= player_var['Median'][0] - Median_var) & (working_roo['Median'] <= player_var['Median'][0] + Median_var)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
+
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median', 'Minutes']]
|
153 |
+
flex_file['Floor'] = (flex_file['Median'] * .25) + (flex_file['Minutes'] * .25)
|
154 |
+
flex_file['Ceiling'] = flex_file['Median'] + 10 + (flex_file['Minutes'] * .25)
|
155 |
+
flex_file['STD'] = (flex_file['Median']/4)
|
156 |
+
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
157 |
+
hold_file = flex_file.copy()
|
158 |
+
overall_file = flex_file.copy()
|
159 |
+
salary_file = flex_file.copy()
|
160 |
+
|
161 |
+
overall_players = overall_file[['Player']]
|
162 |
+
|
163 |
+
for x in range(0,total_sims):
|
164 |
+
salary_file[x] = salary_file['Salary']
|
165 |
+
|
166 |
+
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
167 |
+
|
168 |
+
salary_file = salary_file.div(1000)
|
169 |
+
|
170 |
+
for x in range(0,total_sims):
|
171 |
+
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
172 |
+
|
173 |
+
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
174 |
+
|
175 |
+
players_only = hold_file[['Player']]
|
176 |
+
raw_lineups_file = players_only
|
177 |
+
|
178 |
+
for x in range(0,total_sims):
|
179 |
+
maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
|
180 |
+
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
181 |
+
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
182 |
+
|
183 |
+
players_only=players_only.drop(['Player'], axis=1)
|
184 |
+
|
185 |
+
salary_2x_check = (overall_file - (salary_file*4))
|
186 |
+
salary_3x_check = (overall_file - (salary_file*5))
|
187 |
+
salary_4x_check = (overall_file - (salary_file*6))
|
188 |
+
gpp_check = (overall_file - ((salary_file*5)+10))
|
189 |
+
|
190 |
+
players_only['Average_Rank'] = players_only.mean(axis=1)
|
191 |
+
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
192 |
+
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
193 |
+
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
194 |
+
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
195 |
+
players_only['3x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
196 |
+
players_only['4x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
197 |
+
players_only['5x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
198 |
+
players_only['GPP%'] = salary_4x_check[gpp_check >= 1].count(axis=1)/float(total_sims)
|
199 |
+
|
200 |
+
players_only['Player'] = hold_file[['Player']]
|
201 |
+
|
202 |
+
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
203 |
+
|
204 |
+
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
|
205 |
+
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
206 |
+
|
207 |
+
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
|
208 |
+
final_Proj['Minutes Proj'] = final_Proj['Player'].map(min_dict)
|
209 |
+
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
|
210 |
+
final_Proj['Own'] = final_Proj['Own'].astype('float')
|
211 |
+
final_Proj['Projection Rank'] = final_Proj.Top_finish.rank(pct = True)
|
212 |
+
final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
|
213 |
+
final_Proj['LevX'] = (final_Proj['Projection Rank'] - final_Proj['Own Rank']) * 100
|
214 |
+
final_Proj['ValX'] = ((final_Proj[['4x%', '5x%']].mean(axis=1))*100) + final_Proj['LevX']
|
215 |
+
final_Proj['ValX'] = np.where(final_Proj['ValX'] > 100, 100, final_Proj['ValX'])
|
216 |
+
final_Proj['ValX'] = np.where(final_Proj['ValX'] < 0, 0, final_Proj['ValX'])
|
217 |
+
|
218 |
+
final_Proj = final_Proj[['Player', 'Minutes Proj', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%', 'Own', 'LevX', 'ValX']]
|
219 |
+
final_Proj = final_Proj.set_index('Player')
|
220 |
+
final_Proj = final_Proj.sort_values(by='Median', ascending=False)
|
221 |
+
|
222 |
+
st.session_state.final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
|
223 |
+
|
224 |
+
elif check_seq == 'Top X Owned':
|
225 |
+
if pos_var1 == 'Specific Positions':
|
226 |
+
raw_baselines = raw_baselines[raw_baselines['Position'].isin(pos_var_list)]
|
227 |
+
player_check = raw_baselines['Player'].head(top_x_var).tolist()
|
228 |
+
final_proj_list = []
|
229 |
+
for players in player_check:
|
230 |
+
players_pos = pos_dict[players]
|
231 |
+
player_var = working_roo.loc[working_roo['Player'] == players]
|
232 |
+
player_var = player_var.reset_index()
|
233 |
+
working_roo_temp = working_roo[working_roo['Position'] == players_pos]
|
234 |
+
working_roo_temp = working_roo_temp[working_roo_temp['Team'].isin(team_var1)]
|
235 |
+
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)]
|
236 |
+
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)]
|
237 |
+
|
238 |
+
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median', 'Minutes']]
|
239 |
+
flex_file['Floor'] = (flex_file['Median'] * .25) + (flex_file['Minutes'] * .25)
|
240 |
+
flex_file['Ceiling'] = flex_file['Median'] + 10 + (flex_file['Minutes'] * .25)
|
241 |
+
flex_file['STD'] = (flex_file['Median']/4)
|
242 |
+
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
243 |
+
hold_file = flex_file.copy()
|
244 |
+
overall_file = flex_file.copy()
|
245 |
+
salary_file = flex_file.copy()
|
246 |
+
|
247 |
+
overall_players = overall_file[['Player']]
|
248 |
+
|
249 |
+
for x in range(0,total_sims):
|
250 |
+
salary_file[x] = salary_file['Salary']
|
251 |
+
|
252 |
+
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
253 |
|
254 |
+
salary_file = salary_file.div(1000)
|
|
|
|
|
|
|
255 |
|
256 |
+
for x in range(0,total_sims):
|
257 |
+
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
258 |
+
|
259 |
+
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
260 |
+
|
261 |
+
players_only = hold_file[['Player']]
|
262 |
+
raw_lineups_file = players_only
|
263 |
+
|
264 |
+
for x in range(0,total_sims):
|
265 |
+
maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
|
266 |
+
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
267 |
+
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
268 |
+
|
269 |
+
players_only=players_only.drop(['Player'], axis=1)
|
270 |
+
|
271 |
+
salary_2x_check = (overall_file - (salary_file*4))
|
272 |
+
salary_3x_check = (overall_file - (salary_file*5))
|
273 |
+
salary_4x_check = (overall_file - (salary_file*6))
|
274 |
+
gpp_check = (overall_file - ((salary_file*5)+10))
|
275 |
+
|
276 |
+
players_only['Average_Rank'] = players_only.mean(axis=1)
|
277 |
+
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
278 |
+
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
279 |
+
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
280 |
+
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
281 |
+
players_only['3x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
282 |
+
players_only['4x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
283 |
+
players_only['5x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
284 |
+
players_only['GPP%'] = salary_4x_check[gpp_check >= 1].count(axis=1)/float(total_sims)
|
285 |
+
|
286 |
+
players_only['Player'] = hold_file[['Player']]
|
287 |
+
|
288 |
+
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
289 |
|
290 |
+
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
|
291 |
+
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
292 |
+
|
293 |
+
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
|
294 |
+
final_Proj['Minutes Proj'] = final_Proj['Player'].map(min_dict)
|
295 |
+
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
|
296 |
+
final_Proj['Own'] = final_Proj['Own'].astype('float')
|
297 |
+
final_Proj['Projection Rank'] = final_Proj.Top_finish.rank(pct = True)
|
298 |
+
final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
|
299 |
+
final_Proj['LevX'] = (final_Proj['Projection Rank'] - final_Proj['Own Rank']) * 100
|
300 |
+
final_Proj['ValX'] = ((final_Proj[['4x%', '5x%']].mean(axis=1))*100) + final_Proj['LevX']
|
301 |
+
final_Proj['ValX'] = np.where(final_Proj['ValX'] > 100, 100, final_Proj['ValX'])
|
302 |
+
final_Proj['ValX'] = np.where(final_Proj['ValX'] < 0, 0, final_Proj['ValX'])
|
303 |
+
|
304 |
+
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']]
|
305 |
+
|
306 |
+
final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
|
307 |
+
final_proj_list.append(final_Proj)
|
308 |
+
st.write(f'finished run for {players}')
|
309 |
+
|
310 |
+
# Concatenate all the final_Proj dataframes
|
311 |
+
final_Proj_combined = pd.concat(final_proj_list)
|
312 |
+
final_Proj_combined = final_Proj_combined.sort_values(by='LevX', ascending=False)
|
313 |
+
final_Proj_combined = final_Proj_combined[final_Proj_combined['Player'] != final_Proj_combined['Pivot_source']]
|
314 |
+
st.session_state.final_Proj = final_Proj_combined.reset_index(drop=True) # Assign the combined dataframe back to final_Proj
|
315 |
+
|
316 |
+
placeholder.empty()
|
317 |
+
|
318 |
+
with displayholder.container():
|
319 |
+
if 'final_Proj' in st.session_state:
|
320 |
+
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)
|
321 |
+
|
322 |
+
st.download_button(
|
323 |
+
label="Export Tables",
|
324 |
+
data=convert_df_to_csv(st.session_state.final_Proj),
|
325 |
+
file_name='NFL_pivot_export.csv',
|
326 |
+
mime='text/csv',
|
327 |
+
)
|
328 |
+
else:
|
329 |
+
st.write("Run some pivots my dude/dudette")
|
330 |
+
|
331 |
+
with tab2:
|
332 |
+
st.info("The Projections file can have any columns in any order, but must contain columns explicitly named: 'Player', 'Salary', 'Position', 'Team', 'Opp', 'Minutes', 'Median', 'Own'.")
|
333 |
+
col1, col2 = st.columns([1, 5])
|
334 |
+
|
335 |
+
with col1:
|
336 |
+
proj_file = st.file_uploader("Upload Projections File", key = 'proj_uploader')
|
337 |
+
|
338 |
+
if proj_file is not None:
|
339 |
+
try:
|
340 |
+
proj_dataframe = pd.read_csv(proj_file)
|
341 |
+
try:
|
342 |
+
proj_dataframe = proj_dataframe.replace(',','', regex=True)
|
343 |
+
proj_dataframe['Salary'] = proj_dataframe['Salary'].astype(int)
|
344 |
+
except:
|
345 |
+
pass
|
346 |
+
except:
|
347 |
+
proj_dataframe = pd.read_excel(proj_file)
|
348 |
+
try:
|
349 |
+
proj_dataframe = proj_dataframe.replace(',','', regex=True)
|
350 |
+
proj_dataframe['Salary'] = proj_dataframe['Salary'].astype(int)
|
351 |
+
except:
|
352 |
+
pass
|
353 |
+
with col2:
|
354 |
+
if proj_file is not None:
|
355 |
+
st.dataframe(proj_dataframe.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
356 |
+
|