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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Urgence mathématique (CE) - ISO 5055/5259.
|
| 2 |
+
|
| 3 |
+
Ce module calcule l'urgence mathématique pour le Composition Engine.
|
| 4 |
+
Détermine si les objectifs sont encore atteignables mathématiquement.
|
| 5 |
+
|
| 6 |
+
Features (DOCUMENTÉES ISO 5259):
|
| 7 |
+
- montee_possible: bool - L'équipe peut-elle encore monter?
|
| 8 |
+
Calcul: points_actuels + (rondes_restantes * 2) >= points_1er
|
| 9 |
+
- maintien_assure: bool - L'équipe ne peut plus descendre?
|
| 10 |
+
Calcul: points_actuels > points_releguable + (rondes_restantes * 2)
|
| 11 |
+
- urgence_level: Classification de l'urgence
|
| 12 |
+
- 'critique': Dernier match pour sauver objectif
|
| 13 |
+
- 'haute': 2-3 matchs pour atteindre objectif
|
| 14 |
+
- 'normale': Situation contrôlée
|
| 15 |
+
- 'aucune': Objectif assuré ou abandonné
|
| 16 |
+
|
| 17 |
+
Justification calculs:
|
| 18 |
+
- 2 pts max par match (victoire)
|
| 19 |
+
- Formule standard de mathématiques sportives
|
| 20 |
+
|
| 21 |
+
Conformité:
|
| 22 |
+
- ISO 5055: Module <300 lignes, responsabilité unique
|
| 23 |
+
- ISO 5259: Features depuis données réelles, calculs documentés
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
from __future__ import annotations
|
| 27 |
+
|
| 28 |
+
import logging
|
| 29 |
+
|
| 30 |
+
import pandas as pd
|
| 31 |
+
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def calculate_urgency_features(
|
| 36 |
+
standings: pd.DataFrame,
|
| 37 |
+
ronde_actuelle: int,
|
| 38 |
+
nb_rondes_total: int = 9,
|
| 39 |
+
) -> pd.DataFrame:
|
| 40 |
+
"""Calcule l'urgence mathématique par équipe.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
----
|
| 44 |
+
standings: DataFrame classement avec colonnes:
|
| 45 |
+
- equipe, position, points_cumules, nb_equipes
|
| 46 |
+
ronde_actuelle: Numéro de la ronde actuelle
|
| 47 |
+
nb_rondes_total: Nombre total de rondes (défaut: 9 pour interclubs)
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
-------
|
| 51 |
+
DataFrame avec colonnes:
|
| 52 |
+
- equipe: nom équipe
|
| 53 |
+
- saison: saison concernée
|
| 54 |
+
- montee_possible: bool
|
| 55 |
+
- maintien_assure: bool
|
| 56 |
+
- rondes_restantes: int
|
| 57 |
+
- urgence_level: 'critique', 'haute', 'normale', 'aucune'
|
| 58 |
+
- points_max_possibles: int (points actuels + max restant)
|
| 59 |
+
|
| 60 |
+
ISO 5259: Urgence calculée depuis mathématiques du classement.
|
| 61 |
+
"""
|
| 62 |
+
logger.info(f"Calcul urgence mathématique (ronde {ronde_actuelle}/{nb_rondes_total})...")
|
| 63 |
+
|
| 64 |
+
if standings.empty:
|
| 65 |
+
return pd.DataFrame()
|
| 66 |
+
|
| 67 |
+
rondes_restantes = max(0, nb_rondes_total - ronde_actuelle)
|
| 68 |
+
points_max_restants = rondes_restantes * 2 # 2 pts par victoire
|
| 69 |
+
|
| 70 |
+
# Calculer points du 1er et du relégable
|
| 71 |
+
points_premier = standings["points_cumules"].max() if not standings.empty else 0
|
| 72 |
+
|
| 73 |
+
# Relégable = avant-dernier ou dernier selon nb équipes
|
| 74 |
+
standings_sorted = standings.sort_values("points_cumules", ascending=True)
|
| 75 |
+
if len(standings_sorted) >= 2:
|
| 76 |
+
points_releguable = standings_sorted.iloc[1]["points_cumules"]
|
| 77 |
+
else:
|
| 78 |
+
points_releguable = 0
|
| 79 |
+
|
| 80 |
+
urgency_data = []
|
| 81 |
+
|
| 82 |
+
for _, row in standings.iterrows():
|
| 83 |
+
equipe = row["equipe"]
|
| 84 |
+
saison = row.get("saison", 2025)
|
| 85 |
+
points = row["points_cumules"]
|
| 86 |
+
position = row["position"]
|
| 87 |
+
nb_equipes = row["nb_equipes"]
|
| 88 |
+
|
| 89 |
+
# Points max possibles
|
| 90 |
+
points_max = points + points_max_restants
|
| 91 |
+
|
| 92 |
+
# Montée possible?
|
| 93 |
+
# Peut-on atteindre ou dépasser le 1er?
|
| 94 |
+
montee_possible = points_max >= points_premier or position <= 2
|
| 95 |
+
|
| 96 |
+
# Maintien assuré?
|
| 97 |
+
# Est-on suffisamment au-dessus du relégable?
|
| 98 |
+
if position <= nb_equipes - 2:
|
| 99 |
+
# Pas en zone rouge
|
| 100 |
+
marge = points - points_releguable
|
| 101 |
+
maintien_assure = marge > points_max_restants
|
| 102 |
+
else:
|
| 103 |
+
# En zone rouge
|
| 104 |
+
maintien_assure = False
|
| 105 |
+
|
| 106 |
+
# Niveau d'urgence
|
| 107 |
+
urgence = _classify_urgency(
|
| 108 |
+
rondes_restantes=rondes_restantes,
|
| 109 |
+
montee_possible=montee_possible,
|
| 110 |
+
maintien_assure=maintien_assure,
|
| 111 |
+
position=position,
|
| 112 |
+
nb_equipes=nb_equipes,
|
| 113 |
+
ecart_premier=row.get("ecart_premier", 0),
|
| 114 |
+
ecart_dernier=row.get("ecart_dernier", 0),
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
urgency_data.append(
|
| 118 |
+
{
|
| 119 |
+
"equipe": equipe,
|
| 120 |
+
"saison": saison,
|
| 121 |
+
"ronde": ronde_actuelle,
|
| 122 |
+
"position": position,
|
| 123 |
+
"points_cumules": points,
|
| 124 |
+
"montee_possible": montee_possible,
|
| 125 |
+
"maintien_assure": maintien_assure,
|
| 126 |
+
"rondes_restantes": rondes_restantes,
|
| 127 |
+
"points_max_possibles": points_max,
|
| 128 |
+
"urgence_level": urgence,
|
| 129 |
+
}
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
result = pd.DataFrame(urgency_data)
|
| 133 |
+
logger.info(f" {len(result)} équipes avec urgence mathématique")
|
| 134 |
+
return result
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def _classify_urgency(
|
| 138 |
+
rondes_restantes: int,
|
| 139 |
+
montee_possible: bool,
|
| 140 |
+
maintien_assure: bool,
|
| 141 |
+
position: int,
|
| 142 |
+
nb_equipes: int,
|
| 143 |
+
ecart_premier: int,
|
| 144 |
+
ecart_dernier: int,
|
| 145 |
+
) -> str:
|
| 146 |
+
"""Classifie le niveau d'urgence.
|
| 147 |
+
|
| 148 |
+
Classification (DOCUMENTÉE ISO 5259):
|
| 149 |
+
- 'critique': Dernière ronde + objectif en jeu (maintien ou montée)
|
| 150 |
+
- 'haute': 2-3 rondes restantes + situation tendue
|
| 151 |
+
- 'normale': Situation contrôlée
|
| 152 |
+
- 'aucune': Objectif assuré ou hors course
|
| 153 |
+
|
| 154 |
+
Returns
|
| 155 |
+
-------
|
| 156 |
+
Niveau d'urgence: 'critique', 'haute', 'normale', 'aucune'
|
| 157 |
+
"""
|
| 158 |
+
if maintien_assure and position > 4:
|
| 159 |
+
return "aucune"
|
| 160 |
+
|
| 161 |
+
en_danger = _is_in_danger(maintien_assure, position, nb_equipes, ecart_dernier)
|
| 162 |
+
en_course = _is_in_race(montee_possible, position, ecart_premier)
|
| 163 |
+
|
| 164 |
+
if rondes_restantes <= 1 and (en_danger or en_course):
|
| 165 |
+
return "critique"
|
| 166 |
+
|
| 167 |
+
if rondes_restantes <= 3 and (en_danger or en_course):
|
| 168 |
+
return "haute"
|
| 169 |
+
|
| 170 |
+
return "normale"
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def _is_in_danger(
|
| 174 |
+
maintien_assure: bool, position: int, nb_equipes: int, ecart_dernier: int
|
| 175 |
+
) -> bool:
|
| 176 |
+
"""Determine si l'equipe est en danger de relegation."""
|
| 177 |
+
if maintien_assure:
|
| 178 |
+
return False
|
| 179 |
+
return position > nb_equipes - 2 or ecart_dernier <= 2
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def _is_in_race(montee_possible: bool, position: int, ecart_premier: int) -> bool:
|
| 183 |
+
"""Determine si l'equipe est en course pour la montee."""
|
| 184 |
+
if montee_possible and position <= 2:
|
| 185 |
+
return True
|
| 186 |
+
return ecart_premier <= 2 and position <= 4
|