#!/usr/bin/env python3 """Score an option (one or more detectors OR'd) on cached long-audio tracks. python fa_score.py LIST.txt TAG:THR[:N] [TAG:THR[:N] ...] [--json OUT] Prints alarms, hours, alarms/hour, and each alarm (file, time s, which detector).""" import json, sys from pathlib import Path import numpy as np CACHE = Path("/data/ext/vita_fa2/cache") def track(d, n): c = np.concatenate([[0.0], np.cumsum(d, dtype=np.float64)]); ks = np.arange(146, len(d)) lo = np.maximum(0, ks + 1 - n); return 1 / (1 + np.exp(-(c[ks + 1] - c[lo]) / (ks + 1 - lo))) def score(files, dets): hours = 0.0; al = [] for f in files: st = Path(f).stem; ts = [] for tag, thr, n in dets: p = CACHE / tag / (st + ".npy") if not p.exists(): break ts.append((track(np.load(p), n), thr)) else: hours += (len(ts[0][0]) + 146) / 360000 hit = np.zeros(len(ts[0][0]), bool); who = np.zeros(len(hit), int) for j, (t, thr) in enumerate(ts): h = t >= thr; who[h & ~hit] = j; hit |= h nxt = -1 for k in np.flatnonzero(hit): if k >= nxt: nxt = k + 200 al.append({"file": f, "t": round((k + 147) * 0.01, 2), "det": dets[who[k]][0], "p": [round(float(t[k]), 4) for t, _ in ts]}) return hours, al if __name__ == "__main__": a = sys.argv[1:]; out = None if "--json" in a: i = a.index("--json"); out = a[i + 1]; a = a[:i] + a[i + 2:] files = [x.strip() for x in open(a[0]) if x.strip()] dets = [(s.split(":")[0], float(s.split(":")[1]), int(s.split(":")[2]) if s.count(":") > 1 else 110) for s in a[1:]] h, al = score(files, dets) print(f"{' + '.join(a[1:])}: {len(al)} alarms in {h:.2f} h = {len(al)/max(h,1e-9):.3f}/h") for x in al: print(" ", Path(x["file"]).name, x["t"], x["det"], x["p"]) if out: json.dump({"dets": dets, "hours": h, "alarms": al}, open(out, "w"), indent=1)