Pierrax commited on
Commit
ba69f90
·
verified ·
1 Parent(s): 9fce703

Upload code/scripts/features/player_enrichment.py with huggingface_hub

Browse files
code/scripts/features/player_enrichment.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Player enrichment from joueurs.parquet - ISO 5055/5259.
2
+
3
+ Enrichit les echiquiers avec des donnees joueur depuis joueurs.parquet:
4
+ - elo_type (F/N/E): type de classement FIDE/National/Estime
5
+ - categorie: categorie d'age FFE (U8 -> S65)
6
+ - k_coefficient: coefficient K FIDE (10/20/40) selon FIDE 8.3.3
7
+
8
+ Conformite ISO/IEC:
9
+ - 5055: Module <300 lignes, SRP, fonctions <50 lignes
10
+ - 5259: Qualite donnees ML, enrichissement depuis source officielle
11
+ - 27034: Validation d'entree (Pydantic-style guards)
12
+
13
+ Document ID: ALICE-FEA-ENRICH-001
14
+ Version: 1.0.0
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import logging
20
+ from pathlib import Path
21
+
22
+ import pandas as pd
23
+
24
+ logger = logging.getLogger(__name__)
25
+
26
+ _YOUNG_CATS = frozenset(
27
+ {
28
+ "U8",
29
+ "U8F",
30
+ "U10",
31
+ "U10F",
32
+ "U12",
33
+ "U12F",
34
+ "U14",
35
+ "U14F",
36
+ "U16",
37
+ "U16F",
38
+ "U18",
39
+ "U18F",
40
+ }
41
+ )
42
+
43
+
44
+ def enrich_from_joueurs(df_split: pd.DataFrame, joueurs_path: Path) -> None: # noqa: D417
45
+ """Ajoute elo_type, categorie, k_coefficient depuis joueurs.parquet (in-place).
46
+
47
+ Join par nom_complet <-> blanc_nom / noir_nom.
48
+ Vectorise pour eviter apply(axis=1) sur 3.5M lignes.
49
+
50
+ Args:
51
+ ----
52
+ df_split: DataFrame echiquiers (modifie in-place)
53
+ joueurs_path: Path — chemin absolu vers joueurs.parquet
54
+
55
+ ISO 5259: Enrichissement depuis source officielle FFE.
56
+ """
57
+ if not joueurs_path.exists():
58
+ logger.warning("joueurs.parquet non trouve: %s — skip enrichissement", joueurs_path)
59
+ return
60
+
61
+ joueur_map = _load_joueur_map(joueurs_path)
62
+ if joueur_map.empty:
63
+ return
64
+
65
+ for color in ("blanc", "noir"):
66
+ _enrich_color(df_split, joueur_map, color)
67
+
68
+ logger.info(" Enrichissement joueurs: elo_type, categorie, k_coefficient (blanc + noir)")
69
+
70
+
71
+ def _load_joueur_map(joueurs_path: Path) -> pd.DataFrame:
72
+ """Charge le mapping joueur depuis joueurs.parquet."""
73
+ try:
74
+ joueurs = pd.read_parquet(
75
+ joueurs_path,
76
+ columns=["nom_complet", "elo_type", "categorie"],
77
+ )
78
+ except Exception as exc: # noqa: BLE001
79
+ logger.error("Erreur lecture joueurs.parquet: %s", exc)
80
+ return pd.DataFrame()
81
+
82
+ return joueurs.drop_duplicates("nom_complet").set_index("nom_complet")
83
+
84
+
85
+ def _enrich_color(
86
+ df_split: pd.DataFrame,
87
+ joueur_map: pd.DataFrame,
88
+ color: str,
89
+ ) -> None:
90
+ """Enrichit les colonnes pour une couleur (blanc ou noir)."""
91
+ nom_col = f"{color}_nom"
92
+ elo_col = f"{color}_elo"
93
+
94
+ if nom_col not in df_split.columns:
95
+ return
96
+
97
+ df_split[f"elo_type_{color}"] = df_split[nom_col].map(joueur_map["elo_type"]).fillna("")
98
+ df_split[f"categorie_{color}"] = df_split[nom_col].map(joueur_map["categorie"]).fillna("")
99
+
100
+ if elo_col not in df_split.columns:
101
+ df_split[f"k_coefficient_{color}"] = 20
102
+ return
103
+
104
+ _compute_k_vectorized(df_split, color, elo_col)
105
+
106
+
107
+ def _compute_k_vectorized(
108
+ df_split: pd.DataFrame,
109
+ color: str,
110
+ elo_col: str,
111
+ ) -> None:
112
+ """Calcule k_coefficient via operations vectorisees (FIDE 8.3.3).
113
+
114
+ Ordre d'application:
115
+ 1. Defaut = 20
116
+ 2. Si elo >= 2400 → 10 (prend precedence sur jeune)
117
+ 3. Si categorie jeune ET elo < 2300 → 40
118
+ """
119
+ cat_col = f"categorie_{color}"
120
+ k_col = f"k_coefficient_{color}"
121
+
122
+ elo = df_split[elo_col].fillna(0)
123
+
124
+ df_split[k_col] = 20
125
+ # K=40: joueur jeune avec elo < 2300
126
+ is_young = df_split[cat_col].isin(_YOUNG_CATS)
127
+ df_split.loc[is_young & (elo < 2300), k_col] = 40
128
+ # K=10: elite (prend precedence)
129
+ df_split.loc[elo >= 2400, k_col] = 10