"""Per label: deviation of each target from the level before the label (median of the 24 h before start), in its own noise (median absolute successive difference /0.6745/sqrt2 over the whole range, as a check of ranges.py).""" import json, sys, statistics from datetime import datetime, timedelta, timezone sys.path.insert(0, "tools/scripts") import data D = "labeling/auto/analyses/NEBRASKA_2026-10-22T0000_2026-11-01T0000" m = data.load("data/NEBRASKA") T0 = datetime(2026,10,22,tzinfo=timezone.utc); T1 = datetime(2026,11,1,4,tzinfo=timezone.utc) def P(s): return datetime.strptime(s, "%Y-%m-%dT%H:%MZ").replace(tzinfo=timezone.utc) for l in open(f"{D}/work/requalify_input.jsonl"): lab = json.loads(l); s, e = P(lab["start"]), P(lab["end"]) best = [] for sen, ms in m.items(): if sen.startswith("REF"): continue for col in ("corr_x", "corr_y", "corr_x_mm", "corr_y_mm"): allv = [(x["t"], x[col]) for x in ms if T0 <= x["t"] <= T1 and x[col] is not None] if len(allv) < 10: continue d = [abs(b[1]-a[1]) for a, b in zip(allv, allv[1:])] noise = statistics.median(d) / 0.6745 / 2**0.5 pre = [v for t, v in allv if s - timedelta(hours=24) <= t < s] ins = [(t, v) for t, v in allv if s < t < e] if not pre or not ins or noise == 0: continue lev = statistics.median(pre) t, v = max(ins, key=lambda tv: abs(tv[1]-lev)) best.append((abs(v-lev)/noise, sen, col, (v-lev)/noise, v-lev, t.strftime("%m-%dT%H"))) best.sort(reverse=True) print(lab["id"], lab["start"], lab["end"]) for b in [x for x in best if "mm" not in x[2]][:4]: print(" %-7s %-7s %+6.1f noises %+.4f px at %s" % b[1:]) for b in [x for x in best if "mm" in x[2]][:3]: print(" %-7s %-9s %+6.1f noises %+.2f mm at %s" % b[1:])