Spaces:
Sleeping
Sleeping
Download baseline_join.py from AnayShukla/fpl-solver: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/AnayShukla/fpl-solver/resolve/main/baseline_join.py
- Command line
-
hf download hf://spaces/AnayShukla/fpl-solver/baseline_join.py
-
curl -L -o baseline_join.py https://huggingface.co/spaces/AnayShukla/fpl-solver/resolve/main/baseline_join.py
3.67 kB
| """Join FPL players onto the statistical baselines by name. | |
| Shared by main.py, which serves the result, and run_auto.py, which checks it | |
| after every Solio read. The join is by name, so a spelling change on either | |
| side quietly leaves a player with no baseline row - and main.py zero-fills | |
| those, which hands his share of the team to his teammates instead (Amad going | |
| unmatched put Mbeumo above Haaland in GW8). Keeping one copy of the join means | |
| the check sees exactly what production does. | |
| """ | |
| import json | |
| import os | |
| import unicodedata | |
| import pandas as pd | |
| RENAME_PATH = "rename.json" | |
| OUTFIELD_PATH = "statistical_weighted_baselines.csv" | |
| GK_PATH = "statistical_weighted_baselines_gk.csv" | |
| def _fold_name(name) -> str: | |
| """Casefold and strip accents: 'João Gomes' and 'Joao Gomes' compare equal.""" | |
| s = unicodedata.normalize("NFKD", str(name)) | |
| s = "".join(c for c in s if not unicodedata.combining(c)) | |
| return " ".join(s.casefold().split()) | |
| def _rejoin_accent_misses(merged, stats_df): | |
| """Retry the baseline join, accent-insensitively, for rows that missed it. | |
| FPL writes some names without their accents and Fotmob keeps them, so | |
| 'Joao Gomes' never met 'João Gomes'. A missed row is then zero-filled | |
| below, which silently gave 18 players - Gvardiol, Kerkez, Šeško, Petrović | |
| among them - a baseline of zero in every stat, not just minutes. | |
| Only rows that missed the exact join are retried, so nothing that already | |
| matched can change, and a folded name is used only if exactly one stats | |
| row carries it - an ambiguous fold is left unmatched rather than guessed. | |
| """ | |
| missed = merged["player_name"].isna() | |
| if not missed.any(): | |
| return merged | |
| folded = stats_df["player_name"].map(_fold_name) | |
| counts = folded.value_counts() | |
| unique = stats_df[folded.map(counts) == 1].assign(_fold=folded) | |
| lookup = unique.set_index("_fold") | |
| stat_cols = [c for c in stats_df.columns] | |
| merged = merged.copy() | |
| for idx in merged.index[missed]: | |
| key = _fold_name(merged.at[idx, "name"]) | |
| if key in lookup.index: | |
| for col in stat_cols: | |
| merged.at[idx, col] = lookup.at[key, col] | |
| return merged | |
| def join_baselines(players: pd.DataFrame) -> pd.DataFrame: | |
| """FPL players (needing `name`, `element_type`) with their baseline columns. | |
| `name` is rewritten through rename.json first. Unmatched rows keep NaN in | |
| `player_name` - zero-filling is left to the caller, so a miss can still be | |
| told apart from a player whose stats really are zero. | |
| """ | |
| players = players.copy() | |
| if os.path.exists(RENAME_PATH): | |
| with open(RENAME_PATH, "r", encoding="utf-8") as f: | |
| players["name"] = players["name"].replace(json.load(f)) | |
| gk_stats_df = pd.read_csv(GK_PATH).rename(columns=lambda x: x.strip()) | |
| outfield_stats_df = pd.read_csv(OUTFIELD_PATH).rename(columns=lambda x: x.strip()) | |
| gk_stats_df["player_name"] = gk_stats_df["player_name"].str.strip() | |
| outfield_stats_df["player_name"] = outfield_stats_df["player_name"].str.strip() | |
| gk_mask = players["element_type"] == 1 | |
| gk_merged = players[gk_mask].merge( | |
| gk_stats_df, left_on="name", right_on="player_name", how="left" | |
| ) | |
| outfield_merged = players[~gk_mask].merge( | |
| outfield_stats_df, left_on="name", right_on="player_name", how="left" | |
| ) | |
| gk_merged = _rejoin_accent_misses(gk_merged, gk_stats_df) | |
| outfield_merged = _rejoin_accent_misses(outfield_merged, outfield_stats_df) | |
| return ( | |
| pd.concat([gk_merged, outfield_merged], ignore_index=True) | |
| .sort_values("id") | |
| .reset_index(drop=True) | |
| ) | |