Add causal MA7D_DROP voltage-trend analysis
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
422904757f
commit
041770ec47
2 files changed
+130
-2
No files matched your search
+66
-1
@@ -8,7 +8,27 @@ import glob
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
HOUR = timedelta(hours=1)
|
||||
|
||||
# Constantes de calibration (spec §4.2).
|
||||
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
|
||||
MIN_REF_POINTS = 24 # au moins un jour de référence (choix de conception)
|
||||
THRESHOLD_V = -0.2 # cas de régression : pire écart sain -0,16 V (VERMONT 05-11)
|
||||
|
||||
RULE = {
|
||||
"id": "MA7D_DROP",
|
||||
"window_h": WINDOW_H,
|
||||
"ref_h": REF_H,
|
||||
"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",
|
||||
}
|
||||
|
||||
NAME_RE = re.compile(r"^(?P<station>[^_]+)_(?P<metric>.+?)_\d{4}_\d{2}_\d{2}_\d{2}_\d{2} to .+\.csv$")
|
||||
|
||||
@@ -46,3 +66,48 @@ def load_stations(paths):
|
||||
seen[key] = real
|
||||
stations.setdefault(key[0], {})[key[1]] = load_csv(path)
|
||||
return stations
|
||||
|
||||
|
||||
def run(series):
|
||||
"""Analyse MA7D_DROP, causale : le point t n'utilise que les données <= t (spec §4.1, §5)."""
|
||||
v = series["BusVoltage"]
|
||||
if not v:
|
||||
return {"analysis": RULE["id"], "rule": RULE, "points": []}
|
||||
t0 = min(v)
|
||||
if any((t - t0) % HOUR for t in v):
|
||||
raise ValueError("BusVoltage : horodatages hors de la grille horaire")
|
||||
grid = [t0 + i * HOUR for i in range((max(v) - t0) // HOUR + 1)]
|
||||
vals = [v.get(t) for t in grid]
|
||||
|
||||
# Moyenne glissante sur WINDOW_H créneaux (somme courante), définie si couverture suffisante.
|
||||
ma, cov = [None] * len(grid), [0.0] * len(grid)
|
||||
total = count = 0
|
||||
for i, x in enumerate(vals):
|
||||
if x is not None:
|
||||
total += x
|
||||
count += 1
|
||||
if i >= WINDOW_H and vals[i - WINDOW_H] is not None:
|
||||
total -= vals[i - WINDOW_H]
|
||||
count -= 1
|
||||
cov[i] = count / WINDOW_H
|
||||
if count >= MIN_COVERAGE * WINDOW_H:
|
||||
ma[i] = total / count
|
||||
|
||||
points = []
|
||||
for i, t in enumerate(grid):
|
||||
if t not in v:
|
||||
continue
|
||||
facts = {"coverage": round(cov[i], 3)}
|
||||
value = anomaly = None
|
||||
if ma[i] is not None:
|
||||
facts["ma_v"] = round(ma[i], 3)
|
||||
lo = max(0, i - REF_H)
|
||||
ref_vals = [m for m in ma[lo:i] if m is not None]
|
||||
if len(ref_vals) >= MIN_REF_POINTS:
|
||||
ref = max(ref_vals)
|
||||
value = ma[i] - ref
|
||||
anomaly = value < THRESHOLD_V
|
||||
facts["ref_v"] = round(ref, 3)
|
||||
facts["ref_t"] = grid[ma.index(ref, lo, i)]
|
||||
points.append({"t": t, "value": value, "anomaly": anomaly, "facts": facts})
|
||||
return {"analysis": RULE["id"], "rule": RULE, "points": points}
|
||||
@@ -6,7 +6,7 @@ import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stderr
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -28,6 +28,15 @@ 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"))
|
||||
@@ -64,5 +73,59 @@ class Loading(unittest.TestCase):
|
||||
self.assertIn("export.csv", 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})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user