Files
aimsight-labeling/auto/analyses/WILDSTRUBEL_2026-09-24T0230_2026-10-06T1945/scripts/labeldev.py
T

64 lines
2.9 KiB
Python
Raw Normal View History

"""Per label: the deviation of each target's corr_x, corr_y inside the label, in its own noises.
Noise: median |diff| of consecutive accepted measurements over the whole range (as ranges.py).
ref_edge: median of the accepted measurements just outside the label (up to N before start and N after end, both bounds
excluded? no: bounds included, they are the 'point before' and 'point after').
dev_edge: max |v - ref_edge| over measurements strictly inside (start, end).
dev_roll: max |v - median of the 8 accepted neighbours on each side, itself excluded| strictly inside.
Usage: python3 labeldev.py LABELS.jsonl [--n 4]
"""
import json, sys, statistics, os
sys.path.insert(0, "tools/scripts")
import data
START, END = data.parse_time("2026-09-24T02:30Z"), data.parse_time("2026-10-06T19:45Z")
by = data.load("data/WILDSTRUBEL")
targets = [s for s, ms in by.items() if not data.is_reference(ms)]
win = {s: [m for m in by[s] if START <= m["t"] <= END] for s in targets}
noise = {}
for s in targets:
for c in ("corr_x", "corr_y"):
v = [m[c] for m in win[s] if m[c] is not None]
noise[s, c] = statistics.median(abs(b - a) for a, b in zip(v, v[1:]))
def run(path, n=4, verbose=True):
out = []
for line in open(path):
L = json.loads(line)
a, b = data.parse_time(L["start"]), data.parse_time(L["end"])
best = []
for s in targets:
ms = win[s]
for c in ("corr_x", "corr_y"):
acc = [m for m in ms if m[c] is not None]
before = [m[c] for m in acc if m["t"] <= a][-n:]
after = [m[c] for m in acc if m["t"] >= b][:n]
inside = [m for m in acc if a < m["t"] < b]
if not inside:
continue
ref = statistics.median(before + after) if before + after else None
de = max(((abs(m[c] - ref), m["t"]) for m in inside), default=(0, None)) if ref is not None else (0, None)
# rolling
idx = {id(m): i for i, m in enumerate(acc)}
dr = (0, None)
for m in inside:
i = idx[id(m)]
nb = [x[c] for x in acc[max(0, i - 8):i] + acc[i + 1:i + 9]]
d = abs(m[c] - statistics.median(nb))
if d > dr[0]:
dr = (d, m["t"])
nz = noise[s, c]
best.append((de[0] / nz, dr[0] / nz, s, c, de[1], dr[1]))
best.sort(reverse=True)
top = best[:3]
out.append((L, top))
if verbose:
tag = L.get("id", "")
print(f"{tag:16s} {L['start'][5:16]} {L['end'][5:16]} {L['family'][:12]:12s} " + " ".join(
f"{s}.{c[-1]} e{de:.1f} r{dr:.1f}" for de, dr, s, c, *_ in top))
return out
if __name__ == "__main__":
n = int(sys.argv[sys.argv.index("--n") + 1]) if "--n" in sys.argv else 4
print("noise:", {f"{s}.{c[-1]}": round(v, 4) for (s, c), v in noise.items()})
run(sys.argv[1], n)