[b40915]: / unimol / data / mask_points_dataset.py

Download this file

268 lines (238 with data), 9.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
# Copyright (c) DP Technology.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from functools import lru_cache
import numpy as np
import torch
from unicore.data import Dictionary
from unicore.data import BaseWrapperDataset
from . import data_utils
class MaskPointsDataset(BaseWrapperDataset):
def __init__(
self,
dataset: torch.utils.data.Dataset,
coord_dataset: torch.utils.data.Dataset,
vocab: Dictionary,
pad_idx: int,
mask_idx: int,
noise_type: str,
noise: float = 1.0,
seed: int = 1,
mask_prob: float = 0.15,
leave_unmasked_prob: float = 0.1,
random_token_prob: float = 0.1,
):
assert 0.0 < mask_prob < 1.0
assert 0.0 <= random_token_prob <= 1.0
assert 0.0 <= leave_unmasked_prob <= 1.0
assert random_token_prob + leave_unmasked_prob <= 1.0
self.dataset = dataset
self.coord_dataset = coord_dataset
self.vocab = vocab
self.pad_idx = pad_idx
self.mask_idx = mask_idx
self.noise_type = noise_type
self.noise = noise
self.seed = seed
self.mask_prob = mask_prob
self.leave_unmasked_prob = leave_unmasked_prob
self.random_token_prob = random_token_prob
if random_token_prob > 0.0:
weights = np.ones(len(self.vocab))
weights[vocab.special_index()] = 0
self.weights = weights / weights.sum()
self.epoch = None
if self.noise_type == "trunc_normal":
self.noise_f = lambda num_mask: np.clip(
np.random.randn(num_mask, 3) * self.noise,
a_min=-self.noise * 2.0,
a_max=self.noise * 2.0,
)
elif self.noise_type == "normal":
self.noise_f = lambda num_mask: np.random.randn(num_mask, 3) * self.noise
elif self.noise_type == "uniform":
self.noise_f = lambda num_mask: np.random.uniform(
low=-self.noise, high=self.noise, size=(num_mask, 3)
)
else:
self.noise_f = lambda num_mask: 0.0
def set_epoch(self, epoch, **unused):
super().set_epoch(epoch)
self.coord_dataset.set_epoch(epoch)
self.dataset.set_epoch(epoch)
self.epoch = epoch
def __getitem__(self, index: int):
return self.__getitem_cached__(self.epoch, index)
@lru_cache(maxsize=16)
def __getitem_cached__(self, epoch: int, index: int):
ret = {}
with data_utils.numpy_seed(self.seed, epoch, index):
item = self.dataset[index]
coord = self.coord_dataset[index]
sz = len(item)
# don't allow empty sequence
assert sz > 0
# decide elements to mask
num_mask = int(
# add a random number for probabilistic rounding
self.mask_prob * sz
+ np.random.rand()
)
mask_idc = np.random.choice(sz, num_mask, replace=False)
mask = np.full(sz, False)
mask[mask_idc] = True
ret["targets"] = np.full(len(mask), self.pad_idx)
ret["targets"][mask] = item[mask]
ret["targets"] = torch.from_numpy(ret["targets"]).long()
# decide unmasking and random replacement
rand_or_unmask_prob = self.random_token_prob + self.leave_unmasked_prob
if rand_or_unmask_prob > 0.0:
rand_or_unmask = mask & (np.random.rand(sz) < rand_or_unmask_prob)
if self.random_token_prob == 0.0:
unmask = rand_or_unmask
rand_mask = None
elif self.leave_unmasked_prob == 0.0:
unmask = None
rand_mask = rand_or_unmask
else:
unmask_prob = self.leave_unmasked_prob / rand_or_unmask_prob
decision = np.random.rand(sz) < unmask_prob
unmask = rand_or_unmask & decision
rand_mask = rand_or_unmask & (~decision)
else:
unmask = rand_mask = None
if unmask is not None:
mask = mask ^ unmask
new_item = np.copy(item)
new_item[mask] = self.mask_idx
num_mask = mask.astype(np.int32).sum()
new_coord = np.copy(coord)
new_coord[mask, :] += self.noise_f(num_mask)
if rand_mask is not None:
num_rand = rand_mask.sum()
if num_rand > 0:
new_item[rand_mask] = np.random.choice(
len(self.vocab),
num_rand,
p=self.weights,
)
ret["atoms"] = torch.from_numpy(new_item).long()
ret["coordinates"] = torch.from_numpy(new_coord).float()
return ret
class MaskPointsPocketDataset(BaseWrapperDataset):
def __init__(
self,
dataset: torch.utils.data.Dataset,
coord_dataset: torch.utils.data.Dataset,
residue_dataset: torch.utils.data.Dataset,
vocab: Dictionary,
pad_idx: int,
mask_idx: int,
noise_type: str,
noise: float = 1.0,
seed: int = 1,
mask_prob: float = 0.15,
leave_unmasked_prob: float = 0.1,
random_token_prob: float = 0.1,
):
assert 0.0 < mask_prob < 1.0
assert 0.0 <= random_token_prob <= 1.0
assert 0.0 <= leave_unmasked_prob <= 1.0
assert random_token_prob + leave_unmasked_prob <= 1.0
self.dataset = dataset
self.coord_dataset = coord_dataset
self.residue_dataset = residue_dataset
self.vocab = vocab
self.pad_idx = pad_idx
self.mask_idx = mask_idx
self.noise_type = noise_type
self.noise = noise
self.seed = seed
self.mask_prob = mask_prob
self.leave_unmasked_prob = leave_unmasked_prob
self.random_token_prob = random_token_prob
if random_token_prob > 0.0:
weights = np.ones(len(self.vocab))
weights[vocab.special_index()] = 0
self.weights = weights / weights.sum()
self.epoch = None
if self.noise_type == "trunc_normal":
self.noise_f = lambda num_mask: np.clip(
np.random.randn(num_mask, 3) * self.noise,
a_min=-self.noise * 2.0,
a_max=self.noise * 2.0,
)
elif self.noise_type == "normal":
self.noise_f = lambda num_mask: np.random.randn(num_mask, 3) * self.noise
elif self.noise_type == "uniform":
self.noise_f = lambda num_mask: np.random.uniform(
low=-self.noise, high=self.noise, size=(num_mask, 3)
)
else:
self.noise_f = lambda num_mask: 0.0
def set_epoch(self, epoch, **unused):
super().set_epoch(epoch)
self.coord_dataset.set_epoch(epoch)
self.dataset.set_epoch(epoch)
self.epoch = epoch
def __getitem__(self, index: int):
return self.__getitem_cached__(self.epoch, index)
@lru_cache(maxsize=16)
def __getitem_cached__(self, epoch: int, index: int):
ret = {}
with data_utils.numpy_seed(self.seed, epoch, index):
item = self.dataset[index]
coord = self.coord_dataset[index]
sz = len(item)
# don't allow empty sequence
assert sz > 0
# mask on the level of residues
residue = self.residue_dataset[index]
res_list = list(set(residue))
res_sz = len(res_list)
# decide elements to mask
num_mask = int(
# add a random number for probabilistic rounding
self.mask_prob * res_sz
+ np.random.rand()
)
mask_res = np.random.choice(res_list, num_mask, replace=False).tolist()
mask = np.isin(residue, mask_res)
ret["targets"] = np.full(len(mask), self.pad_idx)
ret["targets"][mask] = item[mask]
ret["targets"] = torch.from_numpy(ret["targets"]).long()
# decide unmasking and random replacement
rand_or_unmask_prob = self.random_token_prob + self.leave_unmasked_prob
if rand_or_unmask_prob > 0.0:
rand_or_unmask = mask & (np.random.rand(sz) < rand_or_unmask_prob)
if self.random_token_prob == 0.0:
unmask = rand_or_unmask
rand_mask = None
elif self.leave_unmasked_prob == 0.0:
unmask = None
rand_mask = rand_or_unmask
else:
unmask_prob = self.leave_unmasked_prob / rand_or_unmask_prob
decision = np.random.rand(sz) < unmask_prob
unmask = rand_or_unmask & decision
rand_mask = rand_or_unmask & (~decision)
else:
unmask = rand_mask = None
if unmask is not None:
mask = mask ^ unmask
new_item = np.copy(item)
new_item[mask] = self.mask_idx
num_mask = mask.astype(np.int32).sum()
new_coord = np.copy(coord)
new_coord[mask, :] += self.noise_f(num_mask)
if rand_mask is not None:
num_rand = rand_mask.sum()
if num_rand > 0:
new_item[rand_mask] = np.random.choice(
len(self.vocab),
num_rand,
p=self.weights,
)
ret["atoms"] = torch.from_numpy(new_item).long()
ret["coordinates"] = torch.from_numpy(new_coord).float()
return ret