trip50 / examples /evaluate_mlip.py
patonlab's picture
initial release: TRIP50 v1.0.0 dataset, scripts, examples
e579e7e verified
"""Reference MLIP evaluator for TRIP50.
Loads structures from `data/trip50.extxyz`, runs an ASE Calculator over every
frame, then computes per-reaction ΔE_rxn / ΔE_fwd / ΔE_rev (kcal/mol) using the
reaction definitions in `data/reactions.parquet`. Reports an MAE table against
the DLPNO-CCSD(T) reference.
Replace `make_calculator()` below with your MLIP. The placeholder uses
`ase.calculators.emt.EMT` so the script runs CPU-only out of the box; EMT
support spans only a small subset of the periodic table, so most species will
fall back to NaN and the MAE table will look terrible — that is expected. The
purpose of this file is to show the wiring.
"""
from __future__ import annotations
from pathlib import Path
import numpy as np
import pandas as pd
from ase.calculators.calculator import Calculator
from ase.calculators.emt import EMT
from ase.io import read
EV_TO_HARTREE = 1.0 / 27.211386245988
HARTREE_TO_KCAL = 627.5094740631
DATA = Path(__file__).resolve().parent.parent / "data"
def make_calculator() -> Calculator:
"""Replace with your MLIP's ASE Calculator."""
return EMT()
def species_total_energies(extxyz_path: Path, calc: Calculator) -> dict[str, float]:
"""Run `calc` over every frame; return {species_id: total_energy in Hartree}."""
totals: dict[str, float] = {}
for atoms in read(extxyz_path, index=":"):
atoms.calc = calc
try:
e_ev = atoms.get_potential_energy()
totals[atoms.info["species_id"]] = e_ev * EV_TO_HARTREE
except Exception as exc:
print(f" {atoms.info['species_id']}: {type(exc).__name__}: {exc}")
totals[atoms.info["species_id"]] = float("nan")
return totals
def reaction_predictions(reactions: pd.DataFrame, totals: dict[str, float]) -> pd.DataFrame:
"""Predict ΔE_rxn, ΔE_fwd, ΔE_rev (kcal/mol) per reaction. Missing partners
(unimolecular legs) contribute 0."""
def E(sid):
return 0.0 if sid is None else totals.get(sid, float("nan"))
out = reactions.copy()
R = out.r1_species_id.map(E) + out.r2_species_id.map(E)
P = out.p1_species_id.map(E) + out.p2_species_id.map(E)
TS = out.ts_species_id.map(E)
out["dE_rxn_kcal_pred"] = (P - R) * HARTREE_TO_KCAL
out["dE_fwd_kcal_pred"] = (TS - R) * HARTREE_TO_KCAL
out["dE_rev_kcal_pred"] = (TS - P) * HARTREE_TO_KCAL
return out
def mae(a: pd.Series, b: pd.Series) -> float:
diff = (a - b).dropna()
return float(diff.abs().mean())
def main() -> None:
extxyz = DATA / "trip50.extxyz"
reactions = pd.read_parquet(DATA / "reactions.parquet")
print(f"Loading {extxyz}")
calc = make_calculator()
print(f"Running {type(calc).__name__} over 156 frames…")
totals = species_total_energies(extxyz, calc)
n_ok = sum(1 for v in totals.values() if not np.isnan(v))
print(f" ok: {n_ok}/{len(totals)}")
pred = reaction_predictions(reactions, totals)
print(
f"\nMAE vs DLPNO-CCSD(T) (kcal/mol):\n"
f" ΔE_rxn = {mae(pred.dE_rxn_kcal_pred, pred.dE_rxn_kcal_dlpno):.2f}\n"
f" ΔE_fwd = {mae(pred.dE_fwd_kcal_pred, pred.dE_fwd_kcal_dlpno):.2f}\n"
f" ΔE_rev = {mae(pred.dE_rev_kcal_pred, pred.dE_rev_kcal_dlpno):.2f}"
)
if __name__ == "__main__":
main()