James McCool commited on
Commit
64bf25c
·
1 Parent(s): 64aa790

Initial upload with current branding

Browse files
Files changed (4) hide show
  1. .streamlit/secrets.toml +1 -0
  2. Dockerfile +12 -0
  3. requirements.txt +8 -3
  4. src/streamlit_app.py +403 -37
.streamlit/secrets.toml ADDED
@@ -0,0 +1 @@
 
 
1
+ mongo_uri = "mongodb+srv://multichem:[email protected]/?retryWrites=true&w=majority&appName=TestCluster"
Dockerfile CHANGED
@@ -11,6 +11,18 @@ RUN apt-get update && apt-get install -y \
11
 
12
  COPY requirements.txt ./
13
  COPY src/ ./src/
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  RUN pip3 install -r requirements.txt
16
 
 
11
 
12
  COPY requirements.txt ./
13
  COPY src/ ./src/
14
+ COPY .streamlit/ ./.streamlit/
15
+
16
+
17
+
18
+ ENV MONGO_URI="mongodb+srv://multichem:[email protected]/?retryWrites=true&w=majority&appName=TestCluster"
19
+ RUN useradd -m -u 1000 user
20
+ USER user
21
+ ENV HOME=/home/user\
22
+ PATH=/home/user/.local/bin:$PATH
23
+ WORKDIR $HOME/app
24
+ RUN pip install --no-cache-dir --upgrade pip
25
+ COPY --chown=user . $HOME/app
26
 
27
  RUN pip3 install -r requirements.txt
28
 
requirements.txt CHANGED
@@ -1,3 +1,8 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
1
+ streamlit
2
+ openpyxl
3
+ matplotlib
4
+ pulp
5
+ docker
6
+ plotly
7
+ scipy
8
+ pymongo
src/streamlit_app.py CHANGED
@@ -1,40 +1,406 @@
1
- import altair as alt
2
  import numpy as np
3
  import pandas as pd
4
- import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
1
+ import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
+ import pymongo
5
+ import re
6
+ import os
7
+
8
+ st.set_page_config(layout="wide")
9
+
10
+ @st.cache_resource
11
+ def init_conn():
12
+ # Try to get from environment variable first, fall back to secrets
13
+ uri = os.getenv('MONGO_URI')
14
+ if not uri:
15
+ uri = st.secrets['mongo_uri']
16
+ client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
17
+ db = client["NFL_Database"]
18
+
19
+ return db
20
+
21
+ db = init_conn()
22
+
23
+ wrong_acro = ['WSH', 'AZ']
24
+ right_acro = ['WAS', 'ARI']
25
+
26
+ game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
27
+ 'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
28
+
29
+ team_roo_format = {'Top Score%': '{:.2%}','0 Runs': '{:.2%}', '1 Run': '{:.2%}', '2 Runs': '{:.2%}', '3 Runs': '{:.2%}', '4 Runs': '{:.2%}',
30
+ '5 Runs': '{:.2%}','6 Runs': '{:.2%}', '7 Runs': '{:.2%}', '8 Runs': '{:.2%}', '9 Runs': '{:.2%}', '10 Runs': '{:.2%}'}
31
+
32
+ player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
33
+ '4x%': '{:.2%}','GPP%': '{:.2%}'}
34
+
35
+ st.markdown("""
36
+ <style>
37
+ /* Tab styling */
38
+ .stElementContainer [data-baseweb="button-group"] {
39
+ gap: 8px;
40
+ padding: 4px;
41
+ }
42
+ .stElementContainer [kind="segmented_control"] {
43
+ height: 45px;
44
+ white-space: pre-wrap;
45
+ background-color: #DAA520;
46
+ color: white;
47
+ border-radius: 10px;
48
+ gap: 1px;
49
+ padding: 10px 20px;
50
+ font-weight: bold;
51
+ transition: all 0.3s ease;
52
+ }
53
+ .stElementContainer [kind="segmented_controlActive"] {
54
+ height: 50px;
55
+ background-color: #DAA520;
56
+ border: 3px solid #FFD700;
57
+ color: white;
58
+ }
59
+ .stElementContainer [kind="segmented_control"]:hover {
60
+ background-color: #FFD700;
61
+ cursor: pointer;
62
+ }
63
+
64
+ div[data-baseweb="select"] > div {
65
+ background-color: #DAA520;
66
+ color: white;
67
+ }
68
+
69
+ </style>""", unsafe_allow_html=True)
70
+
71
+ @st.cache_resource(ttl=60)
72
+ def init_baselines():
73
+
74
+ collection = db["Player_Baselines"]
75
+ cursor = collection.find()
76
+
77
+ raw_display = pd.DataFrame(list(cursor))
78
+ raw_display = raw_display[['name', 'Team', 'Opp', 'Position', 'Salary', 'team_plays', 'team_pass', 'team_rush', 'team_tds', 'team_pass_tds', 'team_rush_tds', 'dropbacks', 'pass_yards', 'pass_tds',
79
+ 'rush_att', 'rush_yards', 'rush_tds', 'targets', 'rec', 'rec_yards', 'rec_tds', 'PPR', 'Half_PPR', 'Own']]
80
+ player_stats = raw_display[raw_display['Position'] != 'K']
81
+
82
+ collection = db["DK_NFL_ROO"]
83
+ cursor = collection.find()
84
+
85
+ raw_display = pd.DataFrame(list(cursor))
86
+ raw_display = raw_display.rename(columns={'player_ID': 'player_id'})
87
+ raw_display = raw_display[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%',
88
+ 'Own', 'Small_Field_Own', 'Large_Field_Own', 'Cash_Field_Own', 'CPT_Own', 'LevX', 'version', 'slate', 'timestamp', 'player_id', 'site']]
89
+ load_display = raw_display[raw_display['Position'] != 'K']
90
+ dk_roo_raw = load_display.dropna(subset=['Median'])
91
+
92
+ collection = db["FD_NFL_ROO"]
93
+ cursor = collection.find()
94
+
95
+ raw_display = pd.DataFrame(list(cursor))
96
+ raw_display = raw_display.rename(columns={'player_ID': 'player_id'})
97
+ raw_display = raw_display[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%',
98
+ 'Own', 'Small_Field_Own', 'Large_Field_Own', 'Cash_Field_Own', 'CPT_Own', 'LevX', 'version', 'slate', 'timestamp', 'player_id', 'site']]
99
+ load_display = raw_display[raw_display['Position'] != 'K']
100
+ fd_roo_raw = load_display.dropna(subset=['Median'])
101
+
102
+ return player_stats, dk_roo_raw, fd_roo_raw
103
+
104
+ @st.cache_data
105
+ def convert_df_to_csv(df):
106
+ return df.to_csv().encode('utf-8')
107
+
108
+ player_stats, dk_roo_raw, fd_roo_raw = init_baselines()
109
+ opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
110
+ t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
111
+
112
+ app_load_reset_column, app_view_site_column = st.columns([1, 9])
113
+ with app_load_reset_column:
114
+ if st.button("Load/Reset Data", key='reset_data_button'):
115
+ st.cache_data.clear()
116
+ player_stats, dk_roo_raw, fd_roo_raw = init_baselines()
117
+ for key in st.session_state.keys():
118
+ del st.session_state[key]
119
+ with app_view_site_column:
120
+ with st.container():
121
+ app_view_column, app_site_column = st.columns([3, 3])
122
+ with app_view_column:
123
+ view_var = st.selectbox("Select view", ["Simple", "Advanced"], key='view_selectbox')
124
+ with app_site_column:
125
+ site_var = st.selectbox("What site do you want to view?", ('Draftkings', 'Fanduel'), key='site_selectbox')
126
+
127
+ selected_tab = st.segmented_control(
128
+ "Select Tab",
129
+ options=["Pivot Finder", "User Upload"],
130
+ selection_mode='single',
131
+ default='Pivot Finder',
132
+ width='stretch',
133
+ label_visibility='collapsed',
134
+ key='tab_selector'
135
+ )
136
+
137
+ if selected_tab == 'Pivot Finder':
138
+ col1, col2 = st.columns([1, 5])
139
+ with col1:
140
+ st.info(t_stamp)
141
+ if st.button("Load/Reset Data", key='reset1'):
142
+ st.cache_data.clear()
143
+ player_stats, dk_stacks_raw, fd_stacks_raw, dk_roo_raw, fd_roo_raw = init_baselines()
144
+ opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
145
+ t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
146
+ data_var1 = st.radio("Which data are you loading?", ('Paydirt', 'User'), key='data_var1')
147
+ site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
148
+ if site_var1 == 'Draftkings':
149
+ if data_var1 == 'User':
150
+ raw_baselines = st.session_state['proj_dataframe']
151
+ elif data_var1 != 'User':
152
+ raw_baselines = dk_roo_raw[dk_roo_raw['slate'] == 'Main Slate']
153
+ raw_baselines = raw_baselines[raw_baselines['version'] == 'overall']
154
+ raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
155
+ elif site_var1 == 'Fanduel':
156
+ if data_var1 == 'User':
157
+ raw_baselines = st.session_state['proj_dataframe']
158
+ elif data_var1 != 'User':
159
+ raw_baselines = fd_roo_raw[fd_roo_raw['slate'] == 'Main Slate']
160
+ raw_baselines = raw_baselines[raw_baselines['version'] == 'overall']
161
+ raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
162
+ 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')
163
+ if check_seq == 'Single Player':
164
+ player_check = st.selectbox('Select player to create comps', options = raw_baselines['Player'].unique(), key='dk_player')
165
+ elif check_seq == 'Top X Owned':
166
+ top_x_var = st.number_input('How many players would you like to check?', min_value = 1, max_value = 10, value = 5, step = 1)
167
+ Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
168
+ Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
169
+ pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
170
+ if pos_var1 == 'Specific Positions':
171
+ pos_var_list = st.multiselect('Which positions would you like to include?', options = raw_baselines['Position'].unique(), key='pos_var_list')
172
+ elif pos_var1 == 'All Positions':
173
+ pos_var_list = raw_baselines.Position.values.tolist()
174
+ split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
175
+ if split_var1 == 'Specific Games':
176
+ team_var1 = st.multiselect('Which teams would you like to include?', options = raw_baselines['Team'].unique(), key='team_var1')
177
+ elif split_var1 == 'Full Slate Run':
178
+ team_var1 = raw_baselines.Team.values.tolist()
179
+
180
+ with col2:
181
+ placeholder = st.empty()
182
+ displayholder = st.empty()
183
+
184
+
185
+ if st.button('Simulate appropriate pivots'):
186
+ with placeholder:
187
+ if site_var1 == 'Draftkings':
188
+ working_roo = raw_baselines
189
+ working_roo.replace('', 0, inplace=True)
190
+ if site_var1 == 'Fanduel':
191
+ working_roo = raw_baselines
192
+ working_roo.replace('', 0, inplace=True)
193
+
194
+ own_dict = dict(zip(working_roo.Player, working_roo.Own))
195
+ team_dict = dict(zip(working_roo.Player, working_roo.Team))
196
+ opp_dict = dict(zip(working_roo.Player, working_roo.Opp))
197
+ pos_dict = dict(zip(working_roo.Player, working_roo.Position))
198
+ total_sims = 1000
199
+
200
+ if check_seq == 'Single Player':
201
+ player_var = working_roo.loc[working_roo['Player'] == player_check]
202
+ player_var = player_var.reset_index()
203
+ working_roo = working_roo[working_roo['Position'].isin(pos_var_list)]
204
+ working_roo = working_roo[working_roo['Team'].isin(team_var1)]
205
+ working_roo = working_roo.loc[(working_roo['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo['Salary'] <= player_var['Salary'][0] + Salary_var)]
206
+ working_roo = working_roo.loc[(working_roo['Median'] >= player_var['Median'][0] - Median_var) & (working_roo['Median'] <= player_var['Median'][0] + Median_var)]
207
+
208
+ flex_file = working_roo[['Player', 'Position', 'Salary', 'Median']]
209
+ flex_file['Floor_raw'] = flex_file['Median'] * .25
210
+ flex_file['Ceiling_raw'] = flex_file['Median'] * 1.75
211
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'QB', (flex_file['Median'] * .33), flex_file['Floor_raw'])
212
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'WR', (flex_file['Median'] * .15), flex_file['Floor_raw'])
213
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'QB', (flex_file['Median'] * 1.75), flex_file['Ceiling_raw'])
214
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'WR', (flex_file['Median'] * 1.85), flex_file['Ceiling_raw'])
215
+ flex_file['STD'] = flex_file['Median'] / 4
216
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
217
+ hold_file = flex_file.copy()
218
+ overall_file = flex_file.copy()
219
+ salary_file = flex_file.copy()
220
+
221
+ overall_players = overall_file[['Player']]
222
+
223
+ for x in range(0,total_sims):
224
+ salary_file[x] = salary_file['Salary']
225
+
226
+ salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
227
+
228
+ salary_file = salary_file.div(1000)
229
+
230
+ for x in range(0,total_sims):
231
+ overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
232
+
233
+ overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
234
+
235
+ players_only = hold_file[['Player']]
236
+ raw_lineups_file = players_only
237
+
238
+ for x in range(0,total_sims):
239
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
240
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
241
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
242
+
243
+ players_only=players_only.drop(['Player'], axis=1)
244
+
245
+ salary_2x_check = (overall_file - (salary_file*2))
246
+ salary_3x_check = (overall_file - (salary_file*3))
247
+ salary_4x_check = (overall_file - (salary_file*4))
248
+
249
+ players_only['Average_Rank'] = players_only.mean(axis=1)
250
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
251
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
252
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
253
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
254
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
255
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
256
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
257
+
258
+ players_only['Player'] = hold_file[['Player']]
259
+
260
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
261
+
262
+ final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
263
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
264
+ final_Proj['Own'] = final_Proj['Player'].map(own_dict)
265
+ final_Proj['Team'] = final_Proj['Player'].map(team_dict)
266
+ final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
267
+ 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']]
268
+ final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
269
+ final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
270
+ final_Proj['LevX'] = 0
271
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'QB', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
272
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'TE', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
273
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'RB', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
274
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'WR', final_Proj[['Projection Rank', 'Top_10_finish']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
275
+ final_Proj['CPT_Own'] = final_Proj['Own'] / 4
276
+
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', 'LevX']]
278
+ final_Proj = final_Proj.set_index('Player')
279
+ st.session_state.final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
280
+
281
+ elif check_seq == 'Top X Owned':
282
+ if pos_var1 == 'Specific Positions':
283
+ raw_baselines = raw_baselines[raw_baselines['Position'].isin(pos_var_list)]
284
+ player_check = raw_baselines['Player'].head(top_x_var).tolist()
285
+ final_proj_list = []
286
+ for players in player_check:
287
+ players_pos = pos_dict[players]
288
+ player_var = working_roo.loc[working_roo['Player'] == players]
289
+ player_var = player_var.reset_index()
290
+ working_roo_temp = working_roo[working_roo['Position'] == players_pos]
291
+ working_roo_temp = working_roo_temp[working_roo_temp['Team'].isin(team_var1)]
292
+ 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)]
293
+ 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)]
294
+
295
+ flex_file = working_roo_temp[['Player', 'Position', 'Salary', 'Median']]
296
+ flex_file['Floor_raw'] = flex_file['Median'] * .25
297
+ flex_file['Ceiling_raw'] = flex_file['Median'] * 1.75
298
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'QB', (flex_file['Median'] * .33), flex_file['Floor_raw'])
299
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'WR', (flex_file['Median'] * .15), flex_file['Floor_raw'])
300
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'QB', (flex_file['Median'] * 1.75), flex_file['Ceiling_raw'])
301
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'WR', (flex_file['Median'] * 1.85), flex_file['Ceiling_raw'])
302
+ flex_file['STD'] = flex_file['Median'] / 4
303
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
304
+ hold_file = flex_file.copy()
305
+ overall_file = flex_file.copy()
306
+ salary_file = flex_file.copy()
307
+
308
+ overall_players = overall_file[['Player']]
309
+
310
+ for x in range(0,total_sims):
311
+ salary_file[x] = salary_file['Salary']
312
+
313
+ salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
314
+
315
+ salary_file = salary_file.div(1000)
316
+
317
+ for x in range(0,total_sims):
318
+ overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
319
+
320
+ overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
321
+
322
+ players_only = hold_file[['Player']]
323
+ raw_lineups_file = players_only
324
+
325
+ for x in range(0,total_sims):
326
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
327
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
328
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
329
+
330
+ players_only=players_only.drop(['Player'], axis=1)
331
+
332
+ salary_2x_check = (overall_file - (salary_file*2))
333
+ salary_3x_check = (overall_file - (salary_file*3))
334
+ salary_4x_check = (overall_file - (salary_file*4))
335
+
336
+ players_only['Average_Rank'] = players_only.mean(axis=1)
337
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
338
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
339
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
340
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
341
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
342
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
343
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
344
+
345
+ players_only['Player'] = hold_file[['Player']]
346
+
347
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
348
+
349
+ final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
350
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
351
+ final_Proj['Own'] = final_Proj['Player'].map(own_dict)
352
+ final_Proj['Team'] = final_Proj['Player'].map(team_dict)
353
+ final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
354
+ 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']]
355
+ final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
356
+ final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
357
+ final_Proj['LevX'] = 0
358
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'QB', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
359
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'TE', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
360
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'RB', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
361
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'WR', final_Proj[['Projection Rank', 'Top_10_finish']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
362
+ final_Proj['CPT_Own'] = final_Proj['Own'] / 4
363
+ final_Proj['Pivot_source'] = players
364
+
365
+ 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']]
366
+
367
+ final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
368
+ final_proj_list.append(final_Proj)
369
+ st.write(f'finished run for {players}')
370
+
371
+ # Concatenate all the final_Proj dataframes
372
+ final_Proj_combined = pd.concat(final_proj_list)
373
+ final_Proj_combined = final_Proj_combined.sort_values(by='LevX', ascending=False)
374
+ final_Proj_combined = final_Proj_combined[final_Proj_combined['Player'] != final_Proj_combined['Pivot_source']]
375
+ st.session_state.final_Proj = final_Proj_combined.reset_index(drop=True) # Assign the combined dataframe back to final_Proj
376
+
377
+ placeholder.empty()
378
+
379
+ with displayholder.container():
380
+ if 'final_Proj' in st.session_state:
381
+ 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)
382
+
383
+ st.download_button(
384
+ label="Export Tables",
385
+ data=convert_df_to_csv(st.session_state.final_Proj),
386
+ file_name='NFL_pivot_export.csv',
387
+ mime='text/csv',
388
+ )
389
+ else:
390
+ st.write("Run some pivots my dude/dudette")
391
+
392
+ if selected_tab == 'User Upload':
393
+ 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'.")
394
+ col1, col2 = st.columns([1, 5])
395
 
396
+ with col1:
397
+ proj_file = st.file_uploader("Upload Projections File", key = 'proj_uploader')
398
+
399
+ if proj_file is not None:
400
+ try:
401
+ st.session_state['proj_dataframe'] = pd.read_csv(proj_file)
402
+ except:
403
+ st.session_state['proj_dataframe'] = pd.read_excel(proj_file)
404
+ with col2:
405
+ if proj_file is not None:
406
+ st.dataframe(st.session_state['proj_dataframe'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)