James McCool commited on
Commit
05de3b7
·
1 Parent(s): f7eb000

Enhance VORP calculation in Streamlit app by adding position lock logic and refining output structure, improving player ranking accuracy and clarity in displayed results.

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +16 -8
src/streamlit_app.py CHANGED
@@ -367,7 +367,7 @@ def assign_vorp(frame: pd.DataFrame, halfPpr_rv: dict, custom_rv: dict, league_s
367
  pos_frame = pos_frame.sort_values(by='Rank', ascending=True).reset_index(drop=True)
368
  pos_frame['custom_rank_raw'] = pos_frame[rv_type].rank(method='first', ascending=False)
369
  pos_frame['custom_calc'] = ((pos_frame['custom_rank_raw'] + pos_frame['Rank']) / 2).astype(int)
370
- pos_frame['custom_proj_rank'] = pos_frame['custom_calc'].rank(method='first', ascending=True)
371
 
372
  pos_frame['max_halfPpr'] = pos_frame['halfPpr'].max()
373
  pos_frame['halfPpr_range'] = (pos_frame['max_halfPpr']) / len(pos_frame)
@@ -376,13 +376,16 @@ def assign_vorp(frame: pd.DataFrame, halfPpr_rv: dict, custom_rv: dict, league_s
376
  pos_frame['max_custom'] = pos_frame[rv_type].max()
377
  pos_frame['custom_range'] = (pos_frame['max_custom']) / len(pos_frame)
378
  pos_frame['adj_custom'] = pos_frame['custom_range'] * (len(pos_frame) - (pos_frame['custom_proj_rank'] - 1))
379
-
380
- print(pos_frame[['Name', 'Rank', 'custom_proj_rank', 'halfPpr', rv_type, 'adj_halfPpr', 'adj_custom']].head(20))
 
381
 
382
  pos_frame['halfPpr_rv'] = halfPpr_rv[positions]
383
  pos_frame['custom_rv'] = custom_rv[positions]
384
  pos_frame['halfPpr_VORP'] = pos_frame['adj_halfPpr'] - halfPpr_rv[positions]
385
  pos_frame['custom_VORP'] = pos_frame['adj_custom'] - custom_rv[positions]
 
 
386
 
387
  vorp_frame = pd.concat([vorp_frame, pos_frame]).reset_index(drop=True)
388
 
@@ -391,11 +394,13 @@ def assign_vorp(frame: pd.DataFrame, halfPpr_rv: dict, custom_rv: dict, league_s
391
  vorp_frame['vorp_diff'] = np.where(vorp_frame['halfPpr_VORP'] == vorp_frame['custom_VORP'], 0, vorp_frame['halfPpr_vorp_rank'] - vorp_frame['custom_vorp_rank'])
392
  for positions in ['QB', 'RB', 'WR', 'TE']:
393
  vorp_frame.loc[vorp_frame['Pos'] == positions, 'Rank_Adjust'] = (vorp_frame['Rank'] - (vorp_frame['vorp_diff'] * pos_vorp_limiters[positions])).astype(float)
394
- vorp_frame['custom_rank'] = vorp_frame['Rank_Adjust'].rank(method='first', ascending=True)
395
-
396
- print(vorp_frame.sort_values(by='custom_vorp_rank', ascending=True).head(50))
 
 
397
 
398
- return vorp_frame.sort_values(by='custom_rank', ascending=True)
399
 
400
  def main():
401
  st.title("Fantasy Football VORP Calculator")
@@ -419,7 +424,10 @@ def main():
419
  custom_rv = create_custom_rv(position_df, custom_pos_reqs, user_league_settings)
420
 
421
  # Calculate VORP and rankings
422
- final_df = assign_vorp(position_df, halfPpr_rv, custom_rv, user_league_settings, user_pos_vorp_limiters)
 
 
 
423
 
424
  # Display results
425
  st.header("Player Rankings")
 
367
  pos_frame = pos_frame.sort_values(by='Rank', ascending=True).reset_index(drop=True)
368
  pos_frame['custom_rank_raw'] = pos_frame[rv_type].rank(method='first', ascending=False)
369
  pos_frame['custom_calc'] = ((pos_frame['custom_rank_raw'] + pos_frame['Rank']) / 2).astype(int)
370
+ pos_frame['custom_proj_rank'] = pos_frame['custom_calc'].rank(method='first', ascending=True).astype(int)
371
 
372
  pos_frame['max_halfPpr'] = pos_frame['halfPpr'].max()
373
  pos_frame['halfPpr_range'] = (pos_frame['max_halfPpr']) / len(pos_frame)
 
376
  pos_frame['max_custom'] = pos_frame[rv_type].max()
377
  pos_frame['custom_range'] = (pos_frame['max_custom']) / len(pos_frame)
378
  pos_frame['adj_custom'] = pos_frame['custom_range'] * (len(pos_frame) - (pos_frame['custom_proj_rank'] - 1))
379
+ pos_frame['pos_lock'] = (pos_frame['Pos'].astype(str) + pos_frame['custom_proj_rank'].astype(str))
380
+
381
+ print(pos_frame[['Name', 'Rank', 'custom_proj_rank', 'halfPpr', rv_type, 'adj_halfPpr', 'adj_custom', 'pos_lock']].head(20))
382
 
383
  pos_frame['halfPpr_rv'] = halfPpr_rv[positions]
384
  pos_frame['custom_rv'] = custom_rv[positions]
385
  pos_frame['halfPpr_VORP'] = pos_frame['adj_halfPpr'] - halfPpr_rv[positions]
386
  pos_frame['custom_VORP'] = pos_frame['adj_custom'] - custom_rv[positions]
387
+
388
+ pos_frame = pos_frame.drop(columns=['custom_rank_raw', 'custom_calc', 'custom_proj_rank', 'max_halfPpr', 'halfPpr_range', 'max_custom', 'custom_range'])
389
 
390
  vorp_frame = pd.concat([vorp_frame, pos_frame]).reset_index(drop=True)
391
 
 
394
  vorp_frame['vorp_diff'] = np.where(vorp_frame['halfPpr_VORP'] == vorp_frame['custom_VORP'], 0, vorp_frame['halfPpr_vorp_rank'] - vorp_frame['custom_vorp_rank'])
395
  for positions in ['QB', 'RB', 'WR', 'TE']:
396
  vorp_frame.loc[vorp_frame['Pos'] == positions, 'Rank_Adjust'] = (vorp_frame['Rank'] - (vorp_frame['vorp_diff'] * pos_vorp_limiters[positions])).astype(float)
397
+ pos_lock_dict = dict(zip(vorp_frame['pos_lock'], vorp_frame['Name']))
398
+ name_lock_dict = dict(zip(vorp_frame['Name'], vorp_frame['pos_lock']))
399
+ vorp_frame['custom_rank'] = vorp_frame['Rank_Adjust'].rank(method='first', ascending=True).astype(int)
400
+ vorp_frame['pos_rank'] = vorp_frame.groupby('Pos')['custom_rank'].rank(method='first', ascending=True).astype(int)
401
+ vorp_frame['pos_designation'] = vorp_frame['Pos'] + vorp_frame['pos_rank'].astype(str)
402
 
403
+ return vorp_frame.sort_values(by='custom_rank', ascending=True), pos_lock_dict, name_lock_dict
404
 
405
  def main():
406
  st.title("Fantasy Football VORP Calculator")
 
424
  custom_rv = create_custom_rv(position_df, custom_pos_reqs, user_league_settings)
425
 
426
  # Calculate VORP and rankings
427
+ final_df, pos_lock_dict, name_lock_dict = assign_vorp(position_df, halfPpr_rv, custom_rv, user_league_settings, user_pos_vorp_limiters)
428
+ final_df = final_df.drop(columns=['SR_ID'], axis=1)
429
+ final_df['pos_lock_lu'] = final_df['Name'].map(name_lock_dict)
430
+ final_df['pos_lock_name'] = final_df['pos_designation'].map(pos_lock_dict)
431
 
432
  # Display results
433
  st.header("Player Rankings")