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