James McCool
commited on
Commit
·
63c301f
1
Parent(s):
3dcdcb7
Refactor app.py and find_name_mismatches.py for improved clarity and functionality
Browse files- Updated app.py to streamline session state usage for contest and projections data, enhancing readability and maintainability.
- Improved the find_name_mismatches function by directly modifying input dataframes, reducing unnecessary copies and improving performance.
- Enhanced user feedback with success messages upon successful file loading and name matching, improving overall user experience.
- app.py +14 -12
- global_func/find_name_mismatches.py +16 -26
app.py
CHANGED
@@ -35,12 +35,12 @@ with tab1:
|
|
35 |
del st.session_state['Contest']
|
36 |
|
37 |
if Contest_file:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if
|
42 |
st.success('Contest file loaded successfully!')
|
43 |
-
st.dataframe(
|
44 |
|
45 |
with col2:
|
46 |
st.subheader("Projections File")
|
@@ -66,17 +66,19 @@ with tab1:
|
|
66 |
)
|
67 |
|
68 |
if projections_file:
|
69 |
-
export_projections,
|
70 |
-
if
|
71 |
st.success('Projections file loaded successfully!')
|
72 |
-
st.dataframe(
|
73 |
|
74 |
if Contest_file and projections_file:
|
75 |
if 'Contest' not in st.session_state and 'projections_df' not in st.session_state:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
80 |
|
81 |
with tab2:
|
82 |
if 'Contest' in st.session_state and 'projections_df' in st.session_state:
|
|
|
35 |
del st.session_state['Contest']
|
36 |
|
37 |
if Contest_file:
|
38 |
+
st.session_state['Contest'], st.session_state['ownership_dict'], st.session_state['actual_dict'], st.session_state['entry_list'] = load_contest_file(Contest_file, sport_select)
|
39 |
+
st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
|
40 |
+
st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
|
41 |
+
if st.session_state['Contest'] is not None:
|
42 |
st.success('Contest file loaded successfully!')
|
43 |
+
st.dataframe(st.session_state['Contest'].head(10))
|
44 |
|
45 |
with col2:
|
46 |
st.subheader("Projections File")
|
|
|
66 |
)
|
67 |
|
68 |
if projections_file:
|
69 |
+
export_projections, st.session_state['projections_df'] = load_file(projections_file)
|
70 |
+
if st.session_state['projections_df'] is not None:
|
71 |
st.success('Projections file loaded successfully!')
|
72 |
+
st.dataframe(st.session_state['projections_df'].head(10))
|
73 |
|
74 |
if Contest_file and projections_file:
|
75 |
if 'Contest' not in st.session_state and 'projections_df' not in st.session_state:
|
76 |
+
st.subheader("Name Matching functions")
|
77 |
+
st.session_state['Contest'], st.session_state['projections_df'], st.session_state['ownership_dict'], st.session_state['actual_dict'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'], st.session_state['ownership_dict'], st.session_state['actual_dict'])
|
78 |
+
st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
|
79 |
+
st.session_state['ownership_dict'] = dict(zip(st.session_state['ownership_dict']['Player'], st.session_state['ownership_dict']['Own']))
|
80 |
+
st.session_state['actual_dict'] = dict(zip(st.session_state['actual_dict']['Player'], st.session_state['actual_dict']['FPTS']))
|
81 |
+
|
82 |
|
83 |
with tab2:
|
84 |
if 'Contest' in st.session_state and 'projections_df' in st.session_state:
|
global_func/find_name_mismatches.py
CHANGED
@@ -5,23 +5,18 @@ import time
|
|
5 |
from fuzzywuzzy import process
|
6 |
|
7 |
def find_name_mismatches(contest_df, projections_df, ownership_df, fpts_df):
|
8 |
-
# Create a copy of the projections dataframe to avoid modifying the original
|
9 |
-
projections_raw = projections_df.copy()
|
10 |
-
contest_raw = contest_df.copy()
|
11 |
-
ownership_raw = ownership_df.copy()
|
12 |
-
fpts_raw = fpts_df.copy()
|
13 |
|
14 |
-
name_columns = [col for col in
|
15 |
|
16 |
-
if 'player_names' not in
|
17 |
st.error("No 'player_names' column found in projections file")
|
18 |
-
return
|
19 |
|
20 |
# Get unique player names from portfolio and projections
|
21 |
portfolio_players = set()
|
22 |
for col in name_columns:
|
23 |
-
portfolio_players.update(
|
24 |
-
projection_players = set(
|
25 |
portfolio_players_list = list(portfolio_players)
|
26 |
projection_players_list = list(projection_players)
|
27 |
|
@@ -79,9 +74,9 @@ def find_name_mismatches(contest_df, projections_df, ownership_df, fpts_df):
|
|
79 |
# Process automatic matches
|
80 |
for projection_name, contest_name in auto_matches.items():
|
81 |
for col in name_columns:
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
st.write(contest_name + ' ' + projection_name)
|
86 |
|
87 |
# Process manual selections
|
@@ -89,26 +84,21 @@ def find_name_mismatches(contest_df, projections_df, ownership_df, fpts_df):
|
|
89 |
if selection != "None of these":
|
90 |
selected_name = selection.split(" (")[0]
|
91 |
for col in name_columns:
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
st.write(contest_name + ' ' + projection_name)
|
96 |
st.success(f"Replaced '{selected_name}' with '{projection_name}'")
|
97 |
|
98 |
-
ownership_dict = dict(zip(ownership_raw['Player'], ownership_raw['Own']))
|
99 |
-
fpts_dict = dict(zip(fpts_raw['Player'], fpts_raw['FPTS']))
|
100 |
-
|
101 |
st.success("All changes applied successfully!")
|
102 |
-
return
|
103 |
else:
|
104 |
st.success("All players have been automatically matched!")
|
105 |
# Apply automatic matches
|
106 |
for projection_name, contest_name in auto_matches.items():
|
107 |
for col in name_columns:
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
st.write(contest_name + ' ' + projection_name)
|
112 |
-
|
113 |
-
fpts_dict = dict(zip(fpts_raw['Player'], fpts_raw['FPTS']))
|
114 |
-
return contest_raw, projections_raw, ownership_dict, fpts_dict
|
|
|
5 |
from fuzzywuzzy import process
|
6 |
|
7 |
def find_name_mismatches(contest_df, projections_df, ownership_df, fpts_df):
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
name_columns = [col for col in contest_df.columns if not col in ['BaseName', 'EntryCount']]
|
10 |
|
11 |
+
if 'player_names' not in projections_df.columns:
|
12 |
st.error("No 'player_names' column found in projections file")
|
13 |
+
return contest_df, projections_df
|
14 |
|
15 |
# Get unique player names from portfolio and projections
|
16 |
portfolio_players = set()
|
17 |
for col in name_columns:
|
18 |
+
portfolio_players.update(contest_df[col].unique())
|
19 |
+
projection_players = set(projections_df['player_names'].unique())
|
20 |
portfolio_players_list = list(portfolio_players)
|
21 |
projection_players_list = list(projection_players)
|
22 |
|
|
|
74 |
# Process automatic matches
|
75 |
for projection_name, contest_name in auto_matches.items():
|
76 |
for col in name_columns:
|
77 |
+
contest_df[col] = contest_df[col].replace(contest_name, projection_name)
|
78 |
+
ownership_df['Player'] = ownership_df['Player'].replace(contest_name, projection_name)
|
79 |
+
fpts_df['Player'] = fpts_df['Player'].replace(contest_name, projection_name)
|
80 |
st.write(contest_name + ' ' + projection_name)
|
81 |
|
82 |
# Process manual selections
|
|
|
84 |
if selection != "None of these":
|
85 |
selected_name = selection.split(" (")[0]
|
86 |
for col in name_columns:
|
87 |
+
contest_df[col] = contest_df[col].replace(selected_name, projection_name)
|
88 |
+
ownership_df['Player'] = ownership_df['Player'].replace(selected_name, projection_name)
|
89 |
+
fpts_df['Player'] = fpts_df['Player'].replace(selected_name, projection_name)
|
90 |
st.write(contest_name + ' ' + projection_name)
|
91 |
st.success(f"Replaced '{selected_name}' with '{projection_name}'")
|
92 |
|
|
|
|
|
|
|
93 |
st.success("All changes applied successfully!")
|
94 |
+
return contest_df, projections_df, ownership_df, fpts_df
|
95 |
else:
|
96 |
st.success("All players have been automatically matched!")
|
97 |
# Apply automatic matches
|
98 |
for projection_name, contest_name in auto_matches.items():
|
99 |
for col in name_columns:
|
100 |
+
contest_df[col] = contest_df[col].replace(contest_name, projection_name)
|
101 |
+
ownership_df['Player'] = ownership_df['Player'].replace(contest_name, projection_name)
|
102 |
+
fpts_df['Player'] = fpts_df['Player'].replace(contest_name, projection_name)
|
103 |
st.write(contest_name + ' ' + projection_name)
|
104 |
+
return contest_df, projections_df, ownership_df, fpts_df
|
|
|
|