Download scripts/build_formats.py from AmirMasoud/GMD: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/datasets/AmirMasoud/GMD/resolve/main/scripts/build_formats.py
- Command line
-
hf download hf://datasets/AmirMasoud/GMD/scripts/build_formats.py
-
curl -L -o build_formats.py https://huggingface.co/datasets/AmirMasoud/GMD/resolve/main/scripts/build_formats.py
3.24 kB
| """Rebuild lmdb/ (ASE-db .aselmdb, FairChem layout), zarr/ (v3) and npz/ (plain NumPy) from the canonical traj/ files. | |
| pip install ase ase-db-backends "zarr>=3" | |
| python scripts/build_formats.py [repo_root] | |
| Every file is read back with gmd.read and compared to the source, so the copies stay identical (npz holds labels only). | |
| """ | |
| import shutil | |
| import sys | |
| from pathlib import Path | |
| import ase.db | |
| import numpy as np | |
| import zarr | |
| root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(root)) | |
| import gmd # noqa: E402 | |
| def same_labels(a, b): | |
| return (a.info == b.info and (a.numbers == b.numbers).all() and np.allclose(a.positions, b.positions) | |
| and np.allclose(a.get_forces(), b.get_forces()) and np.isclose(a.get_potential_energy(), b.get_potential_energy())) | |
| def same(a, b): | |
| return same_labels(a, b) and np.allclose(a.get_momenta(), b.get_momenta()) and np.allclose(a.cell, b.cell) | |
| for src in sorted((root / "traj").glob("*/*.traj")): | |
| frames = gmd.read(src) | |
| assert all((a.numbers == frames[0].numbers).all() for a in frames), src | |
| mol = src.relative_to(root / "traj").with_suffix("") | |
| lm = root / "lmdb" / f"{mol}.aselmdb" | |
| lm.parent.mkdir(parents=True, exist_ok=True) | |
| lm.unlink(missing_ok=True) | |
| with ase.db.connect(lm, use_lock_file=False) as db: | |
| for a in frames: | |
| db.write(a, **a.info) | |
| Path(f"{lm}-lock").unlink(missing_ok=True) # LMDB's runtime lock, not data | |
| zp = root / "zarr" / f"{mol}.zarr" | |
| shutil.rmtree(zp, ignore_errors=True) | |
| g = zarr.open_group(zp, mode="w", attributes={"molecule": mol.name, "family": mol.parent.name, | |
| "units": {"energy": "eV", "forces": "eV/Ang", "positions": "Ang"}}) | |
| arrays = { | |
| "numbers": frames[0].numbers, | |
| "frame": [a.info["frame"] for a in frames], | |
| "positions": [a.positions for a in frames], | |
| "forces": [a.get_forces() for a in frames], | |
| "energy": [a.get_potential_energy() for a in frames], | |
| "momenta": [a.get_momenta() for a in frames], | |
| "initial_charges": [a.get_initial_charges() for a in frames], | |
| "cell": [a.cell.array for a in frames], | |
| "pbc": [a.pbc for a in frames], | |
| } | |
| for name, value in arrays.items(): | |
| value = np.asarray(value) | |
| g.create_array(name, data=value, chunks=value.shape) # one chunk per array: <= a few MB each | |
| # Plain NumPy copy (MD17-style keys) for consumers without ASE/zarr, e.g. torch_geometric.datasets.GMD. | |
| nz = root / "npz" / f"{mol}.npz" | |
| nz.parent.mkdir(parents=True, exist_ok=True) | |
| np.savez_compressed(nz, z=frames[0].numbers, frame=arrays["frame"], pos=arrays["positions"], | |
| energy=arrays["energy"], forces=arrays["forces"]) | |
| back = gmd.read(nz) | |
| assert len(back) == len(frames) and all(map(same_labels, back, frames)), nz | |
| for path in (lm, zp): | |
| back = gmd.read(path) | |
| assert len(back) == len(frames) and all(map(same, back, frames)), path | |
| print(mol, len(frames)) | |
| missing = [m for mols in gmd.TASKS.values() for m in sum(mols, []) if not (root / "traj" / f"{m}.traj").exists()] | |
| assert not missing, missing | |