Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn json keys representing elements into ints #652

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mace/tools/scripts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ def get_atomic_energies(E0s, train_collection, z_table) -> dict:
logging.info(f"Loading atomic energies from {E0s}")
with open(E0s, "r", encoding="utf-8") as f:
atomic_energies_dict = json.load(f)
atomic_energies_dict = {
int(key): value for key, value in atomic_energies_dict.items()
}
else:
try:
atomic_energies_eval = ast.literal_eval(E0s)
Expand Down
112 changes: 112 additions & 0 deletions tests/test_run_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import pytest
from ase.atoms import Atoms
import json

from mace.calculators.mace import MACECalculator

Expand Down Expand Up @@ -600,6 +601,117 @@ def test_run_train_foundation_multihead(tmp_path, fitting_configs):
assert np.allclose(Es, ref_Es, atol=1e-1)


def test_run_train_foundation_multihead(tmp_path, fitting_configs):
fitting_configs_dft = []
fitting_configs_mp2 = []
for i, c in enumerate(fitting_configs):

if i in (0, 1):
continue # skip isolated atoms, as energies specified by json files below
elif i % 2 == 0:
c.info["head"] = "DFT"
fitting_configs_dft.append(c)
else:
c.info["head"] = "MP2"
fitting_configs_mp2.append(c)
ase.io.write(tmp_path / "fit_multihead_dft.xyz", fitting_configs_dft)
ase.io.write(tmp_path / "fit_multihead_mp2.xyz", fitting_configs_mp2)

# write E0s to json files
E0s = {1: 0.0, 8: 0.0}
with open(tmp_path / "fit_multihead_dft.json", "w") as f:
json.dump(E0s, f)
with open(tmp_path / "fit_multihead_mp2.json", "w") as f:
json.dump(E0s, f)

heads = {
"DFT": {"train_file": f"{str(tmp_path)}/fit_multihead_dft.xyz", "E0s": f"{str(tmp_path)}/fit_multihead_dft.json"},
"MP2": {"train_file": f"{str(tmp_path)}/fit_multihead_mp2.xyz", "E0s": f"{str(tmp_path)}/fit_multihead_mp2.json"},
}
yaml_str = "heads:\n"
for key, value in heads.items():
yaml_str += f" {key}:\n"
for sub_key, sub_value in value.items():
yaml_str += f" {sub_key}: {sub_value}\n"
filename = tmp_path / "config.yaml"
with open(filename, "w", encoding="utf-8") as file:
file.write(yaml_str)
mace_params = _mace_params.copy()
mace_params["valid_fraction"] = 0.1
mace_params["checkpoints_dir"] = str(tmp_path)
mace_params["model_dir"] = str(tmp_path)
mace_params["config"] = tmp_path / "config.yaml"
mace_params["loss"] = "weighted"
mace_params["foundation_model"] = "small"
mace_params["hidden_irreps"] = "128x0e"
mace_params["r_max"] = 6.0
mace_params["default_dtype"] = "float64"
mace_params["num_radial_basis"] = 10
mace_params["interaction_first"] = "RealAgnosticResidualInteractionBlock"
mace_params["batch_size"] = 2
mace_params["valid_batch_size"] = 1
mace_params["num_samples_pt"] = 50
mace_params["subselect_pt"] = "random"
# make sure run_train.py is using the mace that is currently being tested
run_env = os.environ.copy()
sys.path.insert(0, str(Path(__file__).parent.parent))
run_env["PYTHONPATH"] = ":".join(sys.path)
print("DEBUG subprocess PYTHONPATH", run_env["PYTHONPATH"])

cmd = (
sys.executable
+ " "
+ str(run_train)
+ " "
+ " ".join(
[
(f"--{k}={v}" if v is not None else f"--{k}")
for k, v in mace_params.items()
]
)
)

p = subprocess.run(cmd.split(), env=run_env, check=True)
assert p.returncode == 0

calc = MACECalculator(
model_paths=tmp_path / "MACE.model", device="cpu", default_dtype="float64"
)

Es = []
for at in fitting_configs:
at.calc = calc
Es.append(at.get_potential_energy())

print("Es", Es)
# from a run on 20/08/2024 on commit
ref_Es = [
1.654685616493225,
0.44693732261657715,
0.8741313815116882,
0.569085955619812,
0.7161882519721985,
0.8654778599739075,
0.8722733855247498,
0.49582308530807495,
0.814422607421875,
0.7027317881584167,
0.7196993827819824,
0.517953097820282,
0.8631765246391296,
0.4679797887802124,
0.8163984417915344,
0.4252359867095947,
1.0861445665359497,
0.6829671263694763,
0.7136879563331604,
0.5160345435142517,
0.7002358436584473,
0.5574042201042175,
]
assert np.allclose(Es, ref_Es, atol=1e-1)


def test_run_train_multihead_replay_custum_finetuning(
tmp_path, fitting_configs, pretraining_configs
):
Expand Down
Loading