forked from torchmd/torchmd-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
275 lines (240 loc) · 10.2 KB
/
utils.py
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Copyright Universitat Pompeu Fabra 2020-2023 https://www.compscience.org
# Distributed under the MIT License.
# (See accompanying file README.md file or copy at http://opensource.org/licenses/MIT)
import yaml
import argparse
import numpy as np
import torch
from os.path import dirname, join, exists
from lightning_utilities.core.rank_zero import rank_zero_warn
import functools
import warnings
# fmt: off
# Atomic masses are based on:
#
# Meija, J., Coplen, T., Berglund, M., et al. (2016). Atomic weights of
# the elements 2013 (IUPAC Technical Report). Pure and Applied Chemistry,
# 88(3), pp. 265-291. Retrieved 30 Nov. 2016,
# from doi:10.1515/pac-2015-0305
#
# Standard atomic weights are taken from Table 1: "Standard atomic weights
# 2013", with the uncertainties ignored.
# For hydrogen, helium, boron, carbon, nitrogen, oxygen, magnesium, silicon,
# sulfur, chlorine, bromine and thallium, where the weights are given as a
# range the "conventional" weights are taken from Table 3 and the ranges are
# given in the comments.
# The mass of the most stable isotope (in Table 4) is used for elements
# where there the element has no stable isotopes (to avoid NaNs): Tc, Pm,
# Po, At, Rn, Fr, Ra, Ac, everything after N
atomic_masses = np.array([
1.0, 1.008, 4.002602, 6.94, 9.0121831,
10.81, 12.011, 14.007, 15.999, 18.998403163,
20.1797, 22.98976928, 24.305, 26.9815385, 28.085,
30.973761998, 32.06, 35.45, 39.948, 39.0983,
40.078, 44.955908, 47.867, 50.9415, 51.9961,
54.938044, 55.845, 58.933194, 58.6934, 63.546,
65.38, 69.723, 72.63, 74.921595, 78.971,
79.904, 83.798, 85.4678, 87.62, 88.90584,
91.224, 92.90637, 95.95, 97.90721, 101.07,
102.9055, 106.42, 107.8682, 112.414, 114.818,
118.71, 121.76, 127.6, 126.90447, 131.293,
132.90545196, 137.327, 138.90547, 140.116, 140.90766,
144.242, 144.91276, 150.36, 151.964, 157.25,
158.92535, 162.5, 164.93033, 167.259, 168.93422,
173.054, 174.9668, 178.49, 180.94788, 183.84,
186.207, 190.23, 192.217, 195.084, 196.966569,
200.592, 204.38, 207.2, 208.9804, 208.98243,
209.98715, 222.01758, 223.01974, 226.02541, 227.02775,
232.0377, 231.03588, 238.02891, 237.04817, 244.06421,
243.06138, 247.07035, 247.07031, 251.07959, 252.083,
257.09511, 258.09843, 259.101, 262.11, 267.122,
268.126, 271.134, 270.133, 269.1338, 278.156,
281.165, 281.166, 285.177, 286.182, 289.19,
289.194, 293.204, 293.208, 294.214,
])
# fmt: on
def train_val_test_split(dset_len, train_size, val_size, test_size, seed, order=None):
assert (train_size is None) + (val_size is None) + (
test_size is None
) <= 1, "Only one of train_size, val_size, test_size is allowed to be None."
is_float = (
isinstance(train_size, float),
isinstance(val_size, float),
isinstance(test_size, float),
)
train_size = round(dset_len * train_size) if is_float[0] else train_size
val_size = round(dset_len * val_size) if is_float[1] else val_size
test_size = round(dset_len * test_size) if is_float[2] else test_size
if train_size is None:
train_size = dset_len - val_size - test_size
elif val_size is None:
val_size = dset_len - train_size - test_size
elif test_size is None:
test_size = dset_len - train_size - val_size
if train_size + val_size + test_size > dset_len:
if is_float[2]:
test_size -= 1
elif is_float[1]:
val_size -= 1
elif is_float[0]:
train_size -= 1
assert train_size >= 0 and val_size >= 0 and test_size >= 0, (
f"One of training ({train_size}), validation ({val_size}) or "
f"testing ({test_size}) splits ended up with a negative size."
)
total = train_size + val_size + test_size
assert dset_len >= total, (
f"The dataset ({dset_len}) is smaller than the "
f"combined split sizes ({total})."
)
if total < dset_len:
rank_zero_warn(f"{dset_len - total} samples were excluded from the dataset")
idxs = np.arange(dset_len, dtype=int)
if order is None:
idxs = np.random.default_rng(seed).permutation(idxs)
idx_train = idxs[:train_size]
idx_val = idxs[train_size : train_size + val_size]
idx_test = idxs[train_size + val_size : total]
if order is not None:
idx_train = [order[i] for i in idx_train]
idx_val = [order[i] for i in idx_val]
idx_test = [order[i] for i in idx_test]
return np.array(idx_train), np.array(idx_val), np.array(idx_test)
def make_splits(
dataset_len,
train_size,
val_size,
test_size,
seed,
filename=None,
splits=None,
order=None,
):
if splits is not None:
splits = np.load(splits)
idx_train = splits["idx_train"]
idx_val = splits["idx_val"]
idx_test = splits["idx_test"]
else:
idx_train, idx_val, idx_test = train_val_test_split(
dataset_len, train_size, val_size, test_size, seed, order
)
if filename is not None:
np.savez(filename, idx_train=idx_train, idx_val=idx_val, idx_test=idx_test)
return (
torch.from_numpy(idx_train),
torch.from_numpy(idx_val),
torch.from_numpy(idx_test),
)
class LoadFromFile(argparse.Action):
# parser.add_argument('--file', type=open, action=LoadFromFile)
def __call__(self, parser, namespace, values, option_string=None):
if values.name.endswith("yaml") or values.name.endswith("yml"):
with values as f:
config = yaml.load(f, Loader=yaml.FullLoader)
for key in config.keys():
if key not in namespace:
raise ValueError(f"Unknown argument in config file: {key}")
if (
"load_model" in config
and namespace.load_model is not None
and config["load_model"] != namespace.load_model
):
rank_zero_warn(
f"The load model argument was specified as a command line argument "
f"({namespace.load_model}) and in the config file ({config['load_model']}). "
f"Ignoring 'load_model' from the config file and loading {namespace.load_model}."
)
del config["load_model"]
namespace.__dict__.update(config)
else:
raise ValueError("Configuration file must end with yaml or yml")
class LoadFromCheckpoint(argparse.Action):
# parser.add_argument('--file', type=open, action=LoadFromFile)
def __call__(self, parser, namespace, values, option_string=None):
hparams_path = join(dirname(values), "hparams.yaml")
if not exists(hparams_path):
print(
"Failed to locate the checkpoint's hparams.yaml file. Relying on command line args."
)
return
with open(hparams_path, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
for key in config.keys():
if key not in namespace and key != "prior_args":
raise ValueError(f"Unknown argument in the model checkpoint: {key}")
namespace.__dict__.update(config)
namespace.__dict__.update(load_model=values)
def save_argparse(args, filename, exclude=None):
import json
if filename.endswith("yaml") or filename.endswith("yml"):
if isinstance(exclude, str):
exclude = [exclude]
args = args.__dict__.copy()
for exl in exclude:
del args[exl]
ds_arg = args.get("dataset_arg")
if ds_arg is not None and isinstance(ds_arg, str):
args["dataset_arg"] = json.loads(args["dataset_arg"])
yaml.dump(args, open(filename, "w"))
else:
raise ValueError("Configuration file should end with yaml or yml")
def number(text):
if text is None or text == "None":
return None
try:
num_int = int(text)
except ValueError:
num_int = None
num_float = float(text)
if num_int == num_float:
return num_int
return num_float
class MissingEnergyException(Exception):
pass
def write_as_hdf5(files, hdf5_dataset):
"""Transform the input numpy files to hdf5 format compatible with the HDF5 Dataset class.
The input files to this function are the same as the ones required by the Custom dataset.
Args:
files (dict): Dictionary of numpy input files. Must contain "pos", "z" and at least one of "y" or "neg_dy".
hdf5_dataset (string): Path to the output HDF5 dataset.
Example:
>>> files = {}
>>> files["pos"] = sorted(glob.glob(join(tmpdir, "coords*")))
>>> files["z"] = sorted(glob.glob(join(tmpdir, "embed*")))
>>> files["y"] = sorted(glob.glob(join(tmpdir, "energy*")))
>>> files["neg_dy"] = sorted(glob.glob(join(tmpdir, "forces*")))
>>> write_as_hdf5(files, join(tmpdir, "test.hdf5"))
"""
import h5py
with h5py.File(hdf5_dataset, "w") as f:
for i in range(len(files["pos"])):
# Create a group for each file
coord_data = np.load(files["pos"][i], mmap_mode="r")
embed_data = np.load(files["z"][i], mmap_mode="r").astype(int)
group = f.create_group(str(i))
num_samples = coord_data.shape[0]
group.create_dataset("pos", data=coord_data)
group.create_dataset("types", data=np.tile(embed_data, (num_samples, 1)))
if "y" in files:
energy_data = np.load(files["y"][i], mmap_mode="r")
group.create_dataset("energy", data=energy_data)
if "neg_dy" in files:
force_data = np.load(files["neg_dy"][i], mmap_mode="r")
group.create_dataset("forces", data=force_data)
def deprecated_class(cls):
"""Decorator to mark classes as deprecated."""
orig_init = cls.__init__
@functools.wraps(orig_init)
def wrapped_init(self, *args, **kwargs):
warnings.simplefilter(
"always", DeprecationWarning
) # ensure all deprecation warnings are shown
warnings.warn(
f"{cls.__name__} is deprecated and will be removed in a future version.",
category=DeprecationWarning,
stacklevel=2,
)
orig_init(self, *args, **kwargs)
cls.__init__ = wrapped_init
return cls