Spaces:
Build error
Build error
James McCool commited on
Commit ·
08d5b57
1
Parent(s): c01b860
Adding some functionality for min-max filtering across stats.
Browse files
app.py
CHANGED
|
@@ -42,7 +42,16 @@ pos_parse_mapping = {
|
|
| 42 |
'Team': 'team_map'
|
| 43 |
}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
pos_parse_options = list(pos_parse_mapping.keys())
|
|
|
|
| 46 |
|
| 47 |
## Should just do a loop with some f'strings here I think
|
| 48 |
showdown_selections = ['Showdown #1', 'Showdown #2', 'Showdown #3', 'Showdown #4', 'Showdown #5', 'Showdown #6', 'Showdown #7', 'Showdown #8', 'Showdown #9', 'Showdown #10', 'Showdown #11', 'Showdown #12', 'Showdown #13', 'Showdown #14', 'Showdown #15']
|
|
@@ -1143,6 +1152,30 @@ def parse_portfolio_on_mapped(portfolio, map_dict, map_key, filter_keys_pos, fil
|
|
| 1143 |
|
| 1144 |
return portfolio[mask]
|
| 1145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1146 |
def recalc_stacks_sizes(df, player_columns, map_dict):
|
| 1147 |
team_map = map_dict['team_map'] # Use this directly (player_name -> team)
|
| 1148 |
df['Stack'] = df.apply(
|
|
@@ -2541,6 +2574,40 @@ if selected_tab == 'Manage Portfolio':
|
|
| 2541 |
parsed_frame = parse_portfolio_on_mapped(parsed_frame, st.session_state['map_dict'], pos_parse_mapping[position_filter], filter_keys_pos, filter_keys_team, position_low_threshold, position_high_threshold, position_choice)
|
| 2542 |
st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False)
|
| 2543 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2544 |
with st.expander('Trimming Options'):
|
| 2545 |
with st.form(key='trim_form'):
|
| 2546 |
st.write("Sorting and trimming variables:")
|
|
|
|
| 42 |
'Team': 'team_map'
|
| 43 |
}
|
| 44 |
|
| 45 |
+
min_max_parse_mapping = {
|
| 46 |
+
'Projection': 'proj_map',
|
| 47 |
+
'Ownership': 'own_map',
|
| 48 |
+
'Salary': 'salary_map',
|
| 49 |
+
'Grid': 'grid_map',
|
| 50 |
+
'Team': 'team_map'
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
pos_parse_options = list(pos_parse_mapping.keys())
|
| 54 |
+
min_max_parse_options = list(min_max_parse_mapping.keys())
|
| 55 |
|
| 56 |
## Should just do a loop with some f'strings here I think
|
| 57 |
showdown_selections = ['Showdown #1', 'Showdown #2', 'Showdown #3', 'Showdown #4', 'Showdown #5', 'Showdown #6', 'Showdown #7', 'Showdown #8', 'Showdown #9', 'Showdown #10', 'Showdown #11', 'Showdown #12', 'Showdown #13', 'Showdown #14', 'Showdown #15']
|
|
|
|
| 1152 |
|
| 1153 |
return portfolio[mask]
|
| 1154 |
|
| 1155 |
+
def parse_portfolio_on_mapped_min_max(portfolio, map_dict, map_key, min_max_value,
|
| 1156 |
+
total_threshold, min_max_choice, above_below,
|
| 1157 |
+
min_max_var, player_columns):
|
| 1158 |
+
|
| 1159 |
+
# Map each player column to their stat values
|
| 1160 |
+
stat_df = portfolio[player_columns].apply(lambda col: col.map(map_dict[map_key]).fillna(0))
|
| 1161 |
+
|
| 1162 |
+
# Count per row how many players are above/below the threshold
|
| 1163 |
+
if above_below == 'Above':
|
| 1164 |
+
counts_per_row = (stat_df > min_max_value).sum(axis=1)
|
| 1165 |
+
else:
|
| 1166 |
+
counts_per_row = (stat_df < min_max_value).sum(axis=1)
|
| 1167 |
+
|
| 1168 |
+
# Create mask for rows that meet the total_threshold
|
| 1169 |
+
meets_threshold = counts_per_row >= total_threshold
|
| 1170 |
+
|
| 1171 |
+
# Apply Keep/Remove logic
|
| 1172 |
+
if min_max_choice == 'Keep':
|
| 1173 |
+
mask = meets_threshold
|
| 1174 |
+
else:
|
| 1175 |
+
mask = ~meets_threshold
|
| 1176 |
+
|
| 1177 |
+
return portfolio[mask]
|
| 1178 |
+
|
| 1179 |
def recalc_stacks_sizes(df, player_columns, map_dict):
|
| 1180 |
team_map = map_dict['team_map'] # Use this directly (player_name -> team)
|
| 1181 |
df['Stack'] = df.apply(
|
|
|
|
| 2574 |
parsed_frame = parse_portfolio_on_mapped(parsed_frame, st.session_state['map_dict'], pos_parse_mapping[position_filter], filter_keys_pos, filter_keys_team, position_low_threshold, position_high_threshold, position_choice)
|
| 2575 |
st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False)
|
| 2576 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
| 2577 |
+
|
| 2578 |
+
with st.expander('Stat Min/Max Filtering'):
|
| 2579 |
+
with st.form(key='stat_min_max_filtering_form'):
|
| 2580 |
+
min_max_choice = st.selectbox("Keep or Remove:", options=['Keep', 'Remove'], index=0)
|
| 2581 |
+
min_max_var = st.selectbox("Lineups with:", options=['Greater than', 'Less than', 'Equal to'], index=0)
|
| 2582 |
+
min_max_total = st.number_input("A total of:", value=0.0, min_value=0.0, step=1.0)
|
| 2583 |
+
min_max_filter = st.selectbox("Based on:", options=min_max_parse_options)
|
| 2584 |
+
above_below = st.selectbox("Above or Below:", options=['Above', 'Below'], index=0)
|
| 2585 |
+
if min_max_filter == 'Projection':
|
| 2586 |
+
min_max_value = st.number_input("This value:", value=20.0, min_value=0.0, step=1.0)
|
| 2587 |
+
elif min_max_filter == 'Ownership':
|
| 2588 |
+
min_max_value = st.number_input("This value:", value=20.0, min_value=0.0, step=1.0)
|
| 2589 |
+
elif min_max_filter == 'Salary':
|
| 2590 |
+
min_max_value = st.number_input("This value:", value=4000.0, min_value=0.0, step=100.0)
|
| 2591 |
+
elif min_max_filter == 'Grid':
|
| 2592 |
+
min_max_value = st.number_input("This value:", value=10.0, min_value=0.0, step=1.0)
|
| 2593 |
+
submitted_col, export_col = st.columns(2)
|
| 2594 |
+
with submitted_col:
|
| 2595 |
+
reg_submitted = st.form_submit_button("Portfolio")
|
| 2596 |
+
with export_col:
|
| 2597 |
+
exp_submitted = st.form_submit_button("Export")
|
| 2598 |
+
if reg_submitted:
|
| 2599 |
+
st.session_state['settings_base'] = False
|
| 2600 |
+
parsed_frame = st.session_state['working_frame'].copy()
|
| 2601 |
+
parsed_frame = parse_portfolio_on_mapped_min_max(parsed_frame, st.session_state['map_dict'], min_max_parse_mapping[min_max_filter], min_max_value, min_max_total, min_max_choice, above_below, min_max_var, st.session_state['player_columns'])
|
| 2602 |
+
st.session_state['working_frame'] = parsed_frame.sort_values(by='median', ascending=False)
|
| 2603 |
+
st.session_state['export_merge'] = st.session_state['working_frame'].copy()
|
| 2604 |
+
elif exp_submitted:
|
| 2605 |
+
st.session_state['settings_base'] = False
|
| 2606 |
+
parsed_frame = st.session_state['export_base'].copy()
|
| 2607 |
+
parsed_frame = parse_portfolio_on_mapped_min_max(parsed_frame, st.session_state['map_dict'], min_max_parse_mapping[min_max_filter], min_max_value, min_max_total, min_max_choice, above_below, min_max_var, st.session_state['player_columns'])
|
| 2608 |
+
st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False)
|
| 2609 |
+
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
| 2610 |
+
|
| 2611 |
with st.expander('Trimming Options'):
|
| 2612 |
with st.form(key='trim_form'):
|
| 2613 |
st.write("Sorting and trimming variables:")
|