-
Notifications
You must be signed in to change notification settings - Fork 41
/
evaluate_naive.py
executable file
·183 lines (162 loc) · 5.53 KB
/
evaluate_naive.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
#!/usr/bin/env python
import numpy as np
import pandas as pd
import click as ck
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.layers import (
Dense, Dropout, Activation, Input, Reshape,
Flatten, BatchNormalization, Embedding,
Conv1D, MaxPooling1D, Add, Concatenate)
from tensorflow.keras.optimizers import Adam, RMSprop, Adadelta, SGD
from sklearn.metrics import classification_report
from sklearn.metrics.pairwise import cosine_similarity
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
import sys
from collections import deque, Counter
import time
import logging
import tensorflow as tf
from sklearn.metrics import roc_curve, auc, matthews_corrcoef
from scipy.spatial import distance
from scipy import sparse
import math
from utils import FUNC_DICT, Ontology, NAMESPACES
from matplotlib import pyplot as plt
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
@ck.command()
@ck.option(
'--train-data-file', '-trdf', default='data-cafa3/train_data.pkl',
help='Data file with training features')
@ck.option(
'--test-data-file', '-tsdf', default='data-cafa3/test_data.pkl',
help='Test data')
@ck.option(
'--ont', '-o', default='mf',
help='GO subontology (bp, mf, cc)')
def main(train_data_file, test_data_file, ont):
go_rels = Ontology('data/go.obo', with_rels=True)
terms_df = pd.read_pickle('data-deepgo/' + ont + '.pkl')
terms = terms_df['functions'].values.flatten()
terms_dict = {v: i for i, v in enumerate(terms)}
train_df = pd.read_pickle(train_data_file)
annotations = train_df['annotations'].values
annotations = list(map(lambda x: set(x), annotations))
test_df = pd.read_pickle(test_data_file)
test_annotations = test_df['annotations'].values
test_annotations = list(map(lambda x: set(x), test_annotations))
go_rels.calculate_ic(annotations + test_annotations)
go_set = go_rels.get_namespace_terms(NAMESPACES[ont])
go_set.remove(FUNC_DICT[ont])
annotations = list(map(lambda x: set(filter(lambda y: y in go_set, x)), annotations))
cnt = Counter()
max_n = 0
for x in annotations:
cnt.update(x)
print(cnt.most_common(10))
max_n = cnt.most_common(1)[0][1]
print(max_n)
scores = {}
for go_id, n in cnt.items():
score = n / max_n
scores[go_id] = score
prot_index = {}
for i, row in enumerate(train_df.itertuples()):
prot_index[row.proteins] = i
labels = test_annotations
labels = list(map(lambda x: set(filter(lambda y: y in go_set, x)), labels))
print(len(go_set))
fmax = 0.0
tmax = 0.0
smin = 1000.0
precisions = []
recalls = []
for t in range(101):
threshold = t / 100.0
preds = []
annots = set()
for go_id, score in scores.items():
if score >= threshold:
annots.add(go_id)
# new_annots = set()
# for go_id in annots:
# new_annots |= go_rels.get_anchestors(go_id)
# new_annots = set(filter(lambda y: y in go_set, new_annots))
for i, row in enumerate(test_df.itertuples()):
preds.append(annots.copy())
fscore, prec, rec, s = evaluate_annotations(go_rels, labels, preds)
precisions.append(prec)
recalls.append(rec)
print(f'Fscore: {fscore}, S: {s}, threshold: {threshold}')
if fmax < fscore:
fmax = fscore
tmax = threshold
if smin > s:
smin = s
print(f'Fmax: {fmax:0.3f}, Smin: {smin:0.3f}, threshold: {tmax}')
precisions = np.array(precisions)
recalls = np.array(recalls)
sorted_index = np.argsort(recalls)
recalls = recalls[sorted_index]
precisions = precisions[sorted_index]
aupr = np.trapz(precisions, recalls)
print(f'AUPR: {aupr:0.3f}')
plt.figure()
lw = 2
plt.plot(recalls, precisions, color='darkorange',
lw=lw, label=f'AUPR curve (area = {aupr:0.3f})')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Area Under the Precision-Recall curve')
plt.legend(loc="lower right")
plt.savefig('aupr.pdf')
plt.show()
def compute_roc(labels, preds):
# Compute ROC curve and ROC area for each class
fpr, tpr, _ = roc_curve(labels.flatten(), preds.flatten())
roc_auc = auc(fpr, tpr)
return roc_auc
def compute_mcc(labels, preds):
# Compute ROC curve and ROC area for each class
mcc = matthews_corrcoef(labels.flatten(), preds.flatten())
return mcc
def evaluate_annotations(go, real_annots, pred_annots):
total = 0
p = 0.0
r = 0.0
p_total= 0
ru = 0.0
mi = 0.0
for i in range(len(real_annots)):
if len(real_annots[i]) == 0:
continue
tp = real_annots[i].intersection(pred_annots[i])
fp = pred_annots[i] - tp
fn = real_annots[i] - tp
for go_id in fp:
mi += go.get_ic(go_id)
for go_id in fn:
ru += go.get_ic(go_id)
tpn = len(tp)
fpn = len(fp)
fnn = len(fn)
total += 1
recall = tpn / (1.0 * (tpn + fnn))
r += recall
if len(pred_annots[i]) > 0:
p_total += 1
precision = tpn / (1.0 * (tpn + fpn))
p += precision
ru /= total
mi /= total
r /= total
if p_total > 0:
p /= p_total
f = 0.0
if p + r > 0:
f = 2 * p * r / (p + r)
s = math.sqrt(ru * ru + mi * mi)
return f, p, r, s
if __name__ == '__main__':
main()