James McCool
commited on
Commit
·
effbc75
1
Parent(s):
2ba2e65
Let's see if Claude is good at this
Browse files
app.py
CHANGED
|
@@ -1742,15 +1742,20 @@ if selected_tab == 'Data Load':
|
|
| 1742 |
|
| 1743 |
if selected_tab == 'Manage Portfolio':
|
| 1744 |
if 'base_frame_names' not in st.session_state:
|
| 1745 |
-
st.session_state['base_frame_names'] = {
|
| 1746 |
if 'origin_portfolio' in st.session_state and 'projections_df' in st.session_state:
|
| 1747 |
with st.container():
|
| 1748 |
reset_port_col, recalc_div_col, set_base_col, blank_reset_col, contest_size_col = st.columns([1, 1, 1, 6, 4])
|
| 1749 |
with reset_port_col:
|
| 1750 |
with st.popover("Reset Portfolio"):
|
| 1751 |
st.markdown("choose a base to reset to:")
|
| 1752 |
-
|
| 1753 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1754 |
with recalc_div_col:
|
| 1755 |
if st.button("Recalculate Diversity"):
|
| 1756 |
st.session_state['display_frame']['Diversity'] = recalc_diversity(st.session_state['display_frame'], st.session_state['player_columns'])
|
|
@@ -1758,12 +1763,14 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1758 |
with st.popover("New Base Setting"):
|
| 1759 |
st.markdown("Name of new base:")
|
| 1760 |
new_base_name = st.text_input("New Base Name", value='New Base')
|
| 1761 |
-
if
|
| 1762 |
-
st.session_state['base_frame_names']
|
| 1763 |
-
|
| 1764 |
-
|
| 1765 |
-
|
| 1766 |
-
|
|
|
|
|
|
|
| 1767 |
|
| 1768 |
with contest_size_col:
|
| 1769 |
with st.form(key='contest_size_form'):
|
|
@@ -1775,35 +1782,42 @@ if selected_tab == 'Manage Portfolio':
|
|
| 1775 |
with submit_col:
|
| 1776 |
submitted = st.form_submit_button("Submit Size/Strength")
|
| 1777 |
if submitted:
|
| 1778 |
-
|
|
|
|
| 1779 |
|
| 1780 |
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Lineup Edge_Raw', 'Weighted Own', 'Geomean', 'Diversity', 'SE Score']
|
| 1781 |
|
| 1782 |
|
| 1783 |
if 'working_frame' not in st.session_state:
|
| 1784 |
st.session_state['settings_base'] = True
|
| 1785 |
-
|
| 1786 |
-
|
|
|
|
|
|
|
| 1787 |
|
| 1788 |
# Use vectorized calculation function
|
| 1789 |
-
|
| 1790 |
-
|
| 1791 |
st.session_state['player_columns'],
|
| 1792 |
st.session_state['map_dict'],
|
| 1793 |
type_var,
|
| 1794 |
sport_var,
|
| 1795 |
st.session_state['projections_df'] if 'stack_dict' in st.session_state else None
|
| 1796 |
)
|
| 1797 |
-
|
| 1798 |
|
| 1799 |
# Map existing stack/size data if available
|
| 1800 |
if 'stack_dict' in st.session_state:
|
| 1801 |
-
|
| 1802 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1803 |
|
| 1804 |
-
st.session_state['base_frame'] = predict_dupes(st.session_state['working_frame'], st.session_state['map_dict'], site_var, type_var, Contest_Size, strength_var, sport_var, salary_max)
|
| 1805 |
-
st.session_state['base_frame_names']['Default'] = st.session_state['base_frame']
|
| 1806 |
-
st.session_state['working_frame'] = st.session_state['base_frame_names']['Default']
|
| 1807 |
# st.session_state['highest_owned_teams'] = st.session_state['projections_df'][~st.session_state['projections_df']['position'].isin(['P', 'SP'])].groupby('team')['ownership'].sum().sort_values(ascending=False).head(3).index.tolist()
|
| 1808 |
# st.session_state['highest_owned_pitchers'] = st.session_state['projections_df'][st.session_state['projections_df']['position'].isin(['P', 'SP'])]['player_names'].sort_values(by='ownership', ascending=False).head(3).tolist()
|
| 1809 |
|
|
|
|
| 1742 |
|
| 1743 |
if selected_tab == 'Manage Portfolio':
|
| 1744 |
if 'base_frame_names' not in st.session_state:
|
| 1745 |
+
st.session_state['base_frame_names'] = {}
|
| 1746 |
if 'origin_portfolio' in st.session_state and 'projections_df' in st.session_state:
|
| 1747 |
with st.container():
|
| 1748 |
reset_port_col, recalc_div_col, set_base_col, blank_reset_col, contest_size_col = st.columns([1, 1, 1, 6, 4])
|
| 1749 |
with reset_port_col:
|
| 1750 |
with st.popover("Reset Portfolio"):
|
| 1751 |
st.markdown("choose a base to reset to:")
|
| 1752 |
+
if st.session_state['base_frame_names']:
|
| 1753 |
+
base_choice = st.selectbox("Base Choice", options=list(st.session_state['base_frame_names'].keys()), index=0)
|
| 1754 |
+
if st.button("Load Selected Base"):
|
| 1755 |
+
st.session_state['working_frame'] = st.session_state['base_frame_names'][base_choice].copy()
|
| 1756 |
+
st.rerun()
|
| 1757 |
+
else:
|
| 1758 |
+
st.info("No saved base frames available")
|
| 1759 |
with recalc_div_col:
|
| 1760 |
if st.button("Recalculate Diversity"):
|
| 1761 |
st.session_state['display_frame']['Diversity'] = recalc_diversity(st.session_state['display_frame'], st.session_state['player_columns'])
|
|
|
|
| 1763 |
with st.popover("New Base Setting"):
|
| 1764 |
st.markdown("Name of new base:")
|
| 1765 |
new_base_name = st.text_input("New Base Name", value='New Base')
|
| 1766 |
+
if st.button("Save Current as Base"):
|
| 1767 |
+
if new_base_name and new_base_name not in st.session_state['base_frame_names']:
|
| 1768 |
+
st.session_state['base_frame_names'][new_base_name] = st.session_state['working_frame'].copy()
|
| 1769 |
+
st.success(f"Base '{new_base_name}' saved successfully!")
|
| 1770 |
+
elif new_base_name in st.session_state['base_frame_names']:
|
| 1771 |
+
st.error("Base name already exists")
|
| 1772 |
+
else:
|
| 1773 |
+
st.error("Please enter a base name")
|
| 1774 |
|
| 1775 |
with contest_size_col:
|
| 1776 |
with st.form(key='contest_size_form'):
|
|
|
|
| 1782 |
with submit_col:
|
| 1783 |
submitted = st.form_submit_button("Submit Size/Strength")
|
| 1784 |
if submitted:
|
| 1785 |
+
if 'working_frame' in st.session_state:
|
| 1786 |
+
del st.session_state['working_frame']
|
| 1787 |
|
| 1788 |
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Lineup Edge_Raw', 'Weighted Own', 'Geomean', 'Diversity', 'SE Score']
|
| 1789 |
|
| 1790 |
|
| 1791 |
if 'working_frame' not in st.session_state:
|
| 1792 |
st.session_state['settings_base'] = True
|
| 1793 |
+
|
| 1794 |
+
# Load and process the origin portfolio
|
| 1795 |
+
initial_frame = pd.read_parquet(io.BytesIO(st.session_state['origin_portfolio']))
|
| 1796 |
+
st.session_state['player_columns'] = [col for col in initial_frame.columns if col not in excluded_cols]
|
| 1797 |
|
| 1798 |
# Use vectorized calculation function
|
| 1799 |
+
processed_frame = calculate_lineup_metrics(
|
| 1800 |
+
initial_frame,
|
| 1801 |
st.session_state['player_columns'],
|
| 1802 |
st.session_state['map_dict'],
|
| 1803 |
type_var,
|
| 1804 |
sport_var,
|
| 1805 |
st.session_state['projections_df'] if 'stack_dict' in st.session_state else None
|
| 1806 |
)
|
| 1807 |
+
processed_frame = processed_frame[processed_frame['salary'] <= salary_max]
|
| 1808 |
|
| 1809 |
# Map existing stack/size data if available
|
| 1810 |
if 'stack_dict' in st.session_state:
|
| 1811 |
+
processed_frame['Stack'] = processed_frame.index.map(st.session_state['stack_dict'])
|
| 1812 |
+
processed_frame['Size'] = processed_frame.index.map(st.session_state['size_dict'])
|
| 1813 |
+
|
| 1814 |
+
# Create the final base frame with dupe predictions
|
| 1815 |
+
final_base_frame = predict_dupes(processed_frame, st.session_state['map_dict'], site_var, type_var, Contest_Size, strength_var, sport_var, salary_max)
|
| 1816 |
+
|
| 1817 |
+
# Set up the Default base and working frame
|
| 1818 |
+
st.session_state['base_frame_names']['Default'] = final_base_frame.copy()
|
| 1819 |
+
st.session_state['working_frame'] = final_base_frame.copy()
|
| 1820 |
|
|
|
|
|
|
|
|
|
|
| 1821 |
# st.session_state['highest_owned_teams'] = st.session_state['projections_df'][~st.session_state['projections_df']['position'].isin(['P', 'SP'])].groupby('team')['ownership'].sum().sort_values(ascending=False).head(3).index.tolist()
|
| 1822 |
# st.session_state['highest_owned_pitchers'] = st.session_state['projections_df'][st.session_state['projections_df']['position'].isin(['P', 'SP'])]['player_names'].sort_values(by='ownership', ascending=False).head(3).tolist()
|
| 1823 |
|