|
import streamlit as st |
|
st.set_page_config(layout="wide") |
|
import numpy as np |
|
import pandas as pd |
|
import time |
|
from rapidfuzz import process, fuzz |
|
import random |
|
import re |
|
from collections import Counter |
|
|
|
|
|
from global_func.clean_player_name import clean_player_name |
|
from global_func.load_file import load_file |
|
from global_func.load_ss_file import load_ss_file |
|
from global_func.load_dk_fd_file import load_dk_fd_file |
|
from global_func.find_name_mismatches import find_name_mismatches |
|
from global_func.predict_dupes import predict_dupes |
|
from global_func.highlight_rows import highlight_changes, highlight_changes_winners, highlight_changes_losers |
|
from global_func.load_csv import load_csv |
|
from global_func.find_csv_mismatches import find_csv_mismatches |
|
from global_func.trim_portfolio import trim_portfolio |
|
from global_func.get_portfolio_names import get_portfolio_names |
|
from global_func.small_field_preset import small_field_preset |
|
from global_func.large_field_preset import large_field_preset |
|
from global_func.distribute_preset import distribute_preset |
|
|
|
freq_format = {'Finish_percentile': '{:.2%}', 'Lineup Edge': '{:.2%}', 'Win%': '{:.2%}'} |
|
stacking_sports = ['MLB', 'NHL', 'NFL'] |
|
player_wrong_names_mlb = ['Enrique Hernandez'] |
|
player_right_names_mlb = ['Kike Hernandez'] |
|
|
|
with st.container(): |
|
|
|
col1, col2, col3, col4 = st.columns(4) |
|
with col1: |
|
if st.button('Clear data', key='reset3'): |
|
st.session_state.clear() |
|
with col2: |
|
site_var = st.selectbox("Select Site", ['Draftkings', 'Fanduel']) |
|
|
|
with col3: |
|
sport_var = st.selectbox("Select Sport", ['NFL', 'MLB', 'NBA', 'NHL', 'MMA', 'CS2', 'TENNIS', 'GOLF', 'WNBA']) |
|
|
|
with col4: |
|
type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown']) |
|
|
|
tab1, tab2 = st.tabs(["Data Load", "Manage Portfolio"]) |
|
with tab1: |
|
if st.button('Clear data', key='reset1'): |
|
st.session_state.clear() |
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
with col1: |
|
st.subheader("Draftkings/Fanduel CSV") |
|
st.info("Upload the player pricing CSV from the site you are playing on") |
|
|
|
upload_csv_col, csv_template_col = st.columns([3, 1]) |
|
with upload_csv_col: |
|
csv_file = st.file_uploader("Upload CSV File", type=['csv']) |
|
if 'csv_file' in st.session_state: |
|
del st.session_state['csv_file'] |
|
with csv_template_col: |
|
|
|
csv_template_df = pd.DataFrame(columns=['Name', 'ID', 'Roster Position', 'Salary']) |
|
|
|
st.download_button( |
|
label="CSV Template", |
|
data=csv_template_df.to_csv(index=False), |
|
file_name="csv_template.csv", |
|
mime="text/csv" |
|
) |
|
st.session_state['csv_file'] = load_csv(csv_file) |
|
try: |
|
st.session_state['csv_file']['Salary'] = st.session_state['csv_file']['Salary'].astype(str).str.replace(',', '').astype(int) |
|
except: |
|
pass |
|
|
|
if csv_file: |
|
|
|
st.success('Projections file loaded successfully!') |
|
st.dataframe(st.session_state['csv_file'].head(10)) |
|
|
|
with col2: |
|
st.subheader("Portfolio File") |
|
st.info("Go ahead and upload a portfolio file here. Only include player columns.") |
|
|
|
upload_toggle = st.selectbox("What source are you uploading from?", options=['SaberSim (Just IDs)', 'Draftkings/Fanduel (Names + IDs)', 'Other (Just Names)']) |
|
if upload_toggle == 'SaberSim (Just IDs)' or upload_toggle == 'Draftkings/Fanduel (Names + IDs)': |
|
portfolio_file = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) |
|
if 'portfolio' in st.session_state: |
|
del st.session_state['portfolio'] |
|
if 'export_portfolio' in st.session_state: |
|
del st.session_state['export_portfolio'] |
|
|
|
else: |
|
portfolio_file = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) |
|
if 'portfolio' in st.session_state: |
|
del st.session_state['portfolio'] |
|
if 'export_portfolio' in st.session_state: |
|
del st.session_state['export_portfolio'] |
|
if 'portfolio' not in st.session_state: |
|
if portfolio_file: |
|
if upload_toggle == 'SaberSim (Just IDs)': |
|
st.session_state['export_portfolio'], st.session_state['portfolio'] = load_ss_file(portfolio_file, st.session_state['csv_file']) |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all') |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True) |
|
st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True) |
|
elif upload_toggle == 'Draftkings/Fanduel (Names + IDs)': |
|
st.session_state['export_portfolio'], st.session_state['portfolio'] = load_dk_fd_file(portfolio_file, st.session_state['csv_file']) |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all') |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True) |
|
st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True) |
|
else: |
|
st.session_state['export_portfolio'], st.session_state['portfolio'] = load_file(portfolio_file) |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all') |
|
st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True) |
|
st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True) |
|
|
|
if 'Stack' in st.session_state['portfolio'].columns: |
|
|
|
stack_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Stack'])) |
|
st.write(f"Found {len(stack_dict)} stack assignments") |
|
st.session_state['portfolio'] = st.session_state['portfolio'].drop(columns=['Stack']) |
|
else: |
|
stack_dict = None |
|
if st.session_state['portfolio'] is not None: |
|
st.success('Portfolio file loaded successfully!') |
|
st.session_state['portfolio'] = st.session_state['portfolio'].apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb)) |
|
st.dataframe(st.session_state['portfolio'].head(10)) |
|
|
|
with col3: |
|
st.subheader("Projections File") |
|
st.info("upload a projections file that has 'player_names', 'salary', 'median', 'ownership', and 'captain ownership' columns. Note that the salary for showdown needs to be the FLEX salary, not the captain salary.") |
|
|
|
|
|
upload_col, template_col = st.columns([3, 1]) |
|
|
|
with upload_col: |
|
projections_file = st.file_uploader("Upload Projections File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) |
|
if 'projections_df' in st.session_state: |
|
del st.session_state['projections_df'] |
|
|
|
with template_col: |
|
|
|
template_df = pd.DataFrame(columns=['player_names', 'position', 'team', 'salary', 'median', 'ownership', 'captain ownership']) |
|
|
|
st.download_button( |
|
label="Template", |
|
data=template_df.to_csv(index=False), |
|
file_name="projections_template.csv", |
|
mime="text/csv" |
|
) |
|
|
|
if projections_file: |
|
export_projections, projections = load_file(projections_file) |
|
if projections is not None: |
|
st.success('Projections file loaded successfully!') |
|
try: |
|
projections['salary'] = projections['salary'].str.replace(',', '').str.replace('$', '').str.replace(' ', '') |
|
st.write('replaced salary symbols') |
|
except: |
|
pass |
|
try: |
|
projections['ownership'] = projections['ownership'].str.replace('%', '').str.replace(' ', '') |
|
st.write('replaced ownership symbols') |
|
except: |
|
pass |
|
projections['salary'] = projections['salary'].dropna().astype(int) |
|
projections['ownership'] = projections['ownership'].astype(float) |
|
if type_var == 'Showdown': |
|
if projections['captain ownership'].isna().all(): |
|
projections['CPT_Own_raw'] = (projections['ownership'] / 2) * ((100 - (100-projections['ownership']))/100) |
|
cpt_own_var = 100 / projections['CPT_Own_raw'].sum() |
|
projections['captain ownership'] = projections['CPT_Own_raw'] * cpt_own_var |
|
projections = projections.drop(columns='CPT_Own_raw', axis=1) |
|
|
|
projections = projections.apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb)) |
|
st.dataframe(projections.head(10)) |
|
|
|
if portfolio_file and projections_file: |
|
if st.session_state['portfolio'] is not None and projections is not None: |
|
st.subheader("Name Matching Analysis") |
|
|
|
|
|
portfolio_names = get_portfolio_names(st.session_state['portfolio']) |
|
try: |
|
csv_names = st.session_state['csv_file']['Name'].tolist() |
|
except: |
|
csv_names = st.session_state['csv_file']['Nickname'].tolist() |
|
projection_names = projections['player_names'].tolist() |
|
|
|
|
|
portfolio_match_dict = {} |
|
unmatched_names = [] |
|
for portfolio_name in portfolio_names: |
|
match = process.extractOne( |
|
portfolio_name, |
|
csv_names, |
|
score_cutoff=87 |
|
) |
|
if match: |
|
portfolio_match_dict[portfolio_name] = match[0] |
|
if match[1] < 100: |
|
st.write(f"{portfolio_name} matched from portfolio to site csv {match[0]} with a score of {match[1]}%") |
|
else: |
|
portfolio_match_dict[portfolio_name] = portfolio_name |
|
unmatched_names.append(portfolio_name) |
|
|
|
|
|
portfolio = st.session_state['portfolio'].copy() |
|
player_columns = [col for col in portfolio.columns |
|
if col not in ['salary', 'median', 'Own']] |
|
|
|
|
|
for col in player_columns: |
|
portfolio[col] = portfolio[col].map(lambda x: portfolio_match_dict.get(x, x)) |
|
st.session_state['portfolio'] = portfolio |
|
|
|
|
|
projections_match_dict = {} |
|
unmatched_proj_names = [] |
|
for projections_name in projection_names: |
|
match = process.extractOne( |
|
projections_name, |
|
csv_names, |
|
score_cutoff=87 |
|
) |
|
if match: |
|
projections_match_dict[projections_name] = match[0] |
|
if match[1] < 100: |
|
st.write(f"{projections_name} matched from projections to site csv {match[0]} with a score of {match[1]}%") |
|
else: |
|
projections_match_dict[projections_name] = projections_name |
|
unmatched_proj_names.append(projections_name) |
|
|
|
|
|
projections['player_names'] = projections['player_names'].map(lambda x: projections_match_dict.get(x, x)) |
|
st.session_state['projections_df'] = projections |
|
|
|
projections_names = st.session_state['projections_df']['player_names'].tolist() |
|
portfolio_names = get_portfolio_names(st.session_state['portfolio']) |
|
|
|
|
|
projections_match_dict = {} |
|
unmatched_proj_names = [] |
|
for projections_name in projection_names: |
|
match = process.extractOne( |
|
projections_name, |
|
portfolio_names, |
|
score_cutoff=87 |
|
) |
|
if match: |
|
projections_match_dict[projections_name] = match[0] |
|
if match[1] < 100: |
|
st.write(f"{projections_name} matched from portfolio to projections {match[0]} with a score of {match[1]}%") |
|
else: |
|
projections_match_dict[projections_name] = projections_name |
|
unmatched_proj_names.append(projections_name) |
|
|
|
|
|
projections['player_names'] = projections['player_names'].map(lambda x: projections_match_dict.get(x, x)) |
|
st.session_state['projections_df'] = projections |
|
|
|
if sport_var in stacking_sports: |
|
team_dict = dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])) |
|
st.session_state['portfolio']['Stack'] = st.session_state['portfolio'].apply( |
|
lambda row: Counter( |
|
team_dict.get(player, '') for player in row[2:] |
|
if team_dict.get(player, '') != '' |
|
).most_common(1)[0][0] if any(team_dict.get(player, '') for player in row[2:]) else '', |
|
axis=1 |
|
) |
|
st.session_state['portfolio']['Size'] = st.session_state['portfolio'].apply( |
|
lambda row: Counter( |
|
team_dict.get(player, '') for player in row[2:] |
|
if team_dict.get(player, '') != '' |
|
).most_common(1)[0][1] if any(team_dict.get(player, '') for player in row[2:]) else 0, |
|
axis=1 |
|
) |
|
stack_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Stack'])) |
|
size_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Size'])) |
|
|
|
working_frame = st.session_state['portfolio'].copy() |
|
try: |
|
st.session_state['export_dict'] = dict(zip(st.session_state['csv_file']['Name'], st.session_state['csv_file']['Name + ID'])) |
|
except: |
|
st.session_state['export_dict'] = dict(zip(st.session_state['csv_file']['Nickname'], st.session_state['csv_file']['Id'])) |
|
st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with tab2: |
|
if 'portfolio' in st.session_state and 'projections_df' in st.session_state: |
|
with st.container(): |
|
col1, col2 = st.columns(2) |
|
with col1: |
|
if st.button('Reset Portfolio', key='reset_port'): |
|
del st.session_state['working_frame'] |
|
|
|
with col2: |
|
with st.form(key='contest_size_form'): |
|
size_col, strength_col, submit_col = st.columns(3) |
|
with size_col: |
|
Contest_Size = st.number_input("Enter Contest Size", value=25000, min_value=1, step=1) |
|
with strength_col: |
|
strength_var = st.selectbox("Select field strength", ['Average', 'Sharp', 'Weak']) |
|
with submit_col: |
|
submitted = st.form_submit_button("Submit Size/Strength") |
|
if submitted: |
|
del st.session_state['working_frame'] |
|
|
|
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean'] |
|
|
|
if 'working_frame' not in st.session_state: |
|
st.session_state['working_frame'] = st.session_state['origin_portfolio'].copy() |
|
if site_var == 'Draftkings': |
|
if type_var == 'Classic': |
|
if sport_var == 'CS2': |
|
st.session_state['map_dict'] = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'] * 1.5)), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
elif sport_var != 'CS2': |
|
st.session_state['map_dict'] = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
elif type_var == 'Showdown': |
|
if sport_var == 'GOLF': |
|
st.session_state['map_dict'] = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])) |
|
} |
|
if sport_var != 'GOLF': |
|
st.session_state['map_dict'] = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'] * 1.5)), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
elif site_var == 'Fanduel': |
|
st.session_state['map_dict'] = { |
|
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])), |
|
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])), |
|
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])), |
|
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])), |
|
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))), |
|
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])), |
|
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)), |
|
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership'])) |
|
} |
|
if type_var == 'Classic': |
|
if sport_var == 'CS2': |
|
|
|
st.session_state['working_frame']['salary'] = st.session_state['working_frame'].apply( |
|
lambda row: st.session_state['map_dict']['cpt_salary_map'].get(row.iloc[0], 0) + |
|
sum(st.session_state['map_dict']['salary_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
|
|
st.session_state['working_frame']['median'] = st.session_state['working_frame'].apply( |
|
lambda row: st.session_state['map_dict']['cpt_proj_map'].get(row.iloc[0], 0) + |
|
sum(st.session_state['map_dict']['proj_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
|
|
st.session_state['working_frame']['Own'] = st.session_state['working_frame'].apply( |
|
lambda row: st.session_state['map_dict']['cpt_own_map'].get(row.iloc[0], 0) + |
|
sum(st.session_state['map_dict']['own_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
elif sport_var != 'CS2': |
|
st.session_state['working_frame']['salary'] = st.session_state['working_frame'].apply(lambda row: sum(st.session_state['map_dict']['salary_map'].get(player, 0) for player in row), axis=1) |
|
st.session_state['working_frame']['median'] = st.session_state['working_frame'].apply(lambda row: sum(st.session_state['map_dict']['proj_map'].get(player, 0) for player in row), axis=1) |
|
st.session_state['working_frame']['Own'] = st.session_state['working_frame'].apply(lambda row: sum(st.session_state['map_dict']['own_map'].get(player, 0) for player in row), axis=1) |
|
if stack_dict is not None: |
|
st.session_state['working_frame']['Stack'] = st.session_state['working_frame'].index.map(stack_dict) |
|
st.session_state['working_frame']['Size'] = st.session_state['working_frame'].index.map(size_dict) |
|
elif type_var == 'Showdown': |
|
|
|
st.session_state['working_frame']['salary'] = st.session_state['working_frame'].apply( |
|
lambda row: st.session_state['map_dict']['cpt_salary_map'].get(row.iloc[0], 0) + |
|
sum(st.session_state['map_dict']['salary_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
|
|
st.session_state['working_frame']['median'] = st.session_state['working_frame'].apply( |
|
lambda row: st.session_state['map_dict']['cpt_proj_map'].get(row.iloc[0], 0) + |
|
sum(st.session_state['map_dict']['proj_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
|
|
|
|
st.session_state['working_frame']['Own'] = st.session_state['working_frame'].apply( |
|
lambda row: st.session_state['map_dict']['cpt_own_map'].get(row.iloc[0], 0) + |
|
sum(st.session_state['map_dict']['own_map'].get(player, 0) for player in row.iloc[1:]), |
|
axis=1 |
|
) |
|
st.session_state['working_frame'] = predict_dupes(st.session_state['working_frame'], st.session_state['map_dict'], site_var, type_var, Contest_Size, strength_var, sport_var) |
|
|
|
|
|
if 'info_columns_dict' not in st.session_state: |
|
st.session_state['info_columns_dict'] = { |
|
'Dupes': st.session_state['working_frame']['Dupes'], |
|
'Finish_percentile': st.session_state['working_frame']['Finish_percentile'], |
|
'Win%': st.session_state['working_frame']['Win%'], |
|
'Lineup Edge': st.session_state['working_frame']['Lineup Edge'], |
|
'Weighted Own': st.session_state['working_frame']['Weighted Own'], |
|
'Geomean': st.session_state['working_frame']['Geomean'], |
|
} |
|
|
|
if 'trimming_dict_maxes' not in st.session_state: |
|
st.session_state['trimming_dict_maxes'] = { |
|
'Own': st.session_state['working_frame']['Own'].max(), |
|
'Geomean': st.session_state['working_frame']['Geomean'].max(), |
|
'Weighted Own': st.session_state['working_frame']['Weighted Own'].max(), |
|
'median': st.session_state['working_frame']['median'].max(), |
|
'Finish_percentile': st.session_state['working_frame']['Finish_percentile'].max() |
|
} |
|
|
|
col1, col2 = st.columns([2, 8]) |
|
with col1: |
|
if 'trimming_dict_maxes' not in st.session_state: |
|
st.session_state['trimming_dict_maxes'] = { |
|
'Own': 500.0, |
|
'Geomean': 500.0, |
|
'Weighted Own': 500.0, |
|
'median': 500.0, |
|
'Finish_percentile': 1.0 |
|
} |
|
with st.expander('Macro Filter Options'): |
|
with st.form(key='macro_filter_form'): |
|
max_dupes = st.number_input("Max acceptable dupes?", value=1000, min_value=1, step=1) |
|
min_salary = st.number_input("Min acceptable salary?", value=1000, min_value=1000, step=100) |
|
max_salary = st.number_input("Max acceptable salary?", value=100000, min_value=1000, step=100) |
|
max_finish_percentile = st.number_input("Max acceptable finish percentile?", value=.50, min_value=0.005, step=.001) |
|
min_lineup_edge = st.number_input("Min acceptable Lineup Edge?", value=-.5, min_value=-1.00, step=.001) |
|
if sport_var in ['NFL', 'MLB', 'NHL']: |
|
stack_include_toggle = st.selectbox("Include specific stacks?", options=['All Stacks', 'Specific Stacks'], index=0) |
|
stack_selections = st.multiselect("If Specific Stacks, Which to include?", options=sorted(list(set(stack_dict.values()))), default=[]) |
|
|
|
stack_remove_toggle = st.selectbox("Remove specific stacks?", options=['No', 'Yes'], index=0) |
|
stack_remove = st.multiselect("If Specific Stacks, Which to remove?", options=sorted(list(set(stack_dict.values()))), default=[]) |
|
|
|
submitted = st.form_submit_button("Submit") |
|
if submitted: |
|
parsed_frame = st.session_state['working_frame'].copy() |
|
parsed_frame = parsed_frame[parsed_frame['Dupes'] <= max_dupes] |
|
parsed_frame = parsed_frame[parsed_frame['salary'] >= min_salary] |
|
parsed_frame = parsed_frame[parsed_frame['salary'] <= max_salary] |
|
parsed_frame = parsed_frame[parsed_frame['Finish_percentile'] <= max_finish_percentile] |
|
parsed_frame = parsed_frame[parsed_frame['Lineup Edge'] >= min_lineup_edge] |
|
if 'Stack' in parsed_frame.columns: |
|
if stack_include_toggle == 'All Stacks': |
|
parsed_frame = parsed_frame |
|
else: |
|
parsed_frame = parsed_frame[parsed_frame['Stack'].isin(stack_selections)] |
|
if stack_remove_toggle == 'Yes': |
|
parsed_frame = parsed_frame[~parsed_frame['Stack'].isin(stack_remove)] |
|
else: |
|
parsed_frame = parsed_frame |
|
st.session_state['working_frame'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True) |
|
st.session_state['export_merge'] = st.session_state['working_frame'].copy() |
|
|
|
with st.expander('Micro Filter Options'): |
|
with st.form(key='micro_filter_form'): |
|
player_names = set() |
|
for col in st.session_state['working_frame'].columns: |
|
if col not in excluded_cols: |
|
player_names.update(st.session_state['working_frame'][col].unique()) |
|
player_lock = st.multiselect("Lock players?", options=sorted(list(player_names)), default=[]) |
|
player_remove = st.multiselect("Remove players?", options=sorted(list(player_names)), default=[]) |
|
team_include = st.multiselect("Include teams?", options=sorted(list(set(st.session_state['projections_df']['team'].unique()))), default=[]) |
|
team_remove = st.multiselect("Remove teams?", options=sorted(list(set(st.session_state['projections_df']['team'].unique()))), default=[]) |
|
if sport_var in ['NFL', 'MLB', 'NHL']: |
|
size_include = st.multiselect("Include sizes?", options=sorted(list(set(st.session_state['working_frame']['Size'].unique()))), default=[]) |
|
else: |
|
size_include = [] |
|
|
|
submitted = st.form_submit_button("Submit") |
|
if submitted: |
|
parsed_frame = st.session_state['working_frame'].copy() |
|
if player_remove: |
|
|
|
player_columns = [col for col in parsed_frame.columns if col not in excluded_cols] |
|
remove_mask = parsed_frame[player_columns].apply( |
|
lambda row: not any(player in list(row) for player in player_remove), axis=1 |
|
) |
|
parsed_frame = parsed_frame[remove_mask] |
|
|
|
if player_lock: |
|
|
|
player_columns = [col for col in parsed_frame.columns if col not in excluded_cols] |
|
|
|
lock_mask = parsed_frame[player_columns].apply( |
|
lambda row: all(player in list(row) for player in player_lock), axis=1 |
|
) |
|
parsed_frame = parsed_frame[lock_mask] |
|
|
|
if team_include: |
|
|
|
filtered_player_columns = [col for col in player_columns if col not in ['SP1', 'SP2']] |
|
team_frame = parsed_frame[filtered_player_columns].apply( |
|
lambda x: x.map(st.session_state['map_dict']['team_map']) |
|
) |
|
|
|
include_mask = team_frame.apply( |
|
lambda row: any(team in list(row) for team in team_include), axis=1 |
|
) |
|
parsed_frame = parsed_frame[include_mask] |
|
|
|
if team_remove: |
|
|
|
filtered_player_columns = [col for col in player_columns if col not in ['SP1', 'SP2']] |
|
team_frame = parsed_frame[filtered_player_columns].apply( |
|
lambda x: x.map(st.session_state['map_dict']['team_map']) |
|
) |
|
|
|
remove_mask = team_frame.apply( |
|
lambda row: not any(team in list(row) for team in team_remove), axis=1 |
|
) |
|
parsed_frame = parsed_frame[remove_mask] |
|
|
|
if size_include: |
|
parsed_frame = parsed_frame[parsed_frame['Size'].isin(size_include)] |
|
|
|
st.session_state['working_frame'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True) |
|
st.session_state['export_merge'] = st.session_state['working_frame'].copy() |
|
|
|
with st.expander('Trimming Options'): |
|
with st.form(key='trim_form'): |
|
st.write("Sorting and trimming variables:") |
|
perf_var, own_var = st.columns(2) |
|
with perf_var: |
|
performance_type = st.selectbox("Sorting variable", ['median', 'Finish_percentile'], key='sort_var') |
|
with own_var: |
|
own_type = st.selectbox("Trimming variable", ['Own', 'Geomean', 'Weighted Own'], key='trim_var') |
|
|
|
trim_slack_var = st.number_input("Trim slack (percentile addition to trimming variable ceiling)", value=0.0, min_value=0.0, max_value=1.0, step=0.1, key='trim_slack') |
|
|
|
st.write("Sorting threshold range:") |
|
min_sort, max_sort = st.columns(2) |
|
with min_sort: |
|
performance_threshold_low = st.number_input("Min", value=0.0, min_value=0.0, step=1.0, key='min_sort') |
|
with max_sort: |
|
performance_threshold_high = st.number_input("Max", value=st.session_state['trimming_dict_maxes'][performance_type], min_value=0.0, step=1.0, key='max_sort') |
|
|
|
st.write("Trimming threshold range:") |
|
min_trim, max_trim = st.columns(2) |
|
with min_trim: |
|
own_threshold_low = st.number_input("Min", value=0.0, min_value=0.0, step=1.0, key='min_trim') |
|
with max_trim: |
|
own_threshold_high = st.number_input("Max", value=st.session_state['trimming_dict_maxes'][own_type], min_value=0.0, step=1.0, key='max_trim') |
|
|
|
submitted = st.form_submit_button("Trim") |
|
if submitted: |
|
st.write('initiated') |
|
parsed_frame = st.session_state['working_frame'].copy() |
|
|
|
st.session_state['working_frame'] = trim_portfolio(parsed_frame, trim_slack_var, performance_type, own_type, performance_threshold_high, performance_threshold_low, own_threshold_high, own_threshold_low) |
|
st.session_state['working_frame'] = st.session_state['working_frame'].sort_values(by='median', ascending=False) |
|
st.session_state['export_merge'] = st.session_state['working_frame'].copy() |
|
with st.expander('Presets'): |
|
st.info("Still heavily in testing here, I'll announce when they are ready for use.") |
|
with st.form(key='Small Field Preset'): |
|
preset_choice = st.selectbox("Preset", options=['Small Field (Heavy Own)', 'Large Field (Finish Percentile / Edge)', 'Distributed (Spread Out Risk)'], index=0) |
|
lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1) |
|
submitted = st.form_submit_button("Submit") |
|
if submitted: |
|
if preset_choice == 'Small Field (Heavy Own)': |
|
parsed_frame = small_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols) |
|
elif preset_choice == 'Large Field (Finish Percentile / Edge)': |
|
parsed_frame = large_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols) |
|
elif preset_choice == 'Distributed (Spread Out Risk)': |
|
parsed_frame = distribute_preset(st.session_state['working_frame'], lineup_target, excluded_cols) |
|
st.session_state['working_frame'] = parsed_frame.reset_index(drop=True) |
|
st.session_state['export_merge'] = st.session_state['working_frame'].copy() |
|
|
|
with col2: |
|
if 'export_base' not in st.session_state: |
|
st.session_state['export_base'] = pd.DataFrame(columns=st.session_state['working_frame'].columns) |
|
|
|
display_frame_source = st.selectbox("Display:", options=['Portfolio', 'Export Base'], key='display_frame_source') |
|
if display_frame_source == 'Portfolio': |
|
display_frame = st.session_state['working_frame'] |
|
st.session_state['export_file'] = display_frame.copy() |
|
|
|
for col in st.session_state['export_file'].columns: |
|
if col not in excluded_cols: |
|
st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict']) |
|
elif display_frame_source == 'Export Base': |
|
display_frame = st.session_state['export_base'] |
|
st.session_state['export_file'] = display_frame.copy() |
|
|
|
for col in st.session_state['export_file'].columns: |
|
if col not in excluded_cols: |
|
st.session_state['export_file'][col] = st.session_state['export_file'][col].map(st.session_state['export_dict']) |
|
|
|
if 'export_file' in st.session_state: |
|
download_port, merge_port, partial_col, clear_export, blank_export_col = st.columns([1, 1, 1, 1, 8]) |
|
with download_port: |
|
st.download_button(label="Download Portfolio", data=st.session_state['export_file'].to_csv(index=False), file_name="portfolio.csv", mime="text/csv") |
|
with merge_port: |
|
if st.button("Add all to Custom Export"): |
|
st.session_state['export_base'] = pd.concat([st.session_state['export_base'], st.session_state['export_merge']]) |
|
st.session_state['export_base'] = st.session_state['export_base'].drop_duplicates() |
|
st.session_state['export_base'] = st.session_state['export_base'].reset_index(drop=True) |
|
with partial_col: |
|
if 'export_merge' in st.session_state: |
|
select_custom_index = st.number_input("Select rows to add (from top)", min_value=0, max_value=len(st.session_state['export_merge']), value=0) |
|
if st.button("Add selected to Custom Export"): |
|
st.session_state['export_base'] = pd.concat([st.session_state['export_base'], st.session_state['export_merge'].head(select_custom_index)]) |
|
st.session_state['export_base'] = st.session_state['export_base'].drop_duplicates() |
|
st.session_state['export_base'] = st.session_state['export_base'].reset_index(drop=True) |
|
with clear_export: |
|
if st.button("Clear Custom Export"): |
|
st.session_state['export_base'] = pd.DataFrame(columns=st.session_state['working_frame'].columns) |
|
if display_frame_source == 'Portfolio': |
|
display_frame = st.session_state['working_frame'] |
|
elif display_frame_source == 'Export Base': |
|
display_frame = st.session_state['export_base'] |
|
|
|
total_rows = len(display_frame) |
|
rows_per_page = 500 |
|
total_pages = (total_rows + rows_per_page - 1) // rows_per_page |
|
|
|
|
|
if 'current_page' not in st.session_state: |
|
st.session_state.current_page = 1 |
|
|
|
|
|
st.write( |
|
f"Showing rows {(st.session_state.current_page - 1) * rows_per_page + 1} " |
|
f"to {min(st.session_state.current_page * rows_per_page, total_rows)} of {total_rows}" |
|
) |
|
|
|
|
|
st.session_state.current_page = st.number_input( |
|
f"Page (1-{total_pages})", |
|
min_value=1, |
|
max_value=total_pages, |
|
value=st.session_state.current_page |
|
) |
|
|
|
|
|
start_idx = (st.session_state.current_page - 1) * rows_per_page |
|
end_idx = min(start_idx + rows_per_page, total_rows) |
|
|
|
|
|
current_page_data = display_frame.iloc[start_idx:end_idx] |
|
|
|
st.dataframe( |
|
current_page_data.style |
|
.background_gradient(axis=0) |
|
.background_gradient(cmap='RdYlGn') |
|
.background_gradient(cmap='RdYlGn_r', subset=['Finish_percentile', 'Own', 'Dupes']) |
|
.format(freq_format, precision=2), |
|
height=1000, |
|
use_container_width=True |
|
) |
|
player_stats_col, stack_stats_col = st.tabs(['Player Stats', 'Stack Stats']) |
|
with player_stats_col: |
|
|
|
player_stats = [] |
|
player_columns = [col for col in display_frame.columns if col not in excluded_cols] |
|
|
|
if type_var == 'Showdown': |
|
for player in player_names: |
|
|
|
cpt_mask = display_frame[player_columns[0]] == player |
|
|
|
if cpt_mask.any(): |
|
player_stats.append({ |
|
'Player': f"{player} (CPT)", |
|
'Lineup Count': cpt_mask.sum(), |
|
'Exposure': cpt_mask.sum() / len(display_frame), |
|
'Avg Median': display_frame[cpt_mask]['median'].mean(), |
|
'Avg Own': display_frame[cpt_mask]['Own'].mean(), |
|
'Avg Dupes': display_frame[cpt_mask]['Dupes'].mean(), |
|
'Avg Finish %': display_frame[cpt_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': display_frame[cpt_mask]['Lineup Edge'].mean(), |
|
}) |
|
|
|
|
|
flex_mask = display_frame[player_columns[1:]].apply( |
|
lambda row: player in list(row), axis=1 |
|
) |
|
|
|
if flex_mask.any(): |
|
player_stats.append({ |
|
'Player': f"{player} (FLEX)", |
|
'Lineup Count': flex_mask.sum(), |
|
'Exposure': flex_mask.sum() / len(display_frame), |
|
'Avg Median': display_frame[flex_mask]['median'].mean(), |
|
'Avg Own': display_frame[flex_mask]['Own'].mean(), |
|
'Avg Dupes': display_frame[flex_mask]['Dupes'].mean(), |
|
'Avg Finish %': display_frame[flex_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': display_frame[flex_mask]['Lineup Edge'].mean(), |
|
}) |
|
else: |
|
if sport_var == 'CS2': |
|
|
|
for player in player_names: |
|
|
|
cpt_mask = display_frame[player_columns[0]] == player |
|
|
|
if cpt_mask.any(): |
|
player_stats.append({ |
|
'Player': f"{player} (CPT)", |
|
'Lineup Count': cpt_mask.sum(), |
|
'Exposure': cpt_mask.sum() / len(display_frame), |
|
'Avg Median': display_frame[cpt_mask]['median'].mean(), |
|
'Avg Own': display_frame[cpt_mask]['Own'].mean(), |
|
'Avg Dupes': display_frame[cpt_mask]['Dupes'].mean(), |
|
'Avg Finish %': display_frame[cpt_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': display_frame[cpt_mask]['Lineup Edge'].mean(), |
|
}) |
|
|
|
|
|
flex_mask = display_frame[player_columns[1:]].apply( |
|
lambda row: player in list(row), axis=1 |
|
) |
|
|
|
if flex_mask.any(): |
|
player_stats.append({ |
|
'Player': f"{player} (FLEX)", |
|
'Lineup Count': flex_mask.sum(), |
|
'Exposure': flex_mask.sum() / len(display_frame), |
|
'Avg Median': display_frame[flex_mask]['median'].mean(), |
|
'Avg Own': display_frame[flex_mask]['Own'].mean(), |
|
'Avg Dupes': display_frame[flex_mask]['Dupes'].mean(), |
|
'Avg Finish %': display_frame[flex_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': display_frame[flex_mask]['Lineup Edge'].mean(), |
|
}) |
|
elif sport_var != 'CS2': |
|
|
|
for player in player_names: |
|
player_mask = display_frame[player_columns].apply( |
|
lambda row: player in list(row), axis=1 |
|
) |
|
|
|
if player_mask.any(): |
|
player_stats.append({ |
|
'Player': player, |
|
'Lineup Count': player_mask.sum(), |
|
'Exposure': player_mask.sum() / len(display_frame), |
|
'Avg Median': display_frame[player_mask]['median'].mean(), |
|
'Avg Own': display_frame[player_mask]['Own'].mean(), |
|
'Avg Dupes': display_frame[player_mask]['Dupes'].mean(), |
|
'Avg Finish %': display_frame[player_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': display_frame[player_mask]['Lineup Edge'].mean(), |
|
}) |
|
|
|
player_summary = pd.DataFrame(player_stats) |
|
player_summary = player_summary.sort_values('Lineup Count', ascending=False) |
|
|
|
st.subheader("Player Summary") |
|
st.dataframe( |
|
player_summary.style |
|
.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Avg Finish %', 'Avg Own', 'Avg Dupes']) |
|
.format({ |
|
'Avg Median': '{:.2f}', |
|
'Avg Own': '{:.2f}', |
|
'Avg Dupes': '{:.2f}', |
|
'Avg Finish %': '{:.2%}', |
|
'Avg Lineup Edge': '{:.2%}', |
|
'Exposure': '{:.2%}' |
|
}), |
|
height=400, |
|
use_container_width=True |
|
) |
|
|
|
with stack_stats_col: |
|
if 'Stack' in display_frame.columns: |
|
stack_stats = [] |
|
stack_columns = [col for col in display_frame.columns if col.startswith('Stack')] |
|
for stack in stack_dict.values(): |
|
stack_mask = display_frame['Stack'] == stack |
|
if stack_mask.any(): |
|
stack_stats.append({ |
|
'Stack': stack, |
|
'Lineup Count': stack_mask.sum(), |
|
'Exposure': stack_mask.sum() / len(display_frame), |
|
'Avg Median': display_frame[stack_mask]['median'].mean(), |
|
'Avg Own': display_frame[stack_mask]['Own'].mean(), |
|
'Avg Dupes': display_frame[stack_mask]['Dupes'].mean(), |
|
'Avg Finish %': display_frame[stack_mask]['Finish_percentile'].mean(), |
|
'Avg Lineup Edge': display_frame[stack_mask]['Lineup Edge'].mean(), |
|
}) |
|
stack_summary = pd.DataFrame(stack_stats) |
|
stack_summary = stack_summary.sort_values('Lineup Count', ascending=False).drop_duplicates() |
|
st.subheader("Stack Summary") |
|
st.dataframe( |
|
stack_summary.style |
|
.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Avg Finish %', 'Avg Own', 'Avg Dupes']) |
|
.format({ |
|
'Avg Median': '{:.2f}', |
|
'Avg Own': '{:.2f}', |
|
'Avg Dupes': '{:.2f}', |
|
'Avg Finish %': '{:.2%}', |
|
'Avg Lineup Edge': '{:.2%}', |
|
'Exposure': '{:.2%}' |
|
}), |
|
height=400, |
|
use_container_width=True |
|
) |
|
else: |
|
stack_summary = pd.DataFrame(columns=['Stack', 'Lineup Count', 'Avg Median', 'Avg Own', 'Avg Dupes', 'Avg Finish %', 'Avg Lineup Edge']) |