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
@@ -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