James McCool commited on
Commit
fca5d51
·
1 Parent(s): 556da92

Enhance exposure_spread function by refining replacement logic for players based on exposure targets. This update improves the handling of player replacements, ensuring that changes are only applied when necessary and maintaining clarity in the code structure.

Browse files
Files changed (1) hide show
  1. global_func/exposure_spread.py +53 -22
global_func/exposure_spread.py CHANGED
@@ -228,10 +228,8 @@ def exposure_spread(working_frame, exposure_player, exposure_target, comp_salary
228
  # key concept here is if they have a lineups to remove above 0 it means that we are trying to replace them with comparable players
229
  # if the lineups to remove is below zero it means we want to find comparable players and replace them with the exposure player
230
  if lineups_to_remove > 0:
231
- print(f"Lineups to remove: {lineups_to_remove}")
232
- print(f"Current Counter: {change_counter}")
233
- if change_counter < math.ceil(lineups_to_remove):
234
- for row in random_row_indices_insert:
235
  if specific_replacements != []:
236
  comparable_players = projections_df[(projections_df['player_names'].isin(specific_replacements)) &
237
  (projections_df['salary'] <= comp_salary_high + (salary_max - working_frame['salary'][row]))
@@ -267,8 +265,7 @@ def exposure_spread(working_frame, exposure_player, exposure_target, comp_salary
267
  except:
268
  comparable_player_list = []
269
 
270
- if comparable_player_list != []:
271
- insert_player = random.choice(comparable_player_list)
272
  # Find which column contains the exposure_player
273
  if specific_columns != []:
274
  row_data = working_frame.iloc[row][specific_columns]
@@ -277,23 +274,57 @@ def exposure_spread(working_frame, exposure_player, exposure_target, comp_salary
277
  row_data = working_frame.iloc[row]
278
  working_columns = working_frame.columns
279
 
280
- for col in working_columns:
281
- if row_data[col] == exposure_player:
282
- # Get the replacement player's positions
283
- replacement_player_positions = projections_df[projections_df['player_names'] == insert_player]['position'].iloc[0].split('/')
284
-
285
- # Check if the replacement player is eligible for this column
286
- if type_var == 'Classic':
287
- if check_position_eligibility(sport_var, col, replacement_player_positions):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  working_frame.at[row, col] = insert_player
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  break
290
- else:
291
- working_frame.at[row, col] = insert_player
292
- break
293
- change_counter += 1
294
  else:
295
- if change_counter < math.ceil(lineups_to_add):
296
- for row in random_row_indices_replace:
297
  if specific_replacements != []:
298
  comparable_players = projections_df[(projections_df['player_names'].isin(specific_replacements))
299
  ]
@@ -326,7 +357,7 @@ def exposure_spread(working_frame, exposure_player, exposure_target, comp_salary
326
 
327
  # Create a list of comparable players
328
  comparable_player_list = comparable_players['player_names'].tolist()
329
- if comparable_player_list != []:
330
  # Find which column contains the exposure_player
331
  if specific_columns != []:
332
  row_data = working_frame.iloc[row][specific_columns]
@@ -353,6 +384,6 @@ def exposure_spread(working_frame, exposure_player, exposure_target, comp_salary
353
  change_counter += 1
354
  break
355
  else:
356
- break
357
  return working_frame
358
 
 
228
  # key concept here is if they have a lineups to remove above 0 it means that we are trying to replace them with comparable players
229
  # if the lineups to remove is below zero it means we want to find comparable players and replace them with the exposure player
230
  if lineups_to_remove > 0:
231
+ for row in random_row_indices_insert:
232
+ if change_counter < math.ceil(lineups_to_remove):
 
 
233
  if specific_replacements != []:
234
  comparable_players = projections_df[(projections_df['player_names'].isin(specific_replacements)) &
235
  (projections_df['salary'] <= comp_salary_high + (salary_max - working_frame['salary'][row]))
 
265
  except:
266
  comparable_player_list = []
267
 
268
+ if comparable_player_list:
 
269
  # Find which column contains the exposure_player
270
  if specific_columns != []:
271
  row_data = working_frame.iloc[row][specific_columns]
 
274
  row_data = working_frame.iloc[row]
275
  working_columns = working_frame.columns
276
 
277
+ # Track if we successfully made a replacement
278
+ replacement_made = False
279
+
280
+ # For exposure_target == 0, replace ALL occurrences of exposure_player in this row
281
+ if exposure_target == 0:
282
+ for col in working_columns:
283
+ if row_data[col] == exposure_player:
284
+ # Try to find a suitable replacement for this specific column
285
+ suitable_replacements = []
286
+ for candidate in comparable_player_list:
287
+ # Get the replacement player's positions
288
+ replacement_player_positions = projections_df[projections_df['player_names'] == candidate]['position'].iloc[0].split('/')
289
+
290
+ # Check if the replacement player is eligible for this column
291
+ if type_var == 'Classic':
292
+ if check_position_eligibility(sport_var, col, replacement_player_positions):
293
+ suitable_replacements.append(candidate)
294
+ else:
295
+ suitable_replacements.append(candidate)
296
+
297
+ if suitable_replacements:
298
+ insert_player = random.choice(suitable_replacements)
299
  working_frame.at[row, col] = insert_player
300
+ replacement_made = True
301
+ # Remove this player from the list to avoid duplicates in the same row
302
+ comparable_player_list = [p for p in comparable_player_list if p != insert_player]
303
+ else:
304
+ # Original logic for non-zero exposure targets
305
+ for col in working_columns:
306
+ if row_data[col] == exposure_player:
307
+ insert_player = random.choice(comparable_player_list)
308
+ # Get the replacement player's positions
309
+ replacement_player_positions = projections_df[projections_df['player_names'] == insert_player]['position'].iloc[0].split('/')
310
+
311
+ # Check if the replacement player is eligible for this column
312
+ if type_var == 'Classic':
313
+ if check_position_eligibility(sport_var, col, replacement_player_positions):
314
+ working_frame.at[row, col] = insert_player
315
+ replacement_made = True
316
+ break
317
+ else:
318
+ working_frame.at[row, col] = insert_player
319
+ replacement_made = True
320
  break
321
+
322
+ # Only increment counter if we actually made a replacement
323
+ if replacement_made:
324
+ change_counter += 1
325
  else:
326
+ for row in random_row_indices_replace:
327
+ if change_counter < math.ceil(lineups_to_add):
328
  if specific_replacements != []:
329
  comparable_players = projections_df[(projections_df['player_names'].isin(specific_replacements))
330
  ]
 
357
 
358
  # Create a list of comparable players
359
  comparable_player_list = comparable_players['player_names'].tolist()
360
+ if comparable_player_list:
361
  # Find which column contains the exposure_player
362
  if specific_columns != []:
363
  row_data = working_frame.iloc[row][specific_columns]
 
384
  change_counter += 1
385
  break
386
  else:
387
+ continue
388
  return working_frame
389