Files
T

32 lines
1.8 KiB
Python
Raw Normal View History

"""Per label: instants inside it and how many an event of its family covers; per event: instants and how many no label of
its family covers (wrongly flagged), with the families labelled there instead.
Usage: python3 perlabel.py <detectors folder> <station folder> <labels.jsonl> <from YYYY-MM-DD> <to YYYY-MM-DD>
"""
import sys
from collections import Counter
from pathlib import Path
det = Path(sys.argv[1]).resolve()
sys.path[:0] = [str(det / "bench"), str(det / "scripts")]
import score # noqa: E402
from data import iso, load, parse_time # noqa: E402
folder, lab, start, end = sys.argv[2], sys.argv[3], iso(parse_time(sys.argv[4])), iso(parse_time(sys.argv[5]))
ms = load(folder)
labels = [e for e in score.read_labels(lab, start, end) if e["family"] != "None"]
evts = score.events(ms, start, end, folder)
instants = sorted({iso(m["t"]) for v in ms.values() for m in v if start <= iso(m["t"]) < end})
inside = lambda e, t: e["start"] < t < e["end"] # noqa: E731
print("== labels")
for e in labels:
ts = [t for t in instants if inside(e, t)]
f = sum(any(v["category"] == e["family"] and v["start"] <= t <= v["end"] for v in evts) for t in ts)
other = Counter(v["category"] for t in ts for v in evts if v["start"] <= t <= v["end"] and v["category"] != e["family"])
print(f"{e['id']:<14} {e['start'][5:16]} → {e['end'][5:16]} {e['family']:<21} found {f}/{len(ts)} others: {dict(other)}")
print("== events")
for v in evts:
ts = [t for t in instants if v["start"] <= t <= v["end"]]
wrong = [t for t in ts if not any(inside(e, t) and e["family"] == v["category"] for e in labels)]
there = Counter(e["family"] for t in wrong for e in labels if inside(e, t))
print(f"{v['start'][5:16]} → {v['end'][5:16]} {v['category']:<21} {v['episodes'][0]['label']:<28} "
f"{len(ts):>4} instants, wrong {len(wrong):>4} {dict(there)}")