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"})
+1 -1
View File
@@ -195,7 +195,7 @@ def _episode(g, total):
def detect(measurements):
return sorted([e for e in episodes(measurements) if e["kind"] == "Displacement"] + drift(measurements),
return sorted(episodes(measurements) + drift(measurements),
key=lambda ep: ep["start"])
-26
View File
@@ -1,26 +0,0 @@
"""Detector of the Obstruction family (the families document): an object hides one or a few targets, for an instant or
for long. It reuses two computations shared with other families:
- the error_value of visibility.py: Brief obstruction (something passing, one instant) and Target obscured (a target
rejected again and again while the others stay seen, or a nuisance that only touches a few targets, no reference);
- the day-by-day rejections of target_issue.py: a target rejected for less than a week, Target obscured.
A sensor already in an overlapping Target issue episode (Degraded target, Target stuck) is not returned as Obstruction:
its curve is already to be ignored, the family is Target issue (boundary "Target issue or an episode of another family"
of the families document; wf-improve session Obstruction, mod. 1: S1, problem targets of FLORIDA, NORDEND, SNCF01,
NIEDERHORN, JUNGFRAU, CLARIDEN cut into Target obscured; S3, their isolated rejections as Brief obstruction).
"""
import target_issue
import visibility
def detect(measurements):
ti = target_issue.episodes(measurements)
issues = [e for e in ti if e["kind"] == "Target issue"]
out = []
for e in visibility.episodes(measurements) + ti:
if e["kind"] != "Obstruction":
continue
taken = {s for h in issues if h["start"] <= e["end"] and h["end"] >= e["start"] for s in h["sensors"]}
if (e := {**e, "sensors": [s for s in e["sensors"] if s not in taken]})["sensors"]: # minus the sensors
out.append(e) # already in Target issue
return sorted(out, key=lambda e: e["start"])
+1 -3
View File
@@ -37,7 +37,6 @@ both axes; one day is enough (a day without noise does not happen by accident);
"""
import statistics
import displacement
from data import is_reference, iso
CALM = 0.25 # share of the other sensors rejected, at most, for a calm instant
@@ -171,5 +170,4 @@ def stuck(measurements, min_instants):
def detect(measurements):
return sorted((e for e in episodes(measurements) + displacement.episodes(measurements) if e["kind"] == "Target issue"),
key=lambda e: e["start"])
return episodes(measurements)
+3 -2
View File
@@ -240,8 +240,9 @@ def diffuse(measurements):
def detect(measurements):
shimmer = heat_shimmer(measurements) # a diffuse nuisance during heat shimmer is the heat shimmer (8)
poor = [e for e in episodes(measurements) if e["kind"] == "Visibility"] # a diffuse nuisance within one of them is it
return poor + shimmer + [e for e in diffuse(measurements)
eps = episodes(measurements)
poor = [e for e in eps if e["kind"] == "Visibility"] # a diffuse nuisance within one of them is it
return eps + shimmer + [e for e in diffuse(measurements)
if not any(t["start"] <= e["start"] and e["end"] <= t["end"] for t in shimmer + poor)]