-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
693 lines (548 loc) · 22.9 KB
/
Utils.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import matplotlib.pyplot as plt
import itertools
from scipy import stats
import os
from torch_geometric.data import InMemoryDataset, DataLoader, Batch
from torch_geometric import data as DATA
import torch
from tqdm import tqdm
import re
import csv
import hashlib
import math
import pandas as pd
import numpy as np
import random
import json, pickle
from collections import OrderedDict
from rdkit import Chem
from rdkit.Chem import MolFromSmiles
import networkx as nx
from pathlib2 import Path
import numpy as np
import pytorch_lightning as pl
import pytorch_lightning.callbacks as plc
from pytorch_lightning.callbacks.progress.rich_progress import RichProgressBarTheme
import subprocess
from math import sqrt
from sklearn.metrics import average_precision_score
def load_model_path(root=None, version=None, v_num=None, best=False):
""" When best = True, return the best model's path in a directory
by selecting the best model with largest epoch. If not, return
the last model saved. You must provide at least one of the
first three args.
Args:
root: The root directory of checkpoints. It can also be a
model ckpt file. Then the function will return it.
version: The name of the version you are going to load.
v_num: The version's number that you are going to load.
best: Whether return the best model.
"""
def sort_by_epoch(path):
name = path.stem
epoch=int(name.split('-')[1].split('=')[1])
return epoch
def generate_root():
if root is not None:
return root
elif version is not None:
return str(Path('lightning_logs', version, 'checkpoints'))
else:
return str(Path('lightning_logs', f'version_{v_num}', 'checkpoints'))
if root==version==v_num==None:
return None
root = generate_root()
if Path(root).is_file():
return root
if best:
files=[i for i in list(Path(root).iterdir()) if i.stem.startswith('best')]
files.sort(key=sort_by_epoch, reverse=True)
res = str(files[0])
else:
res = str(Path(root) / 'last.ckpt')
return res
def load_model_path_by_args(args):
return load_model_path(root=args.load_dir, version=args.load_ver, v_num=args.load_v_num)
def isnan(x):
return isinstance(x, float) and math.isnan(x)
def to_dataset_mapping(ids, n_fold, salt=''):
result = {}
for one_id in ids:
result[one_id] = int(hashlib.sha256((str(one_id)+salt).encode('utf-8')).hexdigest(), 16) % n_fold
return result
def read_and_check(df_path, show_cols=False):
df = pd.read_csv(df_path)
print('Read:', df_path)
if show_cols:
print('cols:', list(df.columns))
print("#case:", len(df))
print("#feature + 1:", len(list(df.columns)))
print(df.describe())
return df.head()
def print_df(df, row=2):
cols = df.columns.tolist()
pd.set_option('display.max_columns', len(cols))
pd.set_option('display.max_rows', row)
print(cols)
print(len(df))
display(df)
pd.reset_option('display.max_columns')
pd.reset_option('display.max_rows')
def vc(series, to_dict=True, dropna=True):
result = series.value_counts(dropna=dropna)
if to_dict:
return print(result.to_dict())
print(result)
def ensure_path(path):
Path(path).mkdir(parents=True, exist_ok=True)
def ensure_file(filepath):
Path(os.path.dirname(filepath)).mkdir(parents=True, exist_ok=True)
def bootstrap(func, y_true, y_pred, n=100, random_state=42, ci=(0.025, 0.975), index=None, with_ci=True):
if isinstance(y_true, pd.Series):
y_true = y_true.values
if isinstance(y_pred, pd.Series):
y_pred = y_pred.values
val = func(y_true, y_pred)
if index is not None:
val = val[index]
if not with_ci:
return val
bootstrapped_scores = []
rng = np.random.RandomState(random_state)
for i in range(n):
indices = rng.randint(0, len(y_pred), len(y_pred))
if len(np.unique(y_true[indices])) < 2:
continue
score = func(y_true[indices], y_pred[indices])
if index is not None:
score = score[index]
bootstrapped_scores.append(score)
sorted_scores = np.array(bootstrapped_scores)
sorted_scores.sort()
ci_lower = sorted_scores[int(ci[0] * len(sorted_scores))]
ci_upper = sorted_scores[int(ci[1] * len(sorted_scores))]
return val, ci_lower, ci_upper
class SetEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
def json_dump(obj, path):
ensure_file(path)
with open(path, 'w', encoding='utf8') as f:
json.dump(obj, f, indent=4, ensure_ascii=False, sort_keys=True, cls=SetEncoder)
def json_load(path):
with open(path, 'r', encoding='utf8') as f:
return json.load(f)
def pkl_dump(obj, path):
ensure_file(path)
with open(path, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def pkl_load(path):
with open(path, 'rb') as f:
return pickle.load(f)
def np_save(obj, path):
ensure_file(path)
with open(path, 'wb') as f:
np.save(f, obj)
def np_load(path):
with open(path, 'rb') as f:
return np.load(f)
def get_aupr(Y, P, threshold=7.0):
# print(Y.shape,P.shape)
Y = np.where(Y >= 7.0, 1, 0)
P = np.where(P >= 7.0, 1, 0)
aupr = average_precision_score(Y, P)
return aupr
def get_cindex(Y, P):
summ = 0
pair = 0
for i in range(1, len(Y)):
for j in range(0, i):
if i is not j:
if (Y[i] > Y[j]):
pair += 1
summ += 1 * (P[i] > P[j]) + 0.5 * (P[i] == P[j])
if pair != 0:
return summ / pair
else:
return 0
def r_squared_error(y_obs, y_pred):
y_obs = np.array(y_obs)
y_pred = np.array(y_pred)
y_obs_mean = [np.mean(y_obs) for y in y_obs]
y_pred_mean = [np.mean(y_pred) for y in y_pred]
mult = sum((y_pred - y_pred_mean) * (y_obs - y_obs_mean))
mult = mult * mult
y_obs_sq = sum((y_obs - y_obs_mean) * (y_obs - y_obs_mean))
y_pred_sq = sum((y_pred - y_pred_mean) * (y_pred - y_pred_mean))
return mult / float(y_obs_sq * y_pred_sq)
def get_k(y_obs, y_pred):
y_obs = np.array(y_obs)
y_pred = np.array(y_pred)
return sum(y_obs * y_pred) / float(sum(y_pred * y_pred))
def squared_error_zero(y_obs, y_pred):
k = get_k(y_obs, y_pred)
y_obs = np.array(y_obs)
y_pred = np.array(y_pred)
y_obs_mean = [np.mean(y_obs) for y in y_obs]
upp = sum((y_obs - (k * y_pred)) * (y_obs - (k * y_pred)))
down = sum((y_obs - y_obs_mean) * (y_obs - y_obs_mean))
return 1 - (upp / float(down))
def get_rm2(ys_orig, ys_line):
r2 = r_squared_error(ys_orig, ys_line)
r02 = squared_error_zero(ys_orig, ys_line)
return r2 * (1 - np.sqrt(np.absolute((r2 * r2) - (r02 * r02))))
def get_rmse(y, f):
rmse = sqrt(((y - f) ** 2).mean(axis=0))
return rmse
def get_mse(y, f):
mse = ((y - f) ** 2).mean(axis=0)
return mse
def get_pearson(y, f):
rp = np.corrcoef(y, f)[0, 1]
return rp
def get_spearman(y, f):
rs = stats.spearmanr(y, f)[0]
return rs
def get_ci(y, f):
ind = np.argsort(y)
y = y[ind]
f = f[ind]
i = len(y) - 1
j = i - 1
z = 0.0
S = 0.0
while i > 0:
while j >= 0:
if y[i] > y[j]:
z = z + 1
u = f[i] - f[j]
if u > 0:
S = S + 1
elif u == 0:
S = S + 0.5
j = j - 1
i = i - 1
j = i - 1
ci = S / z
return ci
# nomarlize
def dic_normalize(dic):
# print(dic)
max_value = dic[max(dic, key=dic.get)]
min_value = dic[min(dic, key=dic.get)]
# print(max_value)
interval = float(max_value) - float(min_value)
for key in dic.keys():
dic[key] = (dic[key] - min_value) / interval
dic['X'] = (max_value + min_value) / 2.0
return dic
pro_res_table = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y',
'X']
pro_res_aliphatic_table = ['A', 'I', 'L', 'M', 'V']
pro_res_aromatic_table = ['F', 'W', 'Y']
pro_res_polar_neutral_table = ['C', 'N', 'Q', 'S', 'T']
pro_res_acidic_charged_table = ['D', 'E']
pro_res_basic_charged_table = ['H', 'K', 'R']
res_weight_table = {'A': 71.08, 'C': 103.15, 'D': 115.09, 'E': 129.12, 'F': 147.18, 'G': 57.05, 'H': 137.14,
'I': 113.16, 'K': 128.18, 'L': 113.16, 'M': 131.20, 'N': 114.11, 'P': 97.12, 'Q': 128.13,
'R': 156.19, 'S': 87.08, 'T': 101.11, 'V': 99.13, 'W': 186.22, 'Y': 163.18}
res_pka_table = {'A': 2.34, 'C': 1.96, 'D': 1.88, 'E': 2.19, 'F': 1.83, 'G': 2.34, 'H': 1.82, 'I': 2.36,
'K': 2.18, 'L': 2.36, 'M': 2.28, 'N': 2.02, 'P': 1.99, 'Q': 2.17, 'R': 2.17, 'S': 2.21,
'T': 2.09, 'V': 2.32, 'W': 2.83, 'Y': 2.32}
res_pkb_table = {'A': 9.69, 'C': 10.28, 'D': 9.60, 'E': 9.67, 'F': 9.13, 'G': 9.60, 'H': 9.17,
'I': 9.60, 'K': 8.95, 'L': 9.60, 'M': 9.21, 'N': 8.80, 'P': 10.60, 'Q': 9.13,
'R': 9.04, 'S': 9.15, 'T': 9.10, 'V': 9.62, 'W': 9.39, 'Y': 9.62}
res_pkx_table = {'A': 0.00, 'C': 8.18, 'D': 3.65, 'E': 4.25, 'F': 0.00, 'G': 0, 'H': 6.00,
'I': 0.00, 'K': 10.53, 'L': 0.00, 'M': 0.00, 'N': 0.00, 'P': 0.00, 'Q': 0.00,
'R': 12.48, 'S': 0.00, 'T': 0.00, 'V': 0.00, 'W': 0.00, 'Y': 0.00}
res_pl_table = {'A': 6.00, 'C': 5.07, 'D': 2.77, 'E': 3.22, 'F': 5.48, 'G': 5.97, 'H': 7.59,
'I': 6.02, 'K': 9.74, 'L': 5.98, 'M': 5.74, 'N': 5.41, 'P': 6.30, 'Q': 5.65,
'R': 10.76, 'S': 5.68, 'T': 5.60, 'V': 5.96, 'W': 5.89, 'Y': 5.96}
res_hydrophobic_ph2_table = {'A': 47, 'C': 52, 'D': -18, 'E': 8, 'F': 92, 'G': 0, 'H': -42, 'I': 100,
'K': -37, 'L': 100, 'M': 74, 'N': -41, 'P': -46, 'Q': -18, 'R': -26, 'S': -7,
'T': 13, 'V': 79, 'W': 84, 'Y': 49}
res_hydrophobic_ph7_table = {'A': 41, 'C': 49, 'D': -55, 'E': -31, 'F': 100, 'G': 0, 'H': 8, 'I': 99,
'K': -23, 'L': 97, 'M': 74, 'N': -28, 'P': -46, 'Q': -10, 'R': -14, 'S': -5,
'T': 13, 'V': 76, 'W': 97, 'Y': 63}
res_weight_table = dic_normalize(res_weight_table)
res_pka_table = dic_normalize(res_pka_table)
res_pkb_table = dic_normalize(res_pkb_table)
res_pkx_table = dic_normalize(res_pkx_table)
res_pl_table = dic_normalize(res_pl_table)
res_hydrophobic_ph2_table = dic_normalize(res_hydrophobic_ph2_table)
res_hydrophobic_ph7_table = dic_normalize(res_hydrophobic_ph7_table)
# print(res_weight_table)
def residue_features(residue):
res_property1 = [1 if residue in pro_res_aliphatic_table else 0, 1 if residue in pro_res_aromatic_table else 0,
1 if residue in pro_res_polar_neutral_table else 0,
1 if residue in pro_res_acidic_charged_table else 0,
1 if residue in pro_res_basic_charged_table else 0]
res_property2 = [res_weight_table[residue], res_pka_table[residue], res_pkb_table[residue], res_pkx_table[residue],
res_pl_table[residue], res_hydrophobic_ph2_table[residue], res_hydrophobic_ph7_table[residue]]
# print(np.array(res_property1 + res_property2).shape)
return np.array(res_property1 + res_property2)
def atom_features(atom):
# 44 + 11 + 11 + 11 + 1 + 3 + 1
return np.array(one_of_k_encoding_unk(atom.GetSymbol(),
['C', 'N', 'O', 'S', 'F', 'Si', 'P', 'Cl', 'Br', 'Mg', 'Na', 'Ca', 'Fe', 'As',
'Al', 'I', 'B', 'V', 'K', 'Tl', 'Yb', 'Sb', 'Sn', 'Ag', 'Pd', 'Co', 'Se',
'Ti', 'Zn', 'H', 'Li', 'Ge', 'Cu', 'Au', 'Ni', 'Cd', 'In', 'Mn', 'Zr', 'Cr',
'Pt', 'Hg', 'Pb', 'X']) +
one_of_k_encoding(atom.GetDegree(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) +
[atom.GetIsAromatic()]
+
one_of_k_encoding_unk(atom.GetFormalCharge() , [-1,0,1]) +
[atom.IsInRing()]
)
# one ont encoding
def one_of_k_encoding(x, allowable_set):
if x not in allowable_set:
# print(x)
raise Exception('input {0} not in allowable set{1}:'.format(x, allowable_set))
return list(map(lambda s: x == s, allowable_set))
def one_of_k_encoding_unk(x, allowable_set):
'''Maps inputs not in the allowable set to the last element.'''
if x not in allowable_set:
x = allowable_set[-1]
return list(map(lambda s: x == s, allowable_set))
# mol smile to mol graph edge index
def smile_to_graph(smile):
mol = Chem.MolFromSmiles(smile)
if mol is None:
return None
c_size = mol.GetNumAtoms()
features = []
for atom in mol.GetAtoms():
feature = atom_features(atom)
features.append(feature / sum(feature))
edges = []
for bond in mol.GetBonds():
edges.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
g = nx.Graph(edges).to_directed()
edge_index = []
mol_adj = np.zeros((c_size, c_size))
for e1, e2 in g.edges:
mol_adj[e1, e2] = 1
# edge_index.append([e1, e2])
mol_adj += np.matrix(np.eye(mol_adj.shape[0]))
index_row, index_col = np.where(mol_adj >= 0.5)
for i, j in zip(index_row, index_col):
edge_index.append([i, j])
# print('smile_to_graph')
# print(np.array(features).shape)
return c_size, features, edge_index
# target feature for target graph
def PSSM_calculation(aln_file, pro_seq):
pfm_mat = np.zeros((len(pro_res_table), len(pro_seq)))
with open(aln_file, 'r') as f:
line_count = len(f.readlines())
for line in f.readlines():
if len(line) != len(pro_seq):
print('error', len(line), len(pro_seq))
continue
count = 0
for res in line:
if res not in pro_res_table:
count += 1
continue
pfm_mat[pro_res_table.index(res), count] += 1
count += 1
# ppm_mat = pfm_mat / float(line_count)
pseudocount = 0.8
ppm_mat = (pfm_mat + pseudocount / 4) / (float(line_count) + pseudocount)
pssm_mat = ppm_mat
# k = float(len(pro_res_table))
# pwm_mat = np.log2(ppm_mat / (1.0 / k))
# pssm_mat = pwm_mat
# print(pssm_mat)
return pssm_mat
def seq_feature(pro_seq):
pro_hot = np.zeros((len(pro_seq), len(pro_res_table)))
pro_property = np.zeros((len(pro_seq), 12))
for i in range(len(pro_seq)):
# if 'X' in pro_seq:
# print(pro_seq)
pro_hot[i,] = one_of_k_encoding(pro_seq[i], pro_res_table)
pro_property[i,] = residue_features(pro_seq[i])
return np.concatenate((pro_hot, pro_property), axis=1)
def target_feature(aln_file, pro_seq):
pssm = PSSM_calculation(aln_file, pro_seq)
other_feature = seq_feature(pro_seq)
# print('target_feature')
# print(pssm.shape)
# print(other_feature.shape)
# print(other_feature.shape)
# return other_feature
return np.concatenate((np.transpose(pssm, (1, 0)), other_feature), axis=1)
# target aln file save in data/dataset/aln
def target_to_feature(target_key, target_sequence, aln_dir):
# aln_dir = 'data/' + dataset + '/aln'
aln_file = os.path.join(aln_dir, target_key + '.aln')
# if 'X' in target_sequence:
# print(target_key)
feature = target_feature(aln_file, target_sequence)
return feature
# pconsc4 predicted contact map save in data/dataset/pconsc4
def target_to_graph(target_key, target_sequence, contact_dir, aln_dir):
target_edge_index = []
target_size = len(target_sequence)
# contact_dir = 'data/' + dataset + '/pconsc4'
contact_file = os.path.join(contact_dir, target_key + '.npy')
contact_map = np.load(contact_file)
contact_map += np.matrix(np.eye(contact_map.shape[0]))
index_row, index_col = np.where(contact_map >= 0.5)
for i, j in zip(index_row, index_col):
target_edge_index.append([i, j])
target_feature = target_to_feature(target_key, target_sequence, aln_dir)
target_edge_index = np.array(target_edge_index)
return target_size, target_feature, target_edge_index
# to judge whether the required files exist
def valid_target(key, dataset, data_dir):
contact_dir = data_dir + dataset + '/pconsc4'
aln_dir = data_dir + dataset + '/aln'
contact_file = os.path.join(contact_dir, key + '.npy')
aln_file = os.path.join(aln_dir, key + '.aln')
# print(contact_file, aln_file)
if os.path.exists(contact_file) and os.path.exists(aln_file):
return True
else:
return False
def data_to_csv(csv_file, datalist):
with open(csv_file, 'w') as f:
f.write('compound_iso_smiles,target_sequence,target_key,affinity\n')
for data in datalist:
f.write(','.join(map(str, data)) + '\n')
def train_test_split(data_df, test_size=0.2, shuffle=True, random_state=None):
if shuffle:
data_df = sklearn.utils.shuffle(data_df, random_state=random_state)
train = data_df[int(len(data_df) * test_size):].reset_index(drop=True)
test = data_df[:int(len(data_df) * test_size)].reset_index(drop=True)
return train, test
def confusion_matrix(preds, labels, conf_matrix):
for p, t in zip(preds, labels):
conf_matrix[p, t] += 1
return conf_matrix
from matplotlib.colors import LinearSegmentedColormap
def get_label_pred_prob(mymodel, val_loader):
mymodel.to(device)
mymodel.eval()
ma_f1 = 0
pred, label = [], []
prob = []
for i, batch in enumerate(tqdm(val_loader)):
batch = batch.to(device)
out = mymodel(batch)
prob.extend(torch.sigmoid(out)[:, 1].detach().cpu().numpy().flatten())
pred.extend(np.argmax(out.detach().cpu().numpy(), axis=1).flatten())
label.extend(batch.y.detach().cpu().numpy().flatten())
return np.array(label), np.array(pred), np.array(prob)
def train(model, device, train_loader, optimizer, epoch, verbose=True): # XXX Use logger
print('Training on {} samples...'.format(len(train_loader.dataset)))
model.train()
LOG_INTERVAL = 130
TRAIN_BATCH_SIZE = 256
loss_fn = torch.nn.MSELoss()
for batch_idx, data in enumerate(tqdm(train_loader)):
data_mol = data[0].to(device)
data_pro = data[1].to(device)
optimizer.zero_grad()
output = model(data_mol, data_pro)
loss = loss_fn(output, data_mol.y.view(-1, 1).float().to(device))
loss.backward()
optimizer.step()
if verbose and batch_idx % LOG_INTERVAL == 0:
print('Train epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(epoch,
batch_idx * TRAIN_BATCH_SIZE,
len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.item()))
# predict
def predicting(model, device, loader):
model.eval()
total_preds = torch.Tensor()
total_labels = torch.Tensor()
print('Make prediction for {} samples...'.format(len(loader.dataset)))
with torch.no_grad():
for data in loader:
data_mol = data[0].to(device)
data_pro = data[1].to(device)
output = model(data_mol, data_pro)
total_preds = torch.cat((total_preds, output.cpu()), 0)
total_labels = torch.cat((total_labels, data_mol.y.view(-1, 1).cpu()), 0)
return total_labels.numpy().flatten(), total_preds.numpy().flatten()
#prepare the protein and drug pairs
def collate(data_list): # TODO
ys = [data[0] for data in data_list]
batch_drug_gs = Batch.from_data_list([data[1] for data in data_list])
prot_gs = [data[2] for data in data_list]
prot_llms = [data[3] for data in data_list]
return torch.stack(ys, 0), batch_drug_gs, prot_gs, prot_llms
def collate_raw(data_list): # TODO
ys = [data[0] for data in data_list]
cis_list = [data[1] for data in data_list]
keys = [data[2] for data in data_list]
pids = [data[3] for data in data_list]
return torch.stack(ys, 0), cis_list, keys, pids
def collate_cm(data_list): # TODO
ys = [data[0] for data in data_list]
batch_drug_gs = Batch.from_data_list([data[1] for data in data_list])
batch_prot_gs = Batch.from_data_list([data[2] for data in data_list])
prot_llms = [data[3] for data in data_list]
return torch.stack(ys, 0), batch_drug_gs, batch_prot_gs, prot_llms
def graph_pad(x, maxsize):
#x should be list [torch(N,features)] *batch
b = len(x)
features = x[0].shape[1]
out = torch.zeros(b, maxsize, features)
for i in range(b):
a = x[i]
out[i,:a.shape[0],:] = a
return out.to(a.device)
def load_callbacks(): # FIXME
callbacks = []
callbacks.append(plc.EarlyStopping(
monitor='test_mse',
mode='min',
patience=100,
min_delta=0.001
))
progress_bar = plc.RichProgressBar(
refresh_rate=1,
theme=RichProgressBarTheme(
description='khaki3',
progress_bar='dark_turquoise',
progress_bar_finished='khaki3',
progress_bar_pulse='#6206E0',
batch_progress='khaki3',
time='grey66',
processing_speed='grey66',
metrics='salmon1',
)
)
callbacks.append(progress_bar)
callbacks.append(plc.ModelCheckpoint(
monitor='test_mse',
filename='{epoch}-min_{test_mse:.3f}',
save_top_k=1,
mode='min',
))
callbacks.append(plc.ModelCheckpoint(
monitor='test_ci',
filename='{epoch}-max_{test_ci:.3f}',
save_top_k=1,
mode='max',
))
callbacks.append(plc.ModelCheckpoint(
monitor='test_rm2',
filename='{epoch}-max_{test_rm2:.3f}',
save_top_k=1,
mode='max',
))
callbacks.append(plc.LearningRateMonitor(
logging_interval='epoch',
))
return callbacks