|
| 1 | +"""Concatenate MACE results from multiple data files generated by slurm job array |
| 2 | +into single file. |
| 3 | +""" |
| 4 | + |
| 5 | + |
| 6 | +# %% |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import os |
| 10 | +import warnings |
| 11 | +from glob import glob |
| 12 | + |
| 13 | +import pandas as pd |
| 14 | +from pymatgen.core import Structure |
| 15 | +from pymatgen.entries.compatibility import MaterialsProject2020Compatibility |
| 16 | +from pymatgen.entries.computed_entries import ComputedStructureEntry |
| 17 | +from pymatviz import density_scatter |
| 18 | +from tqdm import tqdm |
| 19 | + |
| 20 | +from matbench_discovery.data import DATA_FILES, as_dict_handler, df_wbm |
| 21 | +from matbench_discovery.energy import get_e_form_per_atom |
| 22 | +from matbench_discovery.preds import e_form_col |
| 23 | + |
| 24 | +__author__ = "Janosh Riebesell" |
| 25 | +__date__ = "2023-03-01" |
| 26 | + |
| 27 | +warnings.filterwarnings(action="ignore", category=UserWarning, module="pymatgen") |
| 28 | + |
| 29 | + |
| 30 | +# %% |
| 31 | +module_dir = os.path.dirname(__file__) |
| 32 | +task_type = "IS2RE" |
| 33 | +date = "2023-08-14" |
| 34 | +glob_pattern = f"{date}-mace-wbm-{task_type}*/*.json.gz" |
| 35 | +file_paths = sorted(glob(f"{module_dir}/{glob_pattern}")) |
| 36 | +print(f"Found {len(file_paths):,} files for {glob_pattern = }") |
| 37 | +struct_col = "mace_structure" |
| 38 | + |
| 39 | +dfs: dict[str, pd.DataFrame] = {} |
| 40 | + |
| 41 | + |
| 42 | +# %% |
| 43 | +for file_path in tqdm(file_paths): |
| 44 | + if file_path in dfs: |
| 45 | + continue |
| 46 | + df = pd.read_json(file_path).set_index("material_id") |
| 47 | + # drop trajectory to save memory |
| 48 | + dfs[file_path] = df.drop(columns="mace_trajectory") |
| 49 | + |
| 50 | +df_mace = pd.concat(dfs.values()).round(4) |
| 51 | + |
| 52 | + |
| 53 | +# %% |
| 54 | +df_cse = pd.read_json(DATA_FILES.wbm_computed_structure_entries).set_index( |
| 55 | + "material_id" |
| 56 | +) |
| 57 | + |
| 58 | +entry_col = "computed_structure_entry" |
| 59 | +df_cse[entry_col] = [ |
| 60 | + ComputedStructureEntry.from_dict(dct) |
| 61 | + for dct in tqdm(df_cse.computed_structure_entry) |
| 62 | +] |
| 63 | + |
| 64 | + |
| 65 | +# %% transfer mace energies and relaxed structures WBM CSEs since MP2020 energy |
| 66 | +# corrections applied below are structure-dependent (for oxides and sulfides) |
| 67 | +cse: ComputedStructureEntry |
| 68 | +for row in tqdm(df_mace.itertuples(), total=len(df_mace)): |
| 69 | + mat_id, struct_dict, mace_energy, *_ = row |
| 70 | + mlip_struct = Structure.from_dict(struct_dict) |
| 71 | + df_mace.at[mat_id, struct_col] = mlip_struct # noqa: PD008 |
| 72 | + cse = df_cse.loc[mat_id, entry_col] |
| 73 | + cse._energy = mace_energy # cse._energy is the uncorrected energy |
| 74 | + cse._structure = mlip_struct |
| 75 | + df_mace.loc[mat_id, entry_col] = cse |
| 76 | + |
| 77 | + |
| 78 | +# %% apply energy corrections |
| 79 | +out = MaterialsProject2020Compatibility().process_entries( |
| 80 | + df_mace.cse, verbose=True, clean=True |
| 81 | +) |
| 82 | +assert len(out) == len(df_mace) |
| 83 | + |
| 84 | + |
| 85 | +# %% compute corrected formation energies |
| 86 | +e_form_mace_col = "e_form_per_atom_mace" |
| 87 | +df_mace["formula"] = df_wbm.formula |
| 88 | +df_mace[e_form_mace_col] = [ |
| 89 | + get_e_form_per_atom(dict(energy=cse.energy, composition=formula)) |
| 90 | + for formula, cse in tqdm( |
| 91 | + df_mace.set_index("formula")[entry_col].items(), total=len(df_mace) |
| 92 | + ) |
| 93 | +] |
| 94 | +df_wbm[e_form_mace_col] = df_mace[e_form_mace_col] |
| 95 | + |
| 96 | + |
| 97 | +# %% |
| 98 | +bad_mask = (df_wbm[e_form_col] - df_wbm[e_form_mace_col]).abs() > 10 |
| 99 | +ax = density_scatter(df=df_wbm[bad_mask], x=e_form_col, y=e_form_mace_col) |
| 100 | + |
| 101 | + |
| 102 | +# %% |
| 103 | +out_path = file_paths[0].rsplit("/", 1)[0] |
| 104 | +df_mace = df_mace.round(4) |
| 105 | +df_mace[~bad_mask].select_dtypes("number").to_csv(f"{out_path}.csv.gz") |
| 106 | +df_mace.reset_index().to_json(f"{out_path}.json.gz", default_handler=as_dict_handler) |
| 107 | + |
| 108 | +df_bad = df_mace[bad_mask].drop(columns=[entry_col, struct_col]) |
| 109 | +df_bad[e_form_col] = df_wbm[e_form_col] |
| 110 | +df_bad.to_csv(f"{out_path}-bad.csv") |
| 111 | + |
| 112 | +# in_path = f"{module_dir}/2023-08-14-mace-wbm-IS2RE-FIRE" |
| 113 | +# df_mace = pd.read_csv(f"{in_path}.csv.gz").set_index("material_id") |
| 114 | +# df_mace = pd.read_json(f"{in_path}.json.gz").set_index("material_id") |
0 commit comments