James McCool commited on
Commit
5a5c5db
·
1 Parent(s): d0e6684

Add distribute_preset function and update app.py for preset selection

Browse files

- Introduced a new `distribute_preset` function to manage portfolio distribution based on weighted ownership and finish percentiles.
- Updated `app.py` to include the new preset option 'Distributed (Spread Risk)' in the selection dropdown, replacing the previous 'Volatile' option.
- Adjusted logic to call the `distribute_preset` function when the new preset is selected, enhancing lineup generation capabilities.

Files changed (2) hide show
  1. app.py +4 -5
  2. global_func/distribute_preset.py +27 -0
app.py CHANGED
@@ -22,6 +22,7 @@ from global_func.trim_portfolio import trim_portfolio
22
  from global_func.get_portfolio_names import get_portfolio_names
23
  from global_func.small_field_preset import small_field_preset
24
  from global_func.large_field_preset import large_field_preset
 
25
 
26
  freq_format = {'Finish_percentile': '{:.2%}', 'Lineup Edge': '{:.2%}', 'Win%': '{:.2%}'}
27
  stacking_sports = ['MLB', 'NHL', 'NFL']
@@ -1107,7 +1108,7 @@ with tab2:
1107
  with st.expander('Presets'):
1108
  st.info("Still heavily in testing here, I'll announce when they are ready for use.")
1109
  with st.form(key='Small Field Preset'):
1110
- preset_choice = st.selectbox("Preset", options=['Small Field (Heavy Own)', 'Large Field (Finish Percentile / Edge)', 'Volatile', 'Distributed'], index=0)
1111
  lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1)
1112
  submitted = st.form_submit_button("Submit")
1113
  if submitted:
@@ -1115,10 +1116,8 @@ with tab2:
1115
  parsed_frame = small_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols)
1116
  elif preset_choice == 'Large Field (Finish Percentile / Edge)':
1117
  parsed_frame = large_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols)
1118
- # elif preset_choice == 'Volatile':
1119
- # parsed_frame = volatile_preset(st.session_state['working_frame'], lineup_target)
1120
- # elif preset_choice == 'Distributed':
1121
- # parsed_frame = distributed_preset(st.session_state['working_frame'], lineup_target)
1122
  st.session_state['working_frame'] = parsed_frame.reset_index(drop=True)
1123
  st.session_state['export_merge'] = st.session_state['working_frame'].copy()
1124
 
 
22
  from global_func.get_portfolio_names import get_portfolio_names
23
  from global_func.small_field_preset import small_field_preset
24
  from global_func.large_field_preset import large_field_preset
25
+ from global_func.distribute_preset import distribute_preset
26
 
27
  freq_format = {'Finish_percentile': '{:.2%}', 'Lineup Edge': '{:.2%}', 'Win%': '{:.2%}'}
28
  stacking_sports = ['MLB', 'NHL', 'NFL']
 
1108
  with st.expander('Presets'):
1109
  st.info("Still heavily in testing here, I'll announce when they are ready for use.")
1110
  with st.form(key='Small Field Preset'):
1111
+ preset_choice = st.selectbox("Preset", options=['Small Field (Heavy Own)', 'Large Field (Finish Percentile / Edge)', 'Distributed (Spread Risk)'], index=0)
1112
  lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1)
1113
  submitted = st.form_submit_button("Submit")
1114
  if submitted:
 
1116
  parsed_frame = small_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols)
1117
  elif preset_choice == 'Large Field (Finish Percentile / Edge)':
1118
  parsed_frame = large_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols)
1119
+ elif preset_choice == 'Distributed (Spread Risk)':
1120
+ parsed_frame = distribute_preset(st.session_state['working_frame'], lineup_target, excluded_cols)
 
 
1121
  st.session_state['working_frame'] = parsed_frame.reset_index(drop=True)
1122
  st.session_state['export_merge'] = st.session_state['working_frame'].copy()
1123
 
global_func/distribute_preset.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ def distribute_preset(portfolio: pd.DataFrame, lineup_target: int, exclude_cols: list):
4
+
5
+ for slack_var in range(1, 20):
6
+ concat_portfolio = pd.DataFrame(columns=portfolio.columns)
7
+
8
+ for finishing_range in range(1, 20):
9
+ rows_to_drop = []
10
+ working_portfolio = portfolio.copy()
11
+ working_portfolio = working_portfolio[(working_portfolio['Finish_percentile'] <= (finishing_range / 100)) & (working_portfolio['Finish_percentile'] >= ((finishing_range - 1) / 100))].sort_values(by='Median', ascending = True)
12
+ working_portfolio = working_portfolio.reset_index(drop=True)
13
+ curr_own_type_max = working_portfolio.loc[0, 'Weighted Own'] + (slack_var / 20 * working_portfolio.loc[0, 'Weighted Own'])
14
+
15
+ for i in range(1, len(working_portfolio)):
16
+ if working_portfolio.loc[i, 'Weighted'] > curr_own_type_max:
17
+ rows_to_drop.append(i)
18
+ else:
19
+ curr_own_type_max = working_portfolio.loc[i, 'Weighted'] + (slack_var / 20 * working_portfolio.loc[i, 'Weighted'])
20
+
21
+ working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
22
+ concat_portfolio = pd.concat([concat_portfolio, working_portfolio])
23
+
24
+ if len(concat_portfolio) >= lineup_target:
25
+ return concat_portfolio.sort_values(by='Finish_percentile', ascending=True).head(lineup_target)
26
+
27
+ return concat_portfolio.sort_values(by='Finish_percentile', ascending=True)