"""For each studied target, over a window: corr minus the median of the healthy targets at the same instant (common mode removed), then its own noise (median abs consecutive diff of the raw corr, as ranges.py) and the measurements whose residual deviates from its rolling-1d median by more than K noises. Also the healthy targets, leave-one-out, for comparison. Usage: specific.py START END SENSOR[,SENSOR] [K]""" import sys, statistics from datetime import timedelta sys.path.insert(0, "tools/scripts") import data, common start, end = data.parse_time(sys.argv[1]), data.parse_time(sys.argv[2]) studied = sys.argv[3].split(",") K = float(sys.argv[4]) if len(sys.argv) > 4 else 5 by = data.load("data/NIEDERHORN") healthy = ["PWD_RMP_B12_020_C09","PWD_IMP_B08_025_C09","PWD_RMP_B15_018_C09","PWD_IMP_B07_017_C09","PWD_RMP_B12_018_C09", "PWD_RMP_B12_017_C09","PWD_RMP_B15_019_C09","PWD_RMP_B15_020_C09","PWD_RMP_B17_026_C09", "e02","e06","e09","e10","e13","e14","e15"] win = {s: [m for m in data.filter_window(ms, start, end)] for s, ms in by.items()} def series(s, col): return {m["t"]: m[col] for m in win[s] if m[col] is not None} for col in ("corr_x", "corr_y"): H = {s: series(s, col) for s in healthy} print(f"== {col} (K={K}, rolling ±1d, common mode = median of healthy targets)") for s in studied + healthy: own = series(s, col) if len(own) < 10: print(f"{s:22s} n={len(own)} (no curve)"); continue noise = common.own_noise([own[t] for t in sorted(own)]) res = {} for t, v in own.items(): others = [H[h][t] for h in healthy if h != s and t in H[h]] if len(others) >= 5: res[t] = v - statistics.median(others) ts = sorted(res) out = [] for t in ts: nb = [res[u] for u in ts if u != t and abs((u - t).total_seconds()) <= 86400] if len(nb) < 5: continue d = (res[t] - statistics.median(nb)) / noise if abs(d) > K: out.append((t, d)) ext = max(out, key=lambda x: abs(x[1])) if out else None kmm = None mm = [m for m in win[s] if m[col + "_mm"] is not None and m[col] not in (None, 0)] if mm: kmm = mm[0][col + "_mm"] / mm[0][col] days = len({t.date() for t, _ in out}) e = f"extreme {ext[1]:+.1f} noises ({ext[1]*noise:+.3f} px" + (f", {ext[1]*noise*kmm:+.2f} mm" if kmm else "") + f") at {data.iso(ext[0])}" if ext else "" tag = "*" if s in studied else " " print(f"{tag}{s:22s} n={len(own):4d} noise={noise:.4f} beyond: {len(out):3d} ({100*len(out)/max(1,len(res)):4.1f} %) on {days:2d} days {e}")