File size: 2,422 Bytes
697fc54 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# # %%
# from datasets import load_dataset
# dataset = load_dataset("atomind/alexandria", name='1D', streaming=True)
# print(dataset)
# # %%
import argparse
import bz2
import json
from pathlib import Path
from tqdm.auto import tqdm
from ase import Atoms
from ase.calculators.singlepoint import SinglePointCalculator
from ase.io import write, read
from pymatgen.core import Structure
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--src_path", type=str, required=True)
parser.add_argument("--dst_dir", type=str, required=True)
return parser.parse_args()
def main(src_path: Path | str, dst_dir: Path | str):
"""Extracts the structures from a bz2 compressed json file and writes them to an extended xyz file."""
if isinstance(src_path, str):
src_path = Path(src_path)
if isinstance(dst_dir, str):
dst_dir = Path(dst_dir)
dst_dir.mkdir(exist_ok=True, parents=True)
with bz2.open(src_path, "rb") as f:
data = json.load(f)
assert isinstance(data, dict)
for alex_id, u in tqdm(data.items(), desc="Extracting structures"):
for calc_id, v in enumerate(u):
for ionic_step, w in enumerate(v["steps"]):
atoms = Structure.from_dict(w["structure"]).to_ase_atoms()
results = {
"energy": w["energy"],
"forces": w["forces"],
"stress": w["stress"],
}
atoms.calc = SinglePointCalculator(atoms=atoms, **results)
atoms.info = {
"alex_id": alex_id,
"calc_id": calc_id,
"ionic_step": ionic_step,
}
traj_file = dst_dir / f"{atoms.get_chemical_formula()}.traj"
exist = False
if traj_file.exists():
traj = read(traj_file, index=":")
for frame in traj:
assert isinstance(frame, Atoms)
if frame.info == atoms.info:
exist = True
break
if not exist:
write(
traj_file,
atoms,
append=True,
)
if __name__ == "__main__":
args = parse_args()
main(args.src_path, args.dst_dir)
|