-
Notifications
You must be signed in to change notification settings - Fork 16
/
evaluation.py
368 lines (299 loc) · 13.1 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import os.path
import json
import numpy as np
from tqdm import tqdm
rel2id = json.load(open('meta/rel2id.json', 'r'))
id2rel = {value: key for key, value in rel2id.items()}
def get_title2pred(pred: list) -> dict:
'''
Convert predictions into dictionary.
Input:
:pred: list of dictionaries, each dictionary entry is a predicted relation triple. Keys: ['title', 'h_idx', 't_idx', 'r', 'evidence', 'score']
Output:
:title2pred: dictionary with (key, value) = (title, {rel_triple: score})
'''
title2pred = {}
for p in pred:
if p["r"] == "Na":
continue
curr = (p["h_idx"], p["t_idx"], p["r"])
if p["title"] in title2pred:
if curr in title2pred[p["title"]]:
title2pred[p["title"]][curr] = max(p["score"], title2pred[p["title"]][curr])
else:
title2pred[p["title"]][curr] = p["score"]
else:
title2pred[p["title"]] = {curr: p["score"]}
return title2pred
def get_title2gt(features: dict) -> dict:
'''
Convert ground-truth labels to dictionary.
Input:
:features: list of features within each document. Identical to the lists obtained from pre-processing.
Output:
:title2gt: dictionary with (key, value) = (title, [gold_triples])
'''
title2gt = {}
for f in features:
title = f["title"]
title2gt[title] = []
for idx, p in enumerate(f["hts"]):
h,t = p
label = np.array(f['labels'][idx])
rs = np.nonzero(label[1:])[0] + 1 # + 1 for no-label
title2gt[title].extend([(h,t,id2rel[r]) for r in rs])
return title2gt
def select_thresh(cand: list, num_gt: int, correct: int, num_pred: int):
'''
select threshold for relation predictions.
Input:
:cand: list of relation candidates
:num_gt: number of ground-truth relations.
:correct: number of correct relation predictions selected.
:num_pred: number of relation predictions selected.
Output:
:thresh: threshold for selecting relations.
:sorted_pred: predictions selected from cand.
'''
sorted_pred = sorted(cand, key=lambda x:x[1], reverse=True)
precs, recalls = [], []
for pred in sorted_pred:
correct += pred[0]
num_pred += 1
precs.append(correct / num_pred) # Precision
recalls.append(correct / num_gt) # Recall
recalls = np.asarray(recalls, dtype='float32')
precs = np.asarray(precs, dtype='float32')
f1_arr = (2 * recalls * precs / (recalls + precs + 1e-20))
f1 = f1_arr.max()
f1_pos = f1_arr.argmax()
thresh = sorted_pred[f1_pos][1]
print('Best thresh', thresh, '\tbest F1', f1)
return thresh, sorted_pred[:f1_pos + 1]
def merge_results(pred: list, pred_pseudo: list, features: list, thresh: float = None):
'''
Merge relation predictions from the original document and psuedo documents.
Input:
:pred: list of dictionaries, each dictionary entry is a predicted relation triple from the original document. Keys: ['title', 'h_idx', 't_idx', 'r', 'evidence', 'score'].
:pred_pseudo: list of dictionaries, each dictionary entry is a predicted relation triple from pseudo documents. Keys: ['title', 'h_idx', 't_idx', 'r', 'evidence', 'score'].
:features: list of features within each document. Identical to the lists obtained from pre-processing.
:thresh: threshold for selecting predictions.
Output:
:merged_res: list of merged relation predictions. Each relation prediction is a dictionay with keys (title, h_idx, t_idx, r).
:thresh: threshold of selecting relation predictions.
'''
title2pred = get_title2pred(pred)
title2pred_pseudo = get_title2pred(pred_pseudo)
title2gt = get_title2gt(features)
num_gt = sum([len(title2gt[t]) for t in title2gt])
titles = list(title2pred.keys())
cand = []
merged_res = []
correct, num_pred = 0, 0
for t in titles:
rels = title2pred[t]
rels_pseudo = title2pred_pseudo[t] if t in title2pred_pseudo else {}
union = set(rels.keys()) | set(rels_pseudo.keys())
for r in union:
if r in rels and r in rels_pseudo: # add those into predictions
if rels[r] > 0 and rels_pseudo[r] > 0:
merged_res.append({'title':t, 'h_idx':r[0], 't_idx':r[1], 'r': r[2]})
num_pred += 1
correct += r in title2gt[t]
continue
score = rels[r] + rels_pseudo[r]
elif r in rels: # -10 for penalty
score = rels[r] - 10
elif r in rels_pseudo:
score = rels_pseudo[r] - 10
cand.append((r in title2gt[t], score, t, r[0], r[1], r[2]))
if thresh != None:
sorted_pred = sorted(cand, key=lambda x:x[1], reverse=True)
last = min(filter(lambda x: x[1] > thresh, sorted_pred))
until = sorted_pred.index(last)
cand = sorted_pred[:until + 1]
merged_res.extend([{'title':r[2], 'h_idx':r[3], 't_idx':r[4], 'r': r[5]} for r in cand])
return merged_res, thresh
if cand != []:
thresh, cand = select_thresh(cand, num_gt, correct, num_pred)
merged_res.extend([{'title':r[2], 'h_idx':r[3], 't_idx':r[4], 'r': r[5]} for r in cand])
return merged_res, thresh
def extract_relative_score(scores: list, topks: list) -> list:
'''
Get relative score from topk predictions.
Input:
:scores: a list containing scores of topk predictions.
:topks: a list containing relation labels of topk predictions.
Output:
:scores: a list containing relative scores of topk predictions.
'''
na_score = scores[-1].item() - 1
if 0 in topks:
na_score = scores[np.where(topks==0)].item()
scores -= na_score
return scores
def to_official(preds: list, features: list, evi_preds: list = [], scores: list = [], topks: list = []):
'''
Convert the predictions to official format for evaluating.
Input:
:preds: list of dictionaries, each dictionary entry is a predicted relation triple from the original document. Keys: ['title', 'h_idx', 't_idx', 'r', 'evidence', 'score'].
:features: list of features within each document. Identical to the lists obtained from pre-processing.
:evi_preds: list of the evidence prediction corresponding to each relation triple prediction.
:scores: list of scores of topk relation labels for each entity pair.
:topks: list of topk relation labels for each entity pair.
Output:
:official_res: official results used for evaluation.
:res: topk results to be dumped into file, which can be further used during fushion.
'''
h_idx, t_idx, title, sents = [], [], [], []
for f in features:
if "entity_map" in f:
hts = [[f["entity_map"][ht[0]], f["entity_map"][ht[1]]] for ht in f["hts"]]
else:
hts = f["hts"]
h_idx += [ht[0] for ht in hts]
t_idx += [ht[1] for ht in hts]
title += [f["title"] for ht in hts]
sents += [len(f["sent_pos"])] * len(hts)
official_res = []
res = []
for i in tqdm(range(preds.shape[0]), desc="preds"): # for each entity pair
if scores != []:
score = extract_relative_score(scores[i], topks[i])
pred = topks[i]
else:
pred = preds[i]
pred = np.nonzero(pred)[0].tolist()
for p in pred: # for each predicted relation label (topk)
curr_result = {
'title': title[i],
'h_idx': h_idx[i],
't_idx': t_idx[i],
'r': id2rel[p],
}
if evi_preds != []:
curr_evi = evi_preds[i]
evis = np.nonzero(curr_evi)[0].tolist()
curr_result["evidence"] = [evi for evi in evis if evi < sents[i]]
if scores != []:
curr_result["score"] = score[np.where(topks[i] == p)].item()
if p != 0 and p in np.nonzero(preds[i])[0].tolist():
official_res.append(curr_result)
res.append(curr_result)
return official_res, res
def gen_train_facts(data_file_name, truth_dir):
fact_file_name = data_file_name[data_file_name.find("train_"):]
fact_file_name = os.path.join(truth_dir, fact_file_name.replace(".json", ".fact"))
if os.path.exists(fact_file_name):
fact_in_train = set([])
triples = json.load(open(fact_file_name))
for x in triples:
fact_in_train.add(tuple(x))
return fact_in_train
fact_in_train = set([])
ori_data = json.load(open(data_file_name))
for data in ori_data:
vertexSet = data['vertexSet']
for label in data['labels']:
rel = label['r']
for n1 in vertexSet[label['h']]:
for n2 in vertexSet[label['t']]:
fact_in_train.add((n1['name'], n2['name'], rel))
json.dump(list(fact_in_train), open(fact_file_name, "w"))
return fact_in_train
def official_evaluate(tmp, path, train_file = "train_annotated.json", dev_file = "dev.json"):
'''
Adapted from the official evaluation code
'''
truth_dir = os.path.join(path, 'ref')
if not os.path.exists(truth_dir):
os.makedirs(truth_dir)
fact_in_train_annotated = gen_train_facts(os.path.join(path, train_file), truth_dir)
fact_in_train_distant = gen_train_facts(os.path.join(path, "train_distant.json"), truth_dir)
truth = json.load(open(os.path.join(path, dev_file)))
std = {}
tot_evidences = 0
titleset = set([])
title2vectexSet = {}
for x in truth:
title = x['title']
titleset.add(title)
vertexSet = x['vertexSet']
title2vectexSet[title] = vertexSet
if 'labels' not in x: # official test set from DocRED
continue
for label in x['labels']:
r = label['r']
h_idx = label['h']
t_idx = label['t']
std[(title, r, h_idx, t_idx)] = set(label['evidence'])
tot_evidences += len(label['evidence'])
tot_relations = len(std)
tmp.sort(key=lambda x: (x['title'], x['h_idx'], x['t_idx'], x['r']))
submission_answer = [tmp[0]]
for i in range(1, len(tmp)):
x = tmp[i]
y = tmp[i - 1]
if (x['title'], x['h_idx'], x['t_idx'], x['r']) != (y['title'], y['h_idx'], y['t_idx'], y['r']):
submission_answer.append(tmp[i])
correct_re = 0
correct_evidence = 0
pred_evi = 0
correct_in_train_annotated = 0
correct_in_train_distant = 0
titleset2 = set([])
for x in submission_answer:
title = x['title']
h_idx = x['h_idx']
t_idx = x['t_idx']
r = x['r']
titleset2.add(title)
if title not in title2vectexSet:
continue
vertexSet = title2vectexSet[title]
if 'evidence' in x : #and (title, h_idx, t_idx) in std:
evi = set(x['evidence'])
else:
evi = set([])
pred_evi += len(evi)
if (title, r, h_idx, t_idx) in std:
correct_re += 1
stdevi = std[(title, r, h_idx, t_idx)]
correct_evidence += len(stdevi & evi)
in_train_annotated = in_train_distant = False
for n1 in vertexSet[h_idx]:
for n2 in vertexSet[t_idx]:
if (n1['name'], n2['name'], r) in fact_in_train_annotated:
in_train_annotated = True
if (n1['name'], n2['name'], r) in fact_in_train_distant:
in_train_distant = True
if in_train_annotated:
correct_in_train_annotated += 1
if in_train_distant:
correct_in_train_distant += 1
re_p = 1.0 * correct_re / len(submission_answer)
re_r = 1.0 * correct_re / tot_relations if tot_relations != 0 else 0
if re_p + re_r == 0:
re_f1 = 0
else:
re_f1 = 2.0 * re_p * re_r / (re_p + re_r)
evi_p = 1.0 * correct_evidence / pred_evi if pred_evi > 0 else 0
evi_r = 1.0 * correct_evidence / tot_evidences if tot_evidences > 0 else 0
if evi_p + evi_r == 0:
evi_f1 = 0
else:
evi_f1 = 2.0 * evi_p * evi_r / (evi_p + evi_r)
re_p_ignore_train_annotated = 1.0 * (correct_re - correct_in_train_annotated) / (len(submission_answer) - correct_in_train_annotated + 1e-5)
re_p_ignore_train = 1.0 * (correct_re - correct_in_train_distant) / (len(submission_answer) - correct_in_train_distant + 1e-5)
if re_p_ignore_train_annotated + re_r == 0:
re_f1_ignore_train_annotated = 0
else:
re_f1_ignore_train_annotated = 2.0 * re_p_ignore_train_annotated * re_r / (re_p_ignore_train_annotated + re_r)
if re_p_ignore_train + re_r == 0:
re_f1_ignore_train = 0
else:
re_f1_ignore_train = 2.0 * re_p_ignore_train * re_r / (re_p_ignore_train + re_r)
return [re_p, re_r, re_f1], [evi_p, evi_r, evi_f1],\
[re_p_ignore_train_annotated, re_r, re_f1_ignore_train_annotated], \
[re_p_ignore_train, re_r, re_f1_ignore_train]