add preprocess scripts
Browse files- .gitignore +4 -0
- jobs.py +37 -0
- preprocess.py +84 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
**/.ipynb_checkpoints/
|
2 |
+
**/__pycache__
|
3 |
+
**.out
|
4 |
+
**.err
|
jobs.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Module for submitting jobs"""
|
2 |
+
|
3 |
+
import subprocess
|
4 |
+
|
5 |
+
|
6 |
+
def submit_slurm(
|
7 |
+
cmd: str,
|
8 |
+
time: str,
|
9 |
+
partition: str,
|
10 |
+
nodes: int,
|
11 |
+
ntasks_per_node: int,
|
12 |
+
job_name: str,
|
13 |
+
account: str,
|
14 |
+
**kwargs,
|
15 |
+
):
|
16 |
+
"""Submit a job to the slurm scheduler."""
|
17 |
+
scmd = [
|
18 |
+
*f"""sbatch
|
19 |
+
--time={time}
|
20 |
+
--account={account}
|
21 |
+
--qos={partition}
|
22 |
+
--job-name={job_name}
|
23 |
+
--output=%x-%j.out
|
24 |
+
--error=%x-%j.err
|
25 |
+
--nodes={nodes}
|
26 |
+
--ntasks-per-node={ntasks_per_node}""".replace("'", "").split(),
|
27 |
+
]
|
28 |
+
|
29 |
+
if kwargs.get("constraint", False):
|
30 |
+
scmd += [f"--constraint={kwargs['constraint']}"]
|
31 |
+
|
32 |
+
if kwargs.get("exclude", False):
|
33 |
+
scmd += ["--exclude"]
|
34 |
+
|
35 |
+
scmd += ["--wrap", cmd]
|
36 |
+
|
37 |
+
return subprocess.run(scmd, check=True)
|
preprocess.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # %%
|
2 |
+
# from datasets import load_dataset
|
3 |
+
|
4 |
+
# dataset = load_dataset("atomind/alexandria", name='1D', streaming=True)
|
5 |
+
|
6 |
+
# print(dataset)
|
7 |
+
# # %%
|
8 |
+
import argparse
|
9 |
+
import bz2
|
10 |
+
import json
|
11 |
+
from pathlib import Path
|
12 |
+
|
13 |
+
from tqdm.auto import tqdm
|
14 |
+
|
15 |
+
from ase import Atoms
|
16 |
+
from ase.calculators.singlepoint import SinglePointCalculator
|
17 |
+
from ase.io import write, read
|
18 |
+
from pymatgen.core import Structure
|
19 |
+
|
20 |
+
|
21 |
+
def parse_args():
|
22 |
+
parser = argparse.ArgumentParser()
|
23 |
+
parser.add_argument("--src_path", type=str, required=True)
|
24 |
+
parser.add_argument("--dst_dir", type=str, required=True)
|
25 |
+
return parser.parse_args()
|
26 |
+
|
27 |
+
|
28 |
+
def main(src_path: Path | str, dst_dir: Path | str):
|
29 |
+
"""Extracts the structures from a bz2 compressed json file and writes them to an extended xyz file."""
|
30 |
+
|
31 |
+
if isinstance(src_path, str):
|
32 |
+
src_path = Path(src_path)
|
33 |
+
|
34 |
+
if isinstance(dst_dir, str):
|
35 |
+
dst_dir = Path(dst_dir)
|
36 |
+
|
37 |
+
dst_dir.mkdir(exist_ok=True, parents=True)
|
38 |
+
|
39 |
+
with bz2.open(src_path, "rb") as f:
|
40 |
+
data = json.load(f)
|
41 |
+
|
42 |
+
assert isinstance(data, dict)
|
43 |
+
|
44 |
+
for alex_id, u in tqdm(data.items(), desc="Extracting structures"):
|
45 |
+
for calc_id, v in enumerate(u):
|
46 |
+
for ionic_step, w in enumerate(v["steps"]):
|
47 |
+
atoms = Structure.from_dict(w["structure"]).to_ase_atoms()
|
48 |
+
|
49 |
+
results = {
|
50 |
+
"energy": w["energy"],
|
51 |
+
"forces": w["forces"],
|
52 |
+
"stress": w["stress"],
|
53 |
+
}
|
54 |
+
|
55 |
+
atoms.calc = SinglePointCalculator(atoms=atoms, **results)
|
56 |
+
|
57 |
+
atoms.info = {
|
58 |
+
"alex_id": alex_id,
|
59 |
+
"calc_id": calc_id,
|
60 |
+
"ionic_step": ionic_step,
|
61 |
+
}
|
62 |
+
|
63 |
+
traj_file = dst_dir / f"{atoms.get_chemical_formula()}.traj"
|
64 |
+
|
65 |
+
exist = False
|
66 |
+
if traj_file.exists():
|
67 |
+
traj = read(traj_file, index=":")
|
68 |
+
for frame in traj:
|
69 |
+
assert isinstance(frame, Atoms)
|
70 |
+
if frame.info == atoms.info:
|
71 |
+
exist = True
|
72 |
+
break
|
73 |
+
|
74 |
+
if not exist:
|
75 |
+
write(
|
76 |
+
traj_file,
|
77 |
+
atoms,
|
78 |
+
append=True,
|
79 |
+
)
|
80 |
+
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
args = parse_args()
|
84 |
+
main(args.src_path, args.dst_dir)
|