"""Requalify each label of requalify_input.jsonl under the noise rule. For each label, runs tools/scripts/ranges.py (--k 5, corr_x,corr_y) on a window around the label, level and noise computed outside the label (--outside), and keeps the ranges that overlap the label (strictly inside its bounds). Modes: plain : level = median of the window outside the label (thermal: the night level) rolling : --rolling N measurements on each side (brief events under a cycle) Prints, per label, the largest |deviation| in noises among targets and among references, with sensor, column and instant, and the list of targets > 5. Usage: python3 requalify.py [ids...] """ import json, subprocess, sys from datetime import datetime, timedelta REFS = {"8002", "8004", "8011", "8012", "8013", "8014", "8017", "8019", "92H"} FMT = "%Y-%m-%dT%H:%MZ" B0 = datetime(2026, 10, 15, 0, 0) B1 = datetime(2026, 10, 24, 6, 0) def t(s): return datetime.strptime(s, FMT) def run(start, end, outside, rolling=None): cmd = ["python3", "tools/scripts/ranges.py", "data/LENZSPITZE", "--columns", "corr_x,corr_y", "--k", "5", "--merge", "0min", "--start", start.strftime(FMT), "--end", end.strftime(FMT), "--outside", outside] if rolling: cmd += ["--rolling", str(rolling)] out = subprocess.run(cmd, capture_output=True, text=True).stdout res = [] col = None inr = False for line in out.splitlines(): if ": ranges more than" in line: col = line.split(":")[0] inr = True continue if line.startswith("=="): inr = False continue if inr and line.strip() and not line.startswith("sensor"): p = line.split() if len(p) < 8: continue try: res.append(dict(sensor=p[0], col=col, start=t(p[1]), end=t(p[2]), n=int(p[3]), val=float(p[4]), noises=float(p[5]), at=p[6], rej=p[7])) except ValueError: continue return res def analyse(lab, mode): s, e = t(lab["start"]), t(lab["end"]) thermal = lab["family"] == "Thermal displacement" dur = e - s if thermal or mode == "plain": margin = timedelta(hours=8) if thermal else max(timedelta(hours=3), dur) ws, we = max(B0, s - margin), min(B1, e + margin) rr = run(ws, we, f"{lab['start']}/{lab['end']}") else: margin = max(timedelta(hours=3), dur * 2) ws, we = max(B0, s - margin), min(B1, e + margin) rr = run(ws, we, f"{lab['start']}/{lab['end']}", rolling=mode) inside = [r for r in rr if r["end"] > s and r["start"] < e] tg = [r for r in inside if r["sensor"] not in REFS] rf = [r for r in inside if r["sensor"] in REFS] return tg, rf def fmt(r): return f"{r['sensor']} {r['col']} {r['noises']:+.1f} at {r['at'][11:16]} ({r['n']} meas)" def main(): labs = [json.loads(l) for l in open(sys.argv[1]) if l.strip()] mode = sys.argv[2] if len(sys.argv) > 2 else "plain" ids = set(sys.argv[3:]) for lab in labs: if ids and lab["id"] not in ids: continue if lab["family"] == "No data": continue tg, rf = analyse(lab, mode) tg.sort(key=lambda r: -abs(r["noises"])) rf.sort(key=lambda r: -abs(r["noises"])) best = fmt(tg[0]) if tg else "none" bestr = fmt(rf[0]) if rf else "none" sens = sorted({r["sensor"] for r in tg}) print(f"{lab['id']:14s} {lab['start']} {lab['end']} {lab['family'][:12]:12s} " f"targets>5: {len(sens):2d} max {best} | refs max {bestr}") if tg: print(" top:", "; ".join(fmt(r) for r in tg[:6])) if __name__ == "__main__": main()