-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
256 lines (202 loc) · 7.78 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
import argparse
import json
from pathlib import Path
from typing import List, Dict, Union
import pandas as pd
def read_jsonl_file(file_path: Union[str, Path]) -> List[Dict]:
with open(file_path, "r") as f:
rows = [json.loads(line) for line in f]
return rows
def true_positives(preds: List, gts: List) -> int:
tp = 0
for pred in preds:
if pred in gts:
tp += 1
return tp
def precision(preds: List[str], gts: List[str]) -> float:
try:
# When nothing is predicted, precision = 1
# irrespective of the ground truth value
if len(preds) == 0:
return 1
# When the predictions are not empty
return min(true_positives(preds, gts) / len(preds), 1.0)
except TypeError:
return 0.0
def recall(preds: List[str], gts: List[str]) -> float:
try:
# When ground truth is empty return 1
# even if there are predictions (edge case)
if len(gts) == 0:
return 1.0
# When the ground truth is not empty
return min(true_positives(preds, gts) / len(gts), 1.0)
except TypeError:
return 0.0
def f1_score(p: float, r: float) -> float:
try:
return (2 * p * r) / (p + r)
except ZeroDivisionError:
return 0.0
def rows_to_dict(rows: List[Dict]) -> Dict:
"""Index the ground truth/prediction rows by subject entity and relation."""
return {
(r["SubjectEntity"], r["Relation"]): list(set(r["ObjectEntitiesID"]))
for r in rows
}
def evaluate_per_sr_pair(pred_rows, gt_rows) -> List[Dict[str, float]]:
"""Evaluate the predictions per Subject-Relation pair"""
pred_dict = rows_to_dict(pred_rows)
gt_dict = rows_to_dict(gt_rows)
results = []
for subj, rel in gt_dict:
# get the ground truth objects
gts = gt_dict[(subj, rel)]
# get the predictions
preds = pred_dict[(subj, rel)]
# calculate the scores
p = precision(preds, gts)
r = recall(preds, gts)
f1 = f1_score(p, r)
results.append({
"SubjectEntity": subj,
"Relation": rel,
"p": p,
"r": r,
"f1": f1,
"tp": true_positives(preds, gts),
"total_pred": len(preds),
"total_gt": len(gts),
})
return sorted(results, key=lambda x: (x["Relation"], x["SubjectEntity"]))
def macro_average_per_relation(scores_per_sr: List[Dict[str, float]]) -> dict:
"""Compute the macro average scores per relation"""
scores = {}
for r in scores_per_sr:
if r["Relation"] not in scores:
scores[r["Relation"]] = []
scores[r["Relation"]].append({
"p": r["p"],
"r": r["r"],
"f1": r["f1"],
})
macro_averages = {}
for rel in scores:
macro_averages[rel] = {
"macro-p": sum([x["p"] for x in scores[rel]]) / len(scores[rel]),
"macro-r": sum([x["r"] for x in scores[rel]]) / len(scores[rel]),
"macro-f1": sum([x["f1"] for x in scores[rel]]) / len(scores[rel]),
}
# Macro average for all relations
all_rel_macro_p = sum([x["p"] for x in scores_per_sr]) / len(scores_per_sr)
all_rel_macro_r = sum([x["r"] for x in scores_per_sr]) / len(scores_per_sr)
all_rel_macro_f1 = sum([x["f1"] for x in scores_per_sr]) / len(
scores_per_sr)
macro_averages["*** All Relations ***"] = {
"macro-p": all_rel_macro_p,
"macro-r": all_rel_macro_r,
"macro-f1": all_rel_macro_f1,
}
return macro_averages
def micro_average_per_relation(scores_per_sr: List[Dict[str, float]]) -> dict:
"""Compute the micro average scores per relation"""
scores = {}
for r in scores_per_sr:
if r["Relation"] not in scores:
scores[r["Relation"]] = {
"tp": 0,
"total_pred": 0,
"total_gt": 0,
}
scores[r["Relation"]]["tp"] += r["tp"]
scores[r["Relation"]]["total_pred"] += r["total_pred"]
scores[r["Relation"]]["total_gt"] += r["total_gt"]
micro_averages = {}
for rel in scores:
micro_p = scores[rel]["tp"] / scores[rel]["total_pred"] if scores[rel][
"total_pred"] > 0 else 1.0
micro_r = scores[rel]["tp"] / scores[rel]["total_gt"] if scores[rel][
"total_gt"] > 0 else 1.0
micro_averages[rel] = {
"micro-p": micro_p,
"micro-r": micro_r,
"micro-f1": f1_score(micro_p, micro_r),
}
# Micro average for all relations
total_tp = sum([x["tp"] for x in scores.values()])
total_pred = sum([x["total_pred"] for x in scores.values()])
total_gt = sum([x["total_gt"] for x in scores.values()])
all_rel_micro_p = total_tp / total_pred if total_pred > 0 else 1.0
all_rel_micro_r = total_tp / total_gt if total_gt > 0 else 1.0
micro_averages["*** All Relations ***"] = {
"micro-p": all_rel_micro_p,
"micro-r": all_rel_micro_r,
"micro-f1": f1_score(all_rel_micro_p, all_rel_micro_r),
}
return micro_averages
def prediction_statistics(scores_per_sr: List[Dict[str, float]]) -> dict:
"""Get the average numbers of predictions and the numbers of empty predictions per relation."""
stats = {}
for r in scores_per_sr:
if r["Relation"] not in stats:
stats[r["Relation"]] = {
"num_sr_pairs": 0,
"total_pred": 0,
"empty_pred": 0,
}
stats[r["Relation"]]["num_sr_pairs"] += 1
stats[r["Relation"]]["total_pred"] += r["total_pred"]
if r["total_pred"] == 0:
stats[r["Relation"]]["empty_pred"] += 1
final_stats = {}
for rel in stats:
final_stats[rel] = {
"avg. #preds": stats[rel]["total_pred"] / stats[rel][
"num_sr_pairs"],
"#empty preds": stats[rel]["empty_pred"],
}
# Average numbers of predictions and the numbers of empty predictions for all relations
total_sr_pairs = len(scores_per_sr)
total_preds = sum([x["total_pred"] for x in stats.values()])
total_empty_preds = sum([x["empty_pred"] for x in stats.values()])
final_stats["*** All Relations ***"] = {
"avg. #preds": total_preds / total_sr_pairs,
"#empty preds": total_empty_preds,
}
return final_stats
def main():
parser = argparse.ArgumentParser(
description="Evaluate Precision, Recall and F1-score of predictions")
parser.add_argument(
"-p", "--predictions",
type=str,
required=True,
help="Path to the predictions file (required)"
)
parser.add_argument(
"-g", "--ground_truth",
type=str,
required=True,
help="Path to the ground truth file (required)"
)
args = parser.parse_args()
# Read the predictions and ground truth
pred_rows = read_jsonl_file(args.predictions)
gt_rows = read_jsonl_file(args.ground_truth)
# Evaluate the predictions
scores_per_sr_pair = evaluate_per_sr_pair(pred_rows, gt_rows)
# Macro average
macro_per_relation = macro_average_per_relation(scores_per_sr_pair)
macro_df = pd.DataFrame(macro_per_relation).transpose().round(3)
# Micro average
micro_per_relation = micro_average_per_relation(scores_per_sr_pair)
micro_df = pd.DataFrame(micro_per_relation).transpose().round(3)
# Statistics
stats = prediction_statistics(scores_per_sr_pair)
stats_df = pd.DataFrame(stats).transpose().round(3)
stats_df["#empty preds"] = stats_df["#empty preds"].astype(int)
# Combine the results
results = pd.concat([macro_df, micro_df, stats_df], axis=1)
print(results)
if __name__ == "__main__":
main()