"""Print, hour by hour over a span, the visibility signals: median z of error_value (MAD above each sensor's median, as visibility.py), the number of high sensors (K_MAD), of rejected ones, and the median flux ratio to each sensor's median. Usage: python3 diffuse.py """ import statistics import sys from datetime import datetime, timedelta, timezone from pathlib import Path det = Path(sys.argv[1]).resolve() sys.path[:0] = [str(det / "scripts"), str(det.parents[0] / "aimsight-tools" / "scripts")] import visibility as vis # noqa: E402 from episodes import level # noqa: E402 from data import load # noqa: E402 day = lambda d: datetime.strptime(d, "%Y-%m-%d").replace(tzinfo=timezone.utc) # noqa: E731 a, b = day(sys.argv[3]) - timedelta(days=3), day(sys.argv[4]) lo, hi = datetime.fromisoformat(sys.argv[5].replace("Z", "+00:00")), datetime.fromisoformat(sys.argv[6].replace("Z", "+00:00")) ms = {s: k for s, v in load(sys.argv[2]).items() if (k := [m for m in v if a <= m["t"] < b])} keys = sorted({k for v in ms.values() for m in v for k in m}) z, hc, rj, fl = {}, {}, {}, {} for s, v in ms.items(): e = [m["error_value"] for m in v if m["error_value"] is not None] if len(e) < 10: continue med, mad = level(e) fx = [m.get("flux") for m in v if m.get("flux")] fmed = statistics.median(fx) if fx else None for m in v: t, x = m["t"], m["error_value"] if x is None or not lo <= t < hi: continue h = t.replace(minute=0, second=0, microsecond=0) z.setdefault(h, []).append((x - med) / mad) hc[h] = hc.get(h, 0) + (x > med + vis.K_MAD * mad and x > vis.FACTOR * med) rj[h] = rj.get(h, 0) + bool(m["error_threshold"] and x > m["error_threshold"]) if fmed and m.get("flux"): fl.setdefault(h, []).append(m["flux"] / fmed) print("fields:", keys) for h in sorted(z): print(f"{h:%m-%d %H:%M} z_med={statistics.median(z[h]):6.2f} high/h={hc[h]:4d} rejected/h={rj[h]:4d} " f"flux={statistics.median(fl[h]) if h in fl else float('nan'):.2f} n={len(z[h])}")