"""Compare mode-decoder variants against the PhysSet-LNP baseline. Paired, per-task comparison: every model in ``--files`` is aligned on (task, seed) and differenced against the reference so that the large fold-to-fold variance of the external studies cancels out. PYTHONPATH=src python scripts/mode_analyze.py --ref physset_phys \ --files results/screen_physset_phys.csv results/screen_physmode.csv ... """ from __future__ import annotations import argparse from itertools import combinations import numpy as np import pandas as pd from scipy import stats def paired(a: pd.DataFrame, b: pd.DataFrame, ref: str, other: str) -> dict: m = a.merge(b, on=["task", "seed"], suffixes=("_r", "_o")) if not len(m): return {} d = m["spearman_o"] - m["spearman_r"] out = { "ref": ref, "model": other, "n": len(m), "mean_ref": m["spearman_r"].mean(), "mean_model": m["spearman_o"].mean(), "delta": d.mean(), "sd": d.std(ddof=1) if len(d) > 1 else float("nan"), } if len(m) >= 6 and np.any(d != 0): out["p_wilcoxon"] = stats.wilcoxon(d).pvalue out["p_ttest"] = stats.ttest_rel(m["spearman_o"], m["spearman_r"]).pvalue else: out["p_wilcoxon"] = float("nan") out["p_ttest"] = float("nan") return out def main(): ap = argparse.ArgumentParser() ap.add_argument("--files", nargs="+", required=True) ap.add_argument("--ref", default="physset_phys") ap.add_argument("--out", default=None) args = ap.parse_args() frames = {} for f in args.files: d = pd.read_csv(f) d = d[d["task"].astype(str).str.contains("fold")] for m, g in d.groupby("model"): frames.setdefault(m, []).append(g) frames = {m: pd.concat(g, ignore_index=True) for m, g in frames.items()} print("== per-model mean Spearman (all tasks/seeds) ==") for m, g in frames.items(): per = g.groupby("task")["spearman"].mean() print(f"{m:18s} mean={g.spearman.mean():.4f} sd_tasks={per.std(ddof=1):.4f} " f"n={len(g)} val={g.val_spearman.mean():.4f}") print(" " + " ".join(f"{k.split('_fold')[0]}{k[-1]}:{v:.3f}" for k, v in per.items())) ref = frames.get(args.ref) if ref is None: raise SystemExit(f"reference {args.ref} missing") print("\n== paired deltas vs", args.ref, "==") rows = [] for m, g in frames.items(): if m == args.ref: continue r = paired(ref, g, args.ref, m) if r: rows.append(r) print(f"{m:18s} d={r['delta']:+.4f} (sd {r['sd']:.4f}) " f"{r['mean_ref']:.4f}->{r['mean_model']:.4f} " f"p_wilcoxon={r['p_wilcoxon']:.3g} p_t={r['p_ttest']:.3g}") if rows: df = pd.DataFrame(rows) if args.out: df.to_csv(args.out, index=False) print("wrote", args.out) if __name__ == "__main__": main()