-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_test.py
316 lines (268 loc) · 10.4 KB
/
dataset_test.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from __future__ import annotations
import os
import random
from dataclasses import dataclass
from typing import Tuple, List, Optional
from tqdm import tqdm
import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
from rdkit import Chem
from rdkit.Chem import AllChem, MACCSkeys
from pathlib import Path
def molecule_to_maccs(smiles: str):
molecule = Chem.MolFromSmiles(smiles)
return np.packbits(MACCSkeys.GenMACCSKeys(molecule))
from usearch.index import Index, Matches, Key
import stringzilla as sz
from to_fingerprint import (
# smiles_to_maccs_ecfp4_fcfp4,
FingerprintShape,
shape_maccs,
shape_mixed,
)
SEED = 42 # For reproducibility
SHARD_SIZE = 1_000_000 # This would result in files between 150 and 300 MB
# SHARD_SIZE = 1_000_000 # This would result in files between 150 and 300 MB
BATCH_SIZE = 100_000 # A good threshold to split insertions
@dataclass
class FingerprintedEntry:
"""SMILES string augmented with a potentially hybrid fingerprint of known `FingerprintShape` shape."""
# smiles: str
fingerprint: np.ndarray
key: Optional[int] = None
@staticmethod
def from_table_row(
table: pa.Table, row: int, shape: FingerprintShape
) -> FingerprintedEntry:
fingerprint = np.zeros(shape.nbytes, dtype=np.uint8)
progress = 0
if shape.include_maccs:
fingerprint[progress : progress + 21] = table["maccs"][row].as_buffer()
progress += 21
if shape.nbytes_padding:
progress += shape.nbytes_padding
if shape.include_ecfp4:
fingerprint[progress : progress + 256] = table["ecfp4"][row].as_buffer()
progress += 256
if shape.include_fcfp4:
fingerprint[progress : progress + 256] = table["fcfp4"][row].as_buffer()
progress += 256
return FingerprintedEntry(smiles=table["smiles"][row], fingerprint=fingerprint)
@staticmethod
def from_parts(
# smiles: str,
maccs: np.ndarray,
# ecfp4: np.ndarray,
# fcfp4: np.ndarray,
shape: FingerprintShape,
) -> FingerprintedEntry:
fingerprint = np.zeros(shape.nbytes, dtype=np.uint8)
progress = 0
# print(maccs.dtype)
# print(maccs)
if shape.include_maccs:
fingerprint[progress : progress + 21] = maccs
progress += 21
if shape.nbytes_padding:
progress += shape.nbytes_padding
# if shape.include_ecfp4:
# fingerprint[progress : progress + 256] = ecfp4
# progress += 256
# if shape.include_fcfp4:
# fingerprint[progress : progress + 256] = fcfp4
# progress += 256
return FingerprintedEntry(fingerprint=fingerprint)
# return FingerprintedEntry(smiles=smiles, fingerprint=fingerprint)
def shard_name(dir: str, from_index: int, to_index: int, kind: str):
return os.path.join(dir, kind, f"{from_index:0>10}-{to_index:0>10}.{kind}")
def write_table(table: pa.Table, path_out: os.PathLike):
return pq.write_table(
table,
path_out,
# Without compression the file size may be too large.
# compression="NONE",
write_statistics=False,
store_schema=True,
use_dictionary=False,
)
@dataclass
class FingerprintedShard:
"""Potentially cached table and smiles path containing up to `SHARD_SIZE` entries."""
first_key: int
name: str
table_path: os.PathLike
# smiles_path: os.PathLike
table_cached: Optional[pa.Table] = None
# smiles_caches: Optional[sz.Strs] = None
@property
def is_complete(self) -> bool:
return os.path.exists(self.table_path) and os.path.exists(self.smiles_path)
@property
def table(self) -> pa.Table:
return self.load_table()
# @property
# def smiles(self) -> sz.Strs:
# return self.load_smiles()['graph']
# @property
# def label(self) -> sz.Strs:
# return self.load_smiles()['label']
def load_table(self, columns=None, view=False) -> pa.Table:
if not self.table_cached:
self.table_cached = pq.read_table(
self.table_path,
memory_map=view,
columns=columns,
)
return self.table_cached
# def load_smiles(self) -> sz.Strs:
# if not self.smiles_caches:
# print(self.smiles_caches)
# self.smiles_caches = sz.Str(sz.File(self.smiles_path)).splitlines()
# print(self.smiles_caches)
# return self.smiles_caches
# def load_smiles(self, columns=None, view=False) -> pa.Table:
# if not self.table_cached:
# self.table_cached = pq.read_table(
# self.table_path,
# memory_map=view,
# columns=columns,
# )
# return self.table_cached
@dataclass
class FingerprintedDataset:
dir: os.PathLike
shards: List[FingerprintedShard]
shape: Optional[FingerprintShape] = None
index: Optional[Index] = None
@staticmethod
def open(
dir: os.PathLike,
shape: Optional[FingerprintShape] = None,
max_shards: Optional[int] = None,
) -> FingerprintedDataset:
"""Gather a list of files forming the dataset."""
if dir is None:
return FingerprintedDataset(dir=None, shards=[], shape=shape)
shards = []
# filenames = sorted(os.listdir(os.path.join(dir, "parquet")))
filenames = sorted(os.listdir(dir))
if max_shards:
filenames = filenames[:max_shards]
# print(f'filenames={filenames}')
for filename in tqdm(filenames, unit="shard"):
if not filename.endswith(".parquet"):
continue
filename = filename.replace(".parquet", "")
# first_key = int(filename.split("-")[0])
first_key = int(filename.split("_")[-1])
# print(first_key)
table_path = os.path.join(dir, filename + ".parquet")
# table_path = os.path.join(dir, "parquet", filename + ".parquet")
# smiles_path = os.path.join(dir, "smiles", filename + ".smi")
# smiles_path = os.path.join(dir, "parquet", filename + ".parquet")
shard = FingerprintedShard(
first_key=first_key,
name=filename,
table_path=table_path,
# smiles_path=smiles_path,
)
shards.append(shard)
# print(f'shards={shards}')
# print(f"Fetched {len(shards)} shards")
index = None
if shape:
index_path = os.path.join(dir, shape.index_name)
# print(f'index_path: {index_path}')
if os.path.exists(index_path):
index = Index.restore(index_path)
# print(f"Loaded index with {len(index)} entries")
return FingerprintedDataset(dir=dir, shards=shards, shape=shape, index=index)
def shard_containing(self, key: int) -> FingerprintedShard:
# print(f'self.shards={self.shards}')
for shard in self.shards:
if shard.first_key <= key and key <= (shard.first_key + SHARD_SIZE):
return shard
def head(
self,
max_rows: int,
shape: Optional[FingerprintShape] = None,
shuffle: bool = False,
) -> Tuple[List[str], List[int], np.ndarray]:
"""Load the first part of the dataset. Mostly used for preview and testing."""
if self.dir is None:
return None
if not shape:
shape = self.shape
exported_rows = 0
smiles = []
keys = []
fingers = []
for shard in self.shards:
table = shard.load_table()
chunk_size = len(table)
for i in range(chunk_size):
entry = FingerprintedEntry.from_table_row(table, i, shape)
keys.append(exported_rows)
smiles.append(entry.smiles)
fingers.append(entry.fingerprint)
exported_rows += 1
if exported_rows >= max_rows:
break
if exported_rows >= max_rows:
break
smiles = np.array(smiles, dtype=object)
keys = np.array(keys, dtype=Key)
fingers = np.vstack(fingers)
if shuffle:
permutation = np.arange(len(keys))
np.random.shuffle(permutation)
smiles = smiles[permutation]
keys = keys[permutation]
fingers = fingers[permutation]
return smiles, keys, fingers
def search(
self,
# smiles: str,
fingers: str,
count: int = 10,
log: bool = False,
) -> List[Tuple[int, str, float]]:
"""Search for similar molecules in the whole dataset."""
# fingers: tuple = smiles_to_maccs_ecfp4_fcfp4(smiles)
# print('search')
# fingers: tuple = molecule_to_maccs(smiles)
# print(fingers)
entry = FingerprintedEntry.from_parts(
# smiles,
fingers,
# fingers[1],
# fingers[2],
self.shape,
)
# print(len(self.index))
results: Matches = self.index.search(entry.fingerprint, count, log=log)
# print(results) usearch.Matches(100)
filtered_results = []
for match in results:
# print(match.key)
shard = self.shard_containing(match.key)
# print(shard.first_key)
# print(shard) FingerprintedShard(first_key=0, name='00-11', table_path='/home/leslie/GIMLET/my_process/qm9/parquet/00-11.parquet', smiles_path='/home/leslie/GIMLET/my_process/qm9/smiles/00-11.smi', table_cached=None, smiles_caches=None)
row = int(match.key - shard.first_key)
# print(shard.smiles)
# print(shard.smiles[row])
result = str(shard.table['graph'][row])
label = str(shard.table['label'][row])
filtered_results.append((match.key, result, match.distance, label))
return filtered_results
def __len__(self) -> int:
return len(self.index)
def random_smiles(self) -> str:
shard_idx = random.randint(0, len(self.shards) - 1)
shard = self.shards[shard_idx]
row = random.randint(0, len(shard.smiles) - 1)
return str(shard.smiles[row])
if __name__ == "__main__":
dataset = FingerprintedDataset.open("data/pubchem", shape=shape_maccs)
dataset.search("C")