-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
222 lines (180 loc) · 7.23 KB
/
utils.py
File metadata and controls
222 lines (180 loc) · 7.23 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
import os
import dgl
import numpy
import requests
import torch_geometric as pyg
from torch_geometric.data.batch import Batch
from tqdm import tqdm
import pubchempy as pcp
class download_smiles:
def __init__(self):
print('init over')
def pubchempy(self,drugname_list):
smiles_dict = {}
not_find = []
for drug_name in tqdm(drugname_list):
try:
compounds = pcp.get_compounds(drug_name, 'name')
smiles_dict[drug_name] = compounds[0].canonical_smiles
except:
not_find.append(drug_name)
return smiles_dict, not_find
def collate_dgl(batch):
graph1, graph2, cell, label, drug1, drug2, fp1, fp2,hot1,hot2 = zip(*batch)
# 将 DGLGraph 对象转换为元组或其他可用的数据类型
graph1_batch = dgl.batch(graph1)
graph2_batch = dgl.batch(graph2)
fp1 = torch.stack(fp1)
fp2 = torch.stack(fp2)
hot1 = torch.stack(hot1)
hot2 = torch.stack(hot2)
x = {
'drug1': {
'name': drug1,
'graph': graph1_batch,
'fp': fp1,
'one-hot': hot1
},
'drug2': {
'name': drug2,
'graph': graph2_batch,
'fp': fp2,
'one-hot': hot2
},
'cell': torch.stack(cell),
}
y = torch.tensor(label)
return x, y
def collate_pyg(batch):
graph1, graph2, cell, label, drug1, drug2, fp1, fp2, hot1, hot2,tissue= zip(*batch)
# graph1, graph2, cell, label, drug1, drug2, fp1, fp2, hot1, hot2 = zip(*batch)
# 将 DGLGraph 对象转换为元组或其他可用的数据类型
graph1_batch = Batch.from_data_list(graph1)
graph2_batch = Batch.from_data_list(graph2)
fp1 = torch.stack(fp1)
fp2 = torch.stack(fp2)
hot1 = torch.stack(hot1)
hot2 = torch.stack(hot2)
x = {
'drug1': {
'name': drug1,
'graph': graph1_batch,
'fp': fp1,
'one-hot': hot1
},
'drug2': {
'name': drug2,
'graph': graph2_batch,
'fp': fp2,
'one-hot': hot2
},
'cell': torch.stack(cell),
}
y = torch.tensor(label)
tissue = torch.tensor(tissue)
return x, y,tissue
import torch
import numpy as np
class NTXentLoss(torch.nn.Module):
def __init__(self, device, batch_size=128, temperature=0.1, use_cosine_similarity=True):
super(NTXentLoss, self).__init__()
self.batch_size = batch_size
self.temperature = temperature
self.device = device
self.softmax = torch.nn.Softmax(dim=-1)
self.mask_samples_from_same_repr = self._get_correlated_mask().type(torch.bool)
self.similarity_function = self._get_similarity_function(use_cosine_similarity)
self.criterion = torch.nn.CrossEntropyLoss(reduction="sum")
def _get_similarity_function(self, use_cosine_similarity):
if use_cosine_similarity:
self._cosine_similarity = torch.nn.CosineSimilarity(dim=-1)
return self._cosine_simililarity
else:
return self._dot_simililarity
def _get_correlated_mask(self):
diag = np.eye(2 * self.batch_size)
l1 = np.eye((2 * self.batch_size), 2 * self.batch_size, k=-self.batch_size)
l2 = np.eye((2 * self.batch_size), 2 * self.batch_size, k=self.batch_size)
mask = torch.from_numpy((diag + l1 + l2))
mask = (1 - mask).type(torch.bool)
return mask.to(self.device)
@staticmethod
def _dot_simililarity(x, y):
v = torch.tensordot(x.unsqueeze(1), y.T.unsqueeze(0), dims=2)
# x shape: (N, 1, C)
# y shape: (1, C, 2N)
# v shape: (N, 2N)
return v
def _cosine_simililarity(self, x, y):
# x shape: (N, 1, C)
# y shape: (1, 2N, C)
# v shape: (N, 2N)
v = self._cosine_similarity(x.unsqueeze(1), y.unsqueeze(0))
return v
def forward(self, zis, zjs):
try:
representations = torch.cat([zjs, zis], dim=0)
similarity_matrix = self.similarity_function(representations, representations)
# filter out the scores from the positive samples
l_pos = torch.diag(similarity_matrix, self.batch_size)
r_pos = torch.diag(similarity_matrix, -self.batch_size)
positives = torch.cat([l_pos, r_pos]).view(2 * self.batch_size, 1)
negatives = similarity_matrix[self.mask_samples_from_same_repr].view(2 * self.batch_size, -1)
logits = torch.cat((positives, negatives), dim=1)
logits /= self.temperature
labels = torch.zeros(2 * self.batch_size).to(self.device).long()
loss = self.criterion(logits, labels)
except:
print(5)
return loss / (2 * self.batch_size)
class EarlyStopping:
"""Early stops the training if validation loss doesn't improve after a given patience."""
def __init__(self, patience=7, verbose=False, delta=0, path='', trace_func=print):
"""
Args:
patience (int): How long to wait after last time validation loss improved.
Default: 7
verbose (bool): If True, prints a message for each validation loss improvement.
Default: False
delta (float): Minimum change in the monitored quantity to qualify as an improvement.
Default: 0
path (str): Path for the checkpoint to be saved to.
Default: 'checkpoint.pt'
trace_func (function): trace print function.
Default: print
"""
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
self.path = os.path.join(path)
self.trace_func = trace_func
self.metric = None
def __call__(self, val_loss, model, metric):
self.metric = metric
score = -val_loss
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model)
elif score < self.best_score + self.delta:
self.counter += 1
self.trace_func(
f'EarlyStopping counter: {self.counter} out of {self.patience} ({self.val_loss_min:.6f} --> {val_loss:.6f}){self.metric}')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_loss, model)
self.counter = 0
def save_checkpoint(self, val_loss, model):
'''Saves models when validation loss decrease.'''
if self.verbose:
self.trace_func(
f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}){self.metric}. Saving models ...')
# self.trace_func(
# f'{self.metric}')
torch.save(model.state_dict(), self.path)
self.val_loss_min = val_loss