James McCool
commited on
Commit
·
1a21253
1
Parent(s):
2e7d951
Enhance export matching functionality in app.py: replace fuzzywuzzy with rapidfuzz for improved performance, and streamline the name-to-ID mapping process by processing all names in a batch, enhancing efficiency and user experience during exports.
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ st.set_page_config(layout="wide")
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import time
|
| 6 |
-
from
|
| 7 |
import random
|
| 8 |
|
| 9 |
## import global functions
|
|
@@ -145,6 +145,38 @@ with tab1:
|
|
| 145 |
|
| 146 |
# Update projections_df with any new matches
|
| 147 |
st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
|
| 150 |
|
|
@@ -855,30 +887,6 @@ with tab2:
|
|
| 855 |
)
|
| 856 |
st.session_state['portfolio'] = st.session_state['portfolio'][lock_mask]
|
| 857 |
st.session_state['portfolio'] = st.session_state['portfolio'].sort_values(by='median', ascending=False)
|
| 858 |
-
if st.button('Prepare exports'):
|
| 859 |
-
# Create a dictionary of Name to Name+ID from csv_file
|
| 860 |
-
try:
|
| 861 |
-
name_id_map = dict(zip(
|
| 862 |
-
st.session_state['csv_file']['Name'],
|
| 863 |
-
st.session_state['csv_file']['Name + ID']
|
| 864 |
-
))
|
| 865 |
-
except:
|
| 866 |
-
name_id_map = dict(zip(
|
| 867 |
-
st.session_state['csv_file']['Nickname'],
|
| 868 |
-
st.session_state['csv_file']['Id']
|
| 869 |
-
))
|
| 870 |
-
|
| 871 |
-
# Function to find best match
|
| 872 |
-
def find_best_match(name):
|
| 873 |
-
best_match = process.extractOne(name, name_id_map.keys())
|
| 874 |
-
if best_match and best_match[1] >= 85: # 85% match threshold
|
| 875 |
-
return name_id_map[best_match[0]]
|
| 876 |
-
return name # Return original name if no good match found
|
| 877 |
-
|
| 878 |
-
# Apply the matching
|
| 879 |
-
projections['upload_match'] = projections['player_names'].apply(find_best_match)
|
| 880 |
-
st.session_state['export_dict'] = dict(zip(projections['player_names'], projections['upload_match']))
|
| 881 |
-
st.info('Click the above option after you have set up your portfolio if you want to export it as a set ready to upload to DFS sites')
|
| 882 |
with st.expander("Download options"):
|
| 883 |
if stack_dict is not None:
|
| 884 |
download_type = st.selectbox("Simple or Advanced Download?", options=['Simple', 'Advanced'], key='download_choice')
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import time
|
| 6 |
+
from rapidfuzz import process, fuzz
|
| 7 |
import random
|
| 8 |
|
| 9 |
## import global functions
|
|
|
|
| 145 |
|
| 146 |
# Update projections_df with any new matches
|
| 147 |
st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
|
| 148 |
+
if 'export_dict' not in st.session_state and csv_file is not None:
|
| 149 |
+
try:
|
| 150 |
+
name_id_map = dict(zip(
|
| 151 |
+
st.session_state['csv_file']['Name'],
|
| 152 |
+
st.session_state['csv_file']['Name + ID']
|
| 153 |
+
))
|
| 154 |
+
except:
|
| 155 |
+
name_id_map = dict(zip(
|
| 156 |
+
st.session_state['csv_file']['Nickname'],
|
| 157 |
+
st.session_state['csv_file']['Id']
|
| 158 |
+
))
|
| 159 |
+
|
| 160 |
+
# Get all names at once
|
| 161 |
+
names = projections['player_names'].tolist()
|
| 162 |
+
choices = list(name_id_map.keys())
|
| 163 |
+
|
| 164 |
+
# Process all names in one batch
|
| 165 |
+
matches = process.extract(
|
| 166 |
+
names,
|
| 167 |
+
choices,
|
| 168 |
+
scorer=fuzz.ratio,
|
| 169 |
+
score_cutoff=85,
|
| 170 |
+
limit=1 # Only get the best match
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Convert matches to dictionary
|
| 174 |
+
match_dict = {name: name_id_map[match[0][0]] if match else name
|
| 175 |
+
for name, match in zip(names, matches)}
|
| 176 |
+
|
| 177 |
+
# Apply the matches
|
| 178 |
+
projections['upload_match'] = projections['player_names'].map(match_dict)
|
| 179 |
+
st.session_state['export_dict'] = dict(zip(projections['player_names'], projections['upload_match']))
|
| 180 |
|
| 181 |
st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
|
| 182 |
|
|
|
|
| 887 |
)
|
| 888 |
st.session_state['portfolio'] = st.session_state['portfolio'][lock_mask]
|
| 889 |
st.session_state['portfolio'] = st.session_state['portfolio'].sort_values(by='median', ascending=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 890 |
with st.expander("Download options"):
|
| 891 |
if stack_dict is not None:
|
| 892 |
download_type = st.selectbox("Simple or Advanced Download?", options=['Simple', 'Advanced'], key='download_choice')
|