-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
326 lines (259 loc) · 10.3 KB
/
evaluation.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
from matplotlib import pyplot as plt, rcParams
import numpy as np
from Utils import pickle_save, pickle_load
from collections import Counter
import math
import CONSTANTS
import obonet
import networkx as nx
'''
compute ic
script adapated from deepgozero
annotations = train+valid+test annotations
'''
def compute_ics(ontology, go_graph):
parent_term = CONSTANTS.FUNC_DICT[ontology]
ont_terms = nx.ancestors(go_graph, parent_term).union(set([parent_term]))
# get ics and ic norms
groundtruth = pickle_load(CONSTANTS.ROOT_DIR + "groundtruth")
train_valid_proteins = set(pickle_load(CONSTANTS.ROOT_DIR + "{}/all_proteins".format(ontology)))
train_valid_annots = [set(groundtruth[protein]).intersection(ont_terms) for protein in groundtruth if protein in train_valid_proteins]
cnt = Counter()
for x in train_valid_annots:
cnt.update(x)
ics = {}
ic_norm = 0.0
for go_id, n in cnt.items():
parents = list(go_graph.neighbors(go_id))
if len(parents) == 0:
min_n = n
else:
min_n = min([cnt[x] for x in parents])
ics[go_id] = math.log(min_n / n, 2)
ic_norm = max(ic_norm, ics[go_id])
return ics, ic_norm
def get_graph(go_path=CONSTANTS.ROOT_DIR + "/obo/go-basic.obo"):
go_graph = obonet.read_obo(open(go_path, 'r'))
accepted_edges = set()
unaccepted_edges = set()
for edge in go_graph.edges:
if edge[2] == 'is_a' or edge[2] == 'part_of':
accepted_edges.add(edge)
else:
unaccepted_edges.add(edge)
go_graph.remove_edges_from(unaccepted_edges)
return go_graph
def plot_curves(data, ontology):
fig, ax = plt.subplots(1, 1, figsize=(10, 8))
# fig.suptitle("Area Under the Precision-Recall curve")
method_dic = {
"esm2_t48": "ESM", "msa_1b": "MSA",
"interpro": "Interpro", "tale": "Tale",
"netgo": "NetGO3", "full_x": "FULL Hierachical",
"full_biobert": "Full Biobert", "full_gcn": "TransFew",
"full_linear": "Full Linear", "naive": "Naive",
}
for method in data:
recalls = data[method]["recalls"]
precisions = data[method]["precisions"]
fmax_pos = data[method]["fmax_pos"]
aupr = data[method]["aupr"]
color = data[method]["color"]
coverage = data[method]["coverage"]
fmax = data[method]["fmax"]
for i in range(len(precisions)):
if recalls[i] == fmax_pos[0] and precisions[i] == fmax_pos[1]:
fmax_pos = i
break
ax.plot(recalls[1:], precisions[1:], color=color,
label=f'{method_dic[method]}: fmax {fmax: 0.2f}, AUPR {aupr:0.2f})')
ax.plot(recalls[fmax_pos], precisions[fmax_pos], 'ro')#, color='black')
ax.scatter(recalls[fmax_pos], precisions[fmax_pos], s=rcParams['lines.markersize'] ** 3, facecolors='none',
edgecolors='black')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.0])
ax.tick_params(axis='both', which='major', labelsize=16)
ax.set_xlabel('Recall', fontsize=18, fontweight ='bold')
ax.set_ylabel('Precision', fontsize=18, fontweight ='bold')
ax.set_title(CONSTANTS.NAMES[ontology], fontsize=20, fontweight ='bold')
ax.legend(loc="upper left", ncols=2, fontsize=13)#, prop=dict(weight='bold'))
plt.savefig("plots/results_{}.png".format(ontology))
def evaluate_annotations(real_annots, pred_annots, ics, ic_norm):
total = 0
p = 0.0
r = 0.0
wp = 0.0
wr = 0.0
p_total = 0
ru = 0.0
mi = 0.0
avg_ic = 0.0
fps = []
fns = []
for i in range(len(real_annots)):
if len(real_annots[i]) == 0:
continue
tp = set(real_annots[i]).intersection(set(pred_annots[i]))
fp = pred_annots[i].difference(tp)
fn = real_annots[i].difference(tp)
tpic = 0.0
for go_id in tp:
ic = ics.get(go_id, 0.0)
tpic += ic/ic_norm
avg_ic += ic
fpic = 0.0
for go_id in fp:
ic = ics.get(go_id, 0.0)
fpic += ic/ic_norm
mi += ic
fnic = 0.0
for go_id in fn:
ic = ics.get(go_id, 0.0)
fnic += ic/ic_norm
ru += ic
fps.append(fp)
fns.append(fn)
tpn = len(tp)
fpn = len(fp)
fnn = len(fn)
total += 1
recall = tpn / (1.0 * (tpn + fnn))
r += recall
wrecall = tpic / (tpic + fnic)
wr += wrecall
if len(pred_annots[i]) > 0:
p_total += 1
precision = tpn / (1.0 * (tpn + fpn))
p += precision
wp += tpic / (tpic + fpic)
avg_ic = (avg_ic + mi) / total
ru /= total
mi /= total
r /= total
wr /= total
if p_total > 0:
p /= p_total
wp /= p_total
f = 0.0
wf = 0.0
if p + r > 0:
f = 2 * p * r / (p + r)
wf = 2 * wp * wr / (wp + wr)
s = math.sqrt(ru * ru + mi * mi)
return f, p, r, s, ru, mi, fps, fns, avg_ic, wf
def load_predictions(pth):
return pickle_load(pth)
def evaluate_method(proteins, labels, predictions, go_graph, go_set, ics, ic_norm):
fmax = 0.0
tmax = 0.0
wfmax = 0.0
wtmax = 0.0
avgic = 0.0
precisions = []
recalls = []
smin = 1000000.0
rus = []
mis = []
for t in range(0, 101):
threshold = t / 100.0
preds = []
labs = []
for protein in proteins:
# get labels for ontology
labs.append(labels[protein].intersection(go_set).difference(CONSTANTS.root_terms))
annots = set()
for go_id, score in predictions[protein]:
if score >= threshold:
annots.add(go_id)
new_annots = set()
for go_id in annots:
if go_id in go_graph:
new_annots |= nx.descendants(go_graph, go_id) | {go_id}
new_annots = new_annots.difference(CONSTANTS.root_terms)
preds.append(new_annots.intersection(go_set))
fscore, prec, rec, s, ru, mi, fps, fns, avg_ic, wf = evaluate_annotations(labs, preds, ics=ics, ic_norm=ic_norm)
precisions.append(prec)
recalls.append(rec)
if fmax < fscore:
fmax = fscore
tmax = threshold
fmax_pos = (rec, prec)
avgic = avg_ic
if wfmax < wf:
wfmax = wf
wtmax = threshold
if smin > s:
smin = s
return precisions, recalls, fmax, fmax_pos, tmax, wfmax, wtmax, smin, avgic
def evaluating(proteins, groundtruth, go_graph, title="", ontology=""):
parent_term = CONSTANTS.FUNC_DICT[ontology]
ont_terms = nx.ancestors(go_graph, parent_term).union(set([parent_term]))
ics, ic_norm = compute_ics(ontology, go_graph)
# methods = ["naive", "msa_1b", "interpro", "esm2_t48", "tale", "netgo", "full_gcn", "full_combined_gcn"] # , "full_mean", "full_max"
methods = ["naive", "tale", "netgo", "full_gcn"]
methods = ["deepgose", 'sprof']
# methods = ["full_gcn"]
# methods = ["full_gcn"] # , "full_mean", "full_max"
# methods = ["esm2_t48", "msa_1b", "interpro", "full_x", "full_biobert", "full_gcn", "full_linear"] # , "full_mean", "full_max"
colors = ["grey", "orange", "steelblue", "indigo", "blue", "red", "darkgreen", "magenta", "gold", "teal", " black"]
dic = { method: {'color': colors[pos]} for pos, method in enumerate(methods)}
for method in methods:
pth = "evaluation/results/{}_out".format(method)
predictions = load_predictions(pth)
# predictions = predictions[group]
#add both NK and LK
if method == 'sprof' or method == 'deepgose':
predictions = predictions[ontology]
else:
predictions = predictions["LK_"+ontology] | predictions["NK_"+ontology]
coverage = len(set(predictions.keys()).intersection(proteins)) / len(proteins)
precisions, recalls, fmax, fmax_pos, tmax, wfmax, wtmax, smin, avgic = evaluate_method(
proteins=proteins,
labels=groundtruth,
predictions=predictions,
go_graph=go_graph,
go_set=ont_terms,
ics=ics, ic_norm=ic_norm)
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)
dic[method]["recalls"] = recalls
dic[method]["precisions"] = precisions
dic[method]["fmax_pos"] = fmax_pos
dic[method]["aupr"] = aupr
dic[method]["coverage"] = coverage
dic[method]["fmax"] = fmax
print(f'Method: {method} Fmax: {fmax:0.3f}, threshold: {tmax}, Coverage: {coverage}, AUPR: {aupr:0.3f} Weighted Fmax: {wfmax:0.3f}, Smin: {smin:0.3f}')
# print(f'Fmax: {fmax:0.3f}, threshold: {tmax}, AUPR: {aupr:0.3f}')
# plot_curves(dic, ontology=ontology)
go_graph = get_graph()
test_group = pickle_load(CONSTANTS.ROOT_DIR + "test/t3/test_proteins")
test_groundtruth = pickle_load(CONSTANTS.ROOT_DIR + "test/t3/groundtruth")
# add limited known and no known
test_group = {
'bp': test_group['LK_bp'] | test_group['NK_bp'],
'mf': test_group['LK_mf'] | test_group['NK_mf'],
'cc': test_group['LK_cc'] | test_group['NK_cc']
}
titles = {
"cc": "Cellular Component",
"mf": "Molecular Function",
"bp": "Biological Process",
}
to_remove = {'C0HM98', 'C0HM97', 'C0HMA1', 'C0HM44'}
def main():
for ont in test_group:
if ont == "mf" or ont == "cc":
continue
print("###############{}######################".format(ont))
proteins = set(test_group[ont]).difference(to_remove)
print("Evaluating:{}, total number of proteins:{}".format(ont, len(proteins)))
groundtruth = {i: test_groundtruth[i] for i in proteins}
title = titles[ont]
evaluating(proteins=proteins, groundtruth=groundtruth, go_graph=go_graph, title=title, ontology=ont)
print("#####################################")
if __name__ == '__main__':
main()