One computation per module, an explicit list of detectors; obstruction.py removed

Each module returns every family it finds (kind); categories.py imports DETECTORS by name (no introspection, no
CONTEXT) and drops from Obstruction the sensors already in an overlapping Target issue episode (formerly
obstruction.py). No detector computed twice. Benchmark identical to the baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-11-16 16:06:24 +01:00
1 parent 5e9a7aa9d1
commit 5f085bf5ff
5 files changed
+24 -69

No files matched your search

+19 -37
View File
@@ -29,8 +29,7 @@ Usage: python3 scripts/categories.py <station folder> (one line per event: fam
shown; "nothing" for a quiet chunk)
"""
import functools
import importlib.util
import inspect
import importlib
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
@@ -39,17 +38,15 @@ from pathlib import Path
# sibling of this one (one copy of each repository in the home directory).
HERE = Path(__file__).resolve().parent
sys.path[:0] = [str(HERE), str(HERE.parents[1] / "aimsight-tools" / "scripts")]
from data import iso, load, load_camera, load_metrics, load_temperature # noqa: E402
from data import iso, load, load_camera, load_temperature # noqa: E402
# What a detector can read from the station besides the measurements, if it declares it in detect (D-124, D-134):
# temperature [(t, °C)], hourly median of the components; camera [pose] (pan, tilt, rotation, scale, sigma0);
# metrics {name: [(t, value)]}, all numeric metrics (sensor_CPU_deg_c, sensor_GPU_deg_c, BusVoltage…).
CONTEXT = {"temperature": load_temperature, "camera": load_camera, "metrics": load_metrics}
DETECTORS = ("displacement", "no_data", "target_issue", "thermal_displacement", "visibility") # modules next to this file
@functools.lru_cache(maxsize=None)
def _context(folder, key):
return CONTEXT[key](folder)
def _station(folder):
"""What thermal_displacement reads besides the measurements (D-124): temperature, camera pose; once per folder."""
return {"temperature": load_temperature(folder), "camera": load_camera(folder)} if folder else {}
# The families, in the order of the timeline rows; ignore: greyed zone (None: as its episodes
@@ -71,24 +68,6 @@ HOSTS = {"Poor visibility", "Brief obstruction", "Heat shimmer", "Thermal expans
MERGE = timedelta(hours=1) # same variety less than an hour apart: a single event (validated rule, 2026-10-24)
def detectors():
"""Modules of the *.py next to this file that have detect; a file that does not load is skipped with a message."""
mods = []
for path in sorted(Path(__file__).resolve().parent.glob("*.py")):
if path == Path(__file__).resolve():
continue
try:
spec = importlib.util.spec_from_file_location(f"detector_{path.stem}", path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
except Exception as e: # noqa: BLE001 (a broken detector does not block the page)
print(f"Detectors: {path.name} skipped ({type(e).__name__}: {e})", file=sys.stderr)
continue
if hasattr(mod, "detect"):
mods.append(mod)
return mods
def variety(ep):
"""The variety of an episode within its family: shifts together, the thermal effect without "Camera", the loss of
visibility by level."""
@@ -163,6 +142,12 @@ def classify(episodes, references=frozenset()):
"""Episodes of all detectors -> events sorted by start (rules: module docstring); references: the global_ref
sensors, for the sentence."""
eps = sorted(episodes, key=lambda e: e["start"])
# a sensor already in an overlapping Target issue episode is not Obstruction: its curve is already to be ignored
issues = [e for e in eps if e["kind"] == "Target issue"]
for e in eps:
if e["kind"] == "Obstruction":
e["sensors"] = [s for s in e["sensors"] if not any(s in h["sensors"] and overlap(e, h) for h in issues)]
eps = [e for e in eps if e["kind"] != "Obstruction" or e["sensors"]]
# a real movement is never taken for heat shimmer; the shift of a single target is not a movement of the scene
checks = [e for e in eps if e["label"] in SHIFTS and len(e["sensors"]) > 1]
eps = [e for e in eps if e["label"] != "Heat shimmer" or not any(overlap(e, c) for c in checks)]
@@ -196,19 +181,16 @@ def classify(episodes, references=frozenset()):
def events(measurements, end=None, folder=None):
"""Measurements (data.load) -> events of all detectors: what the page receives. A detector also receives what its
detect function declares: end, the end of the window read (batch run), for a hole touching it; a CONTEXT key, read
from the station folder (once per folder), empty without a folder."""
"""Measurements (data.load) -> events of all detectors: what the page receives. end: the end of the window read
(batch run), for a hole touching it; folder: the station folder, for the temperature and the camera pose. A detector
that fails is skipped with a message (a broken detector does not block the page)."""
episodes = []
for mod in detectors():
for name in DETECTORS:
try:
params = inspect.signature(mod.detect).parameters
kw = {k: _context(folder, k) if folder else {} if k == "metrics" else [] for k in CONTEXT if k in params}
if "end" in params:
kw["end"] = end
episodes += mod.detect(measurements, **kw)
kw = {"end": end} if name == "no_data" else _station(folder) if name == "thermal_displacement" else {}
episodes += importlib.import_module(name).detect(measurements, **kw)
except Exception as e: # noqa: BLE001
print(f"Detectors: {mod.__name__} skipped ({type(e).__name__}: {e})", file=sys.stderr)
print(f"Detectors: {name} skipped ({type(e).__name__}: {e})", file=sys.stderr)
return classify(episodes, {s for s, ms in measurements.items() if ms[0]["pattern_type"] == "global_ref"})