James McCool
commited on
Commit
·
45351e3
1
Parent(s):
e084aa6
Add player comparison feature in app.py and create_player_comparison.py
Browse files- Introduced a new function, create_player_comparison, to facilitate player comparison based on exposure data.
- Updated app.py to include a form for selecting players to compare, enhancing user interaction and data analysis capabilities.
- Adjusted position selection logic for various sports, ensuring accurate filtering of player data based on selected positions.
- app.py +65 -35
- global_func/create_player_comparison.py +22 -0
app.py
CHANGED
|
@@ -63,6 +63,7 @@ from global_func.create_stack_exposures import create_stack_exposures
|
|
| 63 |
from global_func.create_stack_size_exposures import create_stack_size_exposures
|
| 64 |
from global_func.create_general_exposures import create_general_exposures
|
| 65 |
from global_func.grab_contest_data import grab_contest_data
|
|
|
|
| 66 |
|
| 67 |
def is_valid_input(file):
|
| 68 |
if isinstance(file, pd.DataFrame):
|
|
@@ -401,50 +402,79 @@ with tab2:
|
|
| 401 |
with col1:
|
| 402 |
pos_var = st.selectbox("Which position(s) would you like to view?", ['All', 'Specific'], key='pos_var')
|
| 403 |
with col2:
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
submitted = st.form_submit_button("Submit")
|
| 406 |
if submitted:
|
| 407 |
if pos_var == 'Specific':
|
| 408 |
pos_select = pos_select
|
| 409 |
else:
|
| 410 |
pos_select = None
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
st.
|
| 426 |
-
|
| 427 |
-
style.background_gradient(cmap='RdYlGn').
|
| 428 |
-
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns),
|
| 429 |
-
hide_index=True)
|
| 430 |
else:
|
|
|
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
else:
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
with tab2:
|
| 449 |
|
| 450 |
if st.session_state['entry_parse_var'] == 'All':
|
|
|
|
| 63 |
from global_func.create_stack_size_exposures import create_stack_size_exposures
|
| 64 |
from global_func.create_general_exposures import create_general_exposures
|
| 65 |
from global_func.grab_contest_data import grab_contest_data
|
| 66 |
+
from global_func.create_player_comparison import create_player_comparison
|
| 67 |
|
| 68 |
def is_valid_input(file):
|
| 69 |
if isinstance(file, pd.DataFrame):
|
|
|
|
| 402 |
with col1:
|
| 403 |
pos_var = st.selectbox("Which position(s) would you like to view?", ['All', 'Specific'], key='pos_var')
|
| 404 |
with col2:
|
| 405 |
+
if sport_select == 'MLB':
|
| 406 |
+
pos_select = st.multiselect("Select your position(s)", ['P', 'C', '1B', '2B', '3B', 'SS', 'OF'], key='pos_select')
|
| 407 |
+
elif sport_select == 'NBA':
|
| 408 |
+
pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF', 'C'], key='pos_select')
|
| 409 |
+
elif sport_select == 'WNBA':
|
| 410 |
+
pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF'], key='pos_select')
|
| 411 |
+
elif sport_select == 'NFL':
|
| 412 |
+
pos_select = st.multiselect("Select your position(s)", ['QB', 'RB', 'WR', 'TE', 'DST'], key='pos_select')
|
| 413 |
+
elif sport_select == 'NHL':
|
| 414 |
+
pos_select = st.multiselect("Select your position(s)", ['W', 'C', 'D', 'G'], key='pos_select')
|
| 415 |
+
elif sport_select == 'MMA':
|
| 416 |
+
pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select')
|
| 417 |
+
elif sport_select == 'GOLF':
|
| 418 |
+
pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select')
|
| 419 |
submitted = st.form_submit_button("Submit")
|
| 420 |
if submitted:
|
| 421 |
if pos_var == 'Specific':
|
| 422 |
pos_select = pos_select
|
| 423 |
else:
|
| 424 |
pos_select = None
|
| 425 |
+
|
| 426 |
+
with st.form(key='player_exp_comp_form'):
|
| 427 |
+
col1, col2 = st.columns(2)
|
| 428 |
+
with col1:
|
| 429 |
+
comp_player_var = st.selectbox("Would you like to compare with anyone?", ['No', 'Yes'], key='comp_player_var')
|
| 430 |
+
with col2:
|
| 431 |
+
comp_player_select = st.multiselect("Select players to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_player_select')
|
| 432 |
+
submitted = st.form_submit_button("Submit")
|
| 433 |
+
if submitted:
|
| 434 |
+
if comp_player_var == 'No':
|
| 435 |
+
comp_player_select = None
|
| 436 |
+
else:
|
| 437 |
+
comp_player_select = comp_player_select
|
| 438 |
+
if comp_player_var == 'Yes':
|
| 439 |
+
st.session_state['player_frame'] = create_player_comparison(st.session_state['display_contest_info'], st.session_state['player_columns'], comp_player_select)
|
| 440 |
+
st.dataframe(st.session_state['player_frame'].style.background_gradient(cmap='RdYlGn', axis=1).format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns), hide_index=True)
|
|
|
|
|
|
|
|
|
|
| 441 |
else:
|
| 442 |
+
if st.session_state['entry_parse_var'] == 'All':
|
| 443 |
|
| 444 |
+
st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'])
|
| 445 |
+
hold_frame = st.session_state['player_frame'].copy()
|
| 446 |
+
if sport_select == 'GOLF':
|
| 447 |
+
hold_frame['Pos'] = 'G'
|
| 448 |
+
else:
|
| 449 |
+
hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map'])
|
| 450 |
+
st.session_state['player_frame'].insert(1, 'Pos', hold_frame['Pos'])
|
| 451 |
+
st.session_state['player_frame'] = st.session_state['player_frame'].dropna(subset=['Pos'])
|
| 452 |
+
if pos_select:
|
| 453 |
+
position_mask = st.session_state['player_frame']['Pos'].apply(lambda x: any(pos in x for pos in pos_select))
|
| 454 |
+
st.session_state['player_frame'] = st.session_state['player_frame'][position_mask]
|
| 455 |
+
st.dataframe(st.session_state['player_frame'].
|
| 456 |
+
sort_values(by='Exposure Overall', ascending=False).
|
| 457 |
+
style.background_gradient(cmap='RdYlGn').
|
| 458 |
+
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns),
|
| 459 |
+
hide_index=True)
|
| 460 |
else:
|
| 461 |
+
|
| 462 |
+
st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'], st.session_state['entry_names'])
|
| 463 |
+
hold_frame = st.session_state['player_frame'].copy()
|
| 464 |
+
if sport_select == 'GOLF':
|
| 465 |
+
hold_frame['Pos'] = 'G'
|
| 466 |
+
else:
|
| 467 |
+
hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map'])
|
| 468 |
+
st.session_state['player_frame'].insert(1, 'Pos', hold_frame['Pos'])
|
| 469 |
+
st.session_state['player_frame'] = st.session_state['player_frame'].dropna(subset=['Pos'])
|
| 470 |
+
if pos_select:
|
| 471 |
+
position_mask = st.session_state['player_frame']['Pos'].apply(lambda x: any(pos in x for pos in pos_select))
|
| 472 |
+
st.session_state['player_frame'] = st.session_state['player_frame'][position_mask]
|
| 473 |
+
st.dataframe(st.session_state['player_frame'].
|
| 474 |
+
sort_values(by='Exposure Overall', ascending=False).
|
| 475 |
+
style.background_gradient(cmap='RdYlGn').
|
| 476 |
+
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns),
|
| 477 |
+
hide_index=True)
|
| 478 |
with tab2:
|
| 479 |
|
| 480 |
if st.session_state['entry_parse_var'] == 'All':
|
global_func/create_player_comparison.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
def create_player_comparison(df: pd.DataFrame, player_columns: list, entrants: list):
|
| 4 |
+
overall_players = pd.Series(list(df[player_columns].values.flatten())).value_counts()
|
| 5 |
+
contest_len = len(df)
|
| 6 |
+
|
| 7 |
+
set_frame = overall_players.to_frame().reset_index().rename(columns={'index': 'Player', 'count': 'Count'})
|
| 8 |
+
set_frame['Percent'] = set_frame['Count'] / contest_len
|
| 9 |
+
set_frame = set_frame[['Player', 'Percent']]
|
| 10 |
+
set_frame = set_frame.rename(columns={'Percent': f'Exposure Overall'})
|
| 11 |
+
player_frame = set_frame
|
| 12 |
+
|
| 13 |
+
for each_user in entrants:
|
| 14 |
+
overall_players = pd.Series(list(df[df['BaseName'] == each_user][player_columns].values.flatten())).value_counts()
|
| 15 |
+
|
| 16 |
+
set_frame = overall_players.to_frame().reset_index().rename(columns={'index': 'Player', 'count': 'Count'})
|
| 17 |
+
set_frame['Percent'] = set_frame['Count'] / len(overall_players)
|
| 18 |
+
set_frame = set_frame[['Player', 'Percent']]
|
| 19 |
+
set_frame = set_frame.rename(columns={'Percent': f'Exposure {each_user}'})
|
| 20 |
+
player_frame = pd.merge(player_frame, set_frame, on='Player', how='outer')
|
| 21 |
+
|
| 22 |
+
return player_frame
|