"""Edge check of each non-thermal label: per sensor, deviation (in noises) of corr_x/corr_y at the measurements around start and end, against the median of the 2 h before start and 2 h after end (outside the label). 'R' = rejected. Prints only sensors touched (>K) at start, end, or the step just outside. Usage: edges.py LABELS K""" import sys, json, statistics from datetime import timedelta sys.path.insert(0, 'tools/scripts') import data lab, K = sys.argv[1], float(sys.argv[2]) by = data.load('data/NORDEND') Q = timedelta(minutes=15) 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']) out = [] for s, ms in by.items(): if s == 'PR04': continue d = {m['t'].replace(second=0, microsecond=0): m for m in ms if a - 9*Q <= m['t'].replace(second=0, microsecond=0) <= b + 9*Q} for c in ('corr_x', 'corr_y'): ref = [m[c] for t, m in d.items() if (t < a or t > b) and m[c] is not None] allv = [d[t][c] for t in sorted(d) if d[t][c] is not None] if len(ref) < 4: continue noise = statistics.median(abs(allv[i+1]-allv[i]) for i in range(len(allv)-1)) or 1e-9 lev = statistics.median(ref) cells = [] for t in (a - Q, a, a + Q, b - Q, b, b + Q): m = d.get(t) if m is None: cells.append(' -'); continue if data.rejected(m): cells.append(' R'); continue cells.append(f"{(m[c]-lev)/noise:+3.0f}") vals = [abs(int(x)) for x in (cells[0], cells[1], cells[4], cells[5]) if x.strip() not in ('-', 'R')] flag = any(v > K for v in vals) or 'R' in (cells[0].strip(), cells[1].strip(), cells[4].strip(), cells[5].strip()) if flag: out.append(f" {s}:{c[-1]} " + ' '.join(cells)) print(l.get('id', '?'), l['start'], l['end'], l['family'], ' cols: s-1 s s+1 e-1 e e+1') print('\n'.join(out) if out else ' (edges clean)')