forked from zhaoqichang/AttentionDTA_BIBM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTA_test.py
113 lines (94 loc) · 3.63 KB
/
DTA_test.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
import tensorflow as tf
import pandas as pd
import numpy as np
import DTA_model as model
import os
MAX_SEQ_LEN = 1200
MAX_SMI_LEN = 100
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score # R square
def calculateMSE(X, Y):
in_bracket = []
for i in range(len(X)):
num = Y[i] - X[i]
num = pow(num, 2)
in_bracket.append(num)
all_sum = sum(in_bracket)
MSE = all_sum / len(X)
return MSE
def parser(record):
read_features = {
'drug': tf.FixedLenFeature([MAX_SMI_LEN], dtype=tf.int64),
'protein': tf.FixedLenFeature([MAX_SEQ_LEN], dtype=tf.int64),
'affinity': tf.FixedLenFeature([1], dtype=tf.float32)
}
read_data = tf.parse_single_example(
serialized=record, features=read_features)
# read_data = tf.parse_example(serialized=record, features=read_features)
drug = tf.cast(read_data['drug'], tf.int32)
protein = tf.cast(read_data['protein'], tf.int32)
affinit_y = read_data['affinity']
return drug, protein, affinit_y
def test(num, test_path):
with tf.Graph().as_default() as g:
dataset = tf.data.TFRecordDataset(test_path)
dataset = dataset.map(parser)
dataset = dataset.batch(
batch_size=3)
iterator = dataset.make_initializable_iterator()
drug_to_embeding, proteins_to_embeding, labels_batch \
= iterator.get_next()
_, _, test_label = \
model.inference(
drug_to_embeding,
proteins_to_embeding,
regularizer=None,
keep_prob=1,
trainlabel=0)
mean_squared_eror = tf.losses.mean_squared_error(
test_label, labels_batch)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(iterator.initializer)
ckpt = tf.train.get_checkpoint_state(
"./" + dataname + "/model%d/" % num)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
predictions_eval = []
labels_eval = []
# MSElist = []
try:
while True:
df, pf, p, l, MSE = sess.run(
[drug_to_embeding, proteins_to_embeding, test_label, labels_batch, mean_squared_eror])
predictions_eval.append(p)
labels_eval.append(l)
except tf.errors.OutOfRangeError:
pass
predictions_eval = np.concatenate(predictions_eval)
labels_eval = np.concatenate(labels_eval)
labels_eval.resize([labels_eval.shape[0], 1])
test_MSE = mean_squared_error(labels_eval, predictions_eval)
test_MAE = mean_absolute_error(labels_eval, predictions_eval)
test_R2 = r2_score(labels_eval, predictions_eval)
print("MSE:", test_MSE, "MAE:", test_MAE, "R2:", test_R2)
return test_MSE,test_MAE,test_R2
# dataname = "davis"
dataname = "kiba"
RESULT_PATH = "./results/" + dataname + "/"
if os.path.exists(RESULT_PATH) is False:
os.makedirs(RESULT_PATH)
MSE = []
MAE = []
R2 = []
for num in range(5):
test_path = "./tfrecord/" + \
dataname + "/test%d.tfrecord" % num
mse,mae,r2 = test(num, test_path)
MSE.append(mse)
MAE.append(mae)
R2.append(r2)
print("Average MSE is %f" % float(sum(MSE)/len(MSE)))
print("Average MAE is %f" % float(sum(MAE)/len(MAE)))
print("Average R2 is %f" % float(sum(R2)/len(R2)))