James McCool commited on
Commit
2028cdc
·
1 Parent(s): f8a70c5

Initial Commit

Browse files
Files changed (3) hide show
  1. app.py +317 -0
  2. app.yaml +10 -0
  3. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import streamlit as st
4
+ import pymongo
5
+
6
+ st.set_page_config(layout="wide")
7
+
8
+ @st.cache_resource
9
+ def init_conn():
10
+
11
+ uri = st.secrets['mongo_uri']
12
+ client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
13
+ db = client["MLB_Database"]
14
+
15
+ return db
16
+
17
+ db = init_conn()
18
+
19
+ player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
20
+ '4x%': '{:.2%}', 'GPP%': '{:.2%}'}
21
+
22
+ st.markdown("""
23
+ <style>
24
+ /* Tab styling */
25
+ .stTabs [data-baseweb="tab-list"] {
26
+ gap: 8px;
27
+ padding: 4px;
28
+ }
29
+ .stTabs [data-baseweb="tab"] {
30
+ height: 50px;
31
+ white-space: pre-wrap;
32
+ background-color: #FFD700;
33
+ color: white;
34
+ border-radius: 10px;
35
+ gap: 1px;
36
+ padding: 10px 20px;
37
+ font-weight: bold;
38
+ transition: all 0.3s ease;
39
+ }
40
+ .stTabs [aria-selected="true"] {
41
+ background-color: #DAA520;
42
+ color: white;
43
+ }
44
+ .stTabs [data-baseweb="tab"]:hover {
45
+ background-color: #DAA520;
46
+ cursor: pointer;
47
+ }
48
+ </style>""", unsafe_allow_html=True)
49
+
50
+ @st.cache_resource(ttl = 60)
51
+ def init_stat_load():
52
+
53
+ collection = db["Player_Range_Of_Outcomes"]
54
+ cursor = collection.find()
55
+
56
+ raw_display = pd.DataFrame(list(cursor))
57
+ raw_display = raw_display[['Player', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Own%']]
58
+ raw_display = raw_display.rename(columns={'Own%': 'Own'})
59
+ initial_concat = raw_display.sort_values(by='Own', ascending=False)
60
+
61
+ return initial_concat
62
+
63
+ @st.cache_data
64
+ def convert_df_to_csv(df):
65
+ return df.to_csv().encode('utf-8')
66
+
67
+ proj_raw = init_stat_load()
68
+
69
+ st.header("MLB DFS Pivot Tool")
70
+ with st.expander("Info and Filters"):
71
+ if st.button("Load/Reset Data", key='reset1'):
72
+ st.cache_data.clear()
73
+ proj_raw, timestamp = init_stat_load()
74
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
75
+ for key in st.session_state.keys():
76
+ del st.session_state[key]
77
+ site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
78
+ slate_var1 = st.radio("What slate are you working with?", ('Main Slate', 'Secondary Slate'), key='slate_var1')
79
+ if site_var1 == 'Draftkings':
80
+ raw_baselines = proj_raw[proj_raw['site'] == 'Draftkings']
81
+ if slate_var1 == 'Main Slate':
82
+ raw_baselines = raw_baselines[raw_baselines['Slate'] == 'main_slate']
83
+ elif slate_var1 == 'Secondary Slate':
84
+ raw_baselines = raw_baselines[raw_baselines['Slate'] == 'secondary_slate']
85
+ raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
86
+ elif site_var1 == 'Fanduel':
87
+ raw_baselines = proj_raw[proj_raw['site'] == 'Fanduel']
88
+ if slate_var1 == 'Main Slate':
89
+ raw_baselines = raw_baselines[raw_baselines['Slate'] == 'main_slate']
90
+ elif slate_var1 == 'Secondary Slate':
91
+ raw_baselines = raw_baselines[raw_baselines['Slate'] == 'secondary_slate']
92
+ raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
93
+ 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')
94
+ if check_seq == 'Single Player':
95
+ player_check = st.selectbox('Select player to create comps', options = raw_baselines['Player'].unique(), key='dk_player')
96
+ elif check_seq == 'Top X Owned':
97
+ top_x_var = st.number_input('How many players would you like to check?', min_value = 1, max_value = 10, value = 5, step = 1)
98
+ Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
99
+ Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
100
+ pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
101
+ if site_var1 == 'Draftkings':
102
+ if pos_var1 == 'Specific Positions':
103
+ pos_var_list = st.multiselect('Which positions would you like to include?', options = ['SP', 'C', '1B', '2B', '3B', 'SS', 'OF'], key='pos_var_list')
104
+ elif pos_var1 == 'All Positions':
105
+ pos_var_list = ['SP', 'C', '1B', '2B', '3B', 'SS', 'OF']
106
+ elif site_var1 == 'Fanduel':
107
+ if pos_var1 == 'Specific Positions':
108
+ pos_var_list = st.multiselect('Which positions would you like to include?', options = ['P', 'C', '1B', '2B', '3B', 'SS', 'OF'], key='pos_var_list')
109
+ elif pos_var1 == 'All Positions':
110
+ pos_var_list = ['P', 'C', '1B', '2B', '3B', 'SS', 'OF']
111
+ split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
112
+ if split_var1 == 'Specific Games':
113
+ team_var1 = st.multiselect('Which teams would you like to include?', options = raw_baselines['Team'].unique(), key='team_var1')
114
+ elif split_var1 == 'Full Slate Run':
115
+ team_var1 = raw_baselines.Team.values.tolist()
116
+
117
+ placeholder = st.empty()
118
+ displayholder = st.empty()
119
+
120
+ if st.button('Simulate appropriate pivots'):
121
+ with placeholder:
122
+ if site_var1 == 'Draftkings':
123
+ working_roo = raw_baselines
124
+ working_roo.replace('', 0, inplace=True)
125
+ if site_var1 == 'Fanduel':
126
+ working_roo = raw_baselines
127
+ working_roo.replace('', 0, inplace=True)
128
+
129
+ own_dict = dict(zip(working_roo.Player, working_roo.Own))
130
+ team_dict = dict(zip(working_roo.Player, working_roo.Team))
131
+ pos_dict = dict(zip(working_roo.Player, working_roo.Position))
132
+ total_sims = 1000
133
+
134
+ if check_seq == 'Single Player':
135
+ player_var = working_roo.loc[working_roo['Player'] == player_check]
136
+ player_var = player_var.reset_index()
137
+ working_roo = working_roo[working_roo['Position'].apply(lambda x: any(pos in x.split('/') for pos in pos_var_list))]
138
+ working_roo = working_roo[working_roo['Team'].isin(team_var1)]
139
+ working_roo = working_roo.loc[(working_roo['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo['Salary'] <= player_var['Salary'][0] + Salary_var)]
140
+ working_roo = working_roo.loc[(working_roo['Median'] >= player_var['Median'][0] - Median_var) & (working_roo['Median'] <= player_var['Median'][0] + Median_var)]
141
+
142
+ flex_file = working_roo[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling']]
143
+ flex_file['STD'] = (flex_file['Median']/3)
144
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
145
+ hold_file = flex_file.copy()
146
+ overall_file = flex_file.copy()
147
+ salary_file = flex_file.copy()
148
+
149
+ overall_players = overall_file[['Player']]
150
+
151
+ for x in range(0,total_sims):
152
+ salary_file[x] = salary_file['Salary']
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
+ for x in range(0,total_sims):
159
+ overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
160
+
161
+ overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
162
+
163
+ players_only = hold_file[['Player']]
164
+ raw_lineups_file = players_only
165
+
166
+ for x in range(0,total_sims):
167
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
168
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
169
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
170
+
171
+ players_only=players_only.drop(['Player'], axis=1)
172
+
173
+ salary_2x_check = (overall_file - (salary_file*2))
174
+ salary_3x_check = (overall_file - (salary_file*3))
175
+ salary_4x_check = (overall_file - (salary_file*4))
176
+ gpp_check = (overall_file - ((salary_file*5)+10))
177
+
178
+ players_only['Average_Rank'] = players_only.mean(axis=1)
179
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
180
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
181
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
182
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
183
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
184
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
185
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
186
+ players_only['GPP%'] = gpp_check[gpp_check >= 1].count(axis=1)/float(total_sims)
187
+
188
+ players_only['Player'] = hold_file[['Player']]
189
+
190
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%']]
191
+
192
+ final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
193
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%']]
194
+
195
+ final_Proj['Own'] = final_Proj['Player'].map(own_dict)
196
+ final_Proj['Minutes Proj'] = final_Proj['Player'].map(min_dict)
197
+ final_Proj['Team'] = final_Proj['Player'].map(team_dict)
198
+ final_Proj['Own'] = final_Proj['Own'].astype('float')
199
+ final_Proj = final_Proj[['Player', 'Minutes Proj', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%', 'Own']]
200
+ final_Proj['Projection Rank'] = final_Proj.Top_finish.rank(pct = True)
201
+ final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
202
+ final_Proj['LevX'] = (final_Proj['Projection Rank'] - final_Proj['Own Rank']) * 100
203
+ final_Proj['ValX'] = ((final_Proj[['2x%', '3x%', '4x%']].mean(axis=1))*100) + final_Proj['LevX']
204
+ final_Proj['ValX'] = np.where(final_Proj['ValX'] > 100, 100, final_Proj['ValX'])
205
+ final_Proj['ValX'] = np.where(final_Proj['ValX'] < 0, 0, final_Proj['ValX'])
206
+
207
+ final_Proj = final_Proj[['Player', 'Minutes Proj', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%', 'Own', 'LevX', 'ValX']]
208
+ final_Proj = final_Proj.set_index('Player')
209
+
210
+ st.session_state.final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
211
+
212
+ elif check_seq == 'Top X Owned':
213
+ if pos_var1 == 'Specific Positions':
214
+ raw_baselines = raw_baselines[raw_baselines['Position'].apply(lambda x: any(pos in x.split('/') for pos in pos_var_list))]
215
+ player_check = raw_baselines['Player'].head(top_x_var).tolist()
216
+ st.write(player_check)
217
+ final_proj_list = []
218
+ for players in player_check:
219
+ players_pos = pos_dict[players]
220
+ player_var = working_roo.loc[working_roo['Player'] == players]
221
+ player_var = player_var.reset_index()
222
+ working_roo_temp = working_roo[working_roo['Team'].isin(team_var1)]
223
+
224
+ 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)]
225
+ 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)]
226
+
227
+ flex_file = working_roo_temp[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling']]
228
+ flex_file['STD'] = (flex_file['Median']/3)
229
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
230
+ hold_file = flex_file.copy()
231
+ overall_file = flex_file.copy()
232
+ salary_file = flex_file.copy()
233
+
234
+ overall_players = overall_file[['Player']]
235
+
236
+ for x in range(0,total_sims):
237
+ salary_file[x] = salary_file['Salary']
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
+ for x in range(0,total_sims):
244
+ overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
245
+
246
+ overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
247
+
248
+ players_only = hold_file[['Player']]
249
+ raw_lineups_file = players_only
250
+
251
+ for x in range(0,total_sims):
252
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
253
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
254
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
255
+
256
+ players_only=players_only.drop(['Player'], axis=1)
257
+
258
+ salary_2x_check = (overall_file - (salary_file*2))
259
+ salary_3x_check = (overall_file - (salary_file*3))
260
+ salary_4x_check = (overall_file - (salary_file*4))
261
+ gpp_check = (overall_file - ((salary_file*5)+10))
262
+
263
+ players_only['Average_Rank'] = players_only.mean(axis=1)
264
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
265
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
266
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
267
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
268
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
269
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
270
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
271
+ players_only['GPP%'] = gpp_check[gpp_check >= 1].count(axis=1)/float(total_sims)
272
+
273
+ players_only['Player'] = hold_file[['Player']]
274
+
275
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%']]
276
+
277
+ final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
278
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%']]
279
+
280
+ final_Proj['Own'] = final_Proj['Player'].map(own_dict)
281
+ final_Proj['Minutes Proj'] = final_Proj['Player'].map(min_dict)
282
+ final_Proj['Team'] = final_Proj['Player'].map(team_dict)
283
+ final_Proj['Own'] = final_Proj['Own'].astype('float')
284
+ final_Proj['Projection Rank'] = final_Proj.Top_finish.rank(pct = True)
285
+ final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
286
+ final_Proj['LevX'] = (final_Proj['Projection Rank'] - final_Proj['Own Rank']) * 100
287
+ final_Proj['ValX'] = ((final_Proj[['2x%', '3x%', '4x%']].mean(axis=1))*100) + final_Proj['LevX']
288
+ final_Proj['ValX'] = np.where(final_Proj['ValX'] > 100, 100, final_Proj['ValX'])
289
+ final_Proj['ValX'] = np.where(final_Proj['ValX'] < 0, 0, final_Proj['ValX'])
290
+ final_Proj['Pivot_source'] = players
291
+
292
+ final_Proj = final_Proj[['Player', 'Pivot_source', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'GPP%', 'Own', 'LevX', 'ValX']]
293
+
294
+ final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
295
+ final_proj_list.append(final_Proj)
296
+ st.write(f'finished run for {players}')
297
+
298
+ # Concatenate all the final_Proj dataframes
299
+ final_Proj_combined = pd.concat(final_proj_list)
300
+ final_Proj_combined = final_Proj_combined.sort_values(by='LevX', ascending=False)
301
+ final_Proj_combined = final_Proj_combined[final_Proj_combined['Player'] != final_Proj_combined['Pivot_source']]
302
+ st.session_state.final_Proj = final_Proj_combined.reset_index(drop=True) # Assign the combined dataframe back to final_Proj
303
+
304
+ placeholder.empty()
305
+
306
+ with displayholder.container():
307
+ if 'final_Proj' in st.session_state:
308
+ 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)
309
+
310
+ st.download_button(
311
+ label="Export Tables",
312
+ data=convert_df_to_csv(st.session_state.final_Proj),
313
+ file_name='MLB_pivot_export.csv',
314
+ mime='text/csv',
315
+ )
316
+ else:
317
+ st.write("Run some pivots my dude/dudette")
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