Download code/scripts/analyze.py from eduzrh/LNPBench: direct link, hf CLI and curl.
- Browser
- Download file 3.64 kB
-
https://huggingface.co/datasets/eduzrh/LNPBench/resolve/4168d53fc6eb5ca3fc229a71f6501bdffe77af08/code/scripts/analyze.py
- Command line
-
hf download hf://datasets/eduzrh/LNPBench@4168d53fc6eb5ca3fc229a71f6501bdffe77af08/code/scripts/analyze.py
-
curl -L -o analyze.py https://huggingface.co/datasets/eduzrh/LNPBench/resolve/4168d53fc6eb5ca3fc229a71f6501bdffe77af08/code/scripts/analyze.py
3.64 kB
| """Aggregate benchmark results into paper tables, statistics and figures.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from itertools import combinations | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from scipy import stats | |
| from lnpbench import paths | |
| MODEL_ORDER = [ | |
| "mean", "xgboost", "randomforest", "extratrees", "mlp_fp", "lion", "agile", | |
| "phcausal", "physset", "seqcausal", | |
| ] | |
| PRETTY = { | |
| "mean": "Mean predictor", | |
| "xgboost": "XGBoost (descriptors)", | |
| "randomforest": "Random forest", | |
| "extratrees": "Extra trees", | |
| "mlp_fp": "Morgan FP + MLP", | |
| "lion": "LiON (D-MPNN)", | |
| "agile": "AGILE (GIN)", | |
| "phcausal": "PhyCausal-LNP (A)", | |
| "physset": "PhysSet-LNP (B)", | |
| "seqcausal": "SeqCausal-LNP (C)", | |
| } | |
| def load(path: Path) -> pd.DataFrame: | |
| df = pd.read_csv(path) | |
| df = df[df["spearman"].notna()] | |
| return df | |
| def summarise(df: pd.DataFrame, protocols=("external",)) -> pd.DataFrame: | |
| d = df[df["protocol"].isin(protocols)] | |
| g = d.groupby(["model"]).agg( | |
| spearman_mean=("spearman", "mean"), | |
| spearman_std=("spearman", "std"), | |
| pearson_mean=("pearson", "mean"), | |
| r2_mean=("r2", "mean"), | |
| ef10_mean=("ef10", "mean"), | |
| ndcg10_mean=("ndcg10", "mean"), | |
| n=("spearman", "size"), | |
| ).reset_index() | |
| order = {m: i for i, m in enumerate(MODEL_ORDER)} | |
| g["ord"] = g["model"].map(lambda m: order.get(m, 99)) | |
| return g.sort_values("ord").drop(columns="ord") | |
| def per_study(df: pd.DataFrame) -> pd.DataFrame: | |
| d = df[df["protocol"] == "external"] | |
| return d.pivot_table(index="model", columns="study", values="spearman", aggfunc="mean") | |
| def paired_tests(df: pd.DataFrame, protocol: str = "external", reference: str = "agile") -> pd.DataFrame: | |
| d = df[df["protocol"] == protocol] | |
| wide = d.pivot_table(index=["task", "seed"], columns="model", values="spearman") | |
| rows = [] | |
| models = [m for m in MODEL_ORDER if m in wide.columns] | |
| for a, b in combinations(models, 2): | |
| sub = wide[[a, b]].dropna() | |
| if len(sub) < 8: | |
| continue | |
| stat, p = stats.wilcoxon(sub[a], sub[b]) | |
| rows.append({"model_a": a, "model_b": b, "delta": float((sub[a] - sub[b]).mean()), "p": float(p), "n": len(sub)}) | |
| return pd.DataFrame(rows) | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--results", nargs="+", default=[str(paths.RESULTS_DIR / "benchmark.csv")]) | |
| ap.add_argument("--outdir", default=str(paths.RESULTS_DIR)) | |
| args = ap.parse_args() | |
| df = pd.concat([load(Path(p)) for p in args.results], ignore_index=True) | |
| outdir = Path(args.outdir) | |
| outdir.mkdir(parents=True, exist_ok=True) | |
| for protocol in ("external", "amine", "cv5"): | |
| sub = df[df["protocol"] == protocol] | |
| if sub.empty: | |
| continue | |
| tab = summarise(sub, protocols=(protocol,)) | |
| tab.to_csv(outdir / f"table_{protocol}.csv", index=False) | |
| print(f"\n=== {protocol} ===") | |
| print(tab.round(4).to_string(index=False)) | |
| ext = df[df["protocol"] == "external"] | |
| if not ext.empty: | |
| ps = per_study(ext).round(3) | |
| ps.to_csv(outdir / "table_external_per_study.csv") | |
| print("\n=== external, per study ===") | |
| print(ps.to_string()) | |
| pt = paired_tests(ext) | |
| pt.to_csv(outdir / "paired_tests.csv", index=False) | |
| print("\n=== paired Wilcoxon (external) ===") | |
| print(pt.round(4).to_string(index=False)) | |
| df.to_csv(outdir / "all_results.csv", index=False) | |
| print(f"\nwrote tables to {outdir}") | |
| if __name__ == "__main__": | |
| main() |