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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Here is Claude's plan: βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Card Lab β Pass 5: Player Identity Layer + Season-Summary Selectors
|
| 2 |
|
| 3 |
+
Context
|
| 4 |
|
| 5 |
+
All prior selector passes used event-level or partial tables (statcast_event_core, batter_zone_events, pitcher_inning_first_seed_events) with COUNT(*) workload
|
| 6 |
+
proxies. These produce missing players, role contamination, and name-format mismatches. The correct architecture uses pybaseball season-level summaries
|
| 7 |
+
(pitching_stats / batting_stats) with IP > 0 / AB > 0 gates. A canonical player_identity_map solves all naming problems permanently β Last/First, accents,
|
| 8 |
+
suffixes, duplicates, pybaseball-vs-statcast mismatches β so Card Lab stops breaking when source formats differ.
|
| 9 |
|
| 10 |
+
---
|
| 11 |
+
Files Created
|
| 12 |
|
| 13 |
+
ββββββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
β File β Purpose β
|
| 15 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 16 |
+
β data/player_identity_map.parquet β Canonical identity layer β
|
| 17 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 18 |
+
β data/card_lab_pitcher_seasons.parquet β Pitcher season summaries (from pybaseball) β
|
| 19 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 20 |
+
β data/card_lab_hitter_seasons.parquet β Hitter season summaries (from pybaseball) β
|
| 21 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 22 |
+
β visualization/cards/player_identity.py β Identity map loader + normalization helpers β
|
| 23 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 24 |
+
β scripts/build_player_identity_map.py οΏ½οΏ½ Offline build: identity map β
|
| 25 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 26 |
+
β scripts/build_card_lab_season_summaries.py β Offline build: season summary parquets β
|
| 27 |
+
ββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 28 |
+
β scripts/validate_card_lab_build.py β Required build validation and acceptance gate before committing Parquet artifacts β
|
| 29 |
+
ββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
|
| 31 |
+
Files Modified
|
| 32 |
|
| 33 |
+
βββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
+
β File β Change β
|
| 35 |
+
βββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 36 |
+
β visualization/cards/card_queries.py β Replace selector functions to read from Parquet via identity layer β
|
| 37 |
+
βββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 38 |
+
β visualization/card_lab_page.py β Selector logic + debug expander β
|
| 39 |
+
βββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 40 |
+
β requirements.txt β Add pybaseball β
|
| 41 |
+
βββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
|
| 43 |
+
Do NOT touch: card_data.py, poster_renderer.py, player_assets.py, app.py, get_player_card_window_df(), get_batter_mlbam_id(), any model file, DB schema.
|
| 44 |
|
| 45 |
+
---
|
| 46 |
+
Part 1 β player_identity_map Schema
|
| 47 |
|
| 48 |
+
File: data/player_identity_map.parquet
|
| 49 |
|
| 50 |
+
βββββββββββββββββββββββββββββ¬βββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
β Column β Type β Description β
|
| 52 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 53 |
+
β player_id β int (nullable) β MLBAM ID β preferred stable identifier β
|
| 54 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 55 |
+
β fangraphs_id β int (nullable) β FanGraphs IDfg β bridge to MLBAM lookup β
|
| 56 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 57 |
+
β canonical_name β str β Clean "First Last" display name β
|
| 58 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 59 |
+
β canonical_name_normalized β str β Lowercase, no accents, no suffixes, no punctuation β matching key only β
|
| 60 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 61 |
+
β statcast_name β str (nullable) β Exact name in statcast_event_core.player_name β needed for hitter window queries β
|
| 62 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 63 |
+
β pybaseball_name β str β Exact Name from pitching_stats / batting_stats β
|
| 64 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 65 |
+
β role_hitter β bool β True if appears in batting_stats with AB > 0 β
|
| 66 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 67 |
+
β role_pitcher β bool β True if appears in pitching_stats with IP > 0 β
|
| 68 |
+
βββββββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 69 |
+
β display_name β str β Collision-safe selector label (canonical_name or canonical_name + suffix if collision) β
|
| 70 |
+
ββββοΏ½οΏ½ββββββββββββββββββββββββΌβββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 71 |
+
β source_note β str β e.g. "pybaseball+mlbam", "statcast-enriched", "collision-resolved" β
|
| 72 |
+
βββββββββββββββββββββββββββββ΄βββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
|
| 74 |
+
---
|
| 75 |
+
Part 2 β Normalization Rules
|
| 76 |
+
|
| 77 |
+
New file: visualization/cards/player_identity.py
|
| 78 |
+
|
| 79 |
+
# visualization/cards/player_identity.py
|
| 80 |
+
"""
|
| 81 |
+
Canonical player identity layer for Card Lab.
|
| 82 |
+
- Normalization helpers (shared, no other imports)
|
| 83 |
+
- Identity map loader (cached read of data/player_identity_map.parquet)
|
| 84 |
+
- display_name β statcast_name / player_id resolvers
|
| 85 |
+
"""
|
| 86 |
+
from __future__ import annotations
|
| 87 |
+
import re
|
| 88 |
+
import unicodedata
|
| 89 |
+
from pathlib import Path
|
| 90 |
+
import pandas as pd
|
| 91 |
+
|
| 92 |
+
_IDENTITY_MAP_PATH = Path(__file__).parent.parent.parent / "data" / "player_identity_map.parquet"
|
| 93 |
+
_PITCHER_SEASONS_PATH = Path(__file__).parent.parent.parent / "data" / "card_lab_pitcher_seasons.parquet"
|
| 94 |
+
_HITTER_SEASONS_PATH = Path(__file__).parent.parent.parent / "data" / "card_lab_hitter_seasons.parquet"
|
| 95 |
+
|
| 96 |
+
_SUFFIXES = re.compile(
|
| 97 |
+
r"\b(jr\.?|sr\.?|ii|iii|iv|v)\b",
|
| 98 |
+
re.IGNORECASE,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
def normalize_for_matching(name: str) -> str:
|
| 102 |
+
"""
|
| 103 |
+
Returns a lowercase matching key: strips diacritics, suffixes,
|
| 104 |
+
punctuation noise, collapses whitespace. NOT for display.
|
| 105 |
+
"""
|
| 106 |
+
if not name:
|
| 107 |
+
return ""
|
| 108 |
+
s = unicodedata.normalize("NFKD", str(name))
|
| 109 |
+
s = "".join(c for c in s if not unicodedata.combining(c))
|
| 110 |
+
s = s.lower()
|
| 111 |
+
s = re.sub(r"[.,'\-]", " ", s)
|
| 112 |
+
s = _SUFFIXES.sub(" ", s)
|
| 113 |
+
s = re.sub(r"\s+", " ", s).strip()
|
| 114 |
+
return s
|
| 115 |
+
|
| 116 |
+
def to_canonical_name(name: str) -> str:
|
| 117 |
+
"""
|
| 118 |
+
Convert 'Last, First' -> 'First Last'. Pass-through for 'First Last'.
|
| 119 |
+
"""
|
| 120 |
+
s = str(name or "").strip()
|
| 121 |
+
if "," in s:
|
| 122 |
+
last, first = [x.strip() for x in s.split(",", 1)]
|
| 123 |
+
return f"{first} {last}"
|
| 124 |
+
return s
|
| 125 |
+
|
| 126 |
+
def load_identity_map() -> pd.DataFrame | None:
|
| 127 |
+
"""Load player_identity_map.parquet. Returns None if missing."""
|
| 128 |
+
if not _IDENTITY_MAP_PATH.exists():
|
| 129 |
+
return None
|
| 130 |
+
return pd.read_parquet(_IDENTITY_MAP_PATH)
|
| 131 |
+
|
| 132 |
+
def get_hitter_selector_data(year: int | None) -> tuple[list[str], dict[str, str]]:
|
| 133 |
+
"""
|
| 134 |
+
Load hitter season summaries. Return:
|
| 135 |
+
- sorted display_names list (ONLY rows with resolved statcast_name β null rows excluded at build time)
|
| 136 |
+
- display_name β statcast_name mapping (guaranteed non-null; statcast_name is the exact name
|
| 137 |
+
for downstream WHERE player_name = :name queries)
|
| 138 |
+
|
| 139 |
+
NOTE: statcast_name nulls are excluded at build time (build_card_lab_season_summaries.py).
|
| 140 |
+
Any row reaching this function has a valid statcast_name. No fallback is needed or allowed.
|
| 141 |
+
"""
|
| 142 |
+
if not _HITTER_SEASONS_PATH.exists():
|
| 143 |
+
return [], {}
|
| 144 |
+
df = pd.read_parquet(_HITTER_SEASONS_PATH)
|
| 145 |
+
df = df[df["AB"] > 0]
|
| 146 |
+
if year is not None:
|
| 147 |
+
df = df[df["Season"] == int(year)]
|
| 148 |
+
# statcast_name must be present β build script guarantees this; drop any that slipped through
|
| 149 |
+
df = df.dropna(subset=["display_name", "statcast_name"])
|
| 150 |
+
display_names = sorted([str(n) for n in df["display_name"].unique() if pd.notna(n)])
|
| 151 |
+
# Build display β statcast_name map (non-null guaranteed)
|
| 152 |
+
name_map: dict[str, str] = {}
|
| 153 |
+
for _, row in df.drop_duplicates(subset=["display_name"]).iterrows():
|
| 154 |
+
name_map[str(row["display_name"])] = str(row["statcast_name"])
|
| 155 |
+
return display_names, name_map
|
| 156 |
+
|
| 157 |
+
def get_pitcher_selector_data(year: int | None) -> tuple[list[tuple[str, int | None]], dict]:
|
| 158 |
+
"""
|
| 159 |
+
Load pitcher season summaries. Return:
|
| 160 |
+
- sorted [(display_name, player_id), ...] list
|
| 161 |
+
- additional metadata dict (for debug)
|
| 162 |
+
"""
|
| 163 |
+
if not _PITCHER_SEASONS_PATH.exists():
|
| 164 |
+
return [], {}
|
| 165 |
+
df = pd.read_parquet(_PITCHER_SEASONS_PATH)
|
| 166 |
+
df = df[df["IP"] > 0]
|
| 167 |
+
if year is not None:
|
| 168 |
+
df = df[df["Season"] == int(year)]
|
| 169 |
+
df = df.dropna(subset=["display_name"])
|
| 170 |
+
# Deduplicate by display_name, keep highest IP record
|
| 171 |
+
deduped = df.sort_values("IP", ascending=False).drop_duplicates(subset=["display_name"])
|
| 172 |
+
pairs = sorted(
|
| 173 |
+
[
|
| 174 |
+
(
|
| 175 |
+
str(row["display_name"]),
|
| 176 |
+
int(row["player_id"]) if pd.notna(row.get("player_id")) else None,
|
| 177 |
+
)
|
| 178 |
+
for _, row in deduped.iterrows()
|
| 179 |
+
if pd.notna(row.get("display_name"))
|
| 180 |
+
],
|
| 181 |
+
key=lambda p: str(p[0]).lower(),
|
| 182 |
+
)
|
| 183 |
+
meta = {"parquet_rows": len(df), "year": year}
|
| 184 |
+
return pairs, meta
|
| 185 |
+
|
| 186 |
+
---
|
| 187 |
+
Part 3 β Build Scripts
|
| 188 |
+
|
| 189 |
+
Deployment mode
|
| 190 |
+
|
| 191 |
+
Option A β Offline build, committed artifacts (CHOSEN): Parquet files are built locally using pybaseball + DB access, then committed to the repo. The Streamlit
|
| 192 |
+
app reads pre-built Parquet files at runtime and does NOT call pybaseball or the DB during selector load. This is safe for HuggingFace Spaces where storage may
|
| 193 |
+
be ephemeral and where live pybaseball calls during page render are unacceptable.
|
| 194 |
+
|
| 195 |
+
pybaseball stays in requirements.txt for reproducibility (so any developer can rebuild the Parquet files). The committed Parquet files ARE the runtime source.
|
| 196 |
+
|
| 197 |
+
Build order:
|
| 198 |
+
# Run locally whenever season data changes:
|
| 199 |
+
python scripts/build_player_identity_map.py --seasons 2021 2022 2023 2024 2025
|
| 200 |
+
python scripts/build_card_lab_season_summaries.py --seasons 2021 2022 2023 2024 2025
|
| 201 |
+
python scripts/validate_card_lab_build.py # must pass before committing
|
| 202 |
+
# Then commit data/*.parquet to repo
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
scripts/build_player_identity_map.py
|
| 206 |
+
|
| 207 |
+
Builds data/player_identity_map.parquet. Requires DB connection (statcast_name enrichment is mandatory for hitters, not optional).
|
| 208 |
+
|
| 209 |
+
Logic:
|
| 210 |
+
1. Fetch pitching_stats(yr, yr, qual=0, ind=1) for each target season β collect unique (Name, IDfg)
|
| 211 |
+
2. Fetch batting_stats(yr, yr, qual=0, ind=1) for each target season β collect unique (Name, IDfg)
|
| 212 |
+
3. Collect all unique (Name, IDfg) pairs from both sources
|
| 213 |
+
4. Use playerid_reverse_lookup(fg_id_list, key_type='fangraphs') to get key_mlbam (MLBAM ID)
|
| 214 |
+
5. Build identity records:
|
| 215 |
+
- canonical_name = to_canonical_name(Name) [no-op for pybaseball "First Last" names]
|
| 216 |
+
- canonical_name_normalized = normalize_for_matching(canonical_name)
|
| 217 |
+
- pybaseball_name = raw Name from pybaseball
|
| 218 |
+
- statcast_name = None (will be populated in step 7)
|
| 219 |
+
- role_hitter = True if player appears in batting_stats with AB > 0 any season
|
| 220 |
+
- role_pitcher = True if player appears in pitching_stats with IP > 0 any season
|
| 221 |
+
- fangraphs_id = IDfg (int)
|
| 222 |
+
- player_id = MLBAM key_mlbam (int, nullable)
|
| 223 |
+
- source_note = "pybaseball+mlbam" if player_id resolved, else "pybaseball-only"
|
| 224 |
+
6. Merge duplicate identity records:
|
| 225 |
+
- Primary merge key: player_id β if two records share player_id, merge roles (keep both True flags)
|
| 226 |
+
- Fallback merge key: canonical_name_normalized β merge only if player_id is None for both
|
| 227 |
+
- Log ambiguous merges (same normalized name, different player_ids) as WARNINGS β keep separate
|
| 228 |
+
7. statcast_name enrichment (REQUIRED):
|
| 229 |
+
- Connect to DB via database.remote_db.get_connection()
|
| 230 |
+
- For each identity record, query:
|
| 231 |
+
SELECT DISTINCT player_name FROM statcast_event_core
|
| 232 |
+
WHERE LOWER(player_name) = LOWER(:canonical_name)
|
| 233 |
+
- If exactly one match β statcast_name = match; source_note += "+statcast"
|
| 234 |
+
- If no match β try normalized fuzzy match using canonical_name_normalized against
|
| 235 |
+
SELECT DISTINCT player_name FROM statcast_event_core
|
| 236 |
+
(fetch batch once, match in Python using normalize_for_matching)
|
| 237 |
+
- If still no match β statcast_name remains None; log WARNING with name
|
| 238 |
+
- If ambiguous (multiple statcast names match the normalized key) β log WARNING; statcast_name = None
|
| 239 |
+
8. Collision handling:
|
| 240 |
+
- Find groups sharing canonical_name_normalized with DIFFERENT player_ids
|
| 241 |
+
- Sort each group by player_id ascending (deterministic)
|
| 242 |
+
- First record: display_name = canonical_name
|
| 243 |
+
- Subsequent records: display_name = f"{canonical_name} ({player_id})" or f"{canonical_name} [?]" if no ID
|
| 244 |
+
- Non-colliding records: display_name = canonical_name
|
| 245 |
+
9. Write data/player_identity_map.parquet
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
scripts/build_card_lab_season_summaries.py
|
| 249 |
+
|
| 250 |
+
Builds pitcher and hitter season Parquets joined through identity map.
|
| 251 |
+
|
| 252 |
+
Logic:
|
| 253 |
+
1. Load player_identity_map.parquet (must exist β run build_player_identity_map.py first)
|
| 254 |
+
2. For each target season:
|
| 255 |
+
A. Fetch pitching_stats(yr, yr, qual=0, ind=1) β pitcher_df
|
| 256 |
+
- filter: IP > 0
|
| 257 |
+
- join to identity map on IDfg (fangraphs_id) β add player_id, display_name, statcast_name, canonical_name
|
| 258 |
+
- fallback join: if IDfg not in identity map, use normalized-name match on canonical_name_normalized
|
| 259 |
+
- keep columns: display_name, canonical_name, statcast_name, player_id, Season, IP
|
| 260 |
+
B. Fetch batting_stats(yr, yr, qual=0, ind=1) β hitter_df
|
| 261 |
+
- filter: AB > 0
|
| 262 |
+
- join to identity map on IDfg
|
| 263 |
+
- fallback join: normalized-name match
|
| 264 |
+
- keep columns: display_name, canonical_name, statcast_name, player_id, Season, AB
|
| 265 |
+
- EXCLUDE any hitter row where statcast_name IS NULL (these cannot be used for downstream hitter queries)
|
| 266 |
+
- Log excluded rows as WARNING with name
|
| 267 |
+
3. Concat all seasons for each role
|
| 268 |
+
4. Write data/card_lab_pitcher_seasons.parquet
|
| 269 |
+
5. Write data/card_lab_hitter_seasons.parquet
|
| 270 |
+
|
| 271 |
+
---
|
| 272 |
+
scripts/validate_card_lab_build.py
|
| 273 |
+
|
| 274 |
+
Required β must run and pass before committing Parquet files.
|
| 275 |
+
|
| 276 |
+
Validation output (printed to stdout):
|
| 277 |
+
|
| 278 |
+
=== player_identity_map.parquet ===
|
| 279 |
+
Total records: XXXX
|
| 280 |
+
With MLBAM player_id: XXXX
|
| 281 |
+
Missing player_id: XXXX
|
| 282 |
+
With statcast_name: XXXX
|
| 283 |
+
Missing statcast_name: XXXX
|
| 284 |
+
Collision records: XXXX
|
| 285 |
+
First 10 missing statcast_name: [list of names]
|
| 286 |
+
|
| 287 |
+
=== card_lab_pitcher_seasons.parquet ===
|
| 288 |
+
By year:
|
| 289 |
+
2021: XXXX rows (XXXX with player_id, XXXX without)
|
| 290 |
+
2022: ...
|
| 291 |
+
...
|
| 292 |
+
Total null player_id: XXXX
|
| 293 |
+
Duplicate display_names by year: XXXX (must be 0)
|
| 294 |
+
|
| 295 |
+
=== card_lab_hitter_seasons.parquet ===
|
| 296 |
+
By year:
|
| 297 |
+
2021: XXXX rows (all have statcast_name β null rows excluded at build time)
|
| 298 |
+
2022: ...
|
| 299 |
+
...
|
| 300 |
+
Rows excluded due to null statcast_name (by year):
|
| 301 |
+
2021: XXXX excluded
|
| 302 |
+
...
|
| 303 |
+
Duplicate display_names by year: XXXX (must be 0)
|
| 304 |
+
|
| 305 |
+
PASS / FAIL:
|
| 306 |
+
[PASS] No duplicate display_names in pitcher selector
|
| 307 |
+
[PASS] No duplicate display_names in hitter selector
|
| 308 |
+
[PASS/FAIL] Spot check β Tarik Skubal in pitcher 2025: FOUND / NOT FOUND
|
| 309 |
+
[PASS/FAIL] Spot check β Alec Burleson in hitter 2025: FOUND / NOT FOUND
|
| 310 |
+
[PASS/FAIL] Spot check β Freddy Peralta NOT in hitter 2025: ABSENT / PRESENT (FAIL)
|
| 311 |
+
|
| 312 |
+
---
|
| 313 |
+
Part 4 β card_queries.py Changes
|
| 314 |
+
|
| 315 |
+
Replace get_pitcher_selector_names() and get_hitter_selector_names() to use identity-backed Parquet reads. Keep conn in signature for API compat (unused).
|
| 316 |
+
|
| 317 |
+
# New imports at top of card_queries.py
|
| 318 |
+
from visualization.cards.player_identity import (
|
| 319 |
+
get_hitter_selector_data,
|
| 320 |
+
get_pitcher_selector_data,
|
| 321 |
+
_HITTER_SEASONS_PATH,
|
| 322 |
+
_PITCHER_SEASONS_PATH,
|
| 323 |
+
_IDENTITY_MAP_PATH,
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
def get_hitter_selector_names(conn, year: int | None = None) -> tuple[list[str], dict[str, str]]:
|
| 327 |
+
"""
|
| 328 |
+
Returns (display_names, display_to_statcast_name_map).
|
| 329 |
+
display_names: sorted plain strings for selectbox β ALL have a resolved statcast_name
|
| 330 |
+
display_to_statcast_name_map: display_name β exact statcast_event_core.player_name string
|
| 331 |
+
Source: card_lab_hitter_seasons.parquet (pybaseball batting_stats, AB > 0 gate)
|
| 332 |
+
Rows with null statcast_name are excluded at build time and absent from this list.
|
| 333 |
+
"""
|
| 334 |
+
try:
|
| 335 |
+
names, name_map = get_hitter_selector_data(year)
|
| 336 |
+
logger.info("[hitter_selector] year=%s total=%s", year, len(names))
|
| 337 |
+
return names, name_map
|
| 338 |
+
except Exception as exc:
|
| 339 |
+
logger.warning("[hitter_selector] failed year=%s: %s", year, exc)
|
| 340 |
+
return [], {}
|
| 341 |
+
|
| 342 |
+
def get_pitcher_selector_names(conn, year: int | None = None) -> list[tuple[str, int | None]]:
|
| 343 |
+
"""
|
| 344 |
+
Returns sorted [(display_name, player_id), ...].
|
| 345 |
+
player_id: MLBAM pitcher ID β passed directly to get_player_card_window_df(pitcher_id=...)
|
| 346 |
+
Source: card_lab_pitcher_seasons.parquet (pybaseball pitching_stats, IP > 0 gate)
|
| 347 |
+
"""
|
| 348 |
+
try:
|
| 349 |
+
pairs, _ = get_pitcher_selector_data(year)
|
| 350 |
+
logger.info("[pitcher_selector] year=%s total=%s", year, len(pairs))
|
| 351 |
+
return pairs
|
| 352 |
+
except Exception as exc:
|
| 353 |
+
logger.warning("[pitcher_selector] failed year=%s: %s", year, exc)
|
| 354 |
+
return []
|
| 355 |
+
|
| 356 |
+
Keep get_pitcher_id_by_name() unchanged as an emergency fallback. Not called in primary flow.
|
| 357 |
+
|
| 358 |
+
---
|
| 359 |
+
Part 5 β card_lab_page.py Changes
|
| 360 |
+
|
| 361 |
+
Hitter branch
|
| 362 |
+
|
| 363 |
+
if card_type == "Hitter":
|
| 364 |
+
hitter_display_names, hitter_display_to_statcast = get_hitter_selector_names(conn, year=selector_year)
|
| 365 |
+
if not hitter_display_names:
|
| 366 |
+
st.info("No hitters found. Run scripts/build_card_lab_season_summaries.py first.")
|
| 367 |
+
return
|
| 368 |
+
|
| 369 |
+
selected_hitter_display = st.selectbox("Player", hitter_display_names, key="cl_player_hitter")
|
| 370 |
+
# statcast_name is the exact name for WHERE player_name = :name
|
| 371 |
+
statcast_name = hitter_display_to_statcast.get(selected_hitter_display) or selected_hitter_display
|
| 372 |
+
player_name = statcast_name # passed to get_player_card_window_df and build_hitter_card_data
|
| 373 |
+
player_name_display = selected_hitter_display # display-only
|
| 374 |
+
|
| 375 |
+
Pitcher branch
|
| 376 |
+
|
| 377 |
+
elif card_type == "Pitcher":
|
| 378 |
+
pitcher_pairs = get_pitcher_selector_names(conn, year=selector_year)
|
| 379 |
+
if not pitcher_pairs:
|
| 380 |
+
st.info("No pitchers found. Run scripts/build_card_lab_season_summaries.py first.")
|
| 381 |
+
return
|
| 382 |
+
|
| 383 |
+
pitcher_display_to_id: dict[str, int | None] = {name: pid for name, pid in pitcher_pairs}
|
| 384 |
+
pitcher_display_names = [name for name, _ in pitcher_pairs] # already sorted
|
| 385 |
+
|
| 386 |
+
selected_pitcher_display = st.selectbox("Player", pitcher_display_names, key="cl_player_pitcher")
|
| 387 |
+
pitcher_id = pitcher_display_to_id[selected_pitcher_display]
|
| 388 |
+
player_name = selected_pitcher_display # display name for poster (injected as literal in pitcher SQL)
|
| 389 |
+
|
| 390 |
+
Initialization block additions
|
| 391 |
+
|
| 392 |
+
# Add to top-level initialization:
|
| 393 |
+
hitter_display_to_statcast: dict[str, str | None] = {}
|
| 394 |
+
pitcher_display_to_id: dict[str, int | None] = {}
|
| 395 |
+
|
| 396 |
+
Import additions
|
| 397 |
+
|
| 398 |
+
from visualization.cards.player_identity import (
|
| 399 |
+
_HITTER_SEASONS_PATH,
|
| 400 |
+
_PITCHER_SEASONS_PATH,
|
| 401 |
+
_IDENTITY_MAP_PATH,
|
| 402 |
+
load_identity_map,
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
---
|
| 406 |
+
Part 6 β Downstream Compatibility Table
|
| 407 |
+
|
| 408 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
|
| 409 |
+
β Function β Current input β New input β Adaptation needed β
|
| 410 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 411 |
+
β get_player_card_window_df(conn, player_name, "Hitter", β raw statcast name β statcast_name from identity map β None β statcast_name IS the β
|
| 412 |
+
β ...) β β (verified exact match) β exact statcast name β
|
| 413 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 414 |
+
β get_player_card_window_df(conn, player_name, "Pitcher", β display name (literal in β same β display name for β None β
|
| 415 |
+
β pitcher_id=...) β SQL), pitcher_id β literal, player_id from Parquet β β
|
| 416 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 417 |
+
β build_hitter_card_data(player_name, windowed_df) β raw statcast name β statcast_name from identity map β None β windowed_df was β
|
| 418 |
+
β β β β fetched by this exact name β
|
| 419 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 420 |
+
β build_pitcher_card_data(player_name, windowed_df) β display name β display name (same) β None β name injected as β
|
| 421 |
+
β β β β literal in pitcher SQL β
|
| 422 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 423 |
+
β get_batter_mlbam_id(conn, player_name, year) β statcast name β statcast_name from identity map β None β
|
| 424 |
+
βββββββοΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 425 |
+
β resolve_player_image(mlbam_id) β pitcher_id / batter_id β player_id from Parquet β None β
|
| 426 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ€
|
| 427 |
+
β _filter_statcast_by_window(statcast_df, player_name, ...) β filters df["player_name"] β same statcast_name β None β DF was fetched by same β
|
| 428 |
+
β (inside card_data.py) β == player_name β β name β
|
| 429 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ
|
| 430 |
+
|
| 431 |
+
No downstream functions need changes. The only change is that the name fed in is now statcast_name (identity-map-verified) instead of a pybaseball-raw name.
|
| 432 |
+
|
| 433 |
+
---
|
| 434 |
+
Part 7 β Collision Handling
|
| 435 |
+
|
| 436 |
+
Implemented in build_player_identity_map.py:
|
| 437 |
+
|
| 438 |
+
1. After building all identity records, find groups sharing the same canonical_name_normalized with different player_id values.
|
| 439 |
+
2. Within each collision group, sort by player_id (ascending).
|
| 440 |
+
3. First record: display_name = canonical_name (no suffix)
|
| 441 |
+
4. Subsequent records: display_name = f"{canonical_name} ({player_id})" if player_id is not None, else f"{canonical_name} [?]"
|
| 442 |
+
5. This is deterministic (stable sort), collision-safe (each display_name is unique within a role/season), and human-readable.
|
| 443 |
+
6. Real-world expectation: very rare (< 5 cases in a full MLB season history). Most "same name" cases across history are the same player.
|
| 444 |
+
|
| 445 |
+
---
|
| 446 |
+
Part 8 β Debug Expander (card_lab_page.py)
|
| 447 |
+
|
| 448 |
+
with st.expander("Debug", expanded=False):
|
| 449 |
+
st.write(f"**Active card type:** {card_type}")
|
| 450 |
+
|
| 451 |
+
# File existence checks
|
| 452 |
+
st.write(f"**Identity map exists:** {_IDENTITY_MAP_PATH.exists()}")
|
| 453 |
+
st.write(f"**Pitcher seasons exists:** {_PITCHER_SEASONS_PATH.exists()}")
|
| 454 |
+
st.write(f"**Hitter seasons exists:** {_HITTER_SEASONS_PATH.exists()}")
|
| 455 |
+
|
| 456 |
+
if card_type == "Pitcher":
|
| 457 |
+
st.write("**Source:** data/card_lab_pitcher_seasons.parquet (pybaseball pitching_stats)")
|
| 458 |
+
st.write(f"**Season:** {selector_year}")
|
| 459 |
+
st.write("**Workload filter:** IP > 0")
|
| 460 |
+
st.write(f"**Total names:** {len(pitcher_display_names)}")
|
| 461 |
+
st.write("**First 20:**", pitcher_display_names[:20])
|
| 462 |
+
# Row count in parquet for selected year
|
| 463 |
+
if _PITCHER_SEASONS_PATH.exists():
|
| 464 |
+
pdf = pd.read_parquet(_PITCHER_SEASONS_PATH)
|
| 465 |
+
yr_count = len(pdf[(pdf["IP"] > 0) & (pdf["Season"] == selector_year)]) if selector_year else len(pdf[pdf["IP"] > 0])
|
| 466 |
+
st.write(f"**Parquet rows for year:** {yr_count}")
|
| 467 |
+
# ID coverage audit
|
| 468 |
+
null_id_count = sum(1 for _, pid in pitcher_pairs if pid is None)
|
| 469 |
+
st.write(f"**Players with null pitcher_id:** {null_id_count}")
|
| 470 |
+
# Spot checks
|
| 471 |
+
st.markdown("**Spot checks:**")
|
| 472 |
+
for check in ["Tarik Skubal", "Sonny Gray", "Aaron Nola"]:
|
| 473 |
+
found = [n for n, _ in pitcher_pairs if check.lower() in n.lower()]
|
| 474 |
+
st.write(f" {'β' if found else 'β'} {check}" + (f" ({len(found)} match{'es' if len(found)!=1 else ''})" if found else ""))
|
| 475 |
+
|
| 476 |
+
elif card_type == "Hitter":
|
| 477 |
+
st.write("**Source:** data/card_lab_hitter_seasons.parquet (pybaseball batting_stats)")
|
| 478 |
+
st.write(f"**Season:** {selector_year}")
|
| 479 |
+
st.write("**Workload filter:** AB > 0")
|
| 480 |
+
st.write(f"**Total names:** {len(hitter_display_names)}")
|
| 481 |
+
st.write("**First 20:**", hitter_display_names[:20])
|
| 482 |
+
# Row count in parquet for selected year
|
| 483 |
+
if _HITTER_SEASONS_PATH.exists():
|
| 484 |
+
hdf = pd.read_parquet(_HITTER_SEASONS_PATH)
|
| 485 |
+
yr_count = len(hdf[(hdf["AB"] > 0) & (hdf["Season"] == selector_year)]) if selector_year else len(hdf[hdf["AB"] > 0])
|
| 486 |
+
st.write(f"**Parquet rows for year:** {yr_count}")
|
| 487 |
+
# All shown hitters have statcast_name (nulls excluded at build time)
|
| 488 |
+
st.write(f"**statcast_name coverage:** all {len(hitter_display_names)} shown rows resolved (null rows excluded at build)")
|
| 489 |
+
# Spot checks
|
| 490 |
+
names_lc = {n.lower() for n in hitter_display_names}
|
| 491 |
+
st.markdown("**Spot checks (hitters β, pitchers β):**")
|
| 492 |
+
for check, expect_present in [
|
| 493 |
+
("Lars Nootbaar", True), ("Alec Burleson", True),
|
| 494 |
+
("Freddy Peralta", False), ("Tarik Skubal", False),
|
| 495 |
+
]:
|
| 496 |
+
found = any(check.lower() in n for n in names_lc)
|
| 497 |
+
ok = found == expect_present
|
| 498 |
+
st.write(f" {'β' if ok else 'β'} {check} β {'present' if found else 'absent'}")
|
| 499 |
+
|
| 500 |
+
if card_type in ("Hitter", "Pitcher"):
|
| 501 |
+
st.write(f"**MLBAM ID:** {mlbam_id}")
|
| 502 |
+
st.write(f"**Image fetched:** {player_pil is not None}")
|
| 503 |
+
|
| 504 |
+
---
|
| 505 |
+
Part 9 β requirements.txt
|
| 506 |
+
|
| 507 |
+
Add pybaseball (build scripts only, not Streamlit runtime dependency):
|
| 508 |
+
pybaseball
|
| 509 |
+
|
| 510 |
+
---
|
| 511 |
+
Part 10 β Acceptance Criteria
|
| 512 |
+
|
| 513 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 514 |
+
β Check β Method β
|
| 515 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 516 |
+
β Tarik Skubal in pitcher selector β Debug β spot check β
|
| 517 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 518 |
+
β Sonny Gray in pitcher selector β Debug β spot check β
|
| 519 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 520 |
+
β Aaron Nola once (unless genuine collision) β Debug shows "1 match" β
|
| 521 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 522 |
+
β Freddy Peralta absent from hitter selector β 0 AB β filtered β
|
| 523 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 524 |
+
β Tarik Skubal absent from hitter selector β 0 AB β filtered β
|
| 525 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 526 |
+
β Lars Nootbaar / Burleson in hitter selector β Debug β spot check β
|
| 527 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 528 |
+
β "Skub" search finds Skubal β Streamlit selectbox substring search β
|
| 529 |
+
ββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 530 |
+
β Hitter card generates without empty data β Full flow test: select Alec Burleson, generate card β
|
| 531 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 532 |
+
β Pitcher card generates without empty data β Full flow test: select Skubal, generate card β
|
| 533 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 534 |
+
β No duplicate display names per role/year β Debug duplicate audit β
|
| 535 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 536 |
+
β pitcher_id resolves from Parquet, no second DB call β Code review: no get_pitcher_id_by_name() in normal flow β
|
| 537 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 538 |
+
β debug shows source file, season, workload filter, count β Visual check in debug expander β
|
| 539 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 540 |
+
|
| 541 |
+
---
|
| 542 |
+
Build Order
|
| 543 |
+
|
| 544 |
+
Run locally with DB access. Commit outputs to repo. Streamlit reads committed files at runtime.
|
| 545 |
+
|
| 546 |
+
# Requires: pybaseball installed + DB connection available (uses database.remote_db.get_connection())
|
| 547 |
+
1. python scripts/build_player_identity_map.py --seasons 2021 2022 2023 2024 2025
|
| 548 |
+
# β data/player_identity_map.parquet (with statcast_name enrichment from DB β REQUIRED for hitters)
|
| 549 |
+
2. python scripts/build_card_lab_season_summaries.py --seasons 2021 2022 2023 2024 2025
|
| 550 |
+
# β data/card_lab_pitcher_seasons.parquet
|
| 551 |
+
# β data/card_lab_hitter_seasons.parquet (null statcast_name rows EXCLUDED)
|
| 552 |
+
3. python scripts/validate_card_lab_build.py
|
| 553 |
+
# β must show all PASS before proceeding
|
| 554 |
+
4. git add data/player_identity_map.parquet data/card_lab_pitcher_seasons.parquet data/card_lab_hitter_seasons.parquet
|
| 555 |
+
5. git commit -m "rebuild card lab season summary parquets"
|
| 556 |
+
6. Push β Streamlit / HuggingFace reads committed Parquet files
|
| 557 |
+
|
| 558 |
+
---
|
| 559 |
+
Risk List
|
| 560 |
+
|
| 561 |
+
βββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 562 |
+
β Risk β Mitigation β
|
| 563 |
+
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 564 |
+
β pybaseball API unavailable at β Build is offline; retry manually β
|
| 565 |
+
β build time β β
|
| 566 |
+
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 567 |
+
β statcast_name cannot be β Row is EXCLUDED from card_lab_hitter_seasons.parquet at build time β no runtime fallback allowed. β
|
| 568 |
+
β resolved for a hitter β validate_card_lab_build.py reports excluded rows. Fix: re-run enrichment, check for name spelling differences, or β
|
| 569 |
+
β β manually add statcast_name to identity map. β
|
| 570 |
+
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 571 |
+
β MLBAM ID not found in β pitcher_id = None; fallback to get_pitcher_id_by_name() for pitchers; hitter image skipped gracefully β
|
| 572 |
+
β playerid_reverse_lookup β β
|
| 573 |
+
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 574 |
+
β Parquet files missing at β Explicit file-existence check in debug; st.info with "Run build script" message instead of crash β
|
| 575 |
+
β runtime β β
|
| 576 |
+
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 577 |
+
β Genuine name collision β Handled deterministically with player_id suffix in display_name β
|
| 578 |
+
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
|
| 579 |
+
β Two-way name format mismatch β normalize_for_matching() handles Last/First, accents, suffixes, whitespace β
|
| 580 |
+
βββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 581 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 582 |
+
|
| 583 |
+
Claude has written up a plan and is ready to execute. Would you like to proceed?
|
| 584 |
+
|
| 585 |
+
β― 1. Yes, clear context (59% used) and auto-accept edits
|
| 586 |
+
2. Yes, auto-accept edits
|
| 587 |
+
3. Yes, manually approve edits
|
| 588 |
+
4. Type here to tell Claude what to change
|