"""Prints visibility.heat_shimmer's per-hour stats (share of sensors agitated at RATIO, median error ratio) between two instants of a range. Args: """ 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")] from data import load, iso # noqa import visibility as V # noqa day = lambda d: datetime.strptime(d, "%Y-%m-%d").replace(tzinfo=timezone.utc) a, b = day(sys.argv[3]) - timedelta(days=3), day(sys.argv[4]) ms = {s: [m for m in v if a <= m["t"] < b] for s, v in load(sys.argv[2]).items()} hours = {} for s, v in ms.items(): ag = V._ratios([(y["t"], abs(y["corr_x"] - x["corr_x"]) + abs(y["corr_y"] - x["corr_y"])) for x, y in zip(v, v[1:]) if x["corr_x"] is not None and y["corr_x"] is not None]) er = V._ratios([(m["t"], m["error_value"]) for m in v if m["error_value"] is not None]) for h in ag.keys() & er.keys(): hours.setdefault(h, {})[s] = (ag[h], er[h]) for h, r in sorted(hours.items()): if sys.argv[5] <= iso(h) <= sys.argv[6]: sh = sum(x >= V.RATIO for x, _ in r.values()) / len(r) print(iso(h), f"agitated share {sh:.2f} median agit {statistics.median(x for x, _ in r.values()):.2f} median err {statistics.median(e for _, e in r.values()):.2f}")