"""Per instant of a window: the sensors visibility.py calls high (point 1), the rejected ones (R), and the median MAD gap of point 8, on the range plus 3 days of history. Usage: python3 high_sensors.py FROM TO SHOW_FROM SHOW_TO""" import statistics import sys from datetime import timedelta sys.path.insert(0, sys.argv[1] + "/scripts") import visibility as v # noqa: E402 from data import iso, load, parse_time # noqa: E402 folder, a, b, sa, sb = sys.argv[2:7] ms = load(folder) h, e = parse_time(a) - timedelta(days=3), parse_time(b) chunk = {s: k for s, x in ms.items() if (k := [m for m in x if h <= m["t"] < e])} refs = {s for s, x in chunk.items() if x[0]["pattern_type"] == "global_ref"} rows = {} for s, x in chunk.items(): ev = [m["error_value"] for m in x if m["error_value"] is not None] med = statistics.median(ev) mad = statistics.median(abs(y - med) for y in ev) or 1e-9 for m in x: y = m["error_value"] if y is None or not (sa <= iso(m["t"]) <= sb): continue rej = m["error_threshold"] and y > m["error_threshold"] hi = y > med + v.K_MAD * mad and (y > v.FACTOR * med or rej and y > v.REJECT_FACTOR * med) r = rows.setdefault(iso(m["t"]), {"hi": [], "rej": [], "z": []}) r["z"].append((y - med) / mad) if hi: r["hi"].append(s + ("*" if s in refs else "")) if rej: r["rej"].append(s + ("*" if s in refs else "")) for t in sorted(rows): r = rows[t] print(t, f"z={statistics.median(r['z']):5.1f}", "high:", ",".join(sorted(r["hi"])), "| R:", ",".join(sorted(r["rej"])))