-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluate.py
293 lines (241 loc) · 10.4 KB
/
evaluate.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
# from fairseq.data.dglred_dataset import BERTDGLREDataset, DGLREDataloader
# from fairseq.data.ace_dataset import BERTACEDataset
from fairseq import options, tasks#, utils, checkpoint_utils, scoring
import argparse
import os
import json
# import numpy as np
# import sklearn
from datetime import datetime
# from collections import defaultdict
# import torch
from IPython import embed
import glob
def logging(s):
print(datetime.now(), s)
# TL = 101
# MAX_SPANL = 48
def ind2se(ind, TL, MAX_SPANL):
start_ind = -max(0, -ind + TL) + max(0, ind - TL) // MAX_SPANL + TL
end_ind = start_ind + max(0, ind - TL) % MAX_SPANL
return start_ind, end_ind
def get_metrics(sent_list, preds_list, labels_list):
n_correct, n_pred, n_label = 0, 0, 0
i_count = 0
for sent, preds, labels in zip(sent_list, preds_list, labels_list):
# print(preds)
# print(labels)
preds = set(preds)
labels = {tuple(x) for x in labels}
n_pred += len(preds)
n_label += len(labels)
n_correct += len(preds & labels)
i_count += 1
precision = n_correct / (n_pred + 1e-8)
recall = n_correct / (n_label + 1e-8)
f1 = 2 / (1 / (precision + 1e-8) + 1 / (recall + 1e-8) + 1e-8)
return precision, recall, f1
def jaccard_score(seta, setb):
return len(seta.intersection(setb)) / len(seta.union(setb))
def ind2original(ind, TL ,SL):
start, end = ind2se(ind, TL, SL)
start, end=start - TL,end - TL + 1
# start=max(1, start)
# end=max(1, end)
return start, end
def seq2pred(task, seq_labels, probs, id2rel, id2ent, bert_starts_rev_dict):
cur_rel_id = None
cur_rel_prob = None
cur_head = None
cur_head_prob = None
relation_list = []
entity_list = []
SEP = task.tgt_dict.sep_index
rel_mlen = task.args.rel_len
type_mlen = task.args.type_len
#print("TYPE", type_mlen)
#print("rel", rel_mlen)
#raise ValueError()
for jj, span_id in enumerate(seq_labels):
if span_id == SEP: # if id2rel(span_id) == "TYPE":
cur_head = None
cur_head_prob = None
cur_rel_id = None
cur_rel_prob = None
continue
if cur_head is None:
cur_head = span_id
# cur_head_prob=probs[jj]
elif cur_rel_id is None:
cur_rel_id = span_id
# cur_rel_prob = probs[jj]
else:
# tail
if cur_head >= type_mlen and cur_rel_id < len(id2rel):
head_start, head_end = ind2original(cur_head, type_mlen, task.args.max_span_len)
# head_start, head_end = max(1, head_start), max(1, head_end)
#head_start, head_end=bert_starts_rev_dict[head_start], bert_starts_rev_dict[head_end]
if id2rel[cur_rel_id] != "TYPE" and span_id >= type_mlen:
tail_start, tail_end = ind2original(span_id, type_mlen, task.args.max_span_len)
#tail_start, tail_end=bert_starts_rev_dict[tail_start], bert_starts_rev_dict[tail_end]
#relation_list.append([head_start, head_end, tail_start, tail_end, id2rel[cur_rel_id]])
relation_list.append([tail_start, tail_end, head_start, head_end, id2rel[cur_rel_id]])
elif id2rel[cur_rel_id] == "TYPE" and (type_mlen > span_id >= rel_mlen):
entity_list.append([head_start, head_end, id2ent[span_id-rel_mlen]])
cur_rel_id = None
cur_rel_prob = None
return entity_list, relation_list
def triple2phrase(tokens, triples):
if not triples: return []
if len(triples[0])==5:
return [(tokens[triple[0]:triple[1]], tokens[triple[2]:triple[3]], triple[-1]) for triple in triples]
return [(tokens[triple[0]:triple[1]], triple[-1]) for triple in triples]
def evaluate(dataset, task,res_file=None):
# with open("valid.ACE05.json", "r") as f:
# ori_data = json.load(f)
if res_file is not None:
with open(res_file, "r") as f:
generated_data = json.load(f)
pred_entities, pred_relations=[],[]
label_entities, label_relations=[],[]
pred_relations_wNER,label_relations_wNER = [],[]
sents=[]
type_mlen= task.args.type_len
class meter():
ree_co = 0
all_co = 0
sect=0
all_sect=0
node=0
def mess(self,out):
vs=[]
for i in range(len(out)):
if i%2==0:
vs.append(out[i])
vert = set(vs)
len_v = len(vert)
for vi in vert:
hi,ti=ind2se(vi,type_mlen,task.args.max_span_len)
if hi==ti and vi >= type_mlen:
self.node+=1
if vi >= type_mlen:
self.all_sect += 1
for vj in vert:
if vi != vj :
hi,ti=ind2se(vi,type_mlen,task.args.max_span_len)
hj,tj=ind2se(vj,type_mlen,task.args.max_span_len)
if vi >=type_mlen and vj>=type_mlen: #and \
# (ti!=hi!=hj!=tj):
#if True:
if (hj<=ti<=tj and hi<=hj) or (hj<=hi<=tj and ti>=tj) :
#print(hi,ti)
#print(hj,tj)
self.sect+=1
#raise ValueError()
self.ree_co+=len(out)//2-len_v
self.all_co+=len(out)//2
def print(self):
print("FRACTION: ", self.ree_co/self.all_co)
print("SECT FRACTION: ", self.sect/self.all_sect)
print("SINGLE FRACTION: ", self.node/self.all_sect)
tgt_meter = meter()
pred_meter = meter()
for i, sample in enumerate(dataset.get_data()):
#
# if i !=2423:
# continue
# embed()
print("\n----sample", i, "----")
tokens=sample['tokens']#[type_mlen:]
id2ent = sample['id2ent']
id2rel = sample['id2rel']
#print("\nid2ent", id2ent)
#print("\nid2rel", id2rel)
#raise ValueError()
seq_labels = sample['seq_labels']
out = [int(item.strip()) for item in generated_data[str(i)]["hypo_str"].split()]
#out = seq_labels
print("\nseq_labels", seq_labels)
print("out", out)
tgt_meter.mess(seq_labels[:-1])
pred_meter.mess(out)
# print("\nseq_labels", seq_labels)
# print("out", out)
bert_starts_rev_dict = sample['bert_starts_rev_dict']
# pred_entities_sample, pred_relations_sample = seq2pred(seq_labels, [], id2rel, id2ent, bert_starts_rev_dict)
a=generated_data[str(i)]["score"]
print("\nsentence:", " ".join(tokens))
pred_entities_sample, pred_relations_sample = seq2pred(task,out, [], id2rel, id2ent, bert_starts_rev_dict)
def is_same(seta, setb):
seta = set([tuple(item) for item in seta])
setb = set([tuple(item) for item in setb])
difa, difb = seta.difference(setb), setb.difference(seta)
if difa or difb:
return False
# print("\ndifa", difa)
# print("difb", difb)
# embed()
return True
pred_entities_sample.sort(key=lambda x: x)
sample['entities'].sort(key=lambda x: x)
if not is_same(pred_entities_sample, sample['entities']):
print("\npred_entities_sample", triple2phrase(tokens, pred_entities_sample))
print("sample['entities'] ", triple2phrase(tokens, sample['entities']))
#is_same(pred_entities_sample, sample['entities'])
pred_relations_sample.sort(key=lambda x: x)
sample['relations'].sort(key=lambda x: x)
if not is_same(pred_relations_sample, sample['relations']):
print("\npred_relations_sample", triple2phrase(tokens, pred_relations_sample))
print("sample['relations'] ", triple2phrase(tokens, sample['relations']))
pred_span_to_etype = [{(ib, ie): etype for ib, ie, etype in pred_entities_sample}]
label_span_to_etype = [{(ib, ie): etype for ib, ie, etype in sample['entities']}]
if not is_same(pred_span_to_etype, label_span_to_etype):
print('\npred_span_to_etype ', pred_span_to_etype)
print('label_span_to_etype', label_span_to_etype)
print("\ntokens", tokens)
print("\nscores", a)
pred_entities.append(pred_entities_sample)
label_entities.append(sample['entities'])
pred_relations.append(pred_relations_sample)
label_relations.append(sample['relations'])
# embed()
# pred_relations_wNER += [
# [
# (ib, ie, m[(ib, ie)], jb, je, m[(jb, je)], rtype) for ib, ie, jb, je, rtype in x
# ] for x, m in zip([pred_relations_sample], pred_span_to_etype)
# ]
# label_relations_wNER += [
# [
# (ib, ie, m[(ib, ie)], jb, je, m[(jb, je)], rtype) for ib, ie, jb, je, rtype in x
# ] for x, m in zip([sample['relations']], label_span_to_etype)
# ]
sents.append(sample['tokens'])
tgt_meter.print()
pred_meter.print()
pred_entities = [[tuple(item) for item in example] for example in pred_entities]
pred_relations = [[tuple(item) for item in example] for example in pred_relations]
# word_id = sample['word_id']
rets = {}
rets['entity_p'], rets['entity_r'], rets['entity_f1'] = get_metrics(
sents, pred_entities, label_entities)
rets['relation_p'], rets['relation_r'], rets['relation_f1'] = get_metrics(
sents, pred_relations, label_relations)
# rets['relation_p_wNER'], rets['relation_r_wNER'], rets['relation_f1_wNER'] = get_metrics(
# sents, pred_relations_wNER, label_relations_wNER)
print(rets)
if not os.path.isdir("results/"):
os.mkdir("results/")
fname=os.path.splitext(res_file)[0].split("__")
with open("results/relf1_{:.5f}__entf1_{:.5f}__{}__{}".format(rets['relation_f1'], rets['entity_f1'], fname[-2], fname[-1]), "w+") as _:
pass
if __name__ == "__main__":
parser = options.get_generation_parser()
args = options.parse_args_and_arch(parser)
split = args.gen_subset
task = tasks.setup_task(args)
task.load_dataset(split)
test_set = task.datasets[split]
docid2outseqs = glob.glob("gen_output/docid2outs*")
for docid2outseq in docid2outseqs:
evaluate(test_set, task,res_file=docid2outseq)
#raise ValueError()