|
| 1 | +""" |
| 2 | +RXN-Path-39: 13 organic reactions (wB97M-V/def2-TZVPD), 3 path-sampling |
| 3 | +trajectories each, 11 arc-length-equidistant frames per trajectory. |
| 4 | +
|
| 5 | +For each trajectory the first frame (index 0) is chosen as the reference. |
| 6 | +The task measures how accurately a LAM reproduces the relative energies of all |
| 7 | +other frames with respect to that reference, i.e. |
| 8 | +
|
| 9 | + ΔE_DFT(i) = E_DFT(i) − E_DFT(frame 0) [kcal/mol] |
| 10 | + ΔE_LAM(i) = E_LAM(i) − E_LAM(frame 0) [kcal/mol] |
| 11 | +
|
| 12 | +and reports MAE and RMSE over all 39 × 10 = 390 (reaction, frame) pairs. |
| 13 | +""" |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | +import logging |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from ase.io import Trajectory |
| 20 | +from sklearn.metrics import mean_absolute_error, root_mean_squared_error |
| 21 | + |
| 22 | +from lambench.models.ase_models import ASEModel |
| 23 | + |
| 24 | +EV_TO_KCAL = 23.0609 # 1 eV = 23.0609 kcal/mol |
| 25 | + |
| 26 | + |
| 27 | +def run_inference(model: ASEModel, test_data: Path) -> dict[str, float]: |
| 28 | + """ |
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + model : ASEModel |
| 32 | + test_data : Path |
| 33 | + Root of the trajectory tree. Expected layout:: |
| 34 | +
|
| 35 | + test_data/ |
| 36 | + <reaction_id>/ |
| 37 | + traj_0.traj |
| 38 | + traj_1.traj |
| 39 | + traj_2.traj |
| 40 | + ... |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + dict with keys "MAE" and "RMSE" in kcal/mol. |
| 45 | + """ |
| 46 | + calc = model.calc |
| 47 | + label_diffs: list[float] = [] |
| 48 | + pred_diffs: list[float] = [] |
| 49 | + |
| 50 | + traj_files = sorted(test_data.rglob("traj_*.traj")) |
| 51 | + if not traj_files: |
| 52 | + raise FileNotFoundError(f"No traj_*.traj files found under {test_data}") |
| 53 | + |
| 54 | + for traj_path in traj_files: |
| 55 | + frames = list(Trajectory(traj_path)) |
| 56 | + |
| 57 | + # DFT reference energies (eV, stored by SinglePointCalculator) |
| 58 | + dft_energies = np.array([a.get_potential_energy() for a in frames]) |
| 59 | + ref_dft_kcal = dft_energies[0] * EV_TO_KCAL |
| 60 | + |
| 61 | + # LAM energy for the first frame (reference) |
| 62 | + frames[0].calc = calc |
| 63 | + try: |
| 64 | + ref_pred_kcal = frames[0].get_potential_energy() * EV_TO_KCAL |
| 65 | + except Exception as e: |
| 66 | + logging.error( |
| 67 | + f"Failed predicting reference frame (idx=0) in {traj_path}: {e}" |
| 68 | + ) |
| 69 | + continue # skip this trajectory entirely |
| 70 | + |
| 71 | + # Relative energies for every non-reference frame |
| 72 | + for i, atoms in enumerate(frames): |
| 73 | + if i == 0: |
| 74 | + continue |
| 75 | + |
| 76 | + label_diffs.append(dft_energies[i] * EV_TO_KCAL - ref_dft_kcal) |
| 77 | + |
| 78 | + atoms.calc = calc |
| 79 | + try: |
| 80 | + pred_kcal = atoms.get_potential_energy() * EV_TO_KCAL |
| 81 | + except Exception as e: |
| 82 | + logging.error(f"Failed predicting frame {i} of {traj_path}: {e}") |
| 83 | + pred_kcal = np.nan |
| 84 | + pred_diffs.append(pred_kcal - ref_pred_kcal) |
| 85 | + |
| 86 | + label_arr = np.array(label_diffs) |
| 87 | + pred_arr = np.array(pred_diffs) |
| 88 | + valid = np.isfinite(pred_arr) |
| 89 | + |
| 90 | + if not valid.any(): |
| 91 | + logging.error("All predictions failed; returning NaN metrics.") |
| 92 | + return {"MAE": np.nan, "RMSE": np.nan} |
| 93 | + |
| 94 | + if not valid.all(): |
| 95 | + n_failed = int((~valid).sum()) |
| 96 | + logging.warning( |
| 97 | + f"{n_failed} frame(s) failed inference and were excluded from metrics." |
| 98 | + ) |
| 99 | + |
| 100 | + return { |
| 101 | + "MAE": float(mean_absolute_error(label_arr[valid], pred_arr[valid])), |
| 102 | + "RMSE": float(root_mean_squared_error(label_arr[valid], pred_arr[valid])), |
| 103 | + } |
0 commit comments