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
+
"""Performance sous pression (matchs décisifs) - ISO 5055/5259.
|
| 2 |
+
|
| 3 |
+
Ce module implémente le calcul de la performance sous pression.
|
| 4 |
+
Feature psychologique: performance en fin de saison ou matchs serrés.
|
| 5 |
+
|
| 6 |
+
Matchs décisifs (SEUILS DOCUMENTÉS ISO 5259):
|
| 7 |
+
- Dernière ronde (ronde >= 7): Format interclubs FFE standard 7-11 rondes.
|
| 8 |
+
Les 3 dernières rondes sont généralement décisives pour le classement final.
|
| 9 |
+
Référence: Règlement FFE A02, format compétition interclubs.
|
| 10 |
+
- Score serré (écart <= 1 point): Match où chaque partie compte.
|
| 11 |
+
Justification: pression psychologique maximale quand résultat incertain.
|
| 12 |
+
|
| 13 |
+
Clutch factor (SEUILS DOCUMENTÉS):
|
| 14 |
+
- > 0.1: "clutch" - joueur surperforme sous pression
|
| 15 |
+
- < -0.1: "choke" - joueur sous-performe sous pression
|
| 16 |
+
- [-0.1, 0.1]: "stable" - pas d'effet significatif
|
| 17 |
+
Seuil 0.1 = ~10% de différence de score, significatif statistiquement.
|
| 18 |
+
|
| 19 |
+
Sources:
|
| 20 |
+
- AI Sports Predictions 2025 (ainewshub.org)
|
| 21 |
+
- Sports Prediction PMC - "Psychological factors in sports"
|
| 22 |
+
- Règlement FFE A02 - Format interclubs
|
| 23 |
+
|
| 24 |
+
Conformité:
|
| 25 |
+
- ISO 5055: Module <300 lignes, responsabilité unique
|
| 26 |
+
- ISO 5259: Features calculées depuis données réelles, seuils documentés
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
from __future__ import annotations
|
| 30 |
+
|
| 31 |
+
import logging
|
| 32 |
+
|
| 33 |
+
import numpy as np
|
| 34 |
+
import pandas as pd
|
| 35 |
+
|
| 36 |
+
logger = logging.getLogger(__name__)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def calculate_pressure_performance(df: pd.DataFrame, min_games: int = 3) -> pd.DataFrame:
|
| 40 |
+
"""Calcule la performance sous pression (matchs décisifs).
|
| 41 |
+
|
| 42 |
+
Feature psychologique: performance en fin de saison ou matchs serrés.
|
| 43 |
+
|
| 44 |
+
Matchs décisifs:
|
| 45 |
+
- Dernière ronde (ronde >= 7)
|
| 46 |
+
- Score serré (écart <= 1 point)
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
----
|
| 50 |
+
df: DataFrame échiquiers
|
| 51 |
+
min_games: Minimum de matchs décisifs
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
-------
|
| 55 |
+
DataFrame avec colonnes:
|
| 56 |
+
- joueur_nom: nom joueur
|
| 57 |
+
- score_normal: performance matchs normaux
|
| 58 |
+
- score_pression: performance matchs décisifs
|
| 59 |
+
- nb_pression: nombre matchs décisifs
|
| 60 |
+
- clutch_factor: score_pression - score_normal
|
| 61 |
+
- pressure_type: 'clutch' (>0.1), 'choke' (<-0.1), 'stable'
|
| 62 |
+
|
| 63 |
+
ISO 5259: Performance pression depuis contexte réel.
|
| 64 |
+
"""
|
| 65 |
+
logger.info("Calcul performance sous pression...")
|
| 66 |
+
|
| 67 |
+
if df.empty:
|
| 68 |
+
return pd.DataFrame()
|
| 69 |
+
|
| 70 |
+
parties = _prepare_pressure_df(df)
|
| 71 |
+
pressure_stats = _collect_pressure_stats(parties)
|
| 72 |
+
result = _build_pressure_result(pressure_stats, min_games)
|
| 73 |
+
|
| 74 |
+
logger.info(f" {len(result)} joueurs avec stats pression")
|
| 75 |
+
return result
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _prepare_pressure_df(df: pd.DataFrame) -> pd.DataFrame:
|
| 79 |
+
"""Prepare le DataFrame avec flag decisif."""
|
| 80 |
+
parties = df[
|
| 81 |
+
~df["type_resultat"].isin(["non_joue", "forfait_blanc", "forfait_noir", "double_forfait"])
|
| 82 |
+
].copy()
|
| 83 |
+
parties["is_decisive"] = parties.apply(_is_decisive, axis=1)
|
| 84 |
+
return parties
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def _is_decisive(row: pd.Series) -> bool:
|
| 88 |
+
"""Determine si un match est decisif."""
|
| 89 |
+
ronde = row.get("ronde", 0)
|
| 90 |
+
score_dom = row.get("score_dom", 0)
|
| 91 |
+
score_ext = row.get("score_ext", 0)
|
| 92 |
+
return ronde >= 7 or abs(score_dom - score_ext) <= 1
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _collect_pressure_stats(parties: pd.DataFrame) -> dict[str, dict[str, list[float]]]:
|
| 96 |
+
"""Collecte les stats de pression par joueur."""
|
| 97 |
+
pressure_stats: dict[str, dict[str, list[float]]] = {}
|
| 98 |
+
|
| 99 |
+
for couleur in ["blanc", "noir"]:
|
| 100 |
+
nom_col, res_col = f"{couleur}_nom", f"resultat_{couleur}"
|
| 101 |
+
if nom_col not in parties.columns or res_col not in parties.columns:
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
for _, row in parties.iterrows():
|
| 105 |
+
joueur = str(row[nom_col])
|
| 106 |
+
if joueur not in pressure_stats:
|
| 107 |
+
pressure_stats[joueur] = {"normal": [], "pressure": []}
|
| 108 |
+
|
| 109 |
+
key = "pressure" if row["is_decisive"] else "normal"
|
| 110 |
+
pressure_stats[joueur][key].append(row[res_col])
|
| 111 |
+
|
| 112 |
+
return pressure_stats
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def _build_pressure_result(
|
| 116 |
+
pressure_stats: dict[str, dict[str, list[float]]], min_games: int
|
| 117 |
+
) -> pd.DataFrame:
|
| 118 |
+
"""Construit le DataFrame resultat."""
|
| 119 |
+
result_data = []
|
| 120 |
+
for joueur, stats in pressure_stats.items():
|
| 121 |
+
entry = _compute_player_pressure(joueur, stats, min_games)
|
| 122 |
+
if entry:
|
| 123 |
+
result_data.append(entry)
|
| 124 |
+
return pd.DataFrame(result_data)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def _compute_player_pressure(joueur: str, stats: dict, min_games: int) -> dict | None:
|
| 128 |
+
"""Calcule les stats de pression d'un joueur."""
|
| 129 |
+
nb_pressure, nb_normal = len(stats["pressure"]), len(stats["normal"])
|
| 130 |
+
if nb_pressure < min_games or nb_normal < min_games:
|
| 131 |
+
return None
|
| 132 |
+
|
| 133 |
+
score_normal = np.mean(stats["normal"])
|
| 134 |
+
score_pressure = np.mean(stats["pressure"])
|
| 135 |
+
clutch = score_pressure - score_normal
|
| 136 |
+
ptype = _classify_pressure_type(clutch)
|
| 137 |
+
|
| 138 |
+
return {
|
| 139 |
+
"joueur_nom": joueur,
|
| 140 |
+
"score_normal": score_normal,
|
| 141 |
+
"score_pression": score_pressure,
|
| 142 |
+
"nb_normal": nb_normal,
|
| 143 |
+
"nb_pression": nb_pressure,
|
| 144 |
+
"clutch_factor": clutch,
|
| 145 |
+
"pressure_type": ptype,
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def _classify_pressure_type(clutch: float) -> str:
|
| 150 |
+
"""Classifie le type de pression."""
|
| 151 |
+
if clutch > 0.1:
|
| 152 |
+
return "clutch"
|
| 153 |
+
if clutch < -0.1:
|
| 154 |
+
return "choke"
|
| 155 |
+
return "stable"
|