File size: 3,240 Bytes
ee0d486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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