James McCool
commited on
Commit
·
00b2e24
1
Parent(s):
872a007
Refactor player column handling in 'app.py' to utilize session state for dynamic column management, improving consistency in player filtering and analysis across various operations.
Browse files
app.py
CHANGED
|
@@ -1103,6 +1103,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1103 |
del st.session_state['working_frame']
|
| 1104 |
|
| 1105 |
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Diversity']
|
|
|
|
| 1106 |
|
| 1107 |
if 'working_frame' not in st.session_state:
|
| 1108 |
st.session_state['settings_base'] = True
|
|
@@ -1274,24 +1275,21 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1274 |
parsed_frame = st.session_state['working_frame'].copy()
|
| 1275 |
if player_remove:
|
| 1276 |
# Create mask for lineups that contain any of the removed players
|
| 1277 |
-
|
| 1278 |
-
remove_mask = parsed_frame[player_columns].apply(
|
| 1279 |
lambda row: not any(player in list(row) for player in player_remove), axis=1
|
| 1280 |
)
|
| 1281 |
parsed_frame = parsed_frame[remove_mask]
|
| 1282 |
|
| 1283 |
if player_lock:
|
| 1284 |
-
# Create mask for lineups that contain all locked players
|
| 1285 |
-
player_columns = [col for col in parsed_frame.columns if col not in excluded_cols]
|
| 1286 |
|
| 1287 |
-
lock_mask = parsed_frame[player_columns].apply(
|
| 1288 |
lambda row: all(player in list(row) for player in player_lock), axis=1
|
| 1289 |
)
|
| 1290 |
parsed_frame = parsed_frame[lock_mask]
|
| 1291 |
|
| 1292 |
if team_include:
|
| 1293 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1294 |
-
filtered_player_columns = [col for col in player_columns if col not in ['SP1', 'SP2']]
|
| 1295 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1296 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1297 |
)
|
|
@@ -1303,7 +1301,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1303 |
|
| 1304 |
if team_remove:
|
| 1305 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1306 |
-
filtered_player_columns = [col for col in player_columns if col not in ['SP1', 'SP2']]
|
| 1307 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1308 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1309 |
)
|
|
@@ -1322,24 +1320,21 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1322 |
parsed_frame = st.session_state['export_base'].copy()
|
| 1323 |
if player_remove:
|
| 1324 |
# Create mask for lineups that contain any of the removed players
|
| 1325 |
-
|
| 1326 |
-
remove_mask = parsed_frame[player_columns].apply(
|
| 1327 |
lambda row: not any(player in list(row) for player in player_remove), axis=1
|
| 1328 |
)
|
| 1329 |
parsed_frame = parsed_frame[remove_mask]
|
| 1330 |
|
| 1331 |
if player_lock:
|
| 1332 |
-
# Create mask for lineups that contain all locked players
|
| 1333 |
-
player_columns = [col for col in parsed_frame.columns if col not in excluded_cols]
|
| 1334 |
|
| 1335 |
-
lock_mask = parsed_frame[player_columns].apply(
|
| 1336 |
lambda row: all(player in list(row) for player in player_lock), axis=1
|
| 1337 |
)
|
| 1338 |
parsed_frame = parsed_frame[lock_mask]
|
| 1339 |
|
| 1340 |
if team_include:
|
| 1341 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1342 |
-
filtered_player_columns = [col for col in player_columns if col not in ['SP1', 'SP2']]
|
| 1343 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1344 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1345 |
)
|
|
@@ -1351,7 +1346,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1351 |
|
| 1352 |
if team_remove:
|
| 1353 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1354 |
-
filtered_player_columns = [col for col in player_columns if col not in ['SP1', 'SP2']]
|
| 1355 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1356 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1357 |
)
|
|
@@ -1854,7 +1849,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1854 |
st.session_state['display_frame'] = st.session_state['export_base']
|
| 1855 |
with recalc_div_col:
|
| 1856 |
if st.button("Recalculate Diversity"):
|
| 1857 |
-
st.session_state['display_frame']['Diversity'] = recalc_diversity(st.session_state['display_frame'], player_columns)
|
| 1858 |
|
| 1859 |
total_rows = len(st.session_state['display_frame'])
|
| 1860 |
rows_per_page = 100
|
|
@@ -1922,7 +1917,6 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1922 |
with player_stats_col:
|
| 1923 |
if st.button("Analyze Players", key='analyze_players'):
|
| 1924 |
player_stats = []
|
| 1925 |
-
player_columns = [col for col in st.session_state['display_frame'].columns if col not in excluded_cols]
|
| 1926 |
|
| 1927 |
if st.session_state['settings_base'] and 'origin_player_exposures' in st.session_state and display_frame_source == 'Portfolio':
|
| 1928 |
st.session_state['player_summary'] = st.session_state['origin_player_exposures']
|
|
@@ -1930,7 +1924,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1930 |
if type_var == 'Showdown':
|
| 1931 |
if sport_var == 'GOLF':
|
| 1932 |
for player in player_names:
|
| 1933 |
-
player_mask = st.session_state['display_frame'][player_columns].apply(
|
| 1934 |
lambda row: player in list(row), axis=1
|
| 1935 |
)
|
| 1936 |
|
|
@@ -1950,7 +1944,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1950 |
else:
|
| 1951 |
for player in player_names:
|
| 1952 |
# Create mask for lineups where this player is Captain (first column)
|
| 1953 |
-
cpt_mask = st.session_state['display_frame'][player_columns[0]] == player
|
| 1954 |
|
| 1955 |
if cpt_mask.any():
|
| 1956 |
player_stats.append({
|
|
@@ -1967,7 +1961,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1967 |
})
|
| 1968 |
|
| 1969 |
# Create mask for lineups where this player is FLEX (other columns)
|
| 1970 |
-
flex_mask = st.session_state['display_frame'][player_columns[1:]].apply(
|
| 1971 |
lambda row: player in list(row), axis=1
|
| 1972 |
)
|
| 1973 |
|
|
@@ -1989,7 +1983,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1989 |
# Handle Captain positions
|
| 1990 |
for player in player_names:
|
| 1991 |
# Create mask for lineups where this player is Captain (first column)
|
| 1992 |
-
cpt_mask = st.session_state['display_frame'][player_columns[0]] == player
|
| 1993 |
|
| 1994 |
if cpt_mask.any():
|
| 1995 |
player_stats.append({
|
|
@@ -2006,7 +2000,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 2006 |
})
|
| 2007 |
|
| 2008 |
# Create mask for lineups where this player is FLEX (other columns)
|
| 2009 |
-
flex_mask = st.session_state['display_frame'][player_columns[1:]].apply(
|
| 2010 |
lambda row: player in list(row), axis=1
|
| 2011 |
)
|
| 2012 |
|
|
@@ -2026,7 +2020,7 @@ if selected_tab == 'Manage Portfolio':
|
|
| 2026 |
elif sport_var != 'CS2' and sport_var != 'LOL':
|
| 2027 |
# Original Classic format processing
|
| 2028 |
for player in player_names:
|
| 2029 |
-
player_mask = st.session_state['display_frame'][player_columns].apply(
|
| 2030 |
lambda row: player in list(row), axis=1
|
| 2031 |
)
|
| 2032 |
|
|
|
|
| 1103 |
del st.session_state['working_frame']
|
| 1104 |
|
| 1105 |
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Diversity']
|
| 1106 |
+
st.session_state['player_columns'] = [col for col in st.session_state['working_frame'].columns if col not in excluded_cols]
|
| 1107 |
|
| 1108 |
if 'working_frame' not in st.session_state:
|
| 1109 |
st.session_state['settings_base'] = True
|
|
|
|
| 1275 |
parsed_frame = st.session_state['working_frame'].copy()
|
| 1276 |
if player_remove:
|
| 1277 |
# Create mask for lineups that contain any of the removed players
|
| 1278 |
+
remove_mask = parsed_frame[st.session_state['player_columns']].apply(
|
|
|
|
| 1279 |
lambda row: not any(player in list(row) for player in player_remove), axis=1
|
| 1280 |
)
|
| 1281 |
parsed_frame = parsed_frame[remove_mask]
|
| 1282 |
|
| 1283 |
if player_lock:
|
|
|
|
|
|
|
| 1284 |
|
| 1285 |
+
lock_mask = parsed_frame[st.session_state['player_columns']].apply(
|
| 1286 |
lambda row: all(player in list(row) for player in player_lock), axis=1
|
| 1287 |
)
|
| 1288 |
parsed_frame = parsed_frame[lock_mask]
|
| 1289 |
|
| 1290 |
if team_include:
|
| 1291 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1292 |
+
filtered_player_columns = [col for col in st.session_state['player_columns'] if col not in ['SP1', 'SP2']]
|
| 1293 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1294 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1295 |
)
|
|
|
|
| 1301 |
|
| 1302 |
if team_remove:
|
| 1303 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1304 |
+
filtered_player_columns = [col for col in st.session_state['player_columns'] if col not in ['SP1', 'SP2']]
|
| 1305 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1306 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1307 |
)
|
|
|
|
| 1320 |
parsed_frame = st.session_state['export_base'].copy()
|
| 1321 |
if player_remove:
|
| 1322 |
# Create mask for lineups that contain any of the removed players
|
| 1323 |
+
remove_mask = parsed_frame[st.session_state['player_columns']].apply(
|
|
|
|
| 1324 |
lambda row: not any(player in list(row) for player in player_remove), axis=1
|
| 1325 |
)
|
| 1326 |
parsed_frame = parsed_frame[remove_mask]
|
| 1327 |
|
| 1328 |
if player_lock:
|
|
|
|
|
|
|
| 1329 |
|
| 1330 |
+
lock_mask = parsed_frame[st.session_state['player_columns']].apply(
|
| 1331 |
lambda row: all(player in list(row) for player in player_lock), axis=1
|
| 1332 |
)
|
| 1333 |
parsed_frame = parsed_frame[lock_mask]
|
| 1334 |
|
| 1335 |
if team_include:
|
| 1336 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1337 |
+
filtered_player_columns = [col for col in st.session_state['player_columns'] if col not in ['SP1', 'SP2']]
|
| 1338 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1339 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1340 |
)
|
|
|
|
| 1346 |
|
| 1347 |
if team_remove:
|
| 1348 |
# Create a copy of the frame with player names replaced by teams, excluding SP1 and SP2
|
| 1349 |
+
filtered_player_columns = [col for col in st.session_state['player_columns'] if col not in ['SP1', 'SP2']]
|
| 1350 |
team_frame = parsed_frame[filtered_player_columns].apply(
|
| 1351 |
lambda x: x.map(st.session_state['map_dict']['team_map'])
|
| 1352 |
)
|
|
|
|
| 1849 |
st.session_state['display_frame'] = st.session_state['export_base']
|
| 1850 |
with recalc_div_col:
|
| 1851 |
if st.button("Recalculate Diversity"):
|
| 1852 |
+
st.session_state['display_frame']['Diversity'] = recalc_diversity(st.session_state['display_frame'], st.session_state['player_columns'])
|
| 1853 |
|
| 1854 |
total_rows = len(st.session_state['display_frame'])
|
| 1855 |
rows_per_page = 100
|
|
|
|
| 1917 |
with player_stats_col:
|
| 1918 |
if st.button("Analyze Players", key='analyze_players'):
|
| 1919 |
player_stats = []
|
|
|
|
| 1920 |
|
| 1921 |
if st.session_state['settings_base'] and 'origin_player_exposures' in st.session_state and display_frame_source == 'Portfolio':
|
| 1922 |
st.session_state['player_summary'] = st.session_state['origin_player_exposures']
|
|
|
|
| 1924 |
if type_var == 'Showdown':
|
| 1925 |
if sport_var == 'GOLF':
|
| 1926 |
for player in player_names:
|
| 1927 |
+
player_mask = st.session_state['display_frame'][st.session_state['player_columns']].apply(
|
| 1928 |
lambda row: player in list(row), axis=1
|
| 1929 |
)
|
| 1930 |
|
|
|
|
| 1944 |
else:
|
| 1945 |
for player in player_names:
|
| 1946 |
# Create mask for lineups where this player is Captain (first column)
|
| 1947 |
+
cpt_mask = st.session_state['display_frame'][st.session_state['player_columns'][0]] == player
|
| 1948 |
|
| 1949 |
if cpt_mask.any():
|
| 1950 |
player_stats.append({
|
|
|
|
| 1961 |
})
|
| 1962 |
|
| 1963 |
# Create mask for lineups where this player is FLEX (other columns)
|
| 1964 |
+
flex_mask = st.session_state['display_frame'][st.session_state['player_columns'][1:]].apply(
|
| 1965 |
lambda row: player in list(row), axis=1
|
| 1966 |
)
|
| 1967 |
|
|
|
|
| 1983 |
# Handle Captain positions
|
| 1984 |
for player in player_names:
|
| 1985 |
# Create mask for lineups where this player is Captain (first column)
|
| 1986 |
+
cpt_mask = st.session_state['display_frame'][st.session_state['player_columns'][0]] == player
|
| 1987 |
|
| 1988 |
if cpt_mask.any():
|
| 1989 |
player_stats.append({
|
|
|
|
| 2000 |
})
|
| 2001 |
|
| 2002 |
# Create mask for lineups where this player is FLEX (other columns)
|
| 2003 |
+
flex_mask = st.session_state['display_frame'][st.session_state['player_columns'][1:]].apply(
|
| 2004 |
lambda row: player in list(row), axis=1
|
| 2005 |
)
|
| 2006 |
|
|
|
|
| 2020 |
elif sport_var != 'CS2' and sport_var != 'LOL':
|
| 2021 |
# Original Classic format processing
|
| 2022 |
for player in player_names:
|
| 2023 |
+
player_mask = st.session_state['display_frame'][st.session_state['player_columns']].apply(
|
| 2024 |
lambda row: player in list(row), axis=1
|
| 2025 |
)
|
| 2026 |
|