-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_encode_test.py
294 lines (230 loc) · 8.42 KB
/
prep_encode_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
"""Fingerprints the molecules, encoding them with 4 techniques, producing 28 Billions fingerprints for 7 Billion molecules."""
import os
import logging
from typing import List, Callable
from multiprocessing import Process, cpu_count
from rdkit.Chem import AllChem, MACCSkeys
from pathlib import Path
import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
from usearch.index import Index, CompiledMetric, MetricKind, MetricSignature, ScalarKind
from usearch.eval import self_recall, SearchStats
from metrics_numba import (
tanimoto_conditional,
tanimoto_maccs,
)
from to_fingerprint import (
# smiles_to_maccs_ecfp4_fcfp4,
# smiles_to_pubchem,
shape_mixed,
shape_maccs,
)
from dataset import (
write_table,
FingerprintedDataset,
FingerprintedEntry,
)
logger = logging.getLogger(__name__)
from rdkit import Chem
import pandas as pd
import pyarrow as pa
import shutil
def molecule_to_maccs(
smiles: str):
molecule = Chem.MolFromSmiles(smiles)
return np.packbits(MACCSkeys.GenMACCSKeys(molecule))
def augment_with_rdkit(parquet_path: os.PathLike):
meta = pq.read_metadata(parquet_path)
column_names: List[str] = meta.schema.names
if "maccs" in column_names:
return
logger.info(f"Starting file {parquet_path}")
table: pa.Table = pq.read_table(parquet_path)
maccs_list = []
for smiles in table["smiles"]:
try:
fingers = molecule_to_maccs(str(smiles))
maccs_list.append(fingers.tobytes())
except Exception:
maccs_list.append(bytes(bytearray(21)))
maccs_list = pa.array(maccs_list, pa.binary(21))
maccs_field = pa.field("maccs", pa.binary(21), nullable=False)
table = table.append_column(maccs_field, maccs_list)
write_table(table, parquet_path)
def augment_parquets_shard(
parquet_dir: os.PathLike,
augmentation: Callable,
shard_index: int,
shards_count: int,
):
filenames: List[str] = sorted(os.listdir(parquet_dir))
files_count = len(filenames)
print(parquet_dir, augmentation, shard_index, shards_count)
try:
for file_idx in range(shard_index, files_count, shards_count):
try:
filename = filenames[file_idx]
augmentation(os.path.join(parquet_dir, filename))
logger.info(
"Augmented shard {}. Process # {} / {}".format(
filename, shard_index, shards_count
)
)
except KeyboardInterrupt as e:
raise e
except KeyboardInterrupt as e:
logger.info(f"Stopping shard {shard_index} / {shards_count}")
raise e
def augment_parquet_shards(
parquet_dir: os.PathLike,
augmentation: Callable,
processes: int = 1,
):
if processes > 1:
process_pool = []
for i in range(processes):
print(parquet_dir, augmentation, i, processes)
p = Process(
target=augment_parquets_shard,
args=(parquet_dir, augmentation, i, processes),
)
p.start()
process_pool.append(p)
for p in process_pool:
p.join()
else:
augment_parquets_shard(parquet_dir, augmentation, 0, 1)
def shards_index(dataset: FingerprintedDataset):
os.makedirs(os.path.join(dataset.dir, "usearch-maccs"), exist_ok=True)
for shard_idx, shard in enumerate(dataset.shards):
index_path_maccs = os.path.join(
dataset.dir, "usearch-maccs", shard.name + ".usearch"
)
if (
Index.metadata(index_path_maccs) is not None
):
continue
logger.info(f"Starting {shard_idx + 1} / {len(dataset.shards)}")
table = shard.load_table()
n = len(table)
# No need to shuffle the entries as they already are:
keys = np.arange(shard.first_key, shard.first_key + n)
maccs_fingerprints = [table["maccs"][i].as_buffer() for i in range(n)]
# First construct the index just for MACCS representations
vectors = np.vstack(
[
FingerprintedEntry.from_parts(
None,
maccs_fingerprints[i],
None,
None,
shape_maccs,
).fingerprint
for i in range(n)
]
)
index_maccs = Index(
ndim=shape_maccs.nbits,
dtype=ScalarKind.B1,
metric=CompiledMetric(
pointer=tanimoto_maccs.address,
kind=MetricKind.Tanimoto,
signature=MetricSignature.ArrayArray,
),
)
index_maccs.add(
keys,
vectors,
log=f"Building {index_path_maccs}",
batch_size=100_000,
)
# Optional self-recall evaluation:
stats: SearchStats = self_recall(index_maccs, sample=0.01)
logger.info(f"Self-recall: {100*stats.mean_recall:.2f} %")
logger.info(f"Efficiency: {100*stats.mean_efficiency:.2f} %")
index_maccs.save(index_path_maccs)
# Discard the objects to save some memory
dataset.shards[shard_idx].table_cached = None
dataset.shards[shard_idx].index_cached = None
def mono_index_maccs(dataset: FingerprintedDataset):
index_path_maccs = os.path.join("indexes", dataset.dir, "usearch-maccs.usearch")
print(index_path_maccs)
os.makedirs(os.path.join("indexes", dataset.dir), exist_ok=True)
index_maccs = Index(
ndim=shape_maccs.nbits,
dtype=ScalarKind.B1,
metric=CompiledMetric(
pointer=tanimoto_maccs.address,
kind=MetricKind.Tanimoto,
signature=MetricSignature.ArrayArray,
),
# path=index_path_maccs,
)
try:
for shard_idx, shard in enumerate(dataset.shards):
if shard.first_key in index_maccs:
logger.info(f"Skipping {shard_idx + 1} / {len(dataset.shards)}")
continue
logger.info(f"Starting {shard_idx + 1} / {len(dataset.shards)}")
table = shard.load_table(["maccs"])
n = len(table)
# No need to shuffle the entries as they already are:
keys = np.arange(shard.first_key, shard.first_key + n)
maccs_fingerprints = [table["maccs"][i].as_buffer() for i in range(n)]
# First construct the index just for MACCS representations
vectors = np.vstack(
[
FingerprintedEntry.from_parts(
None,
maccs_fingerprints[i],
None,
None,
shape_maccs,
).fingerprint
for i in range(n)
]
)
index_maccs.add(keys, vectors, log=f"Building {index_path_maccs}")
# Optional self-recall evaluation:
# stats: SearchStats = self_recall(index_maccs, sample=1000)
# logger.info(f"Self-recall: {100*stats.mean_recall:.2f} %")
# logger.info(f"Efficiency: {100*stats.mean_efficiency:.2f} %")
if shard_idx % 100 == 0:
index_maccs.save(index_path_maccs)
# Discard the objects to save some memory
dataset.shards[shard_idx].table_cached = None
dataset.shards[shard_idx].index_cached = None
index_maccs.save(index_path_maccs)
index_maccs.reset()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
logger.info("Time to encode some molecules!")
processes = max(cpu_count() - 4, 1)
# classfication and regression datasets:
datasets = [
'bace',
'bbbp',
'cyp450',
'hiv',
'muv',
'pcba',
'tox21',
'toxcast',
'esol',
'freesolv',
'lipo'
]
for dataset in datasets:
# encode total
path = os.path.join('./test_process/', dataset)
file_names = [f.name for f in Path(path).iterdir() if f.is_dir()]
for file_name in file_names:
dir = os.path.join(path, file_name)
# print(dir)
augment_parquet_shards(dir, augment_with_rdkit, processes)
# encode test
path = os.path.join('./test_process/', dataset)
# print(path)
augment_parquet_shards(path, augment_with_rdkit, processes)