Files
T

39 lines
1.8 KiB
Python

# Per instant of [FROM, TO) (window read with 3 days of history, as score.py): median error_value z over the sensors (as
# visibility.py), share of sensors with z>=2.5, number high (K_MAD), number strong (rejected), label families.
# Args: <detectors dir> <station folder> <labels.jsonl> FROM TO [hourly]
import statistics, sys
from datetime import timedelta
from pathlib import Path
sys.path.insert(0, str(Path(sys.argv[1]).resolve() / "bench"))
import score # noqa: E402
import visibility as V # noqa: E402
from episodes import level # noqa: E402
from data import iso, load, parse_time # noqa: E402
det, folder, lab, a, b = sys.argv[1:6]
hourly = len(sys.argv) > 6
start, end = parse_time(a), parse_time(b)
ms = {s: k for s, v in load(folder).items() if (k := [m for m in v if start - timedelta(days=3) <= m["t"] < end])}
labels = score.read_labels(lab, iso(start), iso(end))
z, high, strong = {}, {}, {}
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)
for m in v:
t, x = m["t"], m["error_value"]
if x is None:
continue
z.setdefault(t, {})[s] = (x - med) / mad
if x > med + V.K_MAD * mad and x > V.FACTOR * med:
high.setdefault(t, set()).add(s)
if m["error_threshold"] and x > m["error_threshold"] and x > V.FACTOR * med:
strong.setdefault(t, set()).add(s)
for t in sorted(z):
if not (start <= t < end) or (hourly and t.minute >= 5):
continue
ti = iso(t)
fam = ",".join(sorted({e["family"][:4] + e["id"][11:] for e in labels if e["start"] < ti < e["end"]}))
zs = list(z[t].values())
print(ti[5:16], f"medz={statistics.median(zs):6.2f} q25={statistics.quantiles(zs, n=4)[0]:6.2f} n={len(zs):2d} high={len(high.get(t, ())):2d} strong={len(strong.get(t, ())):2d}", fam)