Files
aimsight-labeling/auto/analyses/NORDEND_2026-10-13T0745_2026-10-24T1630/scripts/requalify.py
T

77 lines
3.5 KiB
Python
Raw Normal View History

"""Requalification of the labels of the analysis: for each label, runs tools/scripts/ranges.py at --k 5 on corr_x, corr_y
and keeps the ranges of the targets (global references CTR02, CTR03, CTR04 excluded) that fall inside the label.
- movement labels (Visibility, Displacement, Obstruction): window = label +- 3 h, --outside label (level and noise
of the hours around); labels longer than 4 h: window = the label, --rolling 8 (local level under the thermal cycle).
- Thermal displacement: window = 5 h before the start (end of night) + label, --outside label: level and noise of the
night that precedes; instants covered by a non-thermal label are not counted; ranges of >= 3 measurements reported.
- No data, and the labels of missing points: their ranges are listed too (what the other curves do meanwhile).
Usage: python3 <this> <labels.jsonl> <out.txt>
"""
import json, re, subprocess, sys
from datetime import datetime, timedelta, timezone
REFS = {"CTR02", "CTR03", "CTR04"}
F = "%Y-%m-%dT%H:%MZ"
def p(s):
return datetime.strptime(s, F).replace(tzinfo=timezone.utc)
def f(t):
return t.strftime(F)
def ranges(start, end, extra):
cmd = ["python3", "tools/scripts/ranges.py", "data/NORDEND", "--columns", "corr_x,corr_y", "--k", "5",
"--start", f(start), "--end", f(end)] + extra
out = subprocess.run(cmd, capture_output=True, text=True).stdout
res, col = [], None
for line in out.splitlines():
m = re.match(r"(corr_[xy]): ranges more than", line)
if m:
col = m.group(1)
continue
if line.startswith("=="):
col = None
if col:
tok = line.split()
if len(tok) >= 7 and re.match(r"\d{4}-", tok[1]):
res.append(dict(col=col, s=tok[0], a=p(tok[1]), b=p(tok[2]), n=int(tok[3]), v=float(tok[4]),
k=float(tok[5]), at=tok[6]))
return cmd, res
labels = [json.loads(l) for l in open(sys.argv[1])]
other = [(p(l["start"]), p(l["end"])) for l in labels if l["family"] not in ("Thermal displacement", "No data")]
out = open(sys.argv[2], "w")
for l in labels:
s, e = p(l["start"]), p(l["end"])
fam = l["family"]
if fam == "Thermal displacement":
cmd, rs = ranges(s - timedelta(hours=5), e, ["--outside", f"{f(s)}/{f(e)}"])
keep = []
for r in rs:
if r["s"] in REFS or r["b"] <= s or r["a"] >= e:
continue
# measurements of the range outside the non-thermal labels: approximated by the range span
inside_other = any(a < r["a"] and r["b"] < b for a, b in other)
if not inside_other and r["n"] >= 3:
keep.append(r)
else:
long = e - s > timedelta(hours=4)
if long:
cmd, rs = ranges(s, e, ["--rolling", "8"])
else:
cmd, rs = ranges(s - timedelta(hours=3), e + timedelta(hours=3), ["--outside", f"{f(s)}/{f(e)}"])
keep = [r for r in rs if r["s"] not in REFS and r["a"] < e and r["b"] > s]
keep.sort(key=lambda r: -abs(r["k"]))
best = keep[0] if keep else None
out.write(f"{l.get('id')} {l['start']} {l['end']} {fam}\n cmd: {' '.join(cmd[1:])}\n")
out.write(f" max: {best['s']} {best['col']} {best['k']:+.1f} noises ({best['v']:+.4f}) at {best['at']} n={best['n']}\n"
if best else " max: none beyond 5 noises\n")
for r in keep[:8]:
out.write(f" {r['s']:6} {r['col']} {f(r['a'])}..{f(r['b'])} n={r['n']} {r['k']:+.1f} at {r['at']}\n")
out.write("\n")
out.flush()