-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
205 lines (152 loc) · 4.83 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
# All metrics
import os
import sys
import torch
import numpy as np
from random import shuffle
import matplotlib.pyplot as plt
from torch_geometric.data import Batch
# from utils import *
from scipy import stats
# from gnn import GNNNet
from lifelines.utils import concordance_index
def load_model(model_path):
model = torch.load(model_path)
return model
import time
def calculate_metrics(Y, P, dataset='davis'):
# # aupr = get_aupr(Y, P)
# t = time.time()
# cindex = get_cindex(Y, P)
# print(cindex)
# print(concordance_index(Y, P)) # DeepDTAget_cindex(Y, P)
cindex2 = concordance_index(Y, P) # GraphDTA
rm2 = get_rm2(Y, P) # DeepDTA
mse = get_mse(Y, P)
# t2 = time.time()
# pearson = get_pearson(Y, P)
# t3 = time.time()
# spearman = get_spearman(Y, P)
# rmse = get_rmse(Y, P)
print('metrics for ', dataset)
# print('aupr:', aupr)
# print('cindex:', cindex)
print('cindex2', cindex2)
print('rm2:', rm2)
print('mse:', mse)
# print('pearson', pearson)
# print(t - t1,t2-t1,t3-t2)
# result_file_name = 'results/result_' + model_st + '_' + dataset + '.txt'
result_str = ''
result_str += dataset + '\r\n'
result_str += ' ' + ' mse:' + str(mse) + ' ' + ' '+ ' ' + 'ci:' + str(cindex2)
print(result_str)
# open(result_file_name, 'w').writelines(result_str)
return mse,cindex2,rm2
def plot_density(Y, P, fold=0, dataset='davis'):
plt.figure(figsize=(10, 5))
plt.grid(linestyle='--')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.scatter(P, Y, color='blue', s=40)
plt.title('density of ' + dataset, fontsize=30, fontweight='bold')
plt.xlabel('predicted', fontsize=30, fontweight='bold')
plt.ylabel('measured', fontsize=30, fontweight='bold')
# plt.xlim(0, 21)
# plt.ylim(0, 21)
if dataset == 'davis':
plt.plot([5, 11], [5, 11], color='black')
else:
plt.plot([6, 16], [6, 16], color='black')
# plt.legend()
plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize=12, fontweight='bold')
plt.savefig(os.path.join('results', dataset + '_' + str(fold) + '.png'), dpi=500, bbox_inches='tight')
# plot_density(Y, P, fold, dataset)
import numpy as np
import subprocess
from math import sqrt
from sklearn.metrics import average_precision_score
from scipy import stats
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