Download code/scripts/case_study.py from eduzrh/LNPBench: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/scripts/case_study.py
- Command line
-
hf download hf://datasets/eduzrh/LNPBench/code/scripts/case_study.py
-
curl -L -o case_study.py https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/scripts/case_study.py
3 kB
| """Case study: where do clinically approved ionizable lipids land? | |
| For a held-out study that contains DLin-MC3-DMA, SM-102 or ALC-0315, we train on | |
| the rest of LNPDB and report the predicted and measured percentile of every | |
| formulation built on an approved lipid. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from lnpbench import data, metrics, paths | |
| from lnpbench.registry import build_model | |
| from lnpbench.store import FeatureStore | |
| from lnpbench.trainer import TrainConfig | |
| APPROVED = ["DLin-MC3-DMA", "Dlin-MC3-DMA", "SM-102", "ALC-0315"] | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--models", nargs="+", default=["lion", "phcausal", "physset"]) | |
| ap.add_argument("--study", default="LM_2019") | |
| ap.add_argument("--fold", type=int, default=0) | |
| ap.add_argument("--device", default="cuda") | |
| ap.add_argument("--epochs", type=int, default=80) | |
| ap.add_argument("--out", default=str(paths.RESULTS_DIR / "case_study_approved.csv")) | |
| args = ap.parse_args() | |
| store = FeatureStore() | |
| task = [t for t in data.load_tasks(paths.CACHE_DIR / "tasks.pkl") | |
| if t.protocol == "external" and t.meta.get("study") == args.study and t.meta.get("fold") == args.fold][0] | |
| names = store.samples["IL_name"].astype(str) | |
| approved_mask = names.isin(APPROVED) | names.str.contains("MC3", case=False, na=False) | |
| rows = [] | |
| for model_name in args.models: | |
| model = build_model(model_name) | |
| model.fit(store, task.train_idx, task.val_idx, | |
| TrainConfig(epochs=args.epochs, device=args.device, seed=0)) | |
| pred = model.predict(store, task.test_idx) | |
| sub = store.samples.iloc[task.test_idx].copy() | |
| sub["pred"] = pred | |
| sub["true"] = store.y[task.test_idx] | |
| sub["pred_pct"] = sub["pred"].rank(pct=True) | |
| sub["true_pct"] = sub["true"].rank(pct=True) | |
| sel = sub[sub["IL_name"].astype(str).isin(APPROVED) | sub["IL_name"].astype(str).str.contains("MC3", case=False, na=False)] | |
| for _, r in sel.iterrows(): | |
| rows.append({ | |
| "model": model_name, "study": args.study, "LNP_ID": r["LNP_ID"], | |
| "IL_name": r["IL_name"], "true": r["true"], "pred": r["pred"], | |
| "true_pct": r["true_pct"], "pred_pct": r["pred_pct"], | |
| }) | |
| rho = metrics.spearman(sub["true"].to_numpy(), sub["pred"].to_numpy()) | |
| print(f"{model_name}: study-level rho={rho:.3f}, approved formulations={len(sel)}", flush=True) | |
| df = pd.DataFrame(rows) | |
| df.to_csv(args.out, index=False) | |
| if not df.empty: | |
| print(df.groupby(["model", "IL_name"]).agg(n=("pred_pct", "size"), | |
| mean_pred_pct=("pred_pct", "mean"), | |
| mean_true_pct=("true_pct", "mean")).round(3).to_string()) | |
| print("wrote", args.out) | |
| if __name__ == "__main__": | |
| main() |