File size: 4,607 Bytes
d04558f
 
 
 
 
 
356c7d4
d04558f
 
2c1be4a
d04558f
356c7d4
d04558f
 
 
2c1be4a
d04558f
 
 
356c7d4
 
d04558f
1604a8f
d04558f
 
 
dc1c8da
d04558f
dc1c8da
 
d04558f
 
 
 
 
1604a8f
dc1c8da
d04558f
dc1c8da
 
d04558f
 
 
dc1c8da
 
d04558f
dc1c8da
 
 
 
d04558f
dc1c8da
 
d04558f
dc1c8da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d04558f
dc1c8da
 
d04558f
dc1c8da
 
 
2c1be4a
dc1c8da
d04558f
dc1c8da
 
 
 
 
 
 
 
 
 
ada082f
 
 
d04558f
dc1c8da
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
import numpy as np
import pandas as pd
import time
from fuzzywuzzy import process

def find_name_mismatches(contest_df, projections_df):
    # Create a copy of the projections dataframe to avoid modifying the original
    projections_df = projections_df.copy()
    contest_df = contest_df.copy()
    
    name_columns = [col for col in contest_df.columns if not col in ['BaseName', 'EntryCount']]
    
    if 'player_names' not in projections_df.columns:
        st.error("No 'player_names' column found in projections file")
        return contest_df, projections_df
    
    # Get unique player names from portfolio and projections
    portfolio_players = set()
    for col in name_columns:
        portfolio_players.update(contest_df[col].unique())
    projection_players = set(projections_df['player_names'].unique())
    portfolio_players_list = list(portfolio_players)
    projection_players_list = list(projection_players)
    
    # Find players in portfolio that are missing from projections
    players_missing_from_projections = list(projection_players - portfolio_players)
    
    # Automatically handle 90%+ matches before starting interactive process
    auto_matches = {}
    players_to_process = []
    for player in players_missing_from_projections:
        if not isinstance(player, str):
            st.warning(f"Skipping non-string value: {player}")
            continue
        closest_matches = process.extract(player, portfolio_players_list, limit=1)
        if closest_matches[0][1] >= 90:  # If high confidence match found
            match_name = closest_matches[0][0]
            auto_matches[player] = match_name
            st.success(f"Automatically matched '{player}' with '{match_name}' ({closest_matches[0][1]}% match)")
        else:
            players_to_process.append(player)
    
    if players_to_process:
        st.warning(f"Found {len(players_to_process)} players that need manual matching")
        
        # Create a form for batch processing
        with st.form("name_matching_form"):
            # Create tabs for each player
            tabs = st.tabs([f"Player {i+1}" for i in range(len(players_to_process))])
            
            # Dictionary to store selections
            selections = {}
            
            # Populate each tab
            for i, player in enumerate(players_to_process):
                with tabs[i]:
                    st.write(f"**Projection Name:** {player}")
                    
                    # Find the top 3 closest matches
                    closest_matches = process.extract(player, portfolio_players_list, limit=3)
                    
                    # Create radio buttons for selection
                    options = [f"{match[0]} ({match[1]}%)" for match in closest_matches]
                    options.append("None of these")
                    
                    selections[player] = st.radio(
                        f"Select correct match:",
                        options,
                        key=f"radio_{player}"
                    )
            
            # Submit button for the entire form
            submitted = st.form_submit_button("Apply All Changes")
            
            if submitted:
                # Process automatic matches
                for projection_name, contest_name in auto_matches.items():
                    for col in name_columns:
                        contest_df[col] = contest_df[col].replace(contest_name, projection_name)
                
                # Process manual selections
                for projection_name, selection in selections.items():
                    if selection != "None of these":
                        selected_name = selection.split(" (")[0]
                        for col in name_columns:
                            contest_df[col] = contest_df[col].replace(selected_name, projection_name)
                        st.success(f"Replaced '{selected_name}' with '{projection_name}'")
                
                st.success("All changes applied successfully!")
                return contest_df, projections_df
            
            # Return the current state if form hasn't been submitted yet
            return contest_df, projections_df
    else:
        st.success("All players have been automatically matched!")
        # Apply automatic matches
        for projection_name, contest_name in auto_matches.items():
            for col in name_columns:
                contest_df[col] = contest_df[col].replace(contest_name, projection_name)
        return contest_df, projections_df