"""Diagnose thermal_displacement._wave on a station window: per target and column, the median day likeness and the R2. Usage: python3 wave.py [history days, default 3] """ 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 thermal_displacement as td # noqa: E402 from data import load, is_reference # noqa: E402 day = lambda d: datetime.strptime(d, "%Y-%m-%d").replace(tzinfo=timezone.utc) # noqa: E731 hist = int(sys.argv[5]) if len(sys.argv) > 5 else 3 a, b = day(sys.argv[3]) - timedelta(days=hist), day(sys.argv[4]) ms = {s: k for s, v in load(sys.argv[2]).items() if (k := [m for m in v if a <= m["t"] < b])} for cols in (("corr_x", "corr_y"), ("x", "y")): n_ok = 0 n = 0 for s, v in sorted(ms.items()): if is_reference(v): continue n += 1 row = [] ok = False for c in cols: ax = td._axis(v, c) lk = statistics.median(ax[1].values()) if ax and ax[1] else None ok |= lk is not None and lk >= td.REPRO r2 = td._sine_r2(v, c) row.append(f"{c}: like={lk if lk is None else round(lk, 2)} days={len(ax[1]) if ax else 0} r2={r2 if r2 is None else round(r2, 2)}") n_ok += ok print(s, " | ".join(row)) print(cols, f"targets with a cycle: {n_ok}/{n}\n")