2026-11-13 12:38:29 +01:00
|
|
|
"""Benchmark of the detectors against a reference truth: every labelled station.
|
2026-11-13 12:19:52 +01:00
|
|
|
|
2026-11-13 12:38:29 +01:00
|
|
|
For each <STATION>.jsonl of --labels that has its folder <STATION>/ in --data, the score per family and per instant of
|
2026-11-14 13:53:19 +01:00
|
|
|
score.py, over the range of its labels: from the day of the first to the midnight after the last; with --from/--to, over
|
|
|
|
|
that range for every station, a station without a label on it included (several stations labelled the same days). Then the total,
|
2026-11-13 12:38:29 +01:00
|
|
|
family by family, which says whether the detectors do better overall. The block of a station is its score; the
|
|
|
|
|
others, the non-regression: a family that goes back on a station is a regression, even if the total rises.
|
|
|
|
|
Usage: python3 regression.py --labels <folder of <STATION>.jsonl> --data <folder of <STATION>/> [--stations A,B]
|
2026-11-14 13:53:19 +01:00
|
|
|
[--from YYYY-MM-DD --to YYYY-MM-DD]
|
2026-11-13 12:19:52 +01:00
|
|
|
"""
|
|
|
|
|
import argparse
|
|
|
|
|
from datetime import timedelta
|
2026-11-13 12:38:29 +01:00
|
|
|
from pathlib import Path
|
2026-11-13 12:19:52 +01:00
|
|
|
|
|
|
|
|
import score # next to it
|
|
|
|
|
|
|
|
|
|
from data import iso, parse_time # on the path through score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def label_range(path):
|
2026-11-13 12:38:29 +01:00
|
|
|
"""Labels JSONL -> (start, end) iso, in whole UTC days, end excluded."""
|
|
|
|
|
labels = score.read_labels(path)
|
2026-11-13 12:19:52 +01:00
|
|
|
start = min(e["debut"] for e in labels)[:10]
|
2026-11-13 12:38:29 +01:00
|
|
|
# ponytail: the end of the labelled batch is not written; midnight closest to the last bound (a label bound
|
|
|
|
|
# overflows by one measurement after midnight)
|
2026-11-13 12:19:52 +01:00
|
|
|
end = iso(parse_time(max(e["fin"] for e in labels)) + timedelta(hours=12))[:10]
|
|
|
|
|
return iso(parse_time(start)), iso(parse_time(end))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
p = argparse.ArgumentParser(usage=__doc__)
|
2026-11-13 12:38:29 +01:00
|
|
|
p.add_argument("--labels", required=True, help="folder of the labels, one <STATION>.jsonl per station")
|
|
|
|
|
p.add_argument("--data", required=True, help="folder of the station folders, <STATION>/")
|
2026-11-13 12:19:52 +01:00
|
|
|
p.add_argument("--stations", help="A,B: these stations only (a shorter run)")
|
2026-11-14 13:53:19 +01:00
|
|
|
p.add_argument("--from", dest="start", help="YYYY-MM-DD: this range for every station, not the range of its labels")
|
|
|
|
|
p.add_argument("--to", dest="end", help="YYYY-MM-DD, excluded (with --from)")
|
2026-11-13 12:19:52 +01:00
|
|
|
a = p.parse_args()
|
2026-11-14 13:53:19 +01:00
|
|
|
if bool(a.start) != bool(a.end):
|
|
|
|
|
p.error("--from and --to go together")
|
2026-11-13 12:19:52 +01:00
|
|
|
total = {}
|
2026-11-13 12:38:29 +01:00
|
|
|
for path in sorted(Path(a.labels).glob("*.jsonl")):
|
2026-11-13 12:19:52 +01:00
|
|
|
station = path.stem
|
|
|
|
|
if a.stations and station not in a.stations.split(","):
|
|
|
|
|
continue
|
2026-11-13 12:38:29 +01:00
|
|
|
folder = Path(a.data) / station
|
2026-11-13 12:19:52 +01:00
|
|
|
if not folder.is_dir():
|
2026-11-13 12:38:29 +01:00
|
|
|
print(f"{station}: no {folder}/, skipped\n")
|
2026-11-13 12:19:52 +01:00
|
|
|
continue
|
2026-11-14 13:53:19 +01:00
|
|
|
start, end = (iso(parse_time(a.start)), iso(parse_time(a.end))) if a.start else label_range(path)
|
2026-11-13 12:38:29 +01:00
|
|
|
n, labels = score.family_scores(str(folder), str(path), start, end)
|
2026-11-13 12:19:52 +01:00
|
|
|
score.show(f"{station}: {start} → {end}, {len(labels)} labels", n)
|
|
|
|
|
print()
|
|
|
|
|
for k, v in n.items():
|
|
|
|
|
total[k] = [x + y for x, y in zip(total.get(k, [0, 0, 0]), v)]
|
|
|
|
|
score.show("Total", total)
|