Files
aimsight-optimizer/sessions/WILDSTRUBEL_2026-09-24T0230_2026-10-06T1945_20261118T1119Z/tools/thermal_days.py
T

53 lines
2.6 KiB
Python
Raw Normal View History

# Per Thermal displacement episode of each benchmark station: share of its instants labelled Thermal, and how far the
# targets' corrected curves go from their level just before the episode (in noises, median step of the series).
# Usage: python3 thermal_days.py <detectors dir> <labels dir> <data dir> [STATION,...]
import statistics
import sys
from datetime import timedelta
from pathlib import Path
det = Path(sys.argv[1]).resolve()
sys.path[:0] = [str(det / "scripts"), str(det / "bench")]
import categories # noqa: E402,F401
import regression # noqa: E402
import score # noqa: E402
import thermal_displacement # noqa: E402
from data import is_reference, iso, load, parse_time # noqa: E402
from episodes import noise # noqa: E402
labels_dir, data_dir = Path(sys.argv[2]), Path(sys.argv[3])
only = sys.argv[4].split(",") if len(sys.argv) > 4 else None
BEFORE = timedelta(hours=3)
for path in sorted(labels_dir.glob("*.jsonl")):
st = path.stem
if only and st not in only or not (data_dir / st).is_dir():
continue
start, end = regression.label_range(path)
th = [l for l in score.read_labels(str(path), start, end) if l["family"] == "Thermal displacement"]
ms = load(str(data_dir / st))
h, b = parse_time(start) - timedelta(days=3), parse_time(end)
chunk = {s: k for s, v in ms.items() if (k := [m for m in v if h <= m["t"] < b])}
tg = {s: v for s, v in chunk.items() if not is_reference(v)}
nz = {(s, c): noise([m[c] for m in v if m[c] is not None]) for s, v in tg.items() for c in ("corr_x", "corr_y")}
for e in thermal_displacement.detect(chunk):
if e["end"] < start:
continue
a, z = parse_time(e["start"]), parse_time(e["end"])
inst = sorted({m["t"] for v in chunk.values() for m in v if a <= m["t"] <= z})
lab = sum(any(l["start"] < iso(t) < l["end"] for l in th) for t in inst)
devs = []
for s, v in tg.items():
for c in ("corr_x", "corr_y"):
n = nz[(s, c)]
ref = [m[c] for m in v if a - BEFORE <= m["t"] < a and m[c] is not None]
ins = [m[c] for m in v if a <= m["t"] <= z and m[c] is not None]
if not n or len(ref) < 2 or len(ins) < 2:
continue
lv = statistics.median(ref)
# second largest deviation: two measurements beyond, not a single point
d = sorted((abs(x - lv) / n for x in ins), reverse=True)
devs.append(d[1])
devs.sort(reverse=True)
print(f"{st:12} {e['start']} {e['end']} {e['label']:28} n={len(inst):3} lab={lab:3} "
f"dev2={[round(x, 1) for x in devs[:3]]}")