James McCool
commited on
Commit
·
c744629
1
Parent(s):
c9fd1e3
Add stack comparison feature in app.py and create_stack_comparison.py
Browse files- Introduced a new function, create_stack_comparison, to facilitate the comparison of stack exposures based on user selections.
- Updated app.py to include a form for selecting stacks to compare, enhancing user interaction and data analysis capabilities.
- Improved player exposure comparison by adding position filtering logic and ensuring accurate data representation in the displayed DataFrame.
- app.py +40 -14
- global_func/create_player_comparison.py +1 -1
- global_func/create_stack_comparison.py +22 -0
app.py
CHANGED
|
@@ -64,6 +64,7 @@ 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):
|
|
@@ -438,6 +439,16 @@ with tab2:
|
|
| 438 |
comp_player_select = comp_player_select
|
| 439 |
if comp_player_var == 'Yes':
|
| 440 |
player_exp_comp = create_player_comparison(st.session_state['display_contest_info'], st.session_state['player_columns'], comp_player_select)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
st.dataframe(player_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=player_exp_comp.select_dtypes(include=['number']).columns), hide_index=True)
|
| 442 |
else:
|
| 443 |
if st.session_state['entry_parse_var'] == 'All':
|
|
@@ -476,21 +487,36 @@ with tab2:
|
|
| 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 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
else:
|
| 488 |
-
st.session_state['
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
with tab3:
|
| 495 |
|
| 496 |
if st.session_state['entry_parse_var'] == 'All':
|
|
|
|
| 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 |
+
from global_func.create_stack_comparison import create_stack_comparison
|
| 68 |
|
| 69 |
def is_valid_input(file):
|
| 70 |
if isinstance(file, pd.DataFrame):
|
|
|
|
| 439 |
comp_player_select = comp_player_select
|
| 440 |
if comp_player_var == 'Yes':
|
| 441 |
player_exp_comp = create_player_comparison(st.session_state['display_contest_info'], st.session_state['player_columns'], comp_player_select)
|
| 442 |
+
hold_frame = player_exp_comp.copy()
|
| 443 |
+
if sport_select == 'GOLF':
|
| 444 |
+
hold_frame['Pos'] = 'G'
|
| 445 |
+
else:
|
| 446 |
+
hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map'])
|
| 447 |
+
player_exp_comp.insert(1, 'Pos', hold_frame['Pos'])
|
| 448 |
+
player_exp_comp = player_exp_comp.dropna(subset=['Pos'])
|
| 449 |
+
if pos_select:
|
| 450 |
+
position_mask = player_exp_comp['Pos'].apply(lambda x: any(pos in x for pos in pos_select))
|
| 451 |
+
player_exp_comp = player_exp_comp[position_mask]
|
| 452 |
st.dataframe(player_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=player_exp_comp.select_dtypes(include=['number']).columns), hide_index=True)
|
| 453 |
else:
|
| 454 |
if st.session_state['entry_parse_var'] == 'All':
|
|
|
|
| 487 |
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns),
|
| 488 |
hide_index=True)
|
| 489 |
with tab2:
|
| 490 |
+
with st.form(key='stack_exp_comp_form'):
|
| 491 |
+
col1, col2 = st.columns(2)
|
| 492 |
+
with col1:
|
| 493 |
+
comp_stack_var = st.selectbox("Would you like to compare with anyone?", ['No', 'Yes'], key='comp_stack_var')
|
| 494 |
+
with col2:
|
| 495 |
+
comp_stack_select = st.multiselect("Select stacks to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_stack_select')
|
| 496 |
+
submitted = st.form_submit_button("Submit")
|
| 497 |
+
if submitted:
|
| 498 |
+
if comp_stack_var == 'No':
|
| 499 |
+
comp_stack_select = None
|
| 500 |
+
else:
|
| 501 |
+
comp_stack_select = comp_stack_select
|
| 502 |
+
if comp_stack_var == 'Yes':
|
| 503 |
+
stack_exp_comp = create_stack_comparison(st.session_state['display_contest_info'], comp_stack_select)
|
| 504 |
+
st.dataframe(stack_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=stack_exp_comp.select_dtypes(include=['number']).columns), hide_index=True)
|
| 505 |
else:
|
| 506 |
+
if st.session_state['entry_parse_var'] == 'All':
|
| 507 |
+
st.session_state['stack_frame'] = create_stack_exposures(st.session_state['display_contest_info'])
|
| 508 |
+
st.dataframe(st.session_state['stack_frame'].
|
| 509 |
+
sort_values(by='Exposure Overall', ascending=False).
|
| 510 |
+
style.background_gradient(cmap='RdYlGn').
|
| 511 |
+
format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
| 512 |
+
hide_index=True)
|
| 513 |
+
else:
|
| 514 |
+
st.session_state['stack_frame'] = create_stack_exposures(st.session_state['display_contest_info'], st.session_state['entry_names'])
|
| 515 |
+
st.dataframe(st.session_state['stack_frame'].
|
| 516 |
+
sort_values(by='Exposure Overall', ascending=False).
|
| 517 |
+
style.background_gradient(cmap='RdYlGn').
|
| 518 |
+
format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
| 519 |
+
hide_index=True)
|
| 520 |
with tab3:
|
| 521 |
|
| 522 |
if st.session_state['entry_parse_var'] == 'All':
|
global_func/create_player_comparison.py
CHANGED
|
@@ -19,4 +19,4 @@ def create_player_comparison(df: pd.DataFrame, player_columns: list, entrants: l
|
|
| 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
|
|
|
|
| 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.sort_values(by='Exposure Overall', ascending=False)
|
global_func/create_stack_comparison.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
def create_stack_comparison(df: pd.DataFrame, entrants: list):
|
| 4 |
+
overall_players = pd.Series(list(df['stack'])).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]['stack'])).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(df[df['BaseName'] == each_user])
|
| 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.sort_values(by='Exposure Overall', ascending=False)
|