From 422904757f2397fe224394f371f58e1b03e66286 Mon Sep 17 00:00:00 2001 From: maxime Date: Fri, 9 Oct 2026 19:25:26 +0200 Subject: [PATCH] Add CSV loading per station for energy analysis Co-Authored-By: Claude Opus 5.5 --- src/energy_check.py | 48 +++++++++++++++++++++++++++ tests/test_energy_check.py | 68 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/energy_check.py create mode 100644 tests/test_energy_check.py diff --git a/src/energy_check.py b/src/energy_check.py new file mode 100644 index 0000000..c62dc22 --- /dev/null +++ b/src/energy_check.py @@ -0,0 +1,48 @@ +"""Analyse énergétique MA7D_DROP : tendance de BusVoltage en moyenne glissante. + +Spec : docs/superpowers/specs/2026-10-09-energy-ma7d-design.md +Bibliothèque standard uniquement (cible : système embarqué de la station). +""" +import csv +import glob +import os +import re +import sys +from datetime import datetime + +NAME_RE = re.compile(r"^(?P[^_]+)_(?P.+?)_\d{4}_\d{2}_\d{2}_\d{2}_\d{2} to .+\.csv$") + + +def parse_name(path): + """'__ to .csv' -> (station, metric), ou None.""" + m = NAME_RE.match(os.path.basename(path)) + return (m["station"], m["metric"]) if m else None + + +def load_csv(path): + """CSV ',time_utc,metric' -> {datetime UTC: float ou None (pas de mesure)}.""" + with open(path, newline="") as f: + rows = csv.reader(f) + next(rows) + return {datetime.fromisoformat(r[1]): float(r[2]) if r[2] else None for r in rows} + + +def load_stations(paths): + """Fichiers et/ou dossiers -> {station: {metric: série}}.""" + files = [] + for p in paths: + files += sorted(glob.glob(os.path.join(p, "*.csv"))) if os.path.isdir(p) else [p] + stations, seen = {}, {} + for path in files: + real = os.path.realpath(path) + if real in seen.values(): + continue + key = parse_name(path) + if key is None: + print(f"ignoré (nom non reconnu) : {path}", file=sys.stderr) + continue + if key in seen: + raise ValueError(f"doublon {key[0]}/{key[1]} : {seen[key]} et {path}") + seen[key] = real + stations.setdefault(key[0], {})[key[1]] = load_csv(path) + return stations diff --git a/tests/test_energy_check.py b/tests/test_energy_check.py new file mode 100644 index 0000000..5dd2105 --- /dev/null +++ b/tests/test_energy_check.py @@ -0,0 +1,68 @@ +"""Tests de régression de l'analyse MA7D_DROP, sur données réelles uniquement (spec §9).""" +import functools +import io +import shutil +import sys +import tempfile +import unittest +from contextlib import redirect_stderr +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 + +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)]) + + +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()) + + +if __name__ == "__main__": + unittest.main()