James McCool
commited on
Commit
·
a281a4f
1
Parent(s):
ad14b7e
Add Conditionals Manager for player filtering in 'Manage Portfolio', allowing users to keep or remove lineups based on selected player conditions, enhancing lineup management capabilities.
Browse files
app.py
CHANGED
@@ -1347,61 +1347,6 @@ if selected_tab == 'Manage Portfolio':
|
|
1347 |
parsed_frame = parsed_frame[parsed_frame['Size'].isin(size_include)]
|
1348 |
st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True)
|
1349 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
1350 |
-
|
1351 |
-
# with st.expander('Conditional Manager'):
|
1352 |
-
# # a set of functions for removing lineups that contain a conditional between players and stacks
|
1353 |
-
# with st.form(key='conditional_manager_form'):
|
1354 |
-
# player_names = set()
|
1355 |
-
# for col in st.session_state['working_frame'].columns:
|
1356 |
-
# if col not in excluded_cols:
|
1357 |
-
# player_names.update(st.session_state['working_frame'][col].unique())
|
1358 |
-
# conditional_remove_players = st.multiselect("Remove lineups containing player(s):", options=sorted(list(player_names)), default=[])
|
1359 |
-
# conditional_include_players = st.multiselect("If they also contain player(s):", options=sorted(list(player_names)), default=[])
|
1360 |
-
|
1361 |
-
# submitted_col, export_col = st.columns(2)
|
1362 |
-
# st.info("Portfolio Button applies to your overall Portfolio, Export button applies to your Custom Export")
|
1363 |
-
# with submitted_col:
|
1364 |
-
# reg_submitted = st.form_submit_button("Portfolio")
|
1365 |
-
# with export_col:
|
1366 |
-
# exp_submitted = st.form_submit_button("Export")
|
1367 |
-
# if reg_submitted:
|
1368 |
-
# st.session_state['settings_base'] = False
|
1369 |
-
# parsed_frame = st.session_state['working_frame'].copy()
|
1370 |
-
|
1371 |
-
# player_columns = [col for col in parsed_frame.columns if col not in excluded_cols]
|
1372 |
-
|
1373 |
-
# # Test with a simpler approach
|
1374 |
-
# include_mask = parsed_frame[player_columns].apply(
|
1375 |
-
# lambda row: all(player in row.values for player in conditional_include_players), axis=1
|
1376 |
-
# )
|
1377 |
-
|
1378 |
-
# remove_mask = parsed_frame[player_columns].apply(
|
1379 |
-
# lambda row: not any(player in row.values for player in conditional_remove_players), axis=1
|
1380 |
-
# )
|
1381 |
-
|
1382 |
-
# parsed_frame = parsed_frame[include_mask & remove_mask]
|
1383 |
-
|
1384 |
-
# st.session_state['working_frame'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True)
|
1385 |
-
# st.session_state['export_merge'] = st.session_state['working_frame'].copy()
|
1386 |
-
# elif exp_submitted:
|
1387 |
-
# st.session_state['settings_base'] = False
|
1388 |
-
# parsed_frame = st.session_state['export_base'].copy()
|
1389 |
-
|
1390 |
-
# player_columns = [col for col in parsed_frame.columns if col not in excluded_cols]
|
1391 |
-
|
1392 |
-
# # Test with a simpler approach
|
1393 |
-
# include_mask = parsed_frame[player_columns].apply(
|
1394 |
-
# lambda row: all(player in row.values for player in conditional_include_players), axis=1
|
1395 |
-
# )
|
1396 |
-
|
1397 |
-
# remove_mask = parsed_frame[player_columns].apply(
|
1398 |
-
# lambda row: not any(player in row.values for player in conditional_remove_players), axis=1
|
1399 |
-
# )
|
1400 |
-
|
1401 |
-
# parsed_frame = parsed_frame[include_mask & remove_mask]
|
1402 |
-
|
1403 |
-
# st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True)
|
1404 |
-
# st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
1405 |
|
1406 |
with st.expander('Trimming Options'):
|
1407 |
with st.form(key='trim_form'):
|
@@ -1510,6 +1455,128 @@ if selected_tab == 'Manage Portfolio':
|
|
1510 |
parsed_frame = stratification_function(st.session_state['export_base'], lineup_target, excluded_cols, sport_var, sorting_choice, strat_sample[0], strat_sample[1])
|
1511 |
st.session_state['export_base'] = parsed_frame.reset_index(drop=True)
|
1512 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1513 |
with st.expander('Exposure Management'):
|
1514 |
with st.form(key='Exposures'):
|
1515 |
exposure_player = st.selectbox("Player", options=sorted(list(set(st.session_state['projections_df']['player_names'].unique()))), key='exposure_player')
|
|
|
1347 |
parsed_frame = parsed_frame[parsed_frame['Size'].isin(size_include)]
|
1348 |
st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True)
|
1349 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1350 |
|
1351 |
with st.expander('Trimming Options'):
|
1352 |
with st.form(key='trim_form'):
|
|
|
1455 |
parsed_frame = stratification_function(st.session_state['export_base'], lineup_target, excluded_cols, sport_var, sorting_choice, strat_sample[0], strat_sample[1])
|
1456 |
st.session_state['export_base'] = parsed_frame.reset_index(drop=True)
|
1457 |
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
1458 |
+
|
1459 |
+
with st.expander('Conditionals Manager (players)'):
|
1460 |
+
# a set of functions for removing lineups that contain a conditional between players and stacks
|
1461 |
+
with st.form(key='conditional_players_form'):
|
1462 |
+
player_names = set()
|
1463 |
+
for col in st.session_state['working_frame'].columns:
|
1464 |
+
if col not in excluded_cols:
|
1465 |
+
player_names.update(st.session_state['working_frame'][col].unique())
|
1466 |
+
keep_remove_var = st.selectbox("Conditional:", options=['Keep', 'Remove'], index=0)
|
1467 |
+
conditional_side_alpha = st.multiselect("Lineups containing:", options=sorted(list(player_names)), default=[])
|
1468 |
+
conditional_var = st.selectbox("where they also contain:", options=['Any', 'All', 'None'], index=0)
|
1469 |
+
conditional_side_beta = st.multiselect("of the following player(s):", options=sorted(list(player_names)), default=[])
|
1470 |
+
|
1471 |
+
submitted_col, export_col = st.columns(2)
|
1472 |
+
st.info("Portfolio Button applies to your overall Portfolio, Export button applies to your Custom Export")
|
1473 |
+
with submitted_col:
|
1474 |
+
reg_submitted = st.form_submit_button("Portfolio")
|
1475 |
+
with export_col:
|
1476 |
+
exp_submitted = st.form_submit_button("Export")
|
1477 |
+
if reg_submitted:
|
1478 |
+
st.session_state['settings_base'] = False
|
1479 |
+
parsed_frame = st.session_state['working_frame'].copy()
|
1480 |
+
|
1481 |
+
# Check if we have players selected for both alpha and beta sides
|
1482 |
+
if conditional_side_alpha and conditional_side_beta:
|
1483 |
+
# Create boolean mask for rows containing ALL players from alpha side
|
1484 |
+
alpha_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1485 |
+
for player in conditional_side_alpha:
|
1486 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1487 |
+
alpha_mask = alpha_mask & player_present
|
1488 |
+
|
1489 |
+
# For rows that match alpha condition, check beta condition
|
1490 |
+
if conditional_var == 'Any':
|
1491 |
+
# Check if row contains ANY of the beta players
|
1492 |
+
beta_mask = pd.Series([False] * len(parsed_frame), index=parsed_frame.index)
|
1493 |
+
for player in conditional_side_beta:
|
1494 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1495 |
+
beta_mask = beta_mask | player_present
|
1496 |
+
elif conditional_var == 'All':
|
1497 |
+
# Check if row contains ALL of the beta players
|
1498 |
+
beta_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1499 |
+
for player in conditional_side_beta:
|
1500 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1501 |
+
beta_mask = beta_mask & player_present
|
1502 |
+
elif conditional_var == 'None':
|
1503 |
+
# Check if row contains NONE of the beta players
|
1504 |
+
beta_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1505 |
+
for player in conditional_side_beta:
|
1506 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1507 |
+
beta_mask = beta_mask & (~player_present)
|
1508 |
+
|
1509 |
+
# Combine conditions: alpha_mask AND beta_mask
|
1510 |
+
final_condition = alpha_mask & beta_mask
|
1511 |
+
|
1512 |
+
# Apply keep or remove logic
|
1513 |
+
if keep_remove_var == 'Keep':
|
1514 |
+
parsed_frame = parsed_frame[final_condition]
|
1515 |
+
else: # Remove
|
1516 |
+
parsed_frame = parsed_frame[~final_condition]
|
1517 |
+
|
1518 |
+
elif conditional_side_alpha:
|
1519 |
+
# Only alpha side specified - filter based on presence of alpha players
|
1520 |
+
alpha_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1521 |
+
for player in conditional_side_alpha:
|
1522 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1523 |
+
alpha_mask = alpha_mask & player_present
|
1524 |
+
|
1525 |
+
if keep_remove_var == 'Keep':
|
1526 |
+
parsed_frame = parsed_frame[alpha_mask]
|
1527 |
+
else: # Remove
|
1528 |
+
parsed_frame = parsed_frame[~alpha_mask]
|
1529 |
+
|
1530 |
+
st.session_state['working_frame'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True)
|
1531 |
+
st.session_state['export_merge'] = st.session_state['working_frame'].copy()
|
1532 |
+
elif exp_submitted:
|
1533 |
+
st.session_state['settings_base'] = False
|
1534 |
+
parsed_frame = st.session_state['export_base'].copy()
|
1535 |
+
|
1536 |
+
# Same logic for export path
|
1537 |
+
if conditional_side_alpha and conditional_side_beta:
|
1538 |
+
alpha_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1539 |
+
for player in conditional_side_alpha:
|
1540 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1541 |
+
alpha_mask = alpha_mask & player_present
|
1542 |
+
|
1543 |
+
if conditional_var == 'Any':
|
1544 |
+
beta_mask = pd.Series([False] * len(parsed_frame), index=parsed_frame.index)
|
1545 |
+
for player in conditional_side_beta:
|
1546 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1547 |
+
beta_mask = beta_mask | player_present
|
1548 |
+
elif conditional_var == 'All':
|
1549 |
+
beta_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1550 |
+
for player in conditional_side_beta:
|
1551 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1552 |
+
beta_mask = beta_mask & player_present
|
1553 |
+
elif conditional_var == 'None':
|
1554 |
+
beta_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1555 |
+
for player in conditional_side_beta:
|
1556 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1557 |
+
beta_mask = beta_mask & (~player_present)
|
1558 |
+
|
1559 |
+
final_condition = alpha_mask & beta_mask
|
1560 |
+
|
1561 |
+
if keep_remove_var == 'Keep':
|
1562 |
+
parsed_frame = parsed_frame[final_condition]
|
1563 |
+
else:
|
1564 |
+
parsed_frame = parsed_frame[~final_condition]
|
1565 |
+
|
1566 |
+
elif conditional_side_alpha:
|
1567 |
+
alpha_mask = pd.Series([True] * len(parsed_frame), index=parsed_frame.index)
|
1568 |
+
for player in conditional_side_alpha:
|
1569 |
+
player_present = parsed_frame.apply(lambda row: player in row.values, axis=1)
|
1570 |
+
alpha_mask = alpha_mask & player_present
|
1571 |
+
|
1572 |
+
if keep_remove_var == 'Keep':
|
1573 |
+
parsed_frame = parsed_frame[alpha_mask]
|
1574 |
+
else:
|
1575 |
+
parsed_frame = parsed_frame[~alpha_mask]
|
1576 |
+
|
1577 |
+
st.session_state['export_base'] = parsed_frame.sort_values(by='median', ascending=False).reset_index(drop=True)
|
1578 |
+
st.session_state['export_merge'] = st.session_state['export_base'].copy()
|
1579 |
+
|
1580 |
with st.expander('Exposure Management'):
|
1581 |
with st.form(key='Exposures'):
|
1582 |
exposure_player = st.selectbox("Player", options=sorted(list(set(st.session_state['projections_df']['player_names'].unique()))), key='exposure_player')
|