-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
363 lines (305 loc) · 11.5 KB
/
Copy pathutils.py
File metadata and controls
363 lines (305 loc) · 11.5 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
360
361
362
363
import numpy as np
import networkx as nx
import igraph as ig
import logging
import io
import json
import torch
import torch.utils.data as utils
from math import ceil
from scipy.sparse import csr_matrix, lil_matrix
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.model_selection import StratifiedKFold
from torch_geometric.data import Dataset, Data
from torch_geometric.loader import DataLoader
from torch_geometric.utils.convert import from_networkx, to_networkx
from torch_scatter import scatter_add
from sklearn.metrics import roc_auc_score, average_precision_score
from rdkit import Chem
from rdkit.Chem import BRICS, AllChem
from tqdm import tqdm
import tqdm
import torch.nn.functional as F
def motif_decomp(smiles):
# (未改动)
mol = Chem.MolFromSmiles(smiles)
if mol is None or not hasattr(mol, 'GetAtoms'):
return []
n_atoms = mol.GetNumAtoms()
if n_atoms == 1:
return [[0]]
cliques = []
for bond in mol.GetBonds():
a1 = bond.GetBeginAtom().GetIdx()
a2 = bond.GetEndAtom().GetIdx()
cliques.append([a1, a2])
res = list(BRICS.FindBRICSBonds(mol))
if len(res) != 0:
for bond in res:
bond_pair = [bond[0][0], bond[0][1]]
if bond_pair in cliques:
cliques.remove(bond_pair)
elif bond_pair[::-1] in cliques:
cliques.remove(bond_pair[::-1])
cliques.append([bond[0][0]])
cliques.append([bond[0][1]])
for c in range(len(cliques) - 1):
if c >= len(cliques):
break
for k in range(c + 1, len(cliques)):
if k >= len(cliques):
break
if len(set(cliques[c]) & set(cliques[k])) > 0:
cliques[c] = list(set(cliques[c]) | set(cliques[k]))
cliques[k] = []
cliques = [c for c in cliques if len(c) > 0]
cliques = [c for c in cliques if n_atoms > len(c) > 0]
num_cli = len(cliques)
ssr_mol = Chem.GetSymmSSSR(mol) if num_cli > 0 else []
for i in range(num_cli):
c = cliques[i]
cmol = Chem.RWMol()
atom_map = {}
for idx in c:
atom = mol.GetAtomWithIdx(idx)
new_idx = cmol.AddAtom(atom)
atom_map[idx] = new_idx
for bond in mol.GetBonds():
u = bond.GetBeginAtomIdx()
v = bond.GetEndAtomIdx()
if u in c and v in c:
cmol.AddBond(atom_map[u], atom_map[v], bond.GetBondType())
ssr = Chem.GetSymmSSSR(cmol)
if len(ssr) > 1:
for ring in ssr_mol:
ring_list = list(ring)
if len(set(ring_list) & set(c)) == len(ring_list):
cliques.append(ring_list)
cliques[i] = []
cliques = [c for c in cliques if n_atoms > len(c) > 0]
return cliques
def process_cliques(cliques, n_atoms):
# (未改动)
assigned_atoms = set()
processed = []
for c in cliques:
new_c = [atom for atom in c if atom not in assigned_atoms]
if new_c:
processed.append(new_c)
assigned_atoms.update(new_c)
unassigned = [atom for atom in range(n_atoms) if atom not in assigned_atoms]
for atom in unassigned:
processed.append([atom])
return processed
def eval_ap(y_true, y_pred):
"""
compute Average Precision (AP) averaged across tasks
"""
ap_list = []
for i in range(y_true.shape[1]):
# AUC is only defined when there is at least one positive data.
if np.sum(y_true[:, i] == 1) > 0 and np.sum(y_true[:, i] == 0) > 0:
# ignore nan values
is_labeled = y_true[:, i] == y_true[:, i]
ap = average_precision_score(y_true[is_labeled, i], y_pred[is_labeled, i])
ap_list.append(ap)
if len(ap_list) == 0:
raise RuntimeError(
"No positively labeled data available. Cannot compute Average Precision."
)
return {"ap": sum(ap_list) / len(ap_list)}
class PrinterLogger(object):
def __init__(self, logger):
self.logger = logger
def print_and_log(self, text):
self.logger.info(text)
print(text)
def info(self, text):
self.logger.info(text)
class EarlyStopper:
def stop(
self,
epoch,
val_loss,
val_acc=None,
test_loss=None,
test_acc=None,
train_loss=None,
train_acc=None,
):
raise NotImplementedError("Implement this method!")
def get_best_vl_metrics(self):
return (
self.train_loss,
self.train_acc,
self.val_loss,
self.val_acc,
self.test_loss,
self.test_acc,
self.best_epoch,
)
class Patience(EarlyStopper):
"""
Implement common "patience" technique
"""
def __init__(self, patience=20, use_loss=True, save_path=None, maximize=True):
if use_loss or not maximize:
self.local_val_optimum = float("inf")
self.val_acc = float("inf")
else:
self.local_val_optimum = -float("inf")
self.val_acc = -float("inf")
self.use_loss = use_loss
self.patience = patience
self.best_epoch = -1
self.counter = -1
self.val_loss = None
self.save_path = save_path
self.maximize = maximize
def stop(self, epoch, val_loss, val_acc=None, model=None):
if self.use_loss:
if val_loss <= self.local_val_optimum:
self.counter = 0
self.local_val_optimum = val_loss
self.best_epoch = epoch
self.val_loss, self.val_acc = val_loss, val_acc
self.model = model
if all([model is not None, self.save_path is not None]):
torch.save(
{
"epoch": epoch + 1,
"state_dict": model.state_dict(),
#'optimizer' : optimizer.state_dict(),
},
self.save_path,
)
return False
else:
self.counter += 1
return self.counter >= self.patience
else:
if self.maximize:
cond = val_acc >= self.local_val_optimum
else:
cond = val_acc <= self.local_val_optimum
if cond:
self.counter = 0
self.local_val_optimum = val_acc
self.best_epoch = epoch
self.val_loss, self.val_acc = val_loss, val_acc
self.model = model
if all([model is not None, self.save_path is not None]):
torch.save(
{
"epoch": epoch + 1,
"state_dict": model.state_dict(),
#'optimizer' : optimizer.state_dict(),
},
self.save_path,
)
return False
else:
self.counter += 1
return self.counter >= self.patience
class ModifData(Data):
def __init__(self, edge_index=None, x=None, *args, **kwargs):
super().__init__(x=x, edge_index=edge_index, **kwargs)
def __inc__(self, key, value, *args, **kwargs):
if "index" in key or "path" in key:
return self.num_nodes
elif "indices" in key:
return self.num_edges
else:
return 0
def __cat_dim__(self, key, value, *args, **kwargs):
if "index" in key or "face" in key:
return 1
else:
return 0
class RandomSampler(torch.utils.data.sampler.RandomSampler):
"""
This sampler saves the random permutation applied to the training data,
so it is available for further use (e.g. for saving).
The permutation is saved in the 'permutation' attribute.
The DataLoader can now be instantiated as follows:
>>> data = Dataset()
>>> dataloader = DataLoader(dataset=data, batch_size=32, shuffle=False, sampler=RandomSampler(data))
>>> for batch in dataloader:
>>> print(batch)
>>> print(dataloader.sampler.permutation)
For convenience, one can create a method in the dataloader class to access the random permutation directly, e.g:
class MyDataLoader(DataLoader):
...
def get_permutation(self):
return self.sampler.permutation
...
"""
def __init__(self, data_source, num_samples=None, replacement=False):
super().__init__(data_source, replacement=replacement, num_samples=num_samples)
self.permutation = None
def __iter__(self):
n = len(self.data_source)
self.permutation = torch.randperm(n).tolist()
return iter(self.permutation)
def get_loader(dataset, batch_size=1, shuffle=True, drop_last=False):
sampler = RandomSampler(dataset) if shuffle is True else None
# 'shuffle' needs to be set to False when instantiating the DataLoader,
# because pytorch does not allow to use a custom sampler with shuffle=True.
# Since our shuffler is a random shuffler, either one wants to do shuffling
# (in which case he should instantiate the sampler and set shuffle=False in the
# DataLoader) or he does not (in which case he should set sampler=None
# and shuffle=False when instantiating the DataLoader)
return DataLoader(
dataset,
batch_size=batch_size,
sampler=sampler,
drop_last=drop_last,
shuffle=False, # if shuffle is not None, must stay false, ow is shuffle is false
pin_memory=True,
)
def validate_batch_size(length, batch_size):
"""Returns True if the last batch has size = 1"""
if length % batch_size == 1:
return True
return False
def fast_generate_paths2(g, cutoff, path_type, weights=None, undirected=True, r=None):
if undirected and g.is_directed():
g.to_undirected()
path_length = np.array(g.distances())
if path_type != "all_simple_paths":
diameter = g.diameter(directed=False)
diameter = diameter + 1 if diameter + 1 < cutoff else cutoff
else:
diameter = cutoff
X = [[] for i in range(cutoff - 1)]
sp_dists = [[] for i in range(cutoff - 1)]
for n1 in range(g.vcount()):
if path_type == "all_simple_paths":
paths_ = g.get_all_simple_paths(n1, cutoff=cutoff - 1)
for path in paths_:
# if len(path) >= min_length and len(path) <= cutoff :
idx = len(path) - 2
if len(path) > 0:
X[idx].append(path)
sp_dist = []
for node in path:
sp_dist.append(path_length[n1, node])
sp_dists[idx].append(sp_dist)
else:
valid_ngb = [
i
for i in np.where(
(path_length[n1] <= cutoff - 1) & (path_length[n1] > 0)
)[0]
if i > n1
]
for n2 in valid_ngb:
if path_type == "shortest_path":
paths_ = g.get_shortest_paths(n1, n2, weights=weights)
elif path_type == "all_shortest_paths":
paths_ = g.get_all_shortest_paths(n1, n2, weights=weights)
for path in paths_:
idx = len(path) - 2
X[idx].append(path)
X[idx].append(list(reversed(path)))
return X, diameter, sp_dists