[d3ab9c]: / bert_mixup / late_mixup / enumeration.py

Download this file

360 lines (321 with data), 12.9 kB

  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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Script that performs Enumeration based augmentation for chemical SMILES
Implementation borrow from: https://github.com/EBjerrum/SMILES-enumeration
"""
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
import os
import shutil
import numpy as np
import deepchem as dc
from deepchem.molnet import load_muv
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
# Experimental Class for Smiles Enumeration, Iterator and SmilesIterator adapted from Keras 2.6.0
from rdkit import Chem
import threading
np.random.seed(123)
class Iterator(object):
"""Abstract base class for data iterators.
# Arguments
n: Integer, total number of samples in the dataset to loop over.
batch_size: Integer, size of a batch.
shuffle: Boolean, whether to shuffle the data between epochs.
seed: Random seeding for data shuffling.
"""
def __init__(self, n, batch_size, shuffle, seed):
self.n = n
self.batch_size = batch_size
self.shuffle = shuffle
self.batch_index = 0
self.total_batches_seen = 0
self.lock = threading.Lock()
self.index_generator = self._flow_index(n, batch_size, shuffle, seed)
if n < batch_size:
raise ValueError(
"Input data length is shorter than batch_size\nAdjust batch_size"
)
def reset(self):
self.batch_index = 0
def _flow_index(self, n, batch_size=32, shuffle=False, seed=None):
# Ensure self.batch_index is 0.
self.reset()
while 1:
if seed is not None:
np.random.seed(seed + self.total_batches_seen)
if self.batch_index == 0:
index_array = np.arange(n)
if shuffle:
index_array = np.random.permutation(n)
current_index = (self.batch_index * batch_size) % n
if n > current_index + batch_size:
current_batch_size = batch_size
self.batch_index += 1
else:
current_batch_size = n - current_index
self.batch_index = 0
self.total_batches_seen += 1
yield (
index_array[current_index : current_index + current_batch_size],
current_index,
current_batch_size,
)
def __iter__(self):
# Needed if we want to do something like:
# for x, y in data_gen.flow(...):
return self
def __next__(self, *args, **kwargs):
return self.next(*args, **kwargs)
class SmilesIterator(Iterator):
"""Iterator yielding data from a SMILES array.
# Arguments
x: Numpy array of SMILES input data.
y: Numpy array of targets data.
smiles_data_generator: Instance of `SmilesEnumerator`
to use for random SMILES generation.
batch_size: Integer, size of a batch.
shuffle: Boolean, whether to shuffle the data between epochs.
seed: Random seed for data shuffling.
dtype: dtype to use for returned batch. Set to keras.backend.floatx if using Keras
"""
def __init__(
self,
x,
y,
smiles_data_generator,
batch_size=32,
shuffle=False,
seed=None,
dtype=np.float32,
):
if y is not None and len(x) != len(y):
raise ValueError(
"X (images tensor) and y (labels) "
"should have the same length. "
"Found: X.shape = %s, y.shape = %s"
% (np.asarray(x).shape, np.asarray(y).shape)
)
self.x = np.asarray(x)
if y is not None:
self.y = np.asarray(y)
else:
self.y = None
self.smiles_data_generator = smiles_data_generator
self.dtype = dtype
super(SmilesIterator, self).__init__(x.shape[0], batch_size, shuffle, seed)
def next(self):
"""For python 2.x.
# Returns
The next batch.
"""
# Keeps under lock only the mechanism which advances
# the indexing of each batch.
with self.lock:
index_array, current_index, current_batch_size = next(self.index_generator)
# The transformation of images is not under thread lock
# so it can be done in parallel
batch_x = np.zeros(
tuple(
[current_batch_size]
+ [self.smiles_data_generator.pad, self.smiles_data_generator._charlen]
),
dtype=self.dtype,
)
for i, j in enumerate(index_array):
smiles = self.x[j : j + 1]
x = self.smiles_data_generator.transform(smiles)
batch_x[i] = x
if self.y is None:
return batch_x
batch_y = self.y[index_array]
return batch_x, batch_y
class SmilesEnumerator(object):
"""SMILES Enumerator, vectorizer and devectorizer
#Arguments
charset: string containing the characters for the vectorization
can also be generated via the .fit() method
pad: Length of the vectorization
leftpad: Add spaces to the left of the SMILES
isomericSmiles: Generate SMILES containing information about stereogenic centers
enum: Enumerate the SMILES during transform
canonical: use canonical SMILES during transform (overrides enum)
"""
def __init__(
self,
charset="@C)(=cOn1S2/H[N]\\",
pad=120,
leftpad=True,
isomericSmiles=True,
enum=True,
canonical=False,
):
self._charset = None
self.charset = charset
self.pad = pad
self.leftpad = leftpad
self.isomericSmiles = isomericSmiles
self.enumerate = enum
self.canonical = canonical
@property
def charset(self):
return self._charset
@charset.setter
def charset(self, charset):
self._charset = charset
self._charlen = len(charset)
self._char_to_int = dict((c, i) for i, c in enumerate(charset))
self._int_to_char = dict((i, c) for i, c in enumerate(charset))
def fit(self, smiles, extra_chars=[], extra_pad=5):
"""Performs extraction of the charset and length of a SMILES datasets and sets self.pad and self.charset
#Arguments
smiles: Numpy array or Pandas series containing smiles as strings
extra_chars: List of extra chars to add to the charset (e.g. "\\\\" when "/" is present)
extra_pad: Extra padding to add before or after the SMILES vectorization
"""
charset = set("".join(list(smiles)))
self.charset = "".join(charset.union(set(extra_chars)))
self.pad = max([len(smile) for smile in smiles]) + extra_pad
def randomize_smiles(self, smiles):
"""Perform a randomization of a SMILES string
must be RDKit sanitizable"""
m = Chem.MolFromSmiles(smiles)
ans = list(range(m.GetNumAtoms()))
np.random.shuffle(ans)
nm = Chem.RenumberAtoms(m, ans)
return Chem.MolToSmiles(
nm, canonical=self.canonical, isomericSmiles=self.isomericSmiles
)
def transform(self, smiles):
"""Perform an enumeration (randomization) and vectorization of a Numpy array of smiles strings
#Arguments
smiles: Numpy array or Pandas series containing smiles as strings
"""
one_hot = np.zeros((smiles.shape[0], self.pad, self._charlen), dtype=np.int8)
errors = 0
if self.leftpad:
for i, ss in enumerate(smiles):
if self.enumerate:
ss = self.randomize_smiles(ss)
l = len(ss)
diff = self.pad - l
for j, c in enumerate(ss):
try:
one_hot[i, j + diff, self._char_to_int[c]] = 1
except:
errors += 1
break
# print(f"errors: {errors}")
return one_hot
else:
for i, ss in enumerate(smiles):
if self.enumerate:
ss = self.randomize_smiles(ss)
for j, c in enumerate(ss):
try:
one_hot[i, j, self._char_to_int[c]] = 1
except:
errors += 1
break
# print(f"errors: {errors}")
return one_hot
def reverse_transform(self, vect):
"""Performs a conversion of a vectorized SMILES to a smiles strings
charset must be the same as used for vectorization.
#Arguments
vect: Numpy array of vectorized SMILES.
"""
smiles = []
for v in vect:
# mask v
v = v[v.sum(axis=1) == 1]
# Find one hot encoded index with argmax, translate to char and join to string
smile = "".join(self._int_to_char[i] for i in v.argmax(axis=1))
smiles.append(smile)
return np.array(smiles)
def enumerate_smiles(
self,
data_reader,
smiles_col,
replication_count=2,
random_pairs=False,
rand_proba=0.0,
):
"""
Performs enumeration augmentation on the canonical molecular SMILES
Args:
dataset (_type_): dataframe containing molecular SMILSS
smiles_col (_type_): column corresponding to molecular SMILES
replication_count (int, optional): Number of enumerations for each CHEMICAL SMILE. Defaults to 2.
"""
smiles = np.repeat(data_reader.dataset[smiles_col].values, replication_count)
self.fit(smiles, extra_chars=["\\"])
v = self.transform(smiles)
transformed = self.reverse_transform(v)
# print(len(v), len(original_smiles), len(transformed))
is_enumerated = [1] * len(smiles)
if random_pairs:
assert len(smiles) == len(
transformed
), "The length of augmented SMILES must equal original SMILES"
for idx, _ in enumerate(smiles):
if round(np.random.uniform(), 1) > rand_proba:
continue
else:
transformed[idx] = np.random.choice(smiles)
is_enumerated[idx] = 0
return transformed, list(smiles), is_enumerated
def enumerate_smiles_df(
self,
data_reader,
smiles_col,
replication_count=2,
random_pairs=False,
rand_proba=0.0,
):
"""
Performs enumeration augmentation on the canonical molecular SMILES
Args:
dataset (_type_): dataframe containing molecular SMILSS
smiles_col (_type_): column corresponding to molecular SMILES
replication_count (int, optional): Number of enumerations for each CHEMICAL SMILE. Defaults to 2.
"""
smiles = np.repeat(data_reader[smiles_col].values, replication_count)
self.fit(smiles, extra_chars=["\\"])
v = self.transform(smiles)
transformed = self.reverse_transform(v)
# print(len(v), len(original_smiles), len(transformed))
is_enumerated = [1] * len(smiles)
if random_pairs:
assert len(smiles) == len(
transformed
), "The length of augmented SMILES must equal original SMILES"
for idx, _ in enumerate(smiles):
if round(np.random.uniform(), 1) > rand_proba:
continue
else:
transformed[idx] = np.random.choice(smiles)
is_enumerated[idx] = 0
return transformed, is_enumerated
def smiles_enumeration(self, input_smiles, replication_count=100, n_augment=0):
"""
Performs enumeration augmentation on the canonical molecular SMILES
Args:
dataset (_type_): dataframe containing molecular SMILSS
smiles_col (_type_): column corresponding to molecular SMILES
replication_count (int, optional): Number of enumerations for each CHEMICAL SMILE. Defaults to 2.
"""
enumerations = []
try:
smiles = np.repeat([input_smiles], replication_count)
self.fit(smiles, extra_chars=["\\"])
v = self.transform(smiles)
transformed = self.reverse_transform(v)
for _, enumerated_smiles in enumerate(transformed):
if len(enumerated_smiles) >= len(input_smiles):
enumerations.append(enumerated_smiles)
if len(enumerations) >= n_augment:
break
except:
pass
return enumerations