"""Consolidate every discrete-mode-decoder run into one table. The screen uses the four fold-0 external tasks (LM_2019, BL_2023, SL_2020, ZC_2023), 30 epochs, batch 256 — the same protocol as the tuned PhysSet-LNP reference in ``results_gpu/full_physset_phys.csv`` (seed 0 mean 0.3597 over all 20 tasks). ``physmode_base`` is the nested control: the identical module with the mode branch switched off, so any difference is attributable to the decoder. PYTHONPATH=src python scripts/mode_results.py """ from __future__ import annotations import glob from pathlib import Path import numpy as np import pandas as pd from scipy import stats FILES = ( ["results/screen_physset_phys.csv", "results/screen2_physmode_base.csv"] + sorted(glob.glob("results/screen_*.csv")) + sorted(glob.glob("results/screen2_*.csv")) + sorted(glob.glob("results/screen3_*.csv")) + sorted(glob.glob("results/abl_physmode_chem_*.csv")) ) # human-readable labels for the write-up # the r1 and r2 screens ran under the same registry name, so the configuration # is recovered from the file that produced the row RENAME = { "screen_physmode.csv": "physmode_r1", "screen_diffmode.csv": "diffmode_r1", "screen_physset_phys.csv": "physset_phys", "screen2_physmode.csv": "physmode", "screen2_diffmode.csv": "diffmode", "screen2_physmode_base.csv": "physmode_base", "screen3_physmode_chem.csv": "physmode_chem", "screen3_diffmode_chem.csv": "diffmode_chem", "screen3_physmode_chem_hard.csv": "physmode_chem_hard", } LABELS = { "physset_phys": "PhysSet-LNP (tuned reference)", "physmode_base": "Base-Only (nested control)", "physmode": "PhysMode r2 (lam_base=0, alpha=1)", "physmode_r1": "PhysMode r1 (alpha=0.1, lam_base>0)", "physmode_chem": "PhysMode-LNP (chemistry-only modes)", "physmode_chem_hard": "PhysMode, hard assignment", "physmode_chem_novq": "PhysMode, no codebook (free prototypes)", "physmode_chem_k8": "PhysMode, K=8", "physmode_chem_k32": "PhysMode, K=32", "physmode_chem_resonly": "PhysMode, residual only", "physmode_chem_lin": "PhysMode, linear mixing", "diffmode": "DiffMode r2", "diffmode_r1": "DiffMode r1", "diffmode_chem": "DiffMode-LNP (chemistry-only modes)", } ORDER = ["physset_phys", "physmode_base", "physmode_r1", "physmode", "physmode_chem", "physmode_chem_hard", "physmode_chem_novq", "physmode_chem_k8", "physmode_chem_k32", "physmode_chem_resonly", "physmode_chem_lin", "diffmode_r1", "diffmode", "diffmode_chem"] def main(): frames = [] for f in FILES: if not Path(f).exists(): continue d = pd.read_csv(f) d = d[d["task"].astype(str).str.contains("_fold")] if not len(d): continue d["source"] = Path(f).name if Path(f).name in RENAME: d["model"] = RENAME[Path(f).name] frames.append(d) d = pd.concat(frames, ignore_index=True) d = d.drop_duplicates(subset=["model", "task", "seed"], keep="last") d.to_csv("results/mode_decoders.csv", index=False) print("wrote results/mode_decoders.csv", d.shape) ref = pd.read_csv("results_gpu/full_physset_phys.csv") ref_s0 = ref[ref.seed == 0].set_index("task")["spearman"] rows = [] for m, g in d.groupby("model"): s0 = g[g.seed == 0] r = { "model": m, "label": LABELS.get(m, m), "n_runs": len(g), "n_tasks": s0.task.nunique(), "seeds": ",".join(str(v) for v in sorted(g.seed.unique())), "mean_all": g.spearman.mean(), "mean_seed0": s0.spearman.mean(), "val_seed0": s0.val_spearman.mean(), "runtime_s": g.runtime_s.mean(), } # paired against the tuned PhysSet-LNP reference on the same four tasks j = s0.set_index("task")["spearman"].reindex(ref_s0.index.reindex(s0.task.unique())[0]) pair = pd.concat([j, ref_s0.reindex(j.index)], axis=1).dropna() if len(pair) >= 4: delta = pair.iloc[:, 0] - pair.iloc[:, 1] r["delta_vs_physset"] = delta.mean() r["p_vs_physset"] = (stats.wilcoxon(delta).pvalue if np.any(delta != 0) else np.nan) if m == "physset_phys": r["delta_vs_physset"] = 0.0 r["p_vs_physset"] = float("nan") rows.append(r) out = pd.DataFrame(rows) out["order"] = out.model.map({k: i for i, k in enumerate(ORDER)}).fillna(99) out = out.sort_values("order").drop(columns="order") out.to_csv("results/mode_decoders_summary.csv", index=False) pd.set_option("display.width", 220) print(out[["model", "label", "n_tasks", "seeds", "mean_seed0", "delta_vs_physset", "p_vs_physset", "val_seed0", "runtime_s"]].round(4).to_string(index=False)) # the full 20-task confirmation f20 = pd.read_csv("results/full20_diffmode_chem.csv") f20 = f20.merge(ref_s0.rename("ref"), left_on="task", right_index=True, how="inner") f20["delta"] = f20.spearman - f20.ref print("\n20-task confirmation (DiffMode-LNP vs tuned PhysSet-LNP, seed 0):") print(f" mean {f20.spearman.mean():.4f} vs {f20.ref.mean():.4f} " f"delta {f20.delta.mean():+.4f} (sd {f20.delta.std(ddof=1):.4f}) " f"wilcoxon p={stats.wilcoxon(f20.delta).pvalue:.3g} n={len(f20)}") print(f20.groupby("study") .agg(n=("delta", "size"), base=("ref", "mean"), mode=("spearman", "mean"), delta=("delta", "mean")).round(4).to_string()) if __name__ == "__main__": main()