"""Per station: references rejected (error_value > threshold and > 2x its median), their error_value/threshold ratio, grouped by the family of the label covering the instant (quartiles). Usage: python3 refratio.py STATION ... """ import json import statistics import sys from pathlib import Path ROOT = Path("/home/maxime/aimsight-optimizer") sys.path[:0] = [str(ROOT / "detectors" / "scripts"), str(ROOT / "tools" / "scripts")] from data import load, is_reference, iso # noqa: E402 data, labels = Path(sys.argv[1]), Path(sys.argv[2]) for st in sys.argv[3:]: labs = [json.loads(x) for x in open(labels / f"{st}.jsonl") if x.strip()] out = {} for s, ms in load(str(data / st)).items(): if not is_reference(ms): continue e = [m["error_value"] for m in ms if m["error_value"] is not None] if not e: continue med = statistics.median(e) for m in ms: x, th = m["error_value"], m["error_threshold"] if x is None or not th or x <= th or x <= 2 * med: continue t = iso(m["t"]) fam = ",".join(sorted({l["family"] for l in labs if l["start"] < t < l["end"]})) or "-" out.setdefault(fam, []).append(x / th) for fam, r in sorted(out.items()): q = statistics.quantiles(r, n=4) if len(r) > 1 else r * 3 print(f"{st:12} {fam:40} n={len(r):4} q1={q[0]:.2f} med={q[1]:.2f} q3={q[2]:.2f}")