File size: 3,637 Bytes
d1988f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | """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() |