James McCool
commited on
Commit
·
db65076
1
Parent(s):
110d58b
Refactor name matching analysis in app.py to ensure execution only occurs when 'origin_portfolio' is absent. This update improves the accuracy of player name matching and enhances session state management for portfolio and projections, ensuring proper data handling during user interactions.
Browse files
app.py
CHANGED
|
@@ -180,115 +180,116 @@ with tab1:
|
|
| 180 |
projections = projections.apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb))
|
| 181 |
st.dataframe(projections.head(10))
|
| 182 |
|
| 183 |
-
if
|
| 184 |
-
if
|
| 185 |
-
st.
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
|
| 293 |
# with tab2:
|
| 294 |
# if st.button('Clear data', key='reset2'):
|
|
|
|
| 180 |
projections = projections.apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb))
|
| 181 |
st.dataframe(projections.head(10))
|
| 182 |
|
| 183 |
+
if 'origin_portfolio' not in st.session_state:
|
| 184 |
+
if portfolio_file and projections_file:
|
| 185 |
+
if st.session_state['portfolio'] is not None and projections is not None:
|
| 186 |
+
st.subheader("Name Matching Analysis")
|
| 187 |
+
# Initialize projections_df in session state if it doesn't exist
|
| 188 |
+
# Get unique names from portfolio
|
| 189 |
+
portfolio_names = get_portfolio_names(st.session_state['portfolio'])
|
| 190 |
+
try:
|
| 191 |
+
csv_names = st.session_state['csv_file']['Name'].tolist()
|
| 192 |
+
except:
|
| 193 |
+
csv_names = st.session_state['csv_file']['Nickname'].tolist()
|
| 194 |
+
projection_names = projections['player_names'].tolist()
|
| 195 |
+
|
| 196 |
+
# Create match dictionary for portfolio names to projection names
|
| 197 |
+
portfolio_match_dict = {}
|
| 198 |
+
unmatched_names = []
|
| 199 |
+
for portfolio_name in portfolio_names:
|
| 200 |
+
match = process.extractOne(
|
| 201 |
+
portfolio_name,
|
| 202 |
+
csv_names,
|
| 203 |
+
score_cutoff=87
|
| 204 |
+
)
|
| 205 |
+
if match:
|
| 206 |
+
portfolio_match_dict[portfolio_name] = match[0]
|
| 207 |
+
if match[1] < 100:
|
| 208 |
+
st.write(f"{portfolio_name} matched from portfolio to site csv {match[0]} with a score of {match[1]}%")
|
| 209 |
+
else:
|
| 210 |
+
portfolio_match_dict[portfolio_name] = portfolio_name
|
| 211 |
+
unmatched_names.append(portfolio_name)
|
| 212 |
+
|
| 213 |
+
# Update portfolio with matched names
|
| 214 |
+
portfolio = st.session_state['portfolio'].copy()
|
| 215 |
+
player_columns = [col for col in portfolio.columns
|
| 216 |
+
if col not in ['salary', 'median', 'Own']]
|
| 217 |
+
|
| 218 |
+
# For each player column, update names using the match dictionary
|
| 219 |
+
for col in player_columns:
|
| 220 |
+
portfolio[col] = portfolio[col].map(lambda x: portfolio_match_dict.get(x, x))
|
| 221 |
+
st.session_state['portfolio'] = portfolio
|
| 222 |
+
|
| 223 |
+
# Create match dictionary for portfolio names to projection names
|
| 224 |
+
projections_match_dict = {}
|
| 225 |
+
unmatched_proj_names = []
|
| 226 |
+
for projections_name in projection_names:
|
| 227 |
+
match = process.extractOne(
|
| 228 |
+
projections_name,
|
| 229 |
+
csv_names,
|
| 230 |
+
score_cutoff=87
|
| 231 |
+
)
|
| 232 |
+
if match:
|
| 233 |
+
projections_match_dict[projections_name] = match[0]
|
| 234 |
+
if match[1] < 100:
|
| 235 |
+
st.write(f"{projections_name} matched from projections to site csv {match[0]} with a score of {match[1]}%")
|
| 236 |
+
else:
|
| 237 |
+
projections_match_dict[projections_name] = projections_name
|
| 238 |
+
unmatched_proj_names.append(projections_name)
|
| 239 |
+
|
| 240 |
+
# Update projections with matched names
|
| 241 |
+
projections['player_names'] = projections['player_names'].map(lambda x: projections_match_dict.get(x, x))
|
| 242 |
+
st.session_state['projections_df'] = projections
|
| 243 |
|
| 244 |
+
projections_names = st.session_state['projections_df']['player_names'].tolist()
|
| 245 |
+
portfolio_names = get_portfolio_names(st.session_state['portfolio'])
|
| 246 |
+
|
| 247 |
+
# Create match dictionary for portfolio names to projection names
|
| 248 |
+
projections_match_dict = {}
|
| 249 |
+
unmatched_proj_names = []
|
| 250 |
+
for projections_name in projection_names:
|
| 251 |
+
match = process.extractOne(
|
| 252 |
+
projections_name,
|
| 253 |
+
portfolio_names,
|
| 254 |
+
score_cutoff=87
|
| 255 |
+
)
|
| 256 |
+
if match:
|
| 257 |
+
projections_match_dict[projections_name] = match[0]
|
| 258 |
+
if match[1] < 100:
|
| 259 |
+
st.write(f"{projections_name} matched from portfolio to projections {match[0]} with a score of {match[1]}%")
|
| 260 |
+
else:
|
| 261 |
+
projections_match_dict[projections_name] = projections_name
|
| 262 |
+
unmatched_proj_names.append(projections_name)
|
| 263 |
+
|
| 264 |
+
# Update projections with matched names
|
| 265 |
+
projections['player_names'] = projections['player_names'].map(lambda x: projections_match_dict.get(x, x))
|
| 266 |
+
st.session_state['projections_df'] = projections
|
| 267 |
+
|
| 268 |
+
if sport_var in stacking_sports:
|
| 269 |
+
team_dict = dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team']))
|
| 270 |
+
st.session_state['portfolio']['Stack'] = st.session_state['portfolio'].apply(
|
| 271 |
+
lambda row: Counter(
|
| 272 |
+
team_dict.get(player, '') for player in row[2:]
|
| 273 |
+
if team_dict.get(player, '') != ''
|
| 274 |
+
).most_common(1)[0][0] if any(team_dict.get(player, '') for player in row[2:]) else '',
|
| 275 |
+
axis=1
|
| 276 |
+
)
|
| 277 |
+
st.session_state['portfolio']['Size'] = st.session_state['portfolio'].apply(
|
| 278 |
+
lambda row: Counter(
|
| 279 |
+
team_dict.get(player, '') for player in row[2:]
|
| 280 |
+
if team_dict.get(player, '') != ''
|
| 281 |
+
).most_common(1)[0][1] if any(team_dict.get(player, '') for player in row[2:]) else 0,
|
| 282 |
+
axis=1
|
| 283 |
+
)
|
| 284 |
+
stack_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Stack']))
|
| 285 |
+
size_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Size']))
|
| 286 |
+
|
| 287 |
+
working_frame = st.session_state['portfolio'].copy()
|
| 288 |
+
try:
|
| 289 |
+
st.session_state['export_dict'] = dict(zip(st.session_state['csv_file']['Name'], st.session_state['csv_file']['Name + ID']))
|
| 290 |
+
except:
|
| 291 |
+
st.session_state['export_dict'] = dict(zip(st.session_state['csv_file']['Nickname'], st.session_state['csv_file']['Id']))
|
| 292 |
+
st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
|
| 293 |
|
| 294 |
# with tab2:
|
| 295 |
# if st.button('Clear data', key='reset2'):
|