-
Notifications
You must be signed in to change notification settings - Fork 2
/
dens_lmdb_dataset.py
261 lines (224 loc) · 9.59 KB
/
dens_lmdb_dataset.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
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import bisect
import logging
import pickle
import warnings
from pathlib import Path
from typing import List, Optional, TypeVar
import lmdb
import numpy as np
import torch
from torch.utils.data import Dataset
from torch_geometric.data import Batch
from torch_geometric.data.data import BaseData
from ocpmodels.common.registry import registry
from ocpmodels.common.typing import assert_is_instance
from ocpmodels.common.utils import pyg2_data_transform
from ocpmodels.datasets.target_metadata_guesser import guess_property_metadata
from ocpmodels.modules.transforms import DataTransforms
T_co = TypeVar("T_co", covariant=True)
@registry.register_dataset("dens_lmdb")
class DeNSLmdbDataset(Dataset[T_co]):
metadata_path: Path
sharded: bool
r"""Dataset class to load from LMDB files containing relaxation
trajectories or single point computations.
Useful for Structure to Energy & Force (S2EF), Initial State to
Relaxed State (IS2RS), and Initial State to Relaxed Energy (IS2RE) tasks.
The keys in the LMDB must be integers (stored as ascii objects) starting
from 0 through the length of the LMDB. For historical reasons any key named
"length" is ignored since that was used to infer length of many lmdbs in the same
folder, but lmdb lengths are now calculated directly from the number of keys.
Add `md` to differentiate structures from All and MD splits when training on'
OC20 S2EF-All+MD.
Args:
config (dict): Dataset configuration
transform (callable, optional): Data transform function.
(default: :obj:`None`)
"""
def __init__(self, config, transform=None) -> None:
super(DeNSLmdbDataset, self).__init__()
self.config = config
assert not self.config.get(
"train_on_oc20_total_energies", False
), "For training on total energies set dataset=oc22_lmdb"
self.path = Path(self.config["src"])
if not self.path.is_file():
db_paths = sorted(self.path.glob("*.lmdb"))
assert len(db_paths) > 0, f"No LMDBs found in '{self.path}'"
self.metadata_path = self.path / "metadata.npz"
self._keys = []
self.envs = []
for db_path in db_paths:
cur_env = self.connect_db(db_path)
self.envs.append(cur_env)
# If "length" encoded as ascii is present, use that
length_entry = cur_env.begin().get("length".encode("ascii"))
if length_entry is not None:
num_entries = pickle.loads(length_entry)
else:
# Get the number of stores data from the number of entries
# in the LMDB
num_entries = cur_env.stat()["entries"]
# Append the keys (0->num_entries) as a list
self._keys.append(list(range(num_entries)))
keylens = [len(k) for k in self._keys]
self._keylen_cumulative = np.cumsum(keylens).tolist()
self.num_samples = sum(keylens)
# Differentiate structures from All and MD splits when training on OC20 S2EF-All+MD
# TODO: update "relaxations" and "md" so that the keys are not dependent on how data are generated
txt_paths = sorted(self.path.glob("*.txt"))
index = 0
#self.relax_indices = set()
#self.md_indices = set()
self.is_md_list = []
for txt_path in txt_paths:
txt_file = open(txt_path)
lines = txt_file.read().splitlines()
for line in lines:
if "relaxations" in line:
#self.relax_indices.add(index)
self.is_md_list.append(False)
elif "md" in line:
#self.md_indices.add(index)
self.is_md_list.append(True)
else:
self.is_md_list.append(False)
index += 1
txt_file.close()
else:
self.metadata_path = self.path.parent / "metadata.npz"
self.env = self.connect_db(self.path)
# If "length" encoded as ascii is present, use that
length_entry = self.env.begin().get("length".encode("ascii"))
if length_entry is not None:
num_entries = pickle.loads(length_entry)
else:
# Get the number of stores data from the number of entries
# in the LMDB
num_entries = assert_is_instance(
self.env.stat()["entries"], int
)
self._keys = list(range(num_entries))
self.num_samples = num_entries
# If specified, limit dataset to only a portion of the entire dataset
# total_shards: defines total chunks to partition dataset
# shard: defines dataset shard to make visible
self.sharded = False
if "shard" in self.config and "total_shards" in self.config:
self.sharded = True
self.indices = range(self.num_samples)
# split all available indices into 'total_shards' bins
self.shards = np.array_split(
self.indices, self.config.get("total_shards", 1)
)
# limit each process to see a subset of data based off defined shard
self.available_indices = self.shards[self.config.get("shard", 0)]
self.num_samples = len(self.available_indices)
self.key_mapping = self.config.get("key_mapping", None)
self.transforms = DataTransforms(self.config.get("transforms", {}))
def __len__(self) -> int:
return self.num_samples
def __getitem__(self, idx: int) -> T_co:
# if sharding, remap idx to appropriate idx of the sharded set
if self.sharded:
idx = self.available_indices[idx]
if not self.path.is_file():
# Figure out which db this should be indexed from.
db_idx = bisect.bisect(self._keylen_cumulative, idx)
# Extract index of element within that db.
el_idx = idx
if db_idx != 0:
el_idx = idx - self._keylen_cumulative[db_idx - 1]
assert el_idx >= 0
# Return features.
datapoint_pickled = (
self.envs[db_idx]
.begin()
.get(f"{self._keys[db_idx][el_idx]}".encode("ascii"))
)
data_object = pyg2_data_transform(pickle.loads(datapoint_pickled))
data_object.id = f"{db_idx}_{el_idx}"
else:
datapoint_pickled = self.env.begin().get(
f"{self._keys[idx]}".encode("ascii")
)
data_object = pyg2_data_transform(pickle.loads(datapoint_pickled))
if self.key_mapping is not None:
for _property in self.key_mapping:
# catch for test data not containing labels
if _property in data_object:
new_property = self.key_mapping[_property]
if new_property not in data_object:
data_object[new_property] = data_object[_property]
del data_object[_property]
data_object = self.transforms(data_object)
# Add identifier to distinguish MD and Relaxation data.
#if idx in self.md_indices:
# data_object.md = 1
#else:
# data_object.md = 0
if idx >= len(self.is_md_list):
data_object.md = 0
elif self.is_md_list[idx]:
data_object.md = 1
else:
data_object.md = 0
return data_object
def connect_db(self, lmdb_path: Optional[Path] = None) -> lmdb.Environment:
env = lmdb.open(
str(lmdb_path),
subdir=False,
readonly=True,
lock=False,
readahead=True,
meminit=False,
max_readers=1,
)
return env
def close_db(self) -> None:
if not self.path.is_file():
for env in self.envs:
env.close()
else:
self.env.close()
def get_metadata(self, num_samples: int = 100):
# This will interogate the classic OCP LMDB format to determine
# which properties are present and attempt to guess their shapes
# and whether they are intensive or extensive.
# Grab an example data point
example_pyg_data = self.__getitem__(0)
# Check for all properties we've used for OCP datasets in the past
props = []
for potential_prop in [
"y",
"y_relaxed",
"stress",
"stresses",
"force",
"forces",
]:
if hasattr(example_pyg_data, potential_prop):
props.append(potential_prop)
# Get a bunch of random data samples and the number of atoms
sample_pyg = [
self[i]
for i in np.random.choice(
self.__len__(), size=(num_samples,), replace=False
)
]
atoms_lens = [data.natoms for data in sample_pyg]
# Guess the metadata for targets for each found property
metadata = {
"targets": {
prop: guess_property_metadata(
atoms_lens, [getattr(data, prop) for data in sample_pyg]
)
for prop in props
}
}
return metadata