# Per station, the hours where at least SHARE of the sensors are agitated (visibility.heat_shimmer, without its error gate), binned by median agitation and by median error_value level ratio, counted inside / outside a Visibility label. Args: [detectors dir] import json import statistics import sys from collections import Counter from datetime import datetime from pathlib import Path det = Path(sys.argv[3] if len(sys.argv) > 3 else "/home/maxime/aimsight-detectors") / "scripts" sys.path[:0] = [str(det), "/home/maxime/aimsight-tools/scripts"] from data import load # noqa: E402 import visibility as v # noqa: E402 labels_dir, data_dir = Path(sys.argv[1]), Path(sys.argv[2]) when = lambda s: datetime.fromisoformat(s.replace("Z", "+00:00")) # noqa: E731 bins = (1.5, 2.0, 2.5, 3.0, 99) total = Counter() for f in sorted(labels_dir.glob("*.jsonl")): st = f.stem if not (data_dir / st).exists(): continue vis = [(when(l["start"]), when(l["end"])) for l in map(json.loads, filter(str.strip, open(f))) if l["family"] == "Visibility"] ms = load(str(data_dir / st)) hours = {} for s, x in ms.items(): ag = v._ratios([(q["t"], abs(q["corr_x"] - p["corr_x"]) + abs(q["corr_y"] - p["corr_y"])) for p, q in zip(x, x[1:]) if p["corr_x"] is not None and q["corr_x"] is not None]) er = v._ratios([(m["t"], m["error_value"]) for m in x if m["error_value"] is not None]) for h in ag.keys() & er.keys(): hours.setdefault(h, {})[s] = (ag[h], er[h]) c = Counter() for h, r in hours.items(): if sum(a >= v.RATIO for a, _ in r.values()) < v.SHARE * len(r): continue agit = statistics.median(a for a, _ in r.values()) err = statistics.median(e for _, e in r.values()) b = next(x for x in bins if agit < x) inside = any(a <= h + v.HOUR / 2 <= e for a, e in vis) key = (b, "err+" if err >= v.RATIO else "err-", "in" if inside else "out") c[key] += 1 total[key] += 1 print(st, " ".join(f"<{b}/{g}/{i}={n}" for (b, g, i), n in sorted(c.items()))) print("TOTAL") for (b, g, i), n in sorted(total.items()): print(f" agit<{b} {g} {i}: {n}")