Files
aimsight-labeling/auto/analyses/NORDEND_2026-09-30T0415_2026-10-13T0745/scripts/requalify.py
T

53 lines
2.8 KiB
Python
Raw Normal View History

"""Requalification: for each input label, run tools/scripts/ranges.py at --k 5 and report the
largest deviation (in noises) of a target on corr_x / corr_y within the label's range.
mode short: window = label +-2h, level and noise outside the label (--outside), ranges overlapping the label.
mode thermal: window = the label, plain level (median of the cycle) -- the thermal deviation itself.
mode rolling: window = label +-6h, --rolling 6h (deviations shorter than the drift)."""
import json, subprocess, sys, re
from datetime import datetime, timedelta
D = sys.argv[1]; OUT = D + '/work/requal'
REFS = {'CTR02', 'CTR03', 'CTR04'}
def t(s): return datetime.strptime(s, '%Y-%m-%dT%H:%MZ')
def f(d): return d.strftime('%Y-%m-%dT%H:%MZ')
def run(lab, mode):
s, e = t(lab['start']), t(lab['end'])
args = ['python3', 'tools/scripts/ranges.py', 'data/NORDEND', '--columns', 'corr_x,corr_y', '--k', '5']
if mode == 'short':
args += ['--start', f(s - timedelta(hours=2)), '--end', f(e + timedelta(hours=2)), '--outside', f'{f(s)}/{f(e)}']
elif mode == 'thermal':
args += ['--start', f(s), '--end', f(e)]
else:
args += ['--start', f(s - timedelta(hours=6)), '--end', f(e + timedelta(hours=6)), '--rolling', '6h']
out = subprocess.run(args, capture_output=True, text=True).stdout
open(f"{OUT}/{lab['id']}_{mode}.txt", 'w').write(' '.join(args) + '\n' + out)
best = {}
col = None
for line in out.splitlines():
m = re.match(r'(corr_[xy]): ranges', line)
if m: col = m.group(1); continue
if line.startswith('=='): col = None; continue
if col is None: continue
p = line.split()
if len(p) < 6 or not p[0][:2] in ('PR', 'CT'): continue
try: rs, re_ = t(p[1]), t(p[2])
except Exception: continue
if re_ < s or rs > e: continue # overlap with the label
nm = re.findall(r'\(([-+]?[\d.]+)\)', line) or re.findall(r'\s([-+]?\d+\.\d)\s', line)
# extreme in noises: field after the value
try:
idx = [i for i, x in enumerate(p) if re.match(r'^[-+]?\d+(\.\d+)?$', x)]
val = float(p[idx[2]]) # measurements, extreme value, extreme noises
except Exception: val = None
key = (p[0], col)
if val is not None and abs(val) > abs(best.get(key, (0,))[0]):
best[key] = (val, p[1], p[2])
tg = {k: v for k, v in best.items() if k[0] not in REFS}
top = sorted(tg.items(), key=lambda kv: -abs(kv[1][0]))[:5]
return top
for line in open(D + '/work/requalify_input.jsonl'):
lab = json.loads(line)
modes = ['thermal', 'rolling'] if lab['family'] == 'Thermal displacement' else ['short', 'rolling']
for mode in modes:
top = run(lab, mode)
print(lab['id'], lab['family'][:8], lab['start'], lab['end'], mode,
'; '.join(f"{k[0]} {k[1]} {v[0]:+.1f}" for k, v in top) or 'none')