"""Requalify: per label, ranges.py at --k K in a margin window, level/noise outside the label (and optionally --rolling); keep the ranges overlapping the label; print the max |noises|.""" import json, subprocess, sys, re from datetime import datetime, timedelta D = sys.argv[1]; K = sys.argv[2]; mode = sys.argv[3] # mode: outside | rolling: ids = sys.argv[4].split(',') if len(sys.argv) > 4 else None F = '%Y-%m-%dT%H:%MZ' p = lambda s: datetime.strptime(s, F) for l in open(f'{D}/work/requalify_input.jsonl'): L = json.loads(l) if ids and L['id'] not in ids: continue if L['family'] == 'No data': continue s, e = p(L['start']), p(L['end']) m = max(timedelta(hours=12), (e - s)) cmd = ['python3', 'tools/scripts/ranges.py', 'data/CLARIDEN', '--columns', 'corr_x,corr_y', '--k', K, '--start', (s - m).strftime(F), '--end', (e + m).strftime(F)] if mode == 'outside': cmd += ['--outside', f"{L['start']}/{L['end']}"] else: cmd += ['--rolling', mode.split(':')[1]] out = subprocess.run(cmd, capture_output=True, text=True).stdout col = None; hits = [] for line in out.splitlines(): mm = re.match(r'(corr_[xy]): ranges', line) if mm: col = mm.group(1); continue if line.startswith('=='): col = None; continue f = line.split() if col and len(f) >= 7 and re.match(r'\d{4}-', f[1] if len(f) > 1 else ''): rs, re_ = p(f[1]), p(f[2]) if re_ < s or rs > e: continue # skip ranges touching the label edges only (start/end = untouched by rule) if re_ <= s or rs >= e: continue hits.append((abs(float(f[5])), f[0], col, f[1], f[2], f[3], f[5])) hits.sort(reverse=True) print(f"{L['id']} {L['start']}→{L['end']} {L['family']} [{mode} k{K}]: {len(hits)} ranges") for h in hits[:6]: print(' ', h[1], h[2], h[3], h[4], 'n=' + h[5], h[6], 'noises')