James McCool
commited on
Commit
·
9034a8a
1
Parent(s):
2371c4f
Refactor exposure_spread function to handle player replacement logic based on lineups_to_remove value. Separate logic for positive and negative adjustments to ensure accurate player exposure management and enhance lineup flexibility.
Browse files- global_func/exposure_spread.py +88 -40
global_func/exposure_spread.py
CHANGED
|
@@ -267,50 +267,98 @@ def exposure_spread(working_frame, exposure_player, exposure_target, exposure_st
|
|
| 267 |
random.shuffle(random_row_indices)
|
| 268 |
|
| 269 |
# for each row to the the number of lineups to remove, replace with random choice from comparable player list if they can be inserted
|
| 270 |
-
for row in random_row_indices:
|
| 271 |
-
if change_counter < math.ceil(lineups_to_remove):
|
| 272 |
-
comparable_players = projections_df[
|
| 273 |
-
(projections_df['salary'] >= comp_salary_low) &
|
| 274 |
-
(projections_df['salary'] <= comp_salary_high + (salary_max - working_frame['salary'][row])) &
|
| 275 |
-
(projections_df['median'] >= comp_projection_low) &
|
| 276 |
-
(projections_df['position'].apply(lambda x: has_position_overlap(x, comp_player_position)))
|
| 277 |
-
]
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
|
|
|
|
|
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
working_frame.at[row, col] = insert_player
|
| 310 |
break
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
return working_frame
|
| 316 |
|
|
|
|
| 267 |
random.shuffle(random_row_indices)
|
| 268 |
|
| 269 |
# for each row to the the number of lineups to remove, replace with random choice from comparable player list if they can be inserted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
+
# we will need to use two separate functions here, one for an exposure player who has a lineups to remove above 0 and one for below 0
|
| 272 |
+
# 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
|
| 273 |
+
# if the lineups to remove is below zero it means we want to find comparable players and replace them with the exposure player
|
| 274 |
+
if lineups_to_remove > 0:
|
| 275 |
+
for row in random_row_indices:
|
| 276 |
+
if change_counter < math.ceil(lineups_to_remove):
|
| 277 |
+
comparable_players = projections_df[
|
| 278 |
+
(projections_df['salary'] >= comp_salary_low) &
|
| 279 |
+
(projections_df['salary'] <= comp_salary_high + (salary_max - working_frame['salary'][row])) &
|
| 280 |
+
(projections_df['median'] >= comp_projection_low) &
|
| 281 |
+
(projections_df['position'].apply(lambda x: has_position_overlap(x, comp_player_position)))
|
| 282 |
+
]
|
| 283 |
|
| 284 |
+
if exposure_target == 0:
|
| 285 |
+
comparable_players = comparable_players[comparable_players['player_names'] != exposure_player]
|
| 286 |
+
|
| 287 |
+
if remove_teams is not None:
|
| 288 |
+
remove_mask = comparable_players.apply(
|
| 289 |
+
lambda row: not any(team in list(row) for team in remove_teams), axis=1
|
| 290 |
+
)
|
| 291 |
+
comparable_players = comparable_players[remove_mask]
|
| 292 |
|
| 293 |
+
# Get the current row data to check for existing players
|
| 294 |
+
current_row_data = working_frame.iloc[row]
|
| 295 |
+
|
| 296 |
+
# Filter out players that are already present in this row
|
| 297 |
+
existing_players = set(current_row_data.values)
|
| 298 |
+
comparable_players = comparable_players[~comparable_players['player_names'].isin(existing_players)]
|
| 299 |
+
|
| 300 |
+
# Create a list of comparable players
|
| 301 |
+
comparable_player_list = comparable_players['player_names'].tolist()
|
| 302 |
+
if comparable_player_list:
|
| 303 |
+
insert_player = random.choice(comparable_player_list)
|
| 304 |
+
# Find which column contains the exposure_player
|
| 305 |
+
row_data = working_frame.iloc[row]
|
| 306 |
+
for col in working_frame.columns:
|
| 307 |
+
if row_data[col] == exposure_player:
|
| 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 |
+
break
|
| 316 |
+
else:
|
| 317 |
working_frame.at[row, col] = insert_player
|
| 318 |
break
|
| 319 |
+
change_counter += 1
|
| 320 |
+
else:
|
| 321 |
+
for row in random_row_indices:
|
| 322 |
+
if change_counter < math.ceil(lineups_to_remove):
|
| 323 |
+
comparable_players = projections_df[
|
| 324 |
+
(projections_df['salary'] >= comp_salary_low) &
|
| 325 |
+
(projections_df['salary'] <= comp_salary_high + (salary_max - working_frame['salary'][row])) &
|
| 326 |
+
(projections_df['median'] >= comp_projection_low) &
|
| 327 |
+
(projections_df['position'].apply(lambda x: has_position_overlap(x, comp_player_position)))
|
| 328 |
+
]
|
| 329 |
+
|
| 330 |
+
if remove_teams is not None:
|
| 331 |
+
remove_mask = comparable_players.apply(
|
| 332 |
+
lambda row: not any(team in list(row) for team in remove_teams), axis=1
|
| 333 |
+
)
|
| 334 |
+
comparable_players = comparable_players[remove_mask]
|
| 335 |
+
|
| 336 |
+
# Get the current row data to check for existing players
|
| 337 |
+
current_row_data = working_frame.iloc[row]
|
| 338 |
+
|
| 339 |
+
# Filter out players that are already present in this row
|
| 340 |
+
existing_players = set(current_row_data.values)
|
| 341 |
+
comparable_players = comparable_players[~comparable_players['player_names'].isin(existing_players)]
|
| 342 |
+
|
| 343 |
+
# Create a list of comparable players
|
| 344 |
+
comparable_player_list = comparable_players['player_names'].tolist()
|
| 345 |
+
if comparable_player_list:
|
| 346 |
+
replace_player = random.choice(comparable_player_list)
|
| 347 |
+
# Find which column contains the exposure_player
|
| 348 |
+
row_data = working_frame.iloc[row]
|
| 349 |
+
for col in working_frame.columns:
|
| 350 |
+
if row_data[col] == replace_player:
|
| 351 |
+
# Get the replacement player's positions
|
| 352 |
+
replacement_player_positions = projections_df[projections_df['player_names'] == replace_player]['position'].iloc[0].split('/')
|
| 353 |
+
|
| 354 |
+
# Check if the replacement player is eligible for this column
|
| 355 |
+
if type_var == 'Classic':
|
| 356 |
+
if check_position_eligibility(sport_var, col, replacement_player_positions):
|
| 357 |
+
working_frame.at[row, col] = exposure_player
|
| 358 |
+
break
|
| 359 |
+
else:
|
| 360 |
+
working_frame.at[row, col] = exposure_player
|
| 361 |
+
break
|
| 362 |
+
change_counter += 1
|
| 363 |
return working_frame
|
| 364 |
|