-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_tdc.py
More file actions
283 lines (240 loc) · 9.42 KB
/
Copy pathdata_tdc.py
File metadata and controls
283 lines (240 loc) · 9.42 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
"""TDC (Therapeutics Data Commons) datasets, plugged into our scaffold-split protocol.
We use TDC for the SMILES + target data (it's the standard source for
drug-discovery benchmarks) but apply our own scaffold split + random
in-distribution holdout, identical to ``data_molnet.MolNetDataset``. This
keeps every chemistry dataset in the paper directly comparable.
Splits returned by `__init__` (matches MolNetDataset):
"train" 80 % of scaffold-train (used for bootstrapping)
"test" 20 % of scaffold-train, random split (id_test)
"test_scaffold" scaffold-test (ood_test, novel scaffolds)
"""
from __future__ import annotations
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import Dataset
from data_molnet import (
_scaffold_split,
_scaffold_split_with_id_holdout,
_smiles_to_graph,
_smiles_to_morgan,
_tokenize_smiles_chemberta,
_validate_smiles,
)
# Map our short dataset name → (TDC group, TDC dataset name).
# Every entry is single-task binary classification with columns
# ('Drug_ID', 'Drug', 'Y'); 'Drug' is SMILES, 'Y' ∈ {0, 1}.
_TDC_DATASETS = {
# ADME — absorption / distribution / metabolism / excretion
"hia_hou": ("ADME", "HIA_Hou"),
"bioavailability_ma": ("ADME", "Bioavailability_Ma"),
"pgp_broccatelli": ("ADME", "Pgp_Broccatelli"),
"bbb_martins": ("ADME", "BBB_Martins"),
# ADME — CYP P450 substrate prediction (binary; ~660-940 each)
"cyp2c9_substrate": ("ADME", "CYP2C9_Substrate_CarbonMangels"),
"cyp2d6_substrate": ("ADME", "CYP2D6_Substrate_CarbonMangels"),
"cyp3a4_substrate": ("ADME", "CYP3A4_Substrate_CarbonMangels"),
# Tox
"herg": ("Tox", "hERG"),
"dili": ("Tox", "DILI"),
"ames": ("Tox", "AMES"),
"skin_reaction": ("Tox", "Skin_Reaction"),
}
def _load_tdc(name: str, data_dir: Path):
"""Return (smiles_list, labels) for the requested TDC dataset."""
group_name, tdc_name = _TDC_DATASETS[name]
if group_name == "ADME":
from tdc.single_pred import ADME
data = ADME(name=tdc_name, path=str(data_dir))
elif group_name == "Tox":
from tdc.single_pred import Tox
data = Tox(name=tdc_name, path=str(data_dir))
else:
raise ValueError(f"unknown TDC group {group_name}")
df = data.get_data().dropna(subset=["Drug", "Y"]).reset_index(drop=True)
return df["Drug"].tolist(), df["Y"].astype(int).values
class TDCDataset(Dataset):
"""TDC dataset → Morgan FP + scaffold-split protocol matching MolNetDataset."""
def __init__(
self,
name: str,
split: str = "train",
seed: int = 42,
data_dir: str = "./data/tdc",
n_bits: int = 2048,
) -> None:
if name not in _TDC_DATASETS:
raise ValueError(f"unknown TDC dataset {name}; "
f"available: {sorted(_TDC_DATASETS)}")
cache = Path(data_dir)
cache.mkdir(parents=True, exist_ok=True)
smiles_list, labels = _load_tdc(name, cache)
# Featurize.
fps, valid_idx = [], []
for i, smi in enumerate(smiles_list):
fp = _smiles_to_morgan(smi, n_bits=n_bits)
if fp is not None:
fps.append(fp)
valid_idx.append(i)
features = np.stack(fps)
labels = labels[valid_idx]
smiles_list = [smiles_list[i] for i in valid_idx]
# Same split logic as MolNetDataset.
scaffold_train_idx, scaffold_test_idx = _scaffold_split(
smiles_list, seed=seed)
rng = np.random.RandomState(seed)
rng.shuffle(scaffold_train_idx)
n_id_test = max(1, int(len(scaffold_train_idx) * 0.2))
id_test_idx = scaffold_train_idx[:n_id_test]
train_idx = scaffold_train_idx[n_id_test:]
if split == "train":
idx = train_idx
elif split == "test":
idx = id_test_idx
elif split == "test_scaffold":
idx = scaffold_test_idx
else:
raise ValueError(f"unknown split {split}")
self.images = torch.tensor(features[idx], dtype=torch.float32)
self.labels = torch.tensor(labels[idx], dtype=torch.long)
self._spurious = torch.full((len(idx),),
fill_value=int(split == "test_scaffold"),
dtype=torch.long)
@property
def spurious(self) -> torch.Tensor:
return self._spurious
@property
def input_dim(self) -> int:
return self.images.shape[1]
def __getitem__(self, idx: int) -> dict:
return {
"image": self.images[idx],
"label": self.labels[idx].item(),
"spurious": self._spurious[idx].item(),
"index": idx,
}
def __len__(self) -> int:
return len(self.labels)
class TDCGraphDataset(Dataset):
"""TDC dataset with on-the-fly RDKit graphs, mirroring TDCDataset.
Same scaffold split + 20% random in-distribution holdout as Morgan
TDCDataset, so a GIN backbone can run the cross-sample protocol on
held-out chemistry datasets and the partition matches the
fingerprint baseline.
"""
def __init__(
self,
name: str,
split: str = "train",
seed: int = 42,
data_dir: str = "./data/tdc",
) -> None:
from torch_geometric.data import Data
if name not in _TDC_DATASETS:
raise ValueError(f"unknown TDC dataset {name}; "
f"available: {sorted(_TDC_DATASETS)}")
cache = Path(data_dir)
cache.mkdir(parents=True, exist_ok=True)
smiles_list, labels = _load_tdc(name, cache)
train_idx, id_test_idx, scaffold_test_idx = _scaffold_split_with_id_holdout(
smiles_list, seed
)
if split == "train":
idx = train_idx
elif split == "test":
idx = id_test_idx
elif split == "test_scaffold":
idx = scaffold_test_idx
else:
raise ValueError(f"unknown split {split}")
self._graphs: list = []
self.labels: list[int] = []
for i in idx:
g = _smiles_to_graph(smiles_list[i])
if g is None:
continue
x, edge_index, edge_attr = g
self._graphs.append(
Data(x=x, edge_index=edge_index, edge_attr=edge_attr,
y=torch.tensor(int(labels[i]), dtype=torch.long))
)
self.labels.append(int(labels[i]))
self.labels = torch.tensor(self.labels, dtype=torch.long)
self._spurious = torch.full(
(len(self.labels),), int(split == "test_scaffold"), dtype=torch.long
)
@property
def spurious(self) -> torch.Tensor:
return self._spurious
@property
def atom_feature_dim(self) -> int:
return self._graphs[0].x.shape[1] if self._graphs else 0
def __getitem__(self, idx: int) -> dict:
g = self._graphs[idx]
return {
"image": g,
"label": int(self.labels[idx]),
"spurious": int(self._spurious[idx]),
"index": idx,
}
def __len__(self) -> int:
return len(self.labels)
class TDCTokenDataset(Dataset):
"""TDC dataset with ChemBERTa-tokenised SMILES, mirroring TDCDataset.
Uses the same scaffold split + 20% random holdout as Morgan TDCDataset, so
cross-sample protocols match across featurisers.
"""
def __init__(
self,
name: str,
split: str = "train",
seed: int = 42,
data_dir: str = "./data/tdc",
max_length: int = 128,
) -> None:
if name not in _TDC_DATASETS:
raise ValueError(f"unknown TDC dataset {name}; "
f"available: {sorted(_TDC_DATASETS)}")
cache = Path(data_dir)
cache.mkdir(parents=True, exist_ok=True)
smiles_list, labels = _load_tdc(name, cache)
valid_idx = _validate_smiles(smiles_list)
smiles_list = [smiles_list[i] for i in valid_idx]
labels = labels[valid_idx]
train_idx, id_test_idx, scaffold_test_idx = _scaffold_split_with_id_holdout(
smiles_list, seed
)
if split == "train":
idx = train_idx
elif split == "test":
idx = id_test_idx
elif split == "test_scaffold":
idx = scaffold_test_idx
else:
raise ValueError(f"unknown split {split}")
smiles_subset = [smiles_list[i] for i in idx]
self._input_ids, self._attention_mask = _tokenize_smiles_chemberta(
smiles_subset, max_length=max_length
)
self.labels = torch.tensor(labels[idx], dtype=torch.long)
self._spurious = torch.full(
(len(idx),), int(split == "test_scaffold"), dtype=torch.long
)
@property
def spurious(self) -> torch.Tensor:
return self._spurious
@property
def input_dim(self) -> int:
raise NotImplementedError("ChemBERTa uses tokenized input, not input_dim.")
def __getitem__(self, idx: int) -> dict:
return {
"image": {
"input_ids": self._input_ids[idx],
"attention_mask": self._attention_mask[idx],
},
"label": self.labels[idx].item(),
"spurious": self._spurious[idx].item(),
"index": idx,
}
def __len__(self) -> int:
return len(self.labels)