"""Requalification: per label, per target, the largest deviation of corr_x / corr_y (and in mm) inside the label, in the target's own batch noise (median |consecutive difference| over the measured batch 07/10 05:00 -> 09/10 12:30), against two levels: the batch median, and (for the day labels) a reference level given per label. Also the deviation of the hourly median (thermal / slow part) and the instants excluded (passages labelled apart).""" import json, statistics, sys from datetime import datetime, timezone sys.path.insert(0, "tools/scripts") from data import load, rejected, parse_time TG = ["PA2_2", "PA3_2", "PA_9", "PB1_2", "PB2_2", "PB4", "PB_5"] by = load("data/WILDSTRUBEL") B0, B1 = parse_time("2026-10-07T05:00Z"), parse_time("2026-10-09T12:30Z") def series(s, col, a, b): return [(m["t"], m[col]) for m in by[s] if a <= m["t"] <= b and m.get(col) is not None and not rejected(m)] def noise(vs): d = [abs(y - x) for x, y in zip(vs, vs[1:])] return statistics.median(d) if d else 0 BN = {(s, c): noise([v for _, v in series(s, c, B0, B1)]) for s in TG for c in ("corr_x", "corr_y")} BL = {(s, c): statistics.median([v for _, v in series(s, c, B0, B1)]) for s in TG for c in ("corr_x", "corr_y")} # label id -> (start, end, reference level window or None, instants excluded because labelled apart) L = json.loads(sys.argv[1]) for lid, (a, b, ref, excl) in L.items(): a, b = parse_time(a), parse_time(b) out = [] for s in TG: for c in ("corr_x", "corr_y"): n = BN[(s, c)] lvl = BL[(s, c)] if ref: r = [v for _, v in series(s, c, parse_time(ref[0]), parse_time(ref[1]))] lvl = statistics.median(r) if r else None pts = [(t, v) for t, v in series(s, c, a, b) if t.strftime("%Y-%m-%dT%H:%MZ") not in excl] if not pts or lvl is None: continue t, v = max(pts, key=lambda p: abs(p[1] - lvl)) # hourly medians: the slow part hours = {} for tt, vv in pts: hours.setdefault(tt.replace(minute=0), []).append(vv) hm = max((statistics.median(x) - lvl for x in hours.values()), key=abs) mm = by[s][0].get("corr_x_mm") out.append((abs(v - lvl) / n, s, c, v - lvl, t.strftime("%d %H:%M"), hm / n)) out.sort(reverse=True) print(f"== {lid} {a:%d %H:%M} -> {b:%d %H:%M} level={'batch median' if not ref else ref} excluded={excl}") for d, s, c, dv, t, hm in out[:5]: print(f" {s:6} {c} max {dv:+.3f} px = {d:5.1f} noises at {t}; largest hourly median {hm:+5.1f} noises")