diff --git a/.gitignore b/.gitignore index cfb5405..6258147 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .env .env.* out/ +__pycache__/ diff --git a/src/analyse_01.py b/src/analyse_01.py new file mode 100644 index 0000000..6a9026c --- /dev/null +++ b/src/analyse_01.py @@ -0,0 +1,32 @@ +"""Analyse 01 : MA7D_DROP (tendance de BusVoltage en moyenne glissante), graphiques HTML ouverts dans le navigateur. + +Usage : python3 src/analyse_01.py [CSV ou dossiers...] (défaut : data/, sortie dans out/) +Documentation : docs/analyses/analyse_01.md +""" +import importlib.util +import os +import shutil +import subprocess +import sys +import webbrowser +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +VENV = ROOT / ".venv" / "bin" / "python" +if importlib.util.find_spec("plotly") is None and VENV.exists(): + os.execv(VENV, [str(VENV), *sys.argv]) # plotly n'est installé que dans .venv + +import energy_plot # noqa: E402 + + +def open_html(path): + if shutil.which("explorer.exe") and shutil.which("wslpath"): # WSL : navigateur Windows + subprocess.run(["explorer.exe", subprocess.check_output(["wslpath", "-w", path], text=True).strip()]) + else: + webbrowser.open(Path(path).resolve().as_uri()) + + +if __name__ == "__main__": + args = sys.argv[1:] or [str(ROOT / "data")] + for p in energy_plot.main([*args, "--out", str(ROOT / "out")]): + open_html(p) diff --git a/src/energy_check.py b/src/energy_check.py index 66f0814..f2650cc 100644 --- a/src/energy_check.py +++ b/src/energy_check.py @@ -1,6 +1,6 @@ """Analyse énergétique MA7D_DROP : tendance de BusVoltage en moyenne glissante. -Spec : docs/superpowers/specs/2026-10-09-energy-ma7d-design.md +Documentation : docs/analyses/analyse_01.md Bibliothèque standard uniquement (cible : système embarqué de la station). """ import argparse @@ -15,7 +15,7 @@ from datetime import datetime, timedelta HOUR = timedelta(hours=1) -# Constantes de calibration (spec §4.2). +# Constantes de calibration (fiche analyse_01, « Constantes et sources »). WINDOW_H = 168 # multiple de 24 h : annule le cycle jour/nuit ; 72 h ne lisse pas la météo REF_H = 720 # 30 jours : contient le dernier régime sain avant un déficit (choix de conception) MIN_COVERAGE = 0.8 # à 0,5, JUNGFRAU (sain) tombe à -0,19 V en début d'historique ; à 0,8 : -0,13 V @@ -29,8 +29,8 @@ RULE = { "min_coverage": MIN_COVERAGE, "min_ref_points": MIN_REF_POINTS, "threshold_v": THRESHOLD_V, - "threshold_source": "cas de régression FLORIDA, JUNGFRAU, VERMONT, WILDSTRUBEL, MISSISSIPPI (spec §4.3)", - "spec": "docs/superpowers/specs/2026-10-09-energy-ma7d-design.md", + "threshold_source": "cas de régression FLORIDA, JUNGFRAU, VERMONT, WILDSTRUBEL, MISSISSIPPI (fiche analyse_01, « Calibration »)", + "doc": "docs/analyses/analyse_01.md", } NAME_RE = re.compile(r"^(?P[^_]+)_(?P.+?)_\d{4}_\d{2}_\d{2}_\d{2}_\d{2} to .+\.csv$") @@ -78,7 +78,7 @@ def load_stations(paths): def run(series): - """Analyse MA7D_DROP, causale : le point t n'utilise que les données <= t (spec §4.1, §5).""" + """Analyse MA7D_DROP, causale : le point t n'utilise que les données <= t (fiche analyse_01, « Principe »).""" v = series["BusVoltage"] if not v: return {"analysis": RULE["id"], "rule": RULE, "points": []} @@ -124,7 +124,7 @@ def run(series): def periods(points): - """Séquences maximales True/None commençant et finissant par True (spec §7.1).""" + """Séquences maximales True/None commençant et finissant par True.""" spans, cur = [], None for k, p in enumerate(points): if p["anomaly"] is True: diff --git a/src/energy_plot.py b/src/energy_plot.py index ab1ad03..3ef78f1 100644 --- a/src/energy_plot.py +++ b/src/energy_plot.py @@ -1,6 +1,6 @@ """Graphique HTML de l'analyse MA7D_DROP (poste de dev, plotly dans .venv). -Spec : docs/superpowers/specs/2026-10-09-energy-ma7d-design.md §7.2 +Documentation : docs/analyses/analyse_01.md """ import argparse import os @@ -9,6 +9,42 @@ from plotly.subplots import make_subplots import energy_check as ec +# Plotly ne recale pas l'ordonnée sur la plage visible : après chaque zoom ou déplacement horizontal, +# ce script recalcule l'axe vertical de chaque graphique sur les points visibles (5 % de marge), +# en gardant la ligne de seuil visible. Double-clic : retour à la vue complète. +AUTOSCALE_Y_JS = """ +const gd = document.getElementById('{plot_id}'); +const name = (t, a) => a + 'axis' + (t[a + 'axis'] || a).slice(1); +const xs = gd.data.map(t => t.x.map(v => gd._fullLayout[name(t, 'x')].d2l(v))); +gd.on('plotly_relayout', ev => { + const keys = Object.keys(ev); + if (!keys.some(k => k.startsWith('xaxis'))) return; + const upd = {}; + if (keys.some(k => k.endsWith('autorange'))) { + gd.data.forEach(t => { upd[name(t, 'y') + '.autorange'] = true; }); + return Plotly.relayout(gd, upd); + } + const box = {yaxis: [__SEUIL__, __SEUIL__]}; + gd.data.forEach((t, i) => { + if (t.visible === 'legendonly' || t.visible === false) return; + const xa = gd._fullLayout[name(t, 'x')]; + const [a, b] = xa.range.map(r => xa.r2l(r)); + const r = box[name(t, 'y')] || (box[name(t, 'y')] = [Infinity, -Infinity]); + t.y.forEach((y, j) => { + if (y === null || !isFinite(y) || xs[i][j] < a || xs[i][j] > b) return; + r[0] = Math.min(r[0], y); + r[1] = Math.max(r[1], y); + }); + }); + for (const [ya, [lo, hi]] of Object.entries(box)) { + if (lo > hi) continue; + const pad = (hi - lo) * 0.05 || Math.abs(hi) * 0.01 || 1; + upd[ya + '.range'] = [lo - pad, hi + pad]; + } + Plotly.relayout(gd, upd); +}); +""".replace("__SEUIL__", str(ec.THRESHOLD_V)) + def gaps(v): """Plages de créneaux sans BusVoltage : [(premier créneau vide, première valeur suivante)].""" @@ -25,36 +61,44 @@ def gaps(v): def figure(station, series, result, per): - extra = sorted(m for m in series if m != "BusVoltage") - fig = make_subplots(rows=2 + len(extra), cols=1, shared_xaxes=True, vertical_spacing=0.03, - subplot_titles=["BusVoltage [V]", f"Écart {ec.RULE['id']} [V]"] + extra) - v = series["BusVoltage"] + """Haut : l'analyse, courbes superposées. Dessous : une série temporelle par graphique (échelles propres).""" + metrics = ["BusVoltage"] + sorted(m for m in series if m != "BusVoltage") + fig = make_subplots(rows=1 + len(metrics), cols=1, shared_xaxes=True, vertical_spacing=0.02, + row_heights=[1.5] + [1] * len(metrics), + specs=[[{"secondary_y": True}]] + [[{}]] * len(metrics), + subplot_titles=[f"Analyse {ec.RULE['id']}"] + metrics) + + analyse = dict(legendgroup="analyse", legendgrouptitle_text="Analyse") pts = result["points"] x = [p["t"] for p in pts] ref = [p["facts"].get("ref_v") for p in pts] - fig.add_scatter(x=sorted(v), y=[v[t] for t in sorted(v)], name="BusVoltage", - line=dict(width=1, color="lightgray"), row=1, col=1) - fig.add_scatter(x=x, y=[p["facts"].get("ma_v") for p in pts], name="moyenne 7 j", - line=dict(color="royalblue"), row=1, col=1) - fig.add_scatter(x=x, y=ref, name="référence (max 30 j)", - line=dict(color="green", dash="dot"), row=1, col=1) - fig.add_scatter(x=x, y=[None if r is None else r + ec.THRESHOLD_V for r in ref], name="seuil", - line=dict(color="red", dash="dash"), row=1, col=1) hover = [f"anomalie={p['anomaly']}
" + "
".join(f"{k}={val}" for k, val in p["facts"].items()) for p in pts] fig.add_scatter(x=x, y=[p["value"] for p in pts], name="écart", text=hover, - hovertemplate="%{y:.3f} V
%{text}", line=dict(color="black"), row=2, col=1) - fig.add_hline(y=ec.THRESHOLD_V, line=dict(color="red", dash="dash"), row=2, col=1) - for a, b in gaps(v): - fig.add_vrect(x0=a, x1=b, fillcolor="gray", opacity=0.3, line_width=0, row=1, col=1) + hovertemplate="%{y:.3f} V
%{text}", line=dict(color="black"), row=1, col=1, **analyse) + fig.add_scatter(x=x, y=[p["facts"].get("ma_v") for p in pts], name="moyenne 7 j", + line=dict(color="royalblue"), row=1, col=1, secondary_y=True, **analyse) + fig.add_scatter(x=x, y=ref, name="référence (max 30 j)", line=dict(color="green", dash="dot"), + row=1, col=1, secondary_y=True, **analyse) + fig.add_scatter(x=x, y=[None if r is None else r + ec.THRESHOLD_V for r in ref], name="seuil", + line=dict(color="red", dash="dash"), row=1, col=1, secondary_y=True, **analyse) + fig.add_hline(y=ec.THRESHOLD_V, line=dict(color="black", dash="dash"), row=1, col=1) for p in per: fig.add_vrect(x0=p["start"], x1=p["end"] or x[-1], fillcolor="red", opacity=0.15, - line_width=0, row=2, col=1) - for row, m in enumerate(extra, start=3): + line_width=0, row=1, col=1) + fig.update_yaxes(title_text="écart [V]", row=1, col=1, secondary_y=False) + fig.update_yaxes(title_text="BusVoltage moyennée [V]", row=1, col=1, secondary_y=True) + + mesures = dict(legendgroup="mesures", legendgrouptitle_text="Mesures") + for row, m in enumerate(metrics, start=2): s = series[m] fig.add_scatter(x=sorted(s), y=[s[t] for t in sorted(s)], name=m, line=dict(width=1), - row=row, col=1) - fig.update_layout(title=f"{station} : analyse {ec.RULE['id']}", height=280 * (2 + len(extra))) + row=row, col=1, **mesures) + for a, b in gaps(series["BusVoltage"]): + fig.add_vrect(x0=a, x1=b, fillcolor="gray", opacity=0.3, line_width=0, row=2, col=1) + fig.update_yaxes(fixedrange=True) # zoom horizontal seulement : l'ordonnée est recalée par AUTOSCALE_Y_JS + fig.update_layout(title=f"{station} : analyse {ec.RULE['id']}", height=300 * (1 + len(metrics)), + legend=dict(groupclick="toggleitem")) return fig @@ -64,14 +108,18 @@ def main(argv=None): ap.add_argument("--out", default="out", help="dossier de sortie (défaut : out/)") a = ap.parse_args(argv) os.makedirs(a.out, exist_ok=True) + paths = [] for station, series in sorted(ec.load_stations(a.paths).items()): if "BusVoltage" not in series: print(f"{station} : ignorée, pas de BusVoltage") continue result = ec.run(series) path = os.path.join(a.out, f"{station}_energy.html") - figure(station, series, result, ec.periods(result["points"])).write_html(path, include_plotlyjs="cdn") + figure(station, series, result, ec.periods(result["points"])).write_html( + path, include_plotlyjs="cdn", post_script=AUTOSCALE_Y_JS, config={"scrollZoom": True}) print(path) + paths.append(path) + return paths if __name__ == "__main__": diff --git a/tests/test_energy_check.py b/tests/test_energy_check.py deleted file mode 100644 index f089d1d..0000000 --- a/tests/test_energy_check.py +++ /dev/null @@ -1,206 +0,0 @@ -"""Tests de régression de l'analyse MA7D_DROP, sur données réelles uniquement (spec §9).""" -import functools -import io -import json -import shutil -import sys -import tempfile -import unittest -from contextlib import redirect_stderr, redirect_stdout -from datetime import datetime, timedelta, timezone -from pathlib import Path - -ROOT = Path(__file__).resolve().parents[1] -sys.path.insert(0, str(ROOT / "src")) -import energy_check as ec # noqa: E402 - -DATA, VERMONT = ROOT / "data", ROOT / "vermont" -VERMONT_V = VERMONT / "VERMONT_BusVoltage_2026_04_09_15_00 to 2026_10_09_15_00.csv" -VERMONT_THERMAL = VERMONT / "VERMONT_sensor_thermal_deg_c_2026_02_22_15_00 to 2026_10_09_15_00.csv" -JUNGFRAU_V = DATA / "JUNGFRAU_BusVoltage_2026_09_11_14_00 to 2026_10_09_14_00.csv" - - -def at(*args): - return datetime(*args, tzinfo=timezone.utc) - - -@functools.cache -def stations(): - return ec.load_stations([str(DATA), str(VERMONT)]) - - -@functools.cache -def result(name): - return ec.run(stations()[name]) - - -def first_anomaly(res): - return next((p["t"] for p in res["points"] if p["anomaly"] is True), None) - - -class Loading(unittest.TestCase): - def test_parse_name_metric_with_underscores(self): - self.assertEqual(ec.parse_name(str(VERMONT_THERMAL)), ("VERMONT", "sensor_thermal_deg_c")) - - def test_parse_name_rejects_unknown_pattern(self): - self.assertIsNone(ec.parse_name("data/metadata.csv")) - - def test_load_csv_empty_value_is_none(self): - s = ec.load_csv(VERMONT_V) - self.assertIsNone(s[at(2026, 4, 9, 15)]) - self.assertAlmostEqual(s[at(2026, 4, 9, 16)], 14.011) - - def test_load_stations_groups_by_station(self): - self.assertEqual(sorted(stations()), ["FLORIDA", "JUNGFRAU", "MISSISSIPPI", "VERMONT", "WILDSTRUBEL"]) - self.assertEqual(len(stations()["VERMONT"]), 7) - - def test_same_file_given_twice_is_not_a_duplicate(self): - self.assertIn("BusVoltage", ec.load_stations([str(DATA), str(JUNGFRAU_V)])["JUNGFRAU"]) - - def test_two_files_same_station_metric_raise(self): - with tempfile.TemporaryDirectory() as d: - shutil.copy(JUNGFRAU_V, Path(d) / "JUNGFRAU_BusVoltage_2026_09_12_14_00 to 2026_10_09_14_00.csv") - with self.assertRaises(ValueError): - ec.load_stations([str(DATA), d]) - - def test_unknown_csv_name_is_skipped_with_message(self): - with tempfile.TemporaryDirectory() as d: - shutil.copy(JUNGFRAU_V, Path(d) / "export.csv") - shutil.copy(JUNGFRAU_V, d) - err = io.StringIO() - with redirect_stderr(err): - st = ec.load_stations([d]) - self.assertEqual(list(st), ["JUNGFRAU"]) - self.assertIn("export.csv", err.getvalue()) - - def test_empty_csv_file_is_skipped_with_message(self): - with tempfile.TemporaryDirectory() as d: - shutil.copy(JUNGFRAU_V, d) - empty = Path(d) / "FLORIDA_BusVoltage_2026_04_13_14_00 to 2026_10_09_14_00.csv" - empty.touch() - err = io.StringIO() - with redirect_stderr(err): - st = ec.load_stations([d]) - self.assertEqual(list(st), ["JUNGFRAU"]) - self.assertIn(empty.name, err.getvalue()) - - -class Ma7dDrop(unittest.TestCase): - def test_florida_never_anomalous(self): - self.assertIsNone(first_anomaly(result("FLORIDA"))) - - def test_jungfrau_never_anomalous(self): - self.assertIsNone(first_anomaly(result("JUNGFRAU"))) - - def test_vermont_first_anomaly_before_first_cut(self): - t = first_anomaly(result("VERMONT")) - self.assertIsNotNone(t) - self.assertTrue(at(2026, 9, 20) <= t < at(2026, 9, 27, 1), t) - - def test_wildstrubel_first_anomaly_before_cut(self): - t = first_anomaly(result("WILDSTRUBEL")) - self.assertIsNotNone(t) - self.assertLess(t, at(2026, 9, 9, 22)) - - def test_mississippi_first_anomaly_before_first_cut(self): - t = first_anomaly(result("MISSISSIPPI")) - self.assertIsNotNone(t) - self.assertLess(t, at(2025, 12, 25, 14)) - - def test_causal(self): - t = at(2026, 9, 24, 7) - truncated = {"BusVoltage": {k: x for k, x in stations()["VERMONT"]["BusVoltage"].items() if k <= t}} - last = ec.run(truncated)["points"][-1] - full = next(p for p in result("VERMONT")["points"] if p["t"] == t) - self.assertEqual(last["t"], t) - self.assertEqual((last["value"], last["anomaly"]), (full["value"], full["anomaly"])) - - def test_one_point_per_timestamp(self): - self.assertEqual([p["t"] for p in result("VERMONT")["points"]], - sorted(stations()["VERMONT"]["BusVoltage"])) - - def test_facts_trace_the_verdict(self): - p = next(p for p in result("VERMONT")["points"] if p["anomaly"] is True) - self.assertEqual(set(p["facts"]), {"coverage", "ma_v", "ref_v", "ref_t"}) - self.assertLess(p["facts"]["ref_t"], p["t"]) - - def test_short_history_is_unknown(self): - v = stations()["JUNGFRAU"]["BusVoltage"] - short = {"BusVoltage": {t: x for t, x in v.items() if t < min(v) + timedelta(days=5)}} - self.assertTrue(all(p["anomaly"] is None for p in ec.run(short)["points"])) - - def test_empty_series_gives_no_points(self): - self.assertEqual(ec.run({"BusVoltage": {}})["points"], []) - - def test_off_grid_timestamp_raises(self): - v = dict(stations()["JUNGFRAU"]["BusVoltage"]) - v[min(v) + timedelta(minutes=30)] = None - with self.assertRaises(ValueError): - ec.run({"BusVoltage": v}) - - def test_nan_value_is_treated_as_missing(self): - v = dict(stations()["VERMONT"]["BusVoltage"]) - v[at(2026, 6, 1)] = float("nan") - self.assertEqual(first_anomaly(ec.run({"BusVoltage": v})), at(2026, 9, 24, 7)) - - -class Reporting(unittest.TestCase): - def test_vermont_period_is_ongoing(self): - per = ec.periods(result("VERMONT")["points"]) - self.assertEqual(per[-1]["start"], at(2026, 9, 24, 7)) - self.assertIsNone(per[-1]["end"]) - - def test_mississippi_first_period_is_closed(self): - per = ec.periods(result("MISSISSIPPI")["points"]) - self.assertEqual((per[0]["start"], per[0]["end"]), (at(2025, 12, 9, 12), at(2025, 12, 18, 6))) - - def test_florida_has_no_period(self): - self.assertEqual(ec.periods(result("FLORIDA")["points"]), []) - - def test_summary_vocabulary(self): - for name, word in [("FLORIDA", "not observed"), ("VERMONT", "consistent with")]: - res = result(name) - self.assertIn(word, ec.summary(name, res, ec.periods(res["points"]))) - - def test_summary_short_history_is_insufficient_evidence(self): - v = stations()["JUNGFRAU"]["BusVoltage"] - res = ec.run({"BusVoltage": {t: x for t, x in v.items() if t < min(v) + timedelta(days=5)}}) - self.assertIn("insufficient evidence", ec.summary("JUNGFRAU", res, ec.periods(res["points"]))) - - def test_cli_writes_json_and_summary(self): - out = io.StringIO() - with tempfile.TemporaryDirectory() as d, redirect_stdout(out): - ec.main([str(VERMONT), "--out", d]) - doc = json.loads((Path(d) / "VERMONT_energy.json").read_text()) - self.assertEqual((doc["station"], doc["analysis"]), ("VERMONT", "MA7D_DROP")) - self.assertIsNone(doc["periods"][-1]["end"]) - self.assertIn("VERMONT : anomalie énergétique", out.getvalue()) - - def test_cli_skips_station_without_busvoltage(self): - out = io.StringIO() - with tempfile.TemporaryDirectory() as d, redirect_stdout(out): - ec.main([str(VERMONT_THERMAL), "--out", d]) - self.assertIn("ignorée, pas de BusVoltage", out.getvalue()) - - def test_cli_continues_after_station_error(self): - out, err = io.StringIO(), io.StringIO() - with tempfile.TemporaryDirectory() as d: - shutil.copy(JUNGFRAU_V, d) - florida = Path(d) / "FLORIDA_BusVoltage_2026_04_13_14_00 to 2026_10_09_14_00.csv" - shutil.copy(DATA / florida.name, florida) - with open(florida, "a") as f: - f.write(",2026-10-09 14:30:00+00:00,\n") # horodatage hors grille, sans mesure - with redirect_stdout(out), redirect_stderr(err): - ec.main([d, "--out", d]) - self.assertIn("FLORIDA : ignorée", err.getvalue()) - self.assertIn("JUNGFRAU : aucune anomalie", out.getvalue()) - - def test_summary_reports_longest_unknown_stretch(self): - res = result("MISSISSIPPI") - text = ec.summary("MISSISSIPPI", res, ec.periods(res["points"])) - self.assertIn("sans verdict (UNKNOWN)", text) - self.assertIn("du 2025-12-26 23:00 au 2026-06-07 16:00", text) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_energy_plot.py b/tests/test_energy_plot.py deleted file mode 100644 index a842db9..0000000 --- a/tests/test_energy_plot.py +++ /dev/null @@ -1,49 +0,0 @@ -"""Test de fumée du graphique MA7D_DROP (nécessite plotly : .venv/bin/python).""" -import importlib.util -import io -import sys -import tempfile -import unittest -from contextlib import redirect_stdout -from datetime import datetime, timezone -from pathlib import Path - -ROOT = Path(__file__).resolve().parents[1] -sys.path.insert(0, str(ROOT / "src")) -import energy_check as ec # noqa: E402 - -HAS_PLOTLY = importlib.util.find_spec("plotly") is not None -if HAS_PLOTLY: - import energy_plot as ep - -VERMONT = ROOT / "vermont" - - -def at(*args): - return datetime(*args, tzinfo=timezone.utc) - - -@unittest.skipUnless(HAS_PLOTLY, "plotly absent : lancer avec .venv/bin/python") -class Plot(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.series = ec.load_stations([str(VERMONT)])["VERMONT"] - cls.result = ec.run(cls.series) - - def test_gaps_include_first_vermont_cut(self): - self.assertIn((at(2026, 9, 27, 1), at(2026, 9, 27, 7)), ep.gaps(self.series["BusVoltage"])) - - def test_figure_has_all_series(self): - fig = ep.figure("VERMONT", self.series, self.result, ec.periods(self.result["points"])) - names = {t.name for t in fig.data} - self.assertLessEqual({"BusVoltage", "moyenne 7 j", "référence (max 30 j)", "seuil", "écart"}, names) - self.assertLessEqual(set(self.series) - {"BusVoltage"}, names) - - def test_cli_writes_html(self): - with tempfile.TemporaryDirectory() as d, redirect_stdout(io.StringIO()): - ep.main([str(VERMONT), "--out", d]) - self.assertGreater((Path(d) / "VERMONT_energy.html").stat().st_size, 0) - - -if __name__ == "__main__": - unittest.main()