# # %% # 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)