"""For the report: per label, the largest deviation of each target in mm (corr_x_mm, corr_y_mm), the same three levels as requalify.py (rolling ±3, hour before, hour after); prints the three largest targets per label. Usage: python3 data/JUNGFRAU labels.jsonl """ import json import statistics import sys from datetime import timedelta sys.path.insert(0, "tools/scripts") import data # noqa: E402 folder, labels_path = sys.argv[1:3] meas = data.load(folder) for line in open(labels_path): lab = json.loads(line) a, b = data.parse_time(lab["start"]), data.parse_time(lab["end"]) best = [] for name, ms in meas.items(): if data.is_reference(ms): continue for col in ("corr_x_mm", "corr_y_mm"): pts = [(m["t"], m[col]) for m in ms if a - timedelta(hours=2) <= m["t"] <= b + timedelta(hours=2) and not data.rejected(m) and m[col] is not None] pre = [v for t, v in pts if a - timedelta(hours=1) <= t <= a] post = [v for t, v in pts if b <= t <= b + timedelta(hours=1)] levels = [statistics.median(x) for x in (pre, post) if x] top = (0.0, "") for i, (t, v) in enumerate(pts): if not (a < t.replace(second=0, microsecond=0) < b): continue nb = [w for _, w in pts[max(0, i - 3):i]] + [w for _, w in pts[i + 1:i + 4]] lv = list(levels) + ([statistics.median(nb)] if nb else []) for lvl in lv: if abs(v - lvl) > abs(top[0]): top = (v - lvl, data.iso(t)) if top[1]: best.append((abs(top[0]), f"{name} {col[5]} {top[0]:+.1f} mm at {top[1][11:16]}")) best.sort(reverse=True) print(lab.get("id", "?"), lab["start"], lab["family"][:10], " | ".join(s for _, s in best[:3]))