"""Every target measurement more than K own noises from its local level, grouped by instant, against the labels. Noise: median |diff| of consecutive accepted measurements over the range (as ranges.py). Local level: median of the N accepted neighbours on each side, itself excluded (as ranges.py --rolling N). Output 1: per instant with at least one target > K: the targets and their signed deviation (noises), the references out (rejected r / inlier 0), and the labels covering it (start < t < end). Output 2: per label: the max deviation strictly inside, with its target and instant; '-' if none > K. Usage: python3 exceed.py LABELS.jsonl [--k 5] [--n 8] [--uncovered] """ import json, sys, statistics sys.path.insert(0, "tools/scripts") import data K = float(sys.argv[sys.argv.index("--k") + 1]) if "--k" in sys.argv else 5 N = int(sys.argv[sys.argv.index("--n") + 1]) if "--n" in sys.argv else 8 S, E = data.parse_time("2026-09-24T02:30Z"), data.parse_time("2026-10-06T19:45Z") by = data.load("data/WILDSTRUBEL") for _ms in by.values(): # measurement instants to the minute, as the label bounds for _m in _ms: _m["t"] = _m["t"].replace(second=0, microsecond=0) refs = sorted(s for s, ms in by.items() if data.is_reference(ms)) tg = sorted(s for s, ms in by.items() if not data.is_reference(ms)) labels = [json.loads(l) for l in open(sys.argv[1])] for L in labels: L["a"], L["b"] = data.parse_time(L["start"]), data.parse_time(L["end"]) dev = {} # t -> list of (target, col, signed noises) for s in tg: for c in ("corr_x", "corr_y"): acc = [m for m in by[s] if m[c] is not None and S <= m["t"] <= E] v = [m[c] for m in acc] nz = statistics.median(abs(b - a) for a, b in zip(v, v[1:])) for i, m in enumerate(acc): nb = v[max(0, i - N):i] + v[i + 1:i + 1 + N] d = (v[i] - statistics.median(nb)) / nz dev.setdefault(m["t"], []).append((s, c[-1], d)) refout = {} for r in refs: for m in by[r]: if S <= m["t"] <= E and (data.rejected(m) or m.get("inlier") == 0): refout.setdefault(m["t"], []).append(r + ("r" if data.rejected(m) else "0")) def cover(t): return [L.get("id", "new").replace("WILDSTRUBEL-", "") for L in labels if L["a"] < t < L["b"]] if "--per-label" not in sys.argv: for t in sorted(dev): over = [(s, c, d) for s, c, d in dev[t] if abs(d) > K] if not over: continue cv = cover(t) if "--uncovered" in sys.argv and [x for x in cv if x not in sys.argv[sys.argv.index("--uncovered") + 1].split(",")]: continue over.sort(key=lambda x: -abs(x[2])) print(f"{t:%m-%d %H:%M} " + " ".join(f"{s}.{c}{d:+.1f}" for s, c, d in over) + f" | refs out: {','.join(x for x in refout.get(t, []) if x != 'R3r') or '-'} | labels: {','.join(cv) or 'NONE'}") print("\n== per label: max |dev| strictly inside (rolling), n instants > K ==") for L in labels: ins = [(abs(d), s, c, d, t) for t in dev if L["a"] < t < L["b"] for s, c, d in dev[t]] if not ins: print(f"{L.get('id','new'):16s} {L['start'][5:16]} {L['end'][5:16]} {L['family'][:10]:10s} no measurement inside") continue mx = max(ins) n = len({t for a, s, c, d, t in ins if a > K}) print(f"{L.get('id','new'):16s} {L['start'][5:16]} {L['end'][5:16]} {L['family'][:10]:10s} max {mx[1]}.{mx[2]} {mx[3]:+.1f} @{mx[4]:%m-%d %H:%M} instants>K {n}")