"""Requalification: per label, the largest deviation of each target in its own noises. Noise: median |step| between consecutive accepted measurements over the analysis window (as ranges.py). Levels: 'win' = median over the analysis window (ranges.py default); 'roll' = median of +-4 neighbours without the point (ranges.py --rolling 4); 'night' (thermal labels) = median of 01:00-05:00 UTC of the label's start day. Prints the top sensors per label, and the rejected count of each target within it. Usage: python3 requalify.py labels.jsonl""" import sys, json, statistics sys.path.insert(0, 'tools/scripts') import data from datetime import timedelta S, E = data.parse_time('2026-09-17T05:45Z'), data.parse_time('2026-09-30T04:15Z') by = {s: data.filter_window(m, S - timedelta(hours=6), E) for s, m in data.load('data/NORDEND').items()} refs = {s for s, ms in by.items() if data.is_reference(ms)} acc = {s: [m for m in ms if not data.rejected(m)] for s, ms in by.items()} noise, level = {}, {} for s, ms in acc.items(): for c in ('corr_x', 'corr_y'): v = [m[c] for m in ms if m[c] is not None and S <= m['t'] <= E] d = [abs(v[i + 1] - v[i]) for i in range(len(v) - 1)] noise[s, c] = statistics.median(d) if d else None level[s, c] = statistics.median(v) if v else None def dev(lab): a, b = data.parse_time(lab['start']), data.parse_time(lab['end']) night = None if lab['family'] == 'Thermal displacement': n0 = a.replace(hour=1, minute=0); n1 = a.replace(hour=5, minute=0) night = (n0, n1) rows = [] for s, ms in acc.items(): if s in refs: continue for c in ('corr_x', 'corr_y'): nz = noise[s, c] if not nz: continue vals = [(i, m) for i, m in enumerate(ms) if a <= m['t'] <= b and m[c] is not None] if not vals: continue res = {} res['win'] = max(((m[c] - level[s, c]) / nz for i, m in vals), key=abs) rr = [] for i, m in vals: nb = [ms[j][c] for j in range(max(0, i - 4), min(len(ms), i + 5)) if j != i and ms[j][c] is not None] if len(nb) >= 3: rr.append(((m[c] - statistics.median(nb)) / nz, m['t'])) if rr: res['roll'] = max(rr, key=lambda x: abs(x[0])) if night: nv = [m[c] for m in ms if night[0] <= m['t'] <= night[1] and m[c] is not None] if len(nv) >= 5: lv = statistics.median(nv) res['night'] = max((((m[c] - lv) / nz, m['t']) for i, m in vals), key=lambda x: abs(x[0])) rows.append((s, c, res)) rej = {s: sum(1 for m in by[s] if a <= m['t'] <= b and data.rejected(m)) for s in by} return rows, rej for line in open(sys.argv[1]): lab = json.loads(line) rows, rej = dev(lab) print(f"\n== {lab.get('id','')} {lab['start']} -> {lab['end']} {lab['family']}") for key in ('night', 'roll', 'win'): cand = [(s, c, r[key]) for s, c, r in rows if key in r] if not cand: continue val = lambda x: x[2][0] if isinstance(x[2], tuple) else x[2] cand.sort(key=lambda x: -abs(val(x))) txt = ', '.join(f"{s}.{c[-1]} {val(x):+.1f}" + (f"@{data.iso(x[2][1])[5:16]}" if isinstance(x[2], tuple) else '') for x in cand[:6] for s, c in [(x[0], x[1])]) print(f" {key:5s}: {txt}") r = {s: n for s, n in rej.items() if n} print(' rejected:', r)