116 lines
5.6 KiB
Python
116 lines
5.6 KiB
Python
"""Ce que rendent les outils de lecture sur une plage : une page HTML (texte imprimé + PNG).
|
|
|
|
Usage : python3 aiguiser/voir.py data/NORDEND/ --debut 2026-09-01T00:00Z --fin 2026-09-04T00:00Z
|
|
[--capteurs A,B,C] [--sortie D]
|
|
|
|
Lance, avec les réglages du skill lire-les-donnees, inventaire, ensemble, resume, graphique et vignettes.
|
|
La page est rangée dans out/voir/<STATION>_<début>_<fin>_<version des outils>/ ; la version est le hash
|
|
git de agent-tools/ (« +modif » si le dossier a des changements non commités) : deux pages de
|
|
versions différentes se comparent côte à côte. Sans --capteurs, les vignettes montrent les 3 capteurs qui
|
|
s'écartent le plus de leur niveau habituel (error_value, en MAD) et le plus calme comme témoin.
|
|
Rend ce que les outils rendent, aucun verdict.
|
|
"""
|
|
import argparse
|
|
import html
|
|
import os
|
|
import re
|
|
import statistics
|
|
import subprocess
|
|
import sys
|
|
|
|
RACINE = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
|
OUTILS = os.path.join(RACINE, "agent-tools")
|
|
sys.path.insert(0, OUTILS)
|
|
from donnees import charger, est_reference, filtrer, heure_image, instant, iso, trouver # noqa: E402
|
|
|
|
PYTHON = os.path.join(RACINE, ".venv", "bin", "python")
|
|
if not os.path.exists(PYTHON):
|
|
PYTHON = sys.executable
|
|
NB_IMAGES = 5
|
|
|
|
|
|
def version_outils():
|
|
sh = lambda *a: subprocess.run(["git", "-C", RACINE, *a], capture_output=True, text=True).stdout.strip()
|
|
h = sh("rev-parse", "--short", "HEAD:agent-tools")
|
|
return h + ("+modif" if sh("status", "--porcelain", "--", "agent-tools") else "")
|
|
|
|
|
|
def ecarts(par, debut, fin):
|
|
"""Capteur -> plus grand écart d'error_value à sa médiane, en MAD, sur la fenêtre."""
|
|
out = {}
|
|
for s, ms in par.items():
|
|
v = [m["error_value"] for m in filtrer(ms, debut, fin) if m["error_value"] is not None]
|
|
if len(v) > 2:
|
|
med = statistics.median(v)
|
|
mad = statistics.median(abs(x - med) for x in v) or 1e-9
|
|
out[s] = max(abs(x - med) for x in v) / mad
|
|
return out
|
|
|
|
|
|
def vignettes(par, dossier, debut, fin, capteurs):
|
|
heures = sorted(h for h in map(heure_image, trouver(dossier)["images"]) if h and debut <= h <= fin)
|
|
if not heures:
|
|
return None
|
|
pas = max(len(heures) // NB_IMAGES, 1)
|
|
heures = heures[::pas][:NB_IMAGES]
|
|
if not capteurs:
|
|
e = ecarts(par, debut, fin)
|
|
classes = sorted(e, key=e.get)
|
|
capteurs = classes[::-1][:3] + classes[:1]
|
|
return ["--capteurs", ",".join(capteurs), "--heures", ",".join(iso(h) for h in heures)]
|
|
|
|
|
|
def main():
|
|
a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
a.add_argument("dossier")
|
|
a.add_argument("--debut", required=True, type=instant)
|
|
a.add_argument("--fin", required=True, type=instant)
|
|
a.add_argument("--capteurs", help="capteurs des vignettes, séparés par des virgules")
|
|
a.add_argument("--sortie", help="dossier de la page (défaut : out/voir/…)")
|
|
o = a.parse_args()
|
|
|
|
par = charger(o.dossier)
|
|
if not par:
|
|
sys.exit(f"Aucun export « advanced » dans {o.dossier}.")
|
|
station = os.path.basename(os.path.normpath(o.dossier))
|
|
version = version_outils()
|
|
sortie = o.sortie or os.path.join(RACINE, "out", "voir", f"{station}_{re.sub(r'[^0-9TZ]', '', iso(o.debut))}_"
|
|
f"{re.sub(r'[^0-9TZ]', '', iso(o.fin))}_{version.replace('+', '-')}")
|
|
os.makedirs(sortie, exist_ok=True)
|
|
fenetre = ["--debut", iso(o.debut), "--fin", iso(o.fin)]
|
|
cmds = [
|
|
("inventaire", [PYTHON, f"{OUTILS}/inventaire.py", o.dossier, *fenetre]),
|
|
("ensemble", [PYTHON, f"{OUTILS}/ensemble.py", o.dossier, *fenetre, "--sortie", sortie]),
|
|
("resume error_value, 6 h", [PYTHON, f"{OUTILS}/resume.py", o.dossier, *fenetre, "--niveau", "fenetre", "--pas", "6h"]),
|
|
("graphique", [PYTHON, f"{OUTILS}/graphique.py", o.dossier, *fenetre, "--sortie", sortie]),
|
|
]
|
|
v = vignettes(par, o.dossier, o.debut, o.fin, o.capteurs.split(",") if o.capteurs else None)
|
|
if v:
|
|
cmds.append(("vignettes", [PYTHON, f"{OUTILS}/vignettes.py", o.dossier, *v, "--sortie", sortie]))
|
|
|
|
corps = []
|
|
for titre, cmd in cmds:
|
|
r = subprocess.run(cmd, capture_output=True, text=True, cwd=RACINE)
|
|
texte = (r.stdout + r.stderr).strip()
|
|
pngs = [os.path.relpath(l, sortie) for l in r.stdout.splitlines() if l.endswith(".png") and os.path.exists(l)]
|
|
texte = "\n".join(l for l in texte.splitlines() if not (l.endswith(".png") and os.path.exists(l)))
|
|
corps.append(f"<h2>{html.escape(titre)}{'' if r.returncode == 0 else ' (échec)'}</h2>"
|
|
f"<p class=cmd>{html.escape(' '.join(os.path.relpath(c, RACINE) if os.path.isabs(c) and c.startswith(RACINE) else c for c in cmd[1:]))}</p>"
|
|
f"<pre>{html.escape(texte)}</pre>" + "".join(f'<img src="{html.escape(p)}">' for p in pngs))
|
|
if not v:
|
|
corps.append("<h2>vignettes</h2><pre>Pas d'image dans la fenêtre.</pre>")
|
|
|
|
page = (f"<!doctype html><meta charset=utf-8><title>{station} : outils de lecture</title>"
|
|
"<style>body{font:14px sans-serif;margin:1em 2em}pre{background:#f4f4f4;padding:.6em;overflow:auto;max-height:28em}"
|
|
"img{max-width:100%;display:block;margin:.6em 0}.cmd{color:#666;font:12px monospace}</style>"
|
|
f"<h1>{station} : ce que rendent les outils</h1>"
|
|
f"<p>{iso(o.debut)} → {iso(o.fin)} (UTC) · outils <b>{version}</b></p>" + "".join(corps))
|
|
chemin = os.path.join(sortie, "index.html")
|
|
with open(chemin, "w") as f:
|
|
f.write(page)
|
|
print(chemin)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|