Spaces:
Running
Running
James McCool
commited on
Commit
·
71230df
1
Parent(s):
e54a8b1
Enhance app.py: Introduce DK and FD lineup initialization functions. Added init_DK_lineups and init_FD_lineups to fetch and process data for DraftKings and FanDuel, respectively. Updated data reset logic to clear session state and initialize lineups upon user action. Improved data export functionality and added player frequency analysis for better insights.
Browse files
app.py
CHANGED
@@ -26,6 +26,8 @@ CSV_URL = 'https://docs.google.com/spreadsheets/d/1lMLxWdvCnOFBtG9dhM0zv2USuxZbk
|
|
26 |
|
27 |
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '100+%': '{:.2%}', '10x%': '{:.2%}', '11x%': '{:.2%}',
|
28 |
'12x%': '{:.2%}','LevX': '{:.2%}'}
|
|
|
|
|
29 |
|
30 |
@st.cache_resource(ttl = 600)
|
31 |
def init_baselines():
|
@@ -40,9 +42,56 @@ def init_baselines():
|
|
40 |
|
41 |
return roo_data, timestamp
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
def convert_df_to_csv(df):
|
44 |
return df.to_csv().encode('utf-8')
|
45 |
|
|
|
|
|
|
|
|
|
|
|
46 |
roo_data, timestamp = init_baselines()
|
47 |
hold_display = roo_data
|
48 |
lineup_display = []
|
@@ -55,16 +104,16 @@ tab1, tab2 = st.tabs(["Player Overall Projections", "Not Ready Yet"])
|
|
55 |
|
56 |
with tab1:
|
57 |
if st.button("Reset Data", key='reset1'):
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
st.write(timestamp)
|
69 |
options_container = st.empty()
|
70 |
hold_container = st.empty()
|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
|
|
|
|
|
26 |
|
27 |
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '100+%': '{:.2%}', '10x%': '{:.2%}', '11x%': '{:.2%}',
|
28 |
'12x%': '{:.2%}','LevX': '{:.2%}'}
|
29 |
+
dk_columns = ['FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'FLEX6', 'salary', 'proj', 'Own']
|
30 |
+
fd_columns = ['FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'FLEX6', 'salary', 'proj', 'Own']
|
31 |
|
32 |
@st.cache_resource(ttl = 600)
|
33 |
def init_baselines():
|
|
|
42 |
|
43 |
return roo_data, timestamp
|
44 |
|
45 |
+
@st.cache_data(ttl = 60)
|
46 |
+
def init_DK_lineups():
|
47 |
+
|
48 |
+
collection = db['PGA_DK_Seed_Frame_Name_Map']
|
49 |
+
cursor = collection.find()
|
50 |
+
raw_data = pd.DataFrame(list(cursor))
|
51 |
+
names_dict = dict(zip(raw_data['key'], raw_data['value']))
|
52 |
+
|
53 |
+
collection = db["PGA_DK_Seed_Frame"]
|
54 |
+
cursor = collection.find().limit(10000)
|
55 |
+
|
56 |
+
raw_display = pd.DataFrame(list(cursor))
|
57 |
+
raw_display = raw_display[['FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'FLEX6', 'salary', 'proj', 'Own']]
|
58 |
+
dict_columns = ['FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'FLEX6']
|
59 |
+
st.write("converting names")
|
60 |
+
for col in dict_columns:
|
61 |
+
raw_display[col] = raw_display[col].map(names_dict)
|
62 |
+
DK_seed = raw_display.to_numpy()
|
63 |
+
|
64 |
+
return DK_seed
|
65 |
+
|
66 |
+
@st.cache_data(ttl = 60)
|
67 |
+
def init_FD_lineups():
|
68 |
+
|
69 |
+
collection = db['PGA_DK_Seed_Frame_Name_Map']
|
70 |
+
cursor = collection.find()
|
71 |
+
raw_data = pd.DataFrame(list(cursor))
|
72 |
+
names_dict = dict(zip(raw_data['key'], raw_data['value']))
|
73 |
+
|
74 |
+
collection = db["PGA_DK_Seed_Frame"]
|
75 |
+
cursor = collection.find().limit(10000)
|
76 |
+
|
77 |
+
raw_display = pd.DataFrame(list(cursor))
|
78 |
+
raw_display = raw_display[['FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'FLEX6', 'salary', 'proj', 'Own']]
|
79 |
+
dict_columns = ['FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'FLEX6']
|
80 |
+
st.write("converting names")
|
81 |
+
for col in dict_columns:
|
82 |
+
raw_display[col] = raw_display[col].map(names_dict)
|
83 |
+
FD_seed = raw_display.to_numpy()
|
84 |
+
|
85 |
+
return FD_seed
|
86 |
+
|
87 |
def convert_df_to_csv(df):
|
88 |
return df.to_csv().encode('utf-8')
|
89 |
|
90 |
+
@st.cache_data
|
91 |
+
def convert_df(array):
|
92 |
+
array = pd.DataFrame(array, columns=column_names)
|
93 |
+
return array.to_csv().encode('utf-8')
|
94 |
+
|
95 |
roo_data, timestamp = init_baselines()
|
96 |
hold_display = roo_data
|
97 |
lineup_display = []
|
|
|
104 |
|
105 |
with tab1:
|
106 |
if st.button("Reset Data", key='reset1'):
|
107 |
+
# Clear values from *all* all in-memory and on-disk data caches:
|
108 |
+
# i.e. clear values from both square and cube
|
109 |
+
st.cache_data.clear()
|
110 |
+
roo_data, timestamp = init_baselines()
|
111 |
+
dk_lineups = init_DK_lineups()
|
112 |
+
fd_lineups = init_FD_lineups()
|
113 |
+
hold_display = roo_data
|
114 |
+
for key in st.session_state.keys():
|
115 |
+
del st.session_state[key]
|
116 |
+
|
117 |
st.write(timestamp)
|
118 |
options_container = st.empty()
|
119 |
hold_container = st.empty()
|
|
|
133 |
)
|
134 |
|
135 |
with tab2:
|
136 |
+
col1, col2 = st.columns([1, 7])
|
137 |
+
with col1:
|
138 |
+
if st.button("Load/Reset Data", key='reset2'):
|
139 |
+
st.cache_data.clear()
|
140 |
+
roo_data, timestamp = init_baselines()
|
141 |
+
hold_display = roo_data
|
142 |
+
dk_lineups = init_DK_lineups()
|
143 |
+
fd_lineups = init_FD_lineups()
|
144 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
145 |
+
for key in st.session_state.keys():
|
146 |
+
del st.session_state[key]
|
147 |
+
|
148 |
+
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Just the Main Slate'))
|
149 |
+
site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'))
|
150 |
+
lineup_num_var = st.number_input("How many lineups do you want to display?", min_value=1, max_value=1000, value=150, step=1)
|
151 |
+
|
152 |
+
if site_var1 == 'Draftkings':
|
153 |
+
raw_baselines = hold_display
|
154 |
+
ROO_slice = raw_baselines[raw_baselines['site'] == 'Draftkings']
|
155 |
+
# Get the minimum and maximum ownership values from dk_lineups
|
156 |
+
min_own = np.min(dk_lineups[:,12])
|
157 |
+
max_own = np.max(dk_lineups[:,12])
|
158 |
+
column_names = dk_columns
|
159 |
+
|
160 |
+
player_var1 = st.radio("Do you want a frame with specific Players?", ('Full Slate', 'Specific Players'), key='player_var1')
|
161 |
+
if player_var1 == 'Specific Players':
|
162 |
+
player_var2 = st.multiselect('Which players do you want?', options = raw_baselines['Player'].unique())
|
163 |
+
elif player_var1 == 'Full Slate':
|
164 |
+
player_var2 = raw_baselines.Player.values.tolist()
|
165 |
+
|
166 |
+
elif site_var1 == 'Fanduel':
|
167 |
+
raw_baselines = hold_display
|
168 |
+
ROO_slice = raw_baselines[raw_baselines['site'] == 'Fanduel']
|
169 |
+
min_own = np.min(fd_lineups[:,12])
|
170 |
+
max_own = np.max(fd_lineups[:,12])
|
171 |
+
column_names = fd_columns
|
172 |
+
|
173 |
+
player_var1 = st.radio("Do you want a frame with specific Players?", ('Full Slate', 'Specific Players'), key='player_var1')
|
174 |
+
if player_var1 == 'Specific Players':
|
175 |
+
player_var2 = st.multiselect('Which players do you want?', options = raw_baselines['Player'].unique())
|
176 |
+
elif player_var1 == 'Full Slate':
|
177 |
+
player_var2 = raw_baselines.Player.values.tolist()
|
178 |
+
|
179 |
+
if st.button("Prepare data export", key='data_export'):
|
180 |
+
data_export = st.session_state.working_seed.copy()
|
181 |
+
# if site_var1 == 'Draftkings':
|
182 |
+
# for col_idx in range(6):
|
183 |
+
# data_export[:, col_idx] = np.array([id_dict.get(player, player) for player in data_export[:, col_idx]])
|
184 |
+
# elif site_var1 == 'Fanduel':
|
185 |
+
# for col_idx in range(6):
|
186 |
+
# data_export[:, col_idx] = np.array([id_dict.get(player, player) for player in data_export[:, col_idx]])
|
187 |
+
st.download_button(
|
188 |
+
label="Export optimals set",
|
189 |
+
data=convert_df(data_export),
|
190 |
+
file_name='NBA_optimals_export.csv',
|
191 |
+
mime='text/csv',
|
192 |
+
)
|
193 |
+
with col2:
|
194 |
+
|
195 |
+
if site_var1 == 'Draftkings':
|
196 |
+
if 'working_seed' in st.session_state:
|
197 |
+
st.session_state.working_seed = st.session_state.working_seed
|
198 |
+
if player_var1 == 'Specific Players':
|
199 |
+
st.session_state.working_seed = st.session_state.working_seed[np.equal.outer(st.session_state.working_seed, player_var2).any(axis=1).all(axis=1)]
|
200 |
+
elif player_var1 == 'Full Slate':
|
201 |
+
st.session_state.working_seed = dk_lineups.copy()
|
202 |
+
st.session_state.data_export_display = pd.DataFrame(st.session_state.working_seed[0:lineup_num_var], columns=column_names)
|
203 |
+
elif 'working_seed' not in st.session_state:
|
204 |
+
st.session_state.working_seed = dk_lineups.copy()
|
205 |
+
st.session_state.working_seed = st.session_state.working_seed
|
206 |
+
if player_var1 == 'Specific Players':
|
207 |
+
st.session_state.working_seed = st.session_state.working_seed[np.equal.outer(st.session_state.working_seed, player_var2).any(axis=1).all(axis=1)]
|
208 |
+
elif player_var1 == 'Full Slate':
|
209 |
+
st.session_state.working_seed = dk_lineups.copy()
|
210 |
+
st.session_state.data_export_display = pd.DataFrame(st.session_state.working_seed[0:lineup_num_var], columns=column_names)
|
211 |
+
|
212 |
+
elif site_var1 == 'Fanduel':
|
213 |
+
if 'working_seed' in st.session_state:
|
214 |
+
st.session_state.working_seed = st.session_state.working_seed
|
215 |
+
if player_var1 == 'Specific Players':
|
216 |
+
st.session_state.working_seed = st.session_state.working_seed[np.equal.outer(st.session_state.working_seed, player_var2).any(axis=1).all(axis=1)]
|
217 |
+
elif player_var1 == 'Full Slate':
|
218 |
+
st.session_state.working_seed = fd_lineups.copy()
|
219 |
+
st.session_state.data_export_display = pd.DataFrame(st.session_state.working_seed[0:lineup_num_var], columns=column_names)
|
220 |
+
elif 'working_seed' not in st.session_state:
|
221 |
+
st.session_state.working_seed = fd_lineups.copy()
|
222 |
+
st.session_state.working_seed = st.session_state.working_seed
|
223 |
+
if player_var1 == 'Specific Players':
|
224 |
+
st.session_state.working_seed = st.session_state.working_seed[np.equal.outer(st.session_state.working_seed, player_var2).any(axis=1).all(axis=1)]
|
225 |
+
elif player_var1 == 'Full Slate':
|
226 |
+
st.session_state.working_seed = fd_lineups.copy()
|
227 |
+
st.session_state.data_export_display = pd.DataFrame(st.session_state.working_seed[0:lineup_num_var], columns=column_names)
|
228 |
+
|
229 |
+
export_file = st.session_state.data_export_display.copy()
|
230 |
+
# if site_var1 == 'Draftkings':
|
231 |
+
# for col_idx in range(6):
|
232 |
+
# export_file.iloc[:, col_idx] = export_file.iloc[:, col_idx].map(id_dict)
|
233 |
+
# elif site_var1 == 'Fanduel':
|
234 |
+
# for col_idx in range(6):
|
235 |
+
# export_file.iloc[:, col_idx] = export_file.iloc[:, col_idx].map(id_dict)
|
236 |
+
|
237 |
+
with st.container():
|
238 |
+
if st.button("Reset Optimals", key='reset3'):
|
239 |
+
for key in st.session_state.keys():
|
240 |
+
del st.session_state[key]
|
241 |
+
if site_var1 == 'Draftkings':
|
242 |
+
st.session_state.working_seed = dk_lineups.copy()
|
243 |
+
elif site_var1 == 'Fanduel':
|
244 |
+
st.session_state.working_seed = fd_lineups.copy()
|
245 |
+
if 'data_export_display' in st.session_state:
|
246 |
+
st.dataframe(st.session_state.data_export_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height=500, use_container_width = True)
|
247 |
+
st.download_button(
|
248 |
+
label="Export display optimals",
|
249 |
+
data=convert_df(export_file),
|
250 |
+
file_name='NBA_display_optimals.csv',
|
251 |
+
mime='text/csv',
|
252 |
+
)
|
253 |
+
|
254 |
+
with st.container():
|
255 |
+
if 'working_seed' in st.session_state:
|
256 |
+
# Create a new dataframe with summary statistics
|
257 |
+
if site_var1 == 'Draftkings':
|
258 |
+
summary_df = pd.DataFrame({
|
259 |
+
'Metric': ['Min', 'Average', 'Max', 'STDdev'],
|
260 |
+
'Salary': [
|
261 |
+
np.min(st.session_state.working_seed[:,6]),
|
262 |
+
np.mean(st.session_state.working_seed[:,6]),
|
263 |
+
np.max(st.session_state.working_seed[:,6]),
|
264 |
+
np.std(st.session_state.working_seed[:,6])
|
265 |
+
],
|
266 |
+
'Proj': [
|
267 |
+
np.min(st.session_state.working_seed[:,7]),
|
268 |
+
np.mean(st.session_state.working_seed[:,7]),
|
269 |
+
np.max(st.session_state.working_seed[:,7]),
|
270 |
+
np.std(st.session_state.working_seed[:,7])
|
271 |
+
],
|
272 |
+
'Own': [
|
273 |
+
np.min(st.session_state.working_seed[:,12]),
|
274 |
+
np.mean(st.session_state.working_seed[:,12]),
|
275 |
+
np.max(st.session_state.working_seed[:,12]),
|
276 |
+
np.std(st.session_state.working_seed[:,12])
|
277 |
+
]
|
278 |
+
})
|
279 |
+
elif site_var1 == 'Fanduel':
|
280 |
+
summary_df = pd.DataFrame({
|
281 |
+
'Metric': ['Min', 'Average', 'Max', 'STDdev'],
|
282 |
+
'Salary': [
|
283 |
+
np.min(st.session_state.working_seed[:,6]),
|
284 |
+
np.mean(st.session_state.working_seed[:,6]),
|
285 |
+
np.max(st.session_state.working_seed[:,6]),
|
286 |
+
np.std(st.session_state.working_seed[:,6])
|
287 |
+
],
|
288 |
+
'Proj': [
|
289 |
+
np.min(st.session_state.working_seed[:,7]),
|
290 |
+
np.mean(st.session_state.working_seed[:,7]),
|
291 |
+
np.max(st.session_state.working_seed[:,7]),
|
292 |
+
np.std(st.session_state.working_seed[:,7])
|
293 |
+
],
|
294 |
+
'Own': [
|
295 |
+
np.min(st.session_state.working_seed[:,12]),
|
296 |
+
np.mean(st.session_state.working_seed[:,12]),
|
297 |
+
np.max(st.session_state.working_seed[:,12]),
|
298 |
+
np.std(st.session_state.working_seed[:,12])
|
299 |
+
]
|
300 |
+
})
|
301 |
+
|
302 |
+
# Set the index of the summary dataframe as the "Metric" column
|
303 |
+
summary_df = summary_df.set_index('Metric')
|
304 |
+
|
305 |
+
# Display the summary dataframe
|
306 |
+
st.subheader("Optimal Statistics")
|
307 |
+
st.dataframe(summary_df.style.format({
|
308 |
+
'Salary': '{:.2f}',
|
309 |
+
'Proj': '{:.2f}',
|
310 |
+
'Own': '{:.2f}'
|
311 |
+
}).background_gradient(cmap='RdYlGn', axis=0, subset=['Salary', 'Proj', 'Own']), use_container_width=True)
|
312 |
+
|
313 |
+
with st.container():
|
314 |
+
tab1, tab2 = st.tabs(["Display Frequency", "Seed Frame Frequency"])
|
315 |
+
with tab1:
|
316 |
+
if 'data_export_display' in st.session_state:
|
317 |
+
if site_var1 == 'Draftkings':
|
318 |
+
player_columns = st.session_state.data_export_display.iloc[:, :6]
|
319 |
+
elif site_var1 == 'Fanduel':
|
320 |
+
player_columns = st.session_state.data_export_display.iloc[:, :6]
|
321 |
+
|
322 |
+
# Flatten the DataFrame and count unique values
|
323 |
+
value_counts = player_columns.values.flatten().tolist()
|
324 |
+
value_counts = pd.Series(value_counts).value_counts()
|
325 |
+
|
326 |
+
percentages = (value_counts / lineup_num_var * 100).round(2)
|
327 |
+
|
328 |
+
# Create a DataFrame with the results
|
329 |
+
summary_df = pd.DataFrame({
|
330 |
+
'Player': value_counts.index,
|
331 |
+
'Frequency': value_counts.values,
|
332 |
+
'Percentage': percentages.values
|
333 |
+
})
|
334 |
+
|
335 |
+
# Sort by frequency in descending order
|
336 |
+
summary_df = summary_df.sort_values('Frequency', ascending=False)
|
337 |
+
|
338 |
+
# Display the table
|
339 |
+
st.write("Player Frequency Table:")
|
340 |
+
st.dataframe(summary_df.style.format({'Percentage': '{:.2f}%'}), height=500, use_container_width=True)
|
341 |
+
|
342 |
+
st.download_button(
|
343 |
+
label="Export player frequency",
|
344 |
+
data=convert_df_to_csv(summary_df),
|
345 |
+
file_name='PGA_player_frequency.csv',
|
346 |
+
mime='text/csv',
|
347 |
+
)
|
348 |
+
with tab2:
|
349 |
+
if 'working_seed' in st.session_state:
|
350 |
+
if site_var1 == 'Draftkings':
|
351 |
+
player_columns = st.session_state.working_seed[:, :6]
|
352 |
+
elif site_var1 == 'Fanduel':
|
353 |
+
player_columns = st.session_state.working_seed[:, :6]
|
354 |
+
|
355 |
+
# Flatten the DataFrame and count unique values
|
356 |
+
value_counts = player_columns.flatten().tolist()
|
357 |
+
value_counts = pd.Series(value_counts).value_counts()
|
358 |
+
|
359 |
+
percentages = (value_counts / len(st.session_state.working_seed) * 100).round(2)
|
360 |
+
# Create a DataFrame with the results
|
361 |
+
summary_df = pd.DataFrame({
|
362 |
+
'Player': value_counts.index,
|
363 |
+
'Frequency': value_counts.values,
|
364 |
+
'Percentage': percentages.values
|
365 |
+
})
|
366 |
+
|
367 |
+
# Sort by frequency in descending order
|
368 |
+
summary_df = summary_df.sort_values('Frequency', ascending=False)
|
369 |
+
|
370 |
+
# Display the table
|
371 |
+
st.write("Seed Frame Frequency Table:")
|
372 |
+
st.dataframe(summary_df.style.format({'Percentage': '{:.2f}%'}), height=500, use_container_width=True)
|
373 |
+
|
374 |
+
st.download_button(
|
375 |
+
label="Export seed frame frequency",
|
376 |
+
data=convert_df_to_csv(summary_df),
|
377 |
+
file_name='PGA_seed_frame_frequency.csv',
|
378 |
+
mime='text/csv',
|
379 |
+
)
|