Delete data
Browse files- data/ff_lmdb.py +0 -215
- data/sample_100-dft-hess-eigen.lmdb +0 -3
- data/sample_100.lmdb +0 -3
data/ff_lmdb.py
DELETED
|
@@ -1,215 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Copyright (c) Facebook, Inc. and its affiliates.
|
| 3 |
-
|
| 4 |
-
This source code is licensed under the MIT license found in the
|
| 5 |
-
LICENSE file in the root directory of this source tree.
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
import bisect
|
| 9 |
-
import pickle
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
|
| 12 |
-
import lmdb
|
| 13 |
-
import numpy as np
|
| 14 |
-
from torch.utils.data import Dataset
|
| 15 |
-
|
| 16 |
-
# from torch_geometric.data import Batch
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
class LmdbDataset(Dataset):
|
| 20 |
-
r"""Dataset class to load from LMDB files containing relaxation
|
| 21 |
-
trajectories or single point computations.
|
| 22 |
-
|
| 23 |
-
Useful for Structure to Energy & Force (S2EF), Initial State to
|
| 24 |
-
Relaxed State (IS2RS), and Initial State to Relaxed Energy (IS2RE) tasks.
|
| 25 |
-
|
| 26 |
-
Args:
|
| 27 |
-
config (dict): Dataset configuration
|
| 28 |
-
transform (callable, optional): Data transform function.
|
| 29 |
-
(default: :obj:`None`)
|
| 30 |
-
"""
|
| 31 |
-
|
| 32 |
-
def __init__(self, src, transform=None, **kwargs):
|
| 33 |
-
super(LmdbDataset, self).__init__()
|
| 34 |
-
|
| 35 |
-
self.path = Path(src)
|
| 36 |
-
if not self.path.is_file():
|
| 37 |
-
db_paths = sorted(self.path.glob("*.lmdb"))
|
| 38 |
-
assert len(db_paths) > 0, f"No LMDBs found in '{self.path}'"
|
| 39 |
-
|
| 40 |
-
self.metadata_path = self.path / "metadata.npz"
|
| 41 |
-
|
| 42 |
-
self._keys, self.envs = [], []
|
| 43 |
-
for db_path in db_paths:
|
| 44 |
-
self.envs.append(self.connect_db(db_path))
|
| 45 |
-
length = pickle.loads(
|
| 46 |
-
self.envs[-1].begin().get("length".encode("ascii"))
|
| 47 |
-
)
|
| 48 |
-
self._keys.append(list(range(length)))
|
| 49 |
-
|
| 50 |
-
keylens = [len(k) for k in self._keys]
|
| 51 |
-
self._keylen_cumulative = np.cumsum(keylens).tolist()
|
| 52 |
-
self.num_samples = sum(keylens)
|
| 53 |
-
else:
|
| 54 |
-
self.metadata_path = self.path.parent / "metadata.npz"
|
| 55 |
-
self.env = self.connect_db(self.path)
|
| 56 |
-
try:
|
| 57 |
-
# Try to get the stored length value first
|
| 58 |
-
self.num_samples = pickle.loads(
|
| 59 |
-
self.env.begin().get("length".encode("ascii"))
|
| 60 |
-
)
|
| 61 |
-
except (TypeError, KeyError):
|
| 62 |
-
# Fallback to entries count if length key doesn't exist
|
| 63 |
-
self.num_samples = self.env.stat()["entries"]
|
| 64 |
-
|
| 65 |
-
self._keys = [f"{j}".encode("ascii") for j in range(self.num_samples)]
|
| 66 |
-
|
| 67 |
-
self.transform = transform
|
| 68 |
-
|
| 69 |
-
def __len__(self):
|
| 70 |
-
return self.num_samples
|
| 71 |
-
|
| 72 |
-
def __getitem__(self, idx):
|
| 73 |
-
if idx >= self.num_samples:
|
| 74 |
-
raise IndexError(
|
| 75 |
-
f"Index {idx} out of range for dataset with {self.num_samples} samples"
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
if not self.path.is_file():
|
| 79 |
-
# Figure out which db this should be indexed from.
|
| 80 |
-
db_idx = bisect.bisect(self._keylen_cumulative, idx)
|
| 81 |
-
# Extract index of element within that db.
|
| 82 |
-
el_idx = idx
|
| 83 |
-
if db_idx != 0:
|
| 84 |
-
el_idx = idx - self._keylen_cumulative[db_idx - 1]
|
| 85 |
-
assert el_idx >= 0
|
| 86 |
-
|
| 87 |
-
# Return features.
|
| 88 |
-
datapoint_pickled = (
|
| 89 |
-
self.envs[db_idx]
|
| 90 |
-
.begin()
|
| 91 |
-
.get(f"{self._keys[db_idx][el_idx]}".encode("ascii"))
|
| 92 |
-
)
|
| 93 |
-
data_object = pickle.loads(datapoint_pickled)
|
| 94 |
-
data_object.id = f"{db_idx}_{el_idx}"
|
| 95 |
-
else:
|
| 96 |
-
datapoint_pickled = self.env.begin().get(self._keys[idx])
|
| 97 |
-
if datapoint_pickled is None:
|
| 98 |
-
raise KeyError(f"No data found for index {idx}")
|
| 99 |
-
data_object = pickle.loads(datapoint_pickled)
|
| 100 |
-
|
| 101 |
-
if self.transform is not None:
|
| 102 |
-
data_object = self.transform(data_object)
|
| 103 |
-
|
| 104 |
-
return data_object
|
| 105 |
-
|
| 106 |
-
def connect_db(self, lmdb_path=None):
|
| 107 |
-
env = lmdb.open(
|
| 108 |
-
str(lmdb_path),
|
| 109 |
-
subdir=False,
|
| 110 |
-
readonly=True,
|
| 111 |
-
lock=False,
|
| 112 |
-
readahead=False,
|
| 113 |
-
meminit=False,
|
| 114 |
-
max_readers=1,
|
| 115 |
-
map_size=1099511627776 * 2,
|
| 116 |
-
)
|
| 117 |
-
return env
|
| 118 |
-
|
| 119 |
-
def close_db(self):
|
| 120 |
-
if not self.path.is_file():
|
| 121 |
-
for env in self.envs:
|
| 122 |
-
env.close()
|
| 123 |
-
else:
|
| 124 |
-
self.env.close()
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
def remove_hessian_transform(data):
|
| 128 |
-
# Remove 'hessian' if present as attribute
|
| 129 |
-
if hasattr(data, "hessian"):
|
| 130 |
-
delattr(data, "hessian")
|
| 131 |
-
# Remove 'hessian' if present as key (for dict-like)
|
| 132 |
-
# if isinstance(data, dict) and 'hessian' in data:
|
| 133 |
-
# del data['hessian']
|
| 134 |
-
return data
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
def fix_hessian_eigen_transform(data):
|
| 138 |
-
"""Fixes errors in old versions of the code.
|
| 139 |
-
Only needed for legacy compatibility.
|
| 140 |
-
You can probably remove this function.
|
| 141 |
-
|
| 142 |
-
Sizes of tensors must match except in dimension 0.
|
| 143 |
-
|
| 144 |
-
Hessian eigen information is stored as:
|
| 145 |
-
hessian_eigenvalues: torch.Size([2])
|
| 146 |
-
hessian_eigenvectors: torch.Size([2, N*3])
|
| 147 |
-
|
| 148 |
-
Instead save as:
|
| 149 |
-
hessian_eigenvalue_1: torch.Size([1])
|
| 150 |
-
hessian_eigenvalue_2: torch.Size([1])
|
| 151 |
-
hessian_eigenvector_1: torch.Size([N, 3])
|
| 152 |
-
hessian_eigenvector_2: torch.Size([N, 3])
|
| 153 |
-
"""
|
| 154 |
-
# Check if hessian eigenvalue/eigenvector data exists
|
| 155 |
-
if hasattr(data, "hessian_eigenvalues") and hasattr(data, "hessian_eigenvectors"):
|
| 156 |
-
eigenvalues = data.hessian_eigenvalues
|
| 157 |
-
eigenvectors = data.hessian_eigenvectors
|
| 158 |
-
|
| 159 |
-
# Split eigenvalues into separate attributes
|
| 160 |
-
data.hessian_eigenvalue_1 = eigenvalues[0:1] # Keep as [1] tensor
|
| 161 |
-
data.hessian_eigenvalue_2 = eigenvalues[1:2] # Keep as [1] tensor
|
| 162 |
-
|
| 163 |
-
# Reshape and split eigenvectors from [2, N*3] to [N, 3] format
|
| 164 |
-
n_atoms = len(data.pos) # Get number of atoms from positions
|
| 165 |
-
eigenvector_1 = eigenvectors[0].reshape(n_atoms, 3)
|
| 166 |
-
eigenvector_2 = eigenvectors[1].reshape(n_atoms, 3)
|
| 167 |
-
|
| 168 |
-
data.hessian_eigenvector_1 = eigenvector_1
|
| 169 |
-
data.hessian_eigenvector_2 = eigenvector_2
|
| 170 |
-
|
| 171 |
-
# Remove original attributes
|
| 172 |
-
delattr(data, "hessian_eigenvalues")
|
| 173 |
-
delattr(data, "hessian_eigenvectors")
|
| 174 |
-
|
| 175 |
-
# Remove batch artifacts manually
|
| 176 |
-
for key in ["batch", "ptr"]:
|
| 177 |
-
if hasattr(data, key):
|
| 178 |
-
delattr(data, key)
|
| 179 |
-
|
| 180 |
-
return data
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
if __name__ == "__main__":
|
| 184 |
-
import os
|
| 185 |
-
|
| 186 |
-
dataset_dir = os.path.expanduser(
|
| 187 |
-
"~/.cache/kagglehub/datasets/yunhonghan/hessian-dataset-for-optimizing-reactive-mliphorm/versions/5/"
|
| 188 |
-
)
|
| 189 |
-
dataset_files = [
|
| 190 |
-
"ts1x-val.lmdb",
|
| 191 |
-
"ts1x_hess_train_big.lmdb",
|
| 192 |
-
"RGD1.lmdb",
|
| 193 |
-
]
|
| 194 |
-
lmdb_path = os.path.join(dataset_dir, dataset_files[0])
|
| 195 |
-
lmdb_dataset = LmdbDataset(lmdb_path)
|
| 196 |
-
print("length of lmdb_dataset:", len(lmdb_dataset))
|
| 197 |
-
print("first element of lmdb_dataset:", lmdb_dataset[0])
|
| 198 |
-
print("first element of lmdb_dataset.pos:", lmdb_dataset[0].pos)
|
| 199 |
-
print("first element of lmdb_dataset.ae:", lmdb_dataset[0].ae)
|
| 200 |
-
first_elem = lmdb_dataset[0]
|
| 201 |
-
print("")
|
| 202 |
-
print("hasattr(first_elem, 'hessian'):", hasattr(first_elem, "hessian"))
|
| 203 |
-
print("'hessian' in first_elem:", "hessian" in first_elem)
|
| 204 |
-
|
| 205 |
-
# Test with transform that removes hessian
|
| 206 |
-
lmdb_dataset_no_hessian = LmdbDataset(lmdb_path, transform=remove_hessian_transform)
|
| 207 |
-
first_elem = lmdb_dataset_no_hessian[0]
|
| 208 |
-
print("")
|
| 209 |
-
print("hasattr(first_elem, 'hessian'):", hasattr(first_elem, "hessian"))
|
| 210 |
-
print("'hessian' in first_elem:", "hessian" in first_elem)
|
| 211 |
-
|
| 212 |
-
for fname in dataset_files:
|
| 213 |
-
path = os.path.join(dataset_dir, fname)
|
| 214 |
-
ds = LmdbDataset(path)
|
| 215 |
-
print(f"Size of {fname}: {len(ds)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data/sample_100-dft-hess-eigen.lmdb
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:84b83cc3b3a7b3071df46a7921972c0c71f3033d4eee11c45951b1feaadf05d1
|
| 3 |
-
size 2203648
|
|
|
|
|
|
|
|
|
|
|
|
data/sample_100.lmdb
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:7867b13406cd78642c5a28d06ec4292125d678c01713176bee0b42a599aec054
|
| 3 |
-
size 1339392
|
|
|
|
|
|
|
|
|
|
|
|