-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathutil.py
More file actions
254 lines (216 loc) · 6.87 KB
/
util.py
File metadata and controls
254 lines (216 loc) · 6.87 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python
import json
import os
from contextlib import (
contextmanager,
)
from pathlib import Path
from typing import Union
import dpdata
import h5py
import numpy as np
from dargs import Argument
from dpdata.data_type import Axis, DataType
from dpgen import dlog
"""
some common utilities for generator, auto_test and data
"""
# constants define
MaxLength = 70
def sepline(ch="-", sp="-", screen=False):
r"""Seperate the output by '-'."""
if screen:
print(ch.center(MaxLength, sp))
else:
dlog.info(ch.center(MaxLength, sp))
def box_center(ch="", fill=" ", sp="|"):
r"""Put the string at the center of | |."""
strs = ch.center(MaxLength, fill)
dlog.info(sp + strs[1 : len(strs) - 1 :] + sp)
def expand_sys_str(root_dir: Union[str, Path]) -> list[str]:
"""Recursively iterate over directories taking those that contain `type.raw` file.
If root_dir is a file but not a directory, it will be assumed as an HDF5 file.
Parameters
----------
root_dir : Union[str, Path]
starting directory
Returns
-------
List[str]
list of string pointing to system directories
Raises
------
RuntimeError
No system was found in the directory
"""
root_dir = Path(root_dir)
if root_dir.is_dir():
matches = [str(d) for d in root_dir.rglob("*") if (d / "type.raw").is_file()]
if (root_dir / "type.raw").is_file():
matches.append(str(root_dir))
elif root_dir.is_file():
# HDF5 file
with h5py.File(root_dir, "r") as f:
# list of keys in the h5 file
f_keys = ["/"]
f.visit(lambda x: f_keys.append("/" + x))
matches = [
f"{root_dir}#{d}" for d in f_keys if str(Path(d) / "type.raw") in f_keys
]
else:
raise OSError(f"{root_dir} does not exist.")
if len(matches) == 0:
raise RuntimeError(f"{root_dir} does not contain any systems!")
return matches
def normalize(
arginfo: Argument,
data: dict,
strict_check: bool = True,
allow_ref: bool = False,
) -> dict:
"""Normalize and check input data.
Parameters
----------
arginfo : dargs.Argument
argument information
data : dict
input data
strict_check : bool, default=True
strict check data or not
allow_ref : bool, default=False
Whether to allow loading external JSON/YAML snippets via ``$ref``.
Disabled by default for security.
Returns
-------
dict
normalized data
"""
data = arginfo.normalize_value(data, trim_pattern="_*", allow_ref=allow_ref)
arginfo.check_value(data, strict=strict_check, allow_ref=allow_ref)
return data
def convert_training_data_to_hdf5(input_files: list[str], h5_file: str):
"""Convert training data to HDF5 format and update the input files.
Parameters
----------
input_files : list of str
DeePMD-kit input file names
h5_file : str
HDF5 file name
"""
systems = []
h5_dir = Path(h5_file).parent.absolute()
cwd = Path.cwd().absolute()
for ii in input_files:
ii = Path(ii)
dd = ii.parent.absolute()
with open(ii, "r+") as f:
jinput = json.load(f)
if "training_data" in jinput["training"]:
# v2.0
p_sys = jinput["training"]["training_data"]["systems"]
else:
# v1.x
p_sys = jinput["training"]["systems"]
for ii, pp in enumerate(p_sys):
if "#" in pp:
# HDF5 file
p1, p2 = pp.split("#")
ff = os.path.normpath(str((dd / p1).absolute().relative_to(cwd)))
pp = ff + "#" + p2
new_pp = os.path.normpath(os.path.relpath(ff, h5_dir)) + p2
else:
pp = os.path.normpath(str((dd / pp).absolute().relative_to(cwd)))
new_pp = os.path.normpath(os.path.relpath(pp, h5_dir))
p_sys[ii] = (
os.path.normpath(os.path.relpath(h5_file, dd)) + "#/" + str(new_pp)
)
systems.append(pp)
f.seek(0)
json.dump(jinput, f, indent=4)
systems = list(set(systems))
dlog.info("Combining %d training systems to %s...", len(systems), h5_file)
with h5py.File(h5_file, "w") as f:
for ii in systems:
if "#" in ii:
p1, p2 = ii.split("#")
p1 = os.path.normpath(os.path.relpath(p1, h5_dir))
group = f.create_group(str(p1) + p2)
s = dpdata.LabeledSystem(ii, fmt="deepmd/hdf5")
s.to("deepmd/hdf5", group)
else:
pp = os.path.normpath(os.path.relpath(ii, h5_dir))
group = f.create_group(str(pp))
s = dpdata.LabeledSystem(ii, fmt="deepmd/npy")
s.to("deepmd/hdf5", group)
@contextmanager
def set_directory(path: Path):
"""Sets the current working path within the context.
Parameters
----------
path : Path
The path to the cwd
Yields
------
None
Examples
--------
>>> with set_directory("some_path"):
... do_something()
"""
cwd = Path().absolute()
path.mkdir(exist_ok=True, parents=True)
try:
os.chdir(path)
yield
finally:
os.chdir(cwd)
def load_file(filename: Union[str, os.PathLike]) -> dict:
"""Load data from a JSON or YAML file.
Parameters
----------
filename : str or os.PathLike
The filename to load data from, whose suffix should be .json, .yaml, or .yml
Returns
-------
dict
The data loaded from the file
Raises
------
ValueError
If the file format is not supported
"""
filename = str(filename)
if filename.endswith(".json"):
with open(filename) as fp:
data = json.load(fp)
elif filename.endswith(".yaml") or filename.endswith(".yml"):
from ruamel.yaml import YAML
yaml = YAML(typ="safe", pure=True)
with open(filename) as fp:
data = yaml.load(fp)
else:
raise ValueError(f"Unsupported file format: {filename}")
return data
def setup_ele_temp(atomic: bool):
"""Set electronic temperature as required input data.
Parameters
----------
atomic : bool
Whether to use atomic temperature or frame temperature
"""
if atomic:
ele_temp_data_type = DataType(
"aparam",
np.ndarray,
shape=(Axis.NFRAMES, Axis.NATOMS, 1),
required=False,
)
else:
ele_temp_data_type = DataType(
"fparam",
np.ndarray,
shape=(Axis.NFRAMES, 1),
required=False,
)
dpdata.System.register_data_type(ele_temp_data_type)
dpdata.LabeledSystem.register_data_type(ele_temp_data_type)