diff --git a/ml_peg/analysis/non_covalent_interactions/ncia_sh250x10/analyse_ncia_sh250x10.py b/ml_peg/analysis/non_covalent_interactions/ncia_sh250x10/analyse_ncia_sh250x10.py new file mode 100644 index 00000000..ced8460a --- /dev/null +++ b/ml_peg/analysis/non_covalent_interactions/ncia_sh250x10/analyse_ncia_sh250x10.py @@ -0,0 +1,145 @@ +"""Analyse ncia_sh250x10 benchmark.""" + +from __future__ import annotations + +from pathlib import Path + +from ase import units +from ase.io import read, write +import pytest + +from ml_peg.analysis.utils.decorators import build_table, plot_parity +from ml_peg.analysis.utils.utils import build_d3_name_map, load_metrics_config, mae +from ml_peg.app import APP_ROOT +from ml_peg.calcs import CALCS_ROOT +from ml_peg.models.get_models import load_models +from ml_peg.models.models import current_models + +MODELS = load_models(current_models) +D3_MODEL_NAMES = build_d3_name_map(MODELS) + +KCAL_TO_EV = units.kcal / units.mol +EV_TO_KCAL = 1 / KCAL_TO_EV +CALC_PATH = CALCS_ROOT / "non_covalent_interactions" / "ncia_sh250x10" / "outputs" +OUT_PATH = APP_ROOT / "data" / "non_covalent_interactions" / "ncia_sh250x10" + +METRICS_CONFIG_PATH = Path(__file__).with_name("metrics.yml") +DEFAULT_THRESHOLDS, DEFAULT_TOOLTIPS, DEFAULT_WEIGHTS = load_metrics_config( + METRICS_CONFIG_PATH +) + + +def labels() -> list: + """ + Get list of system names. + + Returns + ------- + list + List of all system names. + """ + for model in MODELS: + labels_list = sorted([path.stem for path in (CALC_PATH / model).glob("*.xyz")]) + break + return labels_list + + +@pytest.fixture +@plot_parity( + filename=OUT_PATH / "figure_ncia_sh250x10.json", + title="Interaction energies", + x_label="Predicted energy / eV", + y_label="Reference energy / eV", + hoverdata={ + "Labels": labels(), + }, +) +def interaction_energies() -> dict[str, list]: + """ + Get interaction energies for all systems. + + Returns + ------- + dict[str, list] + Dictionary of all reference and predicted interaction energies. + """ + results = {"ref": []} | {mlip: [] for mlip in MODELS} + + ref_stored = False + + for model_name in MODELS: + for label in labels(): + atoms = read(CALC_PATH / model_name / f"{label}.xyz") + if not ref_stored: + results["ref"].append(atoms.info["ref_int_energy"]) + + results[model_name].append(atoms.info["model_int_energy"]) + + # Write structures for app + structs_dir = OUT_PATH / model_name + structs_dir.mkdir(parents=True, exist_ok=True) + write(structs_dir / f"{label}.xyz", atoms) + + ref_stored = True + return results + + +@pytest.fixture +def get_mae(interaction_energies) -> dict[str, float]: + """ + Get mean absolute error for energies. + + Parameters + ---------- + interaction_energies + Dictionary of reference and predicted energies. + + Returns + ------- + dict[str, float] + Dictionary of predicted energy errors for all models. + """ + results = {} + for model_name in MODELS: + results[model_name] = mae( + interaction_energies["ref"], interaction_energies[model_name] + ) + return results + + +@pytest.fixture +@build_table( + filename=OUT_PATH / "ncia_sh250x10_metrics_table.json", + metric_tooltips=DEFAULT_TOOLTIPS, + thresholds=DEFAULT_THRESHOLDS, + mlip_name_map=D3_MODEL_NAMES, +) +def metrics(get_mae: dict[str, float]) -> dict[str, dict]: + """ + Get all metrics. + + Parameters + ---------- + get_mae + Mean absolute errors for all models. + + Returns + ------- + dict[str, dict] + Metric names and values for all models. + """ + return { + "MAE": get_mae, + } + + +def test_ncia_sh250x10(metrics: dict[str, dict]) -> None: + """ + Run ncia_sh250x10 test. + + Parameters + ---------- + metrics + All new benchmark metric names and dictionary of values for each model. + """ + return diff --git a/ml_peg/analysis/non_covalent_interactions/ncia_sh250x10/metrics.yml b/ml_peg/analysis/non_covalent_interactions/ncia_sh250x10/metrics.yml new file mode 100644 index 00000000..8118e823 --- /dev/null +++ b/ml_peg/analysis/non_covalent_interactions/ncia_sh250x10/metrics.yml @@ -0,0 +1,7 @@ +metrics: + MAE: + good: 0.0 + bad: 0.5 + unit: eV + tooltip: Mean Absolute Error for all systems + level_of_theory: CCSD(T) diff --git a/ml_peg/app/non_covalent_interactions/ncia_sh250x10/app_ncia_sh250x10.py b/ml_peg/app/non_covalent_interactions/ncia_sh250x10/app_ncia_sh250x10.py new file mode 100644 index 00000000..4ff628f9 --- /dev/null +++ b/ml_peg/app/non_covalent_interactions/ncia_sh250x10/app_ncia_sh250x10.py @@ -0,0 +1,91 @@ +"""Run NCIA_SH250x10 app.""" + +from __future__ import annotations + +from dash import Dash +from dash.html import Div + +from ml_peg.app import APP_ROOT +from ml_peg.app.base_app import BaseApp +from ml_peg.app.utils.build_callbacks import ( + plot_from_table_column, + struct_from_scatter, +) +from ml_peg.app.utils.load import read_plot +from ml_peg.models.get_models import get_model_names +from ml_peg.models.models import current_models + +MODELS = get_model_names(current_models) +BENCHMARK_NAME = "NCIA_SH250x10" +DOCS_URL = ( + "https://ddmms.github.io/ml-peg/user_guide/benchmarks/" + "non_covalent_interactions.html#ncia-sh250x10" +) +DATA_PATH = APP_ROOT / "data" / "non_covalent_interactions" / "ncia_sh250x10" + + +class NCIASH250x10App(BaseApp): + """NCIA_SH250x10 benchmark app layout and callbacks.""" + + def register_callbacks(self) -> None: + """Register callbacks to app.""" + scatter = read_plot( + DATA_PATH / "figure_ncia_sh250x10.json", + id=f"{BENCHMARK_NAME}-figure", + ) + + model_dir = DATA_PATH / MODELS[0] + if model_dir.exists(): + labels = sorted([f.stem for f in model_dir.glob("*.xyz")]) + structs = [ + f"assets/non_covalent_interactions/ncia_sh250x10/{MODELS[0]}/{label}.xyz" + for label in labels + ] + else: + structs = [] + + plot_from_table_column( + table_id=self.table_id, + plot_id=f"{BENCHMARK_NAME}-figure-placeholder", + column_to_plot={"MAE": scatter}, + ) + + struct_from_scatter( + scatter_id=f"{BENCHMARK_NAME}-figure", + struct_id=f"{BENCHMARK_NAME}-struct-placeholder", + structs=structs, + mode="struct", + ) + + +def get_app() -> NCIASH250x10App: + """ + Get NCIA_SH250x10 benchmark app layout and callback registration. + + Returns + ------- + NCIASH250x10App + Benchmark layout and callback registration. + """ + return NCIASH250x10App( + name=BENCHMARK_NAME, + description=( + "Performance in predicting sigma-hole interaction energies " + "for the NCIA SH250x10 dataset (halogen-bonded dimers). " + "Reference data from CCSD(T) calculations." + ), + docs_url=DOCS_URL, + table_path=DATA_PATH / "ncia_sh250x10_metrics_table.json", + extra_components=[ + Div(id=f"{BENCHMARK_NAME}-figure-placeholder"), + Div(id=f"{BENCHMARK_NAME}-struct-placeholder"), + ], + ) + + +if __name__ == "__main__": + full_app = Dash(__name__, assets_folder=DATA_PATH.parent.parent) + benchmark_app = get_app() + full_app.layout = benchmark_app.layout + benchmark_app.register_callbacks() + full_app.run(port=8061, debug=True) diff --git a/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvc/.gitignore b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvc/.gitignore new file mode 100644 index 00000000..528f30c7 --- /dev/null +++ b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvc/.gitignore @@ -0,0 +1,3 @@ +/config.local +/tmp +/cache diff --git a/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvc/config b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvc/config new file mode 100644 index 00000000..e69de29b diff --git a/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvcignore b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvcignore new file mode 100644 index 00000000..51973055 --- /dev/null +++ b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/.dvcignore @@ -0,0 +1,3 @@ +# Add patterns of files dvc should ignore, which could improve +# the performance. Learn more at +# https://dvc.org/doc/user-guide/dvcignore diff --git a/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/calc_ncia_sh250x10.py b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/calc_ncia_sh250x10.py new file mode 100644 index 00000000..2a450827 --- /dev/null +++ b/ml_peg/calcs/non_covalent_interactions/ncia_sh250x10/calc_ncia_sh250x10.py @@ -0,0 +1,161 @@ +""" +Compute the NCIA SH250x10 dataset for sigma-hole interactions. + +Phys. Chem. Chem. Phys., 2022,24, 14794-14804. +""" + +from __future__ import annotations + +from pathlib import Path + +from ase import units +from ase.io import read, write +import mlipx +from mlipx.abc import NodeWithCalculator +from tqdm import tqdm +import zntrack + +from ml_peg.calcs.utils.utils import chdir, download_s3_data +from ml_peg.models.get_models import load_models +from ml_peg.models.models import current_models + +MODELS = load_models(current_models) + +KCAL_TO_EV = units.kcal / units.mol +EV_TO_KCAL = 1 / KCAL_TO_EV + +OUT_PATH = Path(__file__).parent / "outputs" + + +class NCIASH250x10Benchmark(zntrack.Node): + """Benchmark the NCIA_SH250x10 dataset.""" + + model: NodeWithCalculator = zntrack.deps() + model_name: str = zntrack.params() + + def get_ref_energies(self, data_path): + """ + Get reference energies. + + Parameters + ---------- + data_path + Path to data. + """ + self.ref_energies = {} + with open(data_path / "NCIA_SH250x10_benchmark.txt") as lines: + for i, line in enumerate(lines): + if i < 2: + continue + items = line.strip().split() + label = items[0] + ref_energy = float(items[1]) * KCAL_TO_EV + self.ref_energies[label] = ref_energy + + @staticmethod + def get_monomers(atoms): + """ + Get ASE atoms objects of the monomers. + + Parameters + ---------- + atoms + ASE atoms object of the structure. + + Returns + ------- + tuple[ASE.Atoms, ASE.Atoms] + Tuple containing the two monomers. + """ + if isinstance(atoms.info["selection_a"], str): + a_ids = [int(id) for id in atoms.info["selection_a"].split("-")] + a_ids[0] -= 1 + else: + a_ids = [int(atoms.info["selection_a"]) - 1, int(atoms.info["selection_a"])] + + if isinstance(atoms.info["selection_b"], str): + b_ids = [int(id) for id in atoms.info["selection_b"].split("-")] + b_ids[0] -= 1 + else: + b_ids = [int(atoms.info["selection_b"]) - 1, int(atoms.info["selection_b"])] + + atoms_a = atoms[a_ids[0] : a_ids[1]] + atoms_b = atoms[b_ids[0] : b_ids[1]] + assert len(atoms_a) + len(atoms_b) == len(atoms) + + atoms_a.info["charge"] = int(atoms.info["charge_a"]) + atoms_a.info["spin"] = 1 + + atoms_b.info["charge"] = int(atoms.info["charge_b"]) + atoms_b.info["spin"] = 1 + return (atoms_a, atoms_b) + + def run(self): + """Run new benchmark.""" + # Read in data and attach calculator + data_path = ( + download_s3_data( + filename="NCIA_SH250x10.zip", + key="inputs/non_covalent_interactions/NCIA_SH250x10/NCIA_SH250x10.zip", + ) + / "NCIA_SH250x10" + ) + self.get_ref_energies(data_path) + + calc = self.model.get_calculator() + # Add D3 calculator for this test + calc = self.model.add_d3_calculator(calc) + + for label, ref_energy in tqdm(self.ref_energies.items()): + xyz_fname = f"{label}.xyz" + atoms = read(data_path / "geometries" / xyz_fname) + atoms_a, atoms_b = self.get_monomers(atoms) + atoms.info["spin"] = 1 + atoms.info["charge"] = int(atoms_a.info["charge"] + atoms_b.info["charge"]) + atoms.calc = calc + atoms_a.calc = calc + atoms_b.calc = calc + + atoms.info["model_int_energy"] = ( + atoms.get_potential_energy() + - atoms_a.get_potential_energy() + - atoms_b.get_potential_energy() + ) + atoms.info["ref_int_energy"] = ref_energy + atoms.calc = None + + write_dir = OUT_PATH / self.model_name + write_dir.mkdir(parents=True, exist_ok=True) + write(write_dir / f"{label}.xyz", atoms) + + +def build_project(repro: bool = False) -> None: + """ + Build mlipx project. + + Parameters + ---------- + repro + Whether to call dvc repro -f after building. + """ + project = mlipx.Project() + benchmark_node_dict = {} + + for model_name, model in MODELS.items(): + with project.group(model_name): + benchmark = NCIASH250x10Benchmark( + model=model, + model_name=model_name, + ) + benchmark_node_dict[model_name] = benchmark + + if repro: + with chdir(Path(__file__).parent): + project.repro(build=True, force=True) + else: + project.build() + + +def test_ncia_sh250x10(): + """Run NCIA_SH250x10 barriers benchmark via pytest.""" + build_project(repro=True)