|
| 1 | +"""Analyse X23 benchmark.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from ase import units |
| 6 | +from ase.io import read, write |
| 7 | +import pytest |
| 8 | + |
| 9 | +from ml_peg.analysis.utils.decorators import build_table, plot_parity |
| 10 | +from ml_peg.analysis.utils.utils import mae |
| 11 | +from ml_peg.app import APP_ROOT |
| 12 | +from ml_peg.calcs import CALCS_ROOT |
| 13 | +from ml_peg.models.get_models import get_model_names |
| 14 | +from ml_peg.models.models import current_models |
| 15 | + |
| 16 | +MODELS = get_model_names(current_models) |
| 17 | +CALC_PATH = CALCS_ROOT / "molecular_crystal" / "X23" / "outputs" |
| 18 | +OUT_PATH = APP_ROOT / "data" / "molecular_crystal" / "X23" |
| 19 | + |
| 20 | +# Unit conversion |
| 21 | +EV_TO_KJ_PER_MOL = units.mol / units.kJ |
| 22 | + |
| 23 | +DEFAULT_THRESHOLDS = {"MAE": (0.0, 100.0)} |
| 24 | + |
| 25 | + |
| 26 | +def get_system_names() -> list[str]: |
| 27 | + """ |
| 28 | + Get list of X23 system names. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + list[str] |
| 33 | + List of system names from structure files. |
| 34 | + """ |
| 35 | + system_names = [] |
| 36 | + for model_name in MODELS: |
| 37 | + model_dir = CALC_PATH / model_name |
| 38 | + if model_dir.exists(): |
| 39 | + xyz_files = sorted(model_dir.glob("*.xyz")) |
| 40 | + if xyz_files: |
| 41 | + for xyz_file in xyz_files: |
| 42 | + atoms = read(xyz_file) |
| 43 | + system_names.append(atoms.info["system"]) |
| 44 | + break |
| 45 | + return system_names |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +@plot_parity( |
| 50 | + filename=OUT_PATH / "figure_lattice_energies.json", |
| 51 | + title="X23 Lattice Energies", |
| 52 | + x_label="Predicted lattice energy / kJ/mol", |
| 53 | + y_label="Reference lattice energy / kJ/mol", |
| 54 | + hoverdata={ |
| 55 | + "System": get_system_names(), |
| 56 | + }, |
| 57 | +) |
| 58 | +def lattice_energies() -> dict[str, list]: |
| 59 | + """ |
| 60 | + Get lattice energies for all X23 systems. |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + dict[str, list] |
| 65 | + Dictionary of reference and predicted lattice energies. |
| 66 | + """ |
| 67 | + results = {"ref": []} | {mlip: [] for mlip in MODELS} |
| 68 | + ref_stored = False |
| 69 | + |
| 70 | + for model_name in MODELS: |
| 71 | + model_dir = CALC_PATH / model_name |
| 72 | + |
| 73 | + if not model_dir.exists(): |
| 74 | + continue |
| 75 | + |
| 76 | + xyz_files = sorted(model_dir.glob("*.xyz")) |
| 77 | + if not xyz_files: |
| 78 | + continue |
| 79 | + |
| 80 | + for xyz_file in xyz_files: |
| 81 | + structs = read(xyz_file, index=":") |
| 82 | + |
| 83 | + solid_energy = structs[0].get_potential_energy() |
| 84 | + num_molecules = structs[0].info["num_molecules"] |
| 85 | + system = structs[0].info["system"] |
| 86 | + molecule_energy = structs[1].get_potential_energy() |
| 87 | + |
| 88 | + lattice_energy = (solid_energy / num_molecules) - molecule_energy |
| 89 | + results[model_name].append(lattice_energy * EV_TO_KJ_PER_MOL) |
| 90 | + |
| 91 | + # Copy individual structure files to app data directory |
| 92 | + structs_dir = OUT_PATH / model_name |
| 93 | + structs_dir.mkdir(parents=True, exist_ok=True) |
| 94 | + write(structs_dir / f"{system}.xyz", structs) |
| 95 | + |
| 96 | + # Store reference energies (only once) |
| 97 | + if not ref_stored: |
| 98 | + results["ref"].append(structs[0].info["ref"]) |
| 99 | + |
| 100 | + ref_stored = True |
| 101 | + |
| 102 | + return results |
| 103 | + |
| 104 | + |
| 105 | +@pytest.fixture |
| 106 | +def x23_errors(lattice_energies) -> dict[str, float]: |
| 107 | + """ |
| 108 | + Get mean absolute error for lattice energies. |
| 109 | +
|
| 110 | + Parameters |
| 111 | + ---------- |
| 112 | + lattice_energies |
| 113 | + Dictionary of reference and predicted lattice energies. |
| 114 | +
|
| 115 | + Returns |
| 116 | + ------- |
| 117 | + dict[str, float] |
| 118 | + Dictionary of predicted lattice energy errors for all models. |
| 119 | + """ |
| 120 | + results = {} |
| 121 | + for model_name in MODELS: |
| 122 | + if lattice_energies[model_name]: |
| 123 | + results[model_name] = mae( |
| 124 | + lattice_energies["ref"], lattice_energies[model_name] |
| 125 | + ) |
| 126 | + else: |
| 127 | + results[model_name] = None |
| 128 | + return results |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +@build_table( |
| 133 | + filename=OUT_PATH / "x23_metrics_table.json", |
| 134 | + metric_tooltips={ |
| 135 | + "Model": "Name of the model", |
| 136 | + "MAE": "Mean Absolute Error for all systems (kJ/mol)", |
| 137 | + }, |
| 138 | + thresholds=DEFAULT_THRESHOLDS, |
| 139 | +) |
| 140 | +def metrics(x23_errors: dict[str, float]) -> dict[str, dict]: |
| 141 | + """ |
| 142 | + Get all X23 metrics. |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + x23_errors |
| 147 | + Mean absolute errors for all systems. |
| 148 | +
|
| 149 | + Returns |
| 150 | + ------- |
| 151 | + dict[str, dict] |
| 152 | + Metric names and values for all models. |
| 153 | + """ |
| 154 | + return { |
| 155 | + "MAE": x23_errors, |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +def test_x23(metrics: dict[str, dict]) -> None: |
| 160 | + """ |
| 161 | + Run X23 test. |
| 162 | +
|
| 163 | + Parameters |
| 164 | + ---------- |
| 165 | + metrics |
| 166 | + All X23 metrics. |
| 167 | + """ |
| 168 | + return |
0 commit comments