#!/usr/bin/env python """classifier2: results table. Candidate FROC-low (CONTRA OOF, DEV-A, dev-test primary, LAB) + rescue + END-TO-END at operating points chosen OFF-TEST: t@b = score threshold at which base-novel (p_d331_max < 0.5) FALSE candidates per case = b, averaged over CONTRA OOF and DEV-A (mean sources) (each population's curve computed separately, threshold = mean of the two log-odds thresholds). cls2_report.py --heads h1,h2 --e2e_tag T [--budgets 0.25,0.5]""" from __future__ import annotations import argparse, sys from pathlib import Path import numpy as np import pandas as pd sys.path.insert(0, str(Path(__file__).resolve().parent)) import cls2_common as Q # noqa: E402 def lg(x): x = np.clip(x, 1e-6, 1 - 1e-6); return np.log(x / (1 - x)) def offtest_threshold(head, b): import cls2_eval as E P = E.pops(); ts = [] for pop in ("contra", "dev"): tab, _ = P[pop] t = E.attach(tab, head, pop) if t is None: return np.nan nov = t[(t["p_d331_max"] < 0.5) & (t["pos"] == 0)] ncase = t.groupby(["key", "src"]).ngroups s = np.sort(nov["score"].fillna(-1).to_numpy())[::-1] k = int(np.floor(b * ncase)) ts.append(lg(s[k]) if k < len(s) else -20) return float(1 / (1 + np.exp(-np.mean(ts)))) def e2e_at(tag, head, t, cohorts): import cls2_e2e_test as X s, _ = X.summarize(tag, cohorts) s = s[s["head"] == head] out = {} for b, g in s.groupby("base"): g = g.sort_values("t") r = g[g["t"] >= t].iloc[0] if (g["t"] >= t).any() else g.iloc[-1] out[b] = r return out def main(): ap = argparse.ArgumentParser(); ap.add_argument("--heads", required=True); ap.add_argument("--e2e_tag", default="") ap.add_argument("--budgets", default="0.25,0.5") a = ap.parse_args() import cls2_eval as E, cls2_novel as N heads = a.heads.split(",") res = E.evaluate(heads, quiet=True) resc = N.report(heads, pops=("test_prim", "test_lab")) rows = [] for h in heads: r = {"head": h} for p in ("contra", "dev", "test_prim", "test_lab"): r[f"froc_{p}"] = res.get((h, p), {}).get("froc_low", np.nan) r["auc_test_prim"] = res.get((h, "test_prim"), {}).get("auc", np.nan) for p in ("test_prim", "test_lab"): x = resc.get((h, p)) r[f"resc_{p}"] = "/".join(str(x[f"resc@{b}"]) for b in N.BUD) if x else "" if a.e2e_tag: for b in [float(x) for x in a.budgets.split(",")]: t = offtest_threshold(h, b) if not h.startswith("col:") else np.nan r[f"t@{b}"] = t for coh, nm in ((("atlas", "north"), "prim"), (("lab",), "lab")): try: ee = e2e_at(a.e2e_tag, h, t, coh) except Exception: continue for base, x in ee.items(): r[f"e2e_{nm}_{base}@{b}"] = f"{x['sm_recall']:.3f}/{x['les_precision']:.3f}/{x['les_f1']:.3f}/{x['dice']:.3f}" rows.append(r) df = pd.DataFrame(rows) pd.set_option("display.width", 300); pd.set_option("display.max_columns", 50) print(df.round(3).T.to_string()) return df if __name__ == "__main__": main()