"""For each label of a jsonl: per target, the largest deviation of corr_x, corr_y (accepted measurements) inside [start, end], in own noises, against (a) the window level (median), (b) the level of the preceding night 20-03 UTC, (c) for short labels (<= 4 steps), the jump against the mean of the measurement before and after the label. Usage: python3 check_labels.py [--sensors A,B]""" import json import statistics import sys from datetime import timedelta sys.path.insert(0, "tools/scripts") import data # noqa: E402 import common # noqa: E402 FOLDER = "data/WILDSTRUBEL" W0 = data.parse_time("2026-09-01T00:00Z") W1 = data.parse_time("2026-09-09T21:45Z") by = data.load(FOLDER) targets = [s for s, ms in by.items() if not data.is_reference(ms)] series = {} noise = {} for s in targets: ms = [m for m in by[s] if W0 - timedelta(days=1) <= m["t"] <= W1] for c in ("corr_x", "corr_y"): pts = [(m["t"].replace(second=0, microsecond=0), m[c]) for m in ms if m[c] is not None] series[(s, c)] = pts noise[(s, c)] = common.own_noise([v for t, v in pts if t >= W0]) def night_level(s, c, t): d = t - timedelta(hours=t.hour, minutes=t.minute) if t.hour < 20: d -= timedelta(days=1) a, b = d + timedelta(hours=20), d + timedelta(hours=27) v = [x for tt, x in series[(s, c)] if a <= tt <= b] return statistics.median(v) if v else None def report(lab): a, b = data.parse_time(lab["start"]), data.parse_time(lab["end"]) rows = [] for s in targets: for c in ("corr_x", "corr_y"): pts = series[(s, c)] n = noise[(s, c)] inside = [(t, v) for t, v in pts if a < t < b] or [(t, v) for t, v in pts if a <= t <= b] if not inside or not n: continue lvl = night_level(s, c, a) dev_n = max(((v - lvl) / n, t) for t, v in inside) if lvl is not None else None dev_n2 = min(((v - lvl) / n, t) for t, v in inside) if lvl is not None else None jump = None before = [v for t, v in pts if t <= a][-1:] after = [v for t, v in pts if t >= b][:1] if before and after and (b - a) <= timedelta(minutes=75): ref = (before[0] + after[0]) / 2 jump = max(((v - ref) / n, t) for t, v in inside), min(((v - ref) / n, t) for t, v in inside) rows.append((s, c, dev_n, dev_n2, jump)) return rows def main(): path = sys.argv[1] for line in open(path): lab = json.loads(line) print(f"== {lab.get('id','')} {lab['start']} {lab['end']} {lab['family']}") for s, c, hi, lo, jump in report(lab): parts = [] if jump: (jh, th), (jl, tl) = jump j = jh if abs(jh) >= abs(jl) else jl tj = th if abs(jh) >= abs(jl) else tl if abs(j) >= 3: parts.append(f"jump {j:+.1f}n @{tj:%d %H:%M}") if hi and lo: d, t = hi if abs(hi[0]) >= abs(lo[0]) else lo if abs(d) >= 5 and not jump: parts.append(f"vs night {d:+.1f}n @{t:%d %H:%M}") if parts: print(f" {s:6} {c}: " + "; ".join(parts)) if __name__ == "__main__": main()