"""Requalification of the thermal labels: per label and per sensor, the largest deviation of corr_x / corr_y from the night level (median of the evening measurements 12, 16, 20 UTC of the day before the label's first morning), in the sensor's own noise as printed by ranges.py (file given). Usage: requalify_thermal.py FOLDER RANGES_TXT LABELS_JSONL""" import sys, json, re, statistics from datetime import timedelta sys.path.insert(0, "tools/scripts") import data folder, rtxt, lab = sys.argv[1:4] noise, col = {}, None for line in open(rtxt): m = re.match(r"== (corr_[xy]): level", line) if m: col = m.group(1); continue if line.startswith("corr_"): col = None p = line.split() if col and len(p) >= 4 and p[0] != "sensor": try: noise[(p[0], col)] = float(p[3]) except ValueError: pass meas = data.load(folder) ser = {s: {m["t"].replace(second=0, microsecond=0): m for m in v} for s, v in meas.items()} for l in map(json.loads, open(lab)): if l["family"] != "Thermal displacement": continue a, b = data.parse_time(l["start"]), data.parse_time(l["end"]) morning = b.replace(hour=0, minute=0) d0 = morning - timedelta(days=1) ev = [d0 + timedelta(hours=h) for h in (12, 16, 20)] print(f"\n{l.get('id','')} {l['start']} -> {l['end']} (night level: evening of {data.iso(d0)[:10]})") rows = [] for s, sv in ser.items(): for c in ("corr_x", "corr_y"): n = noise.get((s, c)) base = [sv[t][c] for t in ev if t in sv and sv[t][c] is not None] if not n or not base: continue lvl = statistics.median(base) best = None for t, m in sv.items(): if a < t < b and m[c] is not None: d = (m[c] - lvl) / n if best is None or abs(d) > abs(best[0]): best = (d, t, m[c] - lvl) if best: rows.append((abs(best[0]), s, c, best)) for _, s, c, (d, t, v) in sorted(rows, reverse=True)[:6]: print(f" {s:10} {c} {d:+5.1f} noises ({v:+.4f} px) at {data.iso(t)}")