Add the diagnostic agent's numeric summary tool (D-046)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
80d54a688f
commit
f33f09dbf6
1 file changed
+90
@@ -0,0 +1,90 @@
|
||||
"""Résumé chiffré compact des séries d'un export « advanced », pour l'agent de diagnostic.
|
||||
|
||||
Usage : python3 agents/diagnostic/outils/resume.py data/PS_DES_COUDRES/ [--colonne error_value]
|
||||
[--stat median|min|max] [--pas 1h] [--capteurs REF_G,REF_D] [--debut 2026-10-13T00:00Z] [--fin ...]
|
||||
|
||||
1. Niveau habituel de chaque capteur sur toute la période : médiane, MAD (écart absolu médian), min, max.
|
||||
2. Tableau : une ligne par pas de temps (début du pas, UTC), une colonne par capteur, la statistique
|
||||
choisie sur les mesures du pas. « rej » : toutes les mesures du pas sont rejetées ; « - » : aucune mesure.
|
||||
Rend des valeurs, pas de verdict.
|
||||
"""
|
||||
import argparse
|
||||
import re
|
||||
import statistics
|
||||
import sys
|
||||
from datetime import timedelta
|
||||
|
||||
from donnees import charger, est_reference, filtrer, instant, iso, rejetee
|
||||
|
||||
COLONNES = ("error_value", "corr_x", "corr_y", "relative_x", "relative_y", "x", "y", "sigma0", "reproj_err")
|
||||
STATS = {"median": statistics.median, "min": min, "max": max}
|
||||
|
||||
|
||||
def duree(texte):
|
||||
"""'15min', '1h', '6h', '1d' -> timedelta."""
|
||||
m = re.fullmatch(r"(\d+)(min|h|d)", texte)
|
||||
if not m:
|
||||
raise argparse.ArgumentTypeError(f"pas invalide : {texte!r} (ex. 15min, 1h, 1d)")
|
||||
n, unite = int(m.group(1)), m.group(2)
|
||||
return {"min": timedelta(minutes=n), "h": timedelta(hours=n), "d": timedelta(days=n)}[unite]
|
||||
|
||||
|
||||
def valeurs(ms, col):
|
||||
return [m[col] for m in ms if m[col] is not None]
|
||||
|
||||
|
||||
def main():
|
||||
a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
a.add_argument("dossier")
|
||||
a.add_argument("--colonne", default="error_value", choices=COLONNES)
|
||||
a.add_argument("--stat", default="median", choices=STATS)
|
||||
a.add_argument("--pas", default="1h", type=duree)
|
||||
a.add_argument("--capteurs", help="liste séparée par des virgules (défaut : tous)")
|
||||
a.add_argument("--debut", type=instant)
|
||||
a.add_argument("--fin", type=instant)
|
||||
o = a.parse_args()
|
||||
|
||||
par = charger(o.dossier)
|
||||
if not par:
|
||||
sys.exit(f"Aucun export « advanced » dans {o.dossier} (voir inventaire.py).")
|
||||
noms = o.capteurs.split(",") if o.capteurs else sorted(par, key=lambda s: (not est_reference(par[s]), s))
|
||||
inconnus = [s for s in noms if s not in par]
|
||||
if inconnus:
|
||||
sys.exit(f"Capteurs absents : {', '.join(inconnus)}. Présents : {', '.join(sorted(par))}.")
|
||||
|
||||
t0 = min(m["t"] for s in noms for m in par[s]).replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
cases = {}
|
||||
for s in noms:
|
||||
for m in filtrer(par[s], o.debut, o.fin):
|
||||
debut_du_pas = t0 + ((m["t"] - t0) // o.pas) * o.pas
|
||||
cases.setdefault(debut_du_pas, {}).setdefault(s, []).append(m)
|
||||
if not cases:
|
||||
sys.exit("Aucune mesure dans la fenêtre demandée.")
|
||||
|
||||
print(f"Colonne : {o.colonne}. Niveau habituel sur toute la période (mesures non vides) :")
|
||||
print(f"{'capteur':10} {'n':>5} {'médiane':>9} {'MAD':>8} {'min':>9} {'max':>9}")
|
||||
for s in noms:
|
||||
v = valeurs(par[s], o.colonne)
|
||||
if not v:
|
||||
print(f"{s:10} {0:5} (aucune valeur)")
|
||||
continue
|
||||
med = statistics.median(v)
|
||||
mad = statistics.median(abs(x - med) for x in v)
|
||||
print(f"{s:10} {len(v):5} {med:9.4f} {mad:8.4f} {min(v):9.4f} {max(v):9.4f}")
|
||||
|
||||
print(f"\n{o.stat} de {o.colonne} par pas de {o.pas} (début du pas, UTC) :")
|
||||
print(f"{'début':17} " + " ".join(f"{s:>8}" for s in noms))
|
||||
for k in sorted(cases):
|
||||
cellules = []
|
||||
for s in noms:
|
||||
ms = cases[k].get(s, [])
|
||||
v = valeurs(ms, o.colonne)
|
||||
if v:
|
||||
cellules.append(f"{STATS[o.stat](v):8.3f}")
|
||||
else:
|
||||
cellules.append(f"{'rej' if ms and all(map(rejetee, ms)) else '-':>8}")
|
||||
print(f"{iso(k):17} " + " ".join(cellules))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user