-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstep3_score.py
400 lines (312 loc) · 13.3 KB
/
step3_score.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
Script for scoring predicted doping entries in according to several
potential schemas presented in the publication.
"""
import os
import argparse
import json
import pandas as pd
from monty.serialization import loadfn
import pprint
import seaborn as sns
import matplotlib.pyplot as plt
import jellyfish
import numpy as np
from constants import DATADIR
from step2_train_predict import llm_completion_from_sentence_json
EVALUATE_MODIFIERS_AND_RESULTS = False
def evaluate(gold, test, loud=False, lowercase=False):
"""
Evaluate the performance of the model on the test set.
Args:
gold (list): list of dictionaries containing the gold standard annotations
test (list): list of dictionaries containing the model's predictions (in same format)
loud (bool): whether to print out the results of each sentence
lowercase (bool=): if true, use lowerase
Returns:
scores_computed (dict): dictionary of scores by entity
ent_categories ([str]): and list of entities used in the evaluation
sequences_distances ([float]): The jaro winkler distances for each completion from raw string
sequences_total (int): The total number of sequences evaluated for sequence accuracy
sequences_correct (int): The total number of sequences exactly correct.
"""
if EVALUATE_MODIFIERS_AND_RESULTS:
ent_categories = ["basemats", "dopants", "results", "doping_modifiers"]
else:
ent_categories = ["basemats", "dopants"]
scores = {
k: {k2: 0 for k2 in ["tp", "tn", "fp", "fn"]} for k in ent_categories
}
scores["dopants2basemats"] = {"n_correct": 0, "test_retrieved": 0, "gold_retrieved": 0}
sequences_correct = 0
sequences_total = 0
sequences_parsable = 0
sequences_distances = []
support = {
"ents": {k: 0 for k in ent_categories},
"words": {k: 0 for k in ent_categories},
"links_words": 0,
"links_ents": 0,
}
parsability_valid = True
for i, val_entry in enumerate(gold):
for j, s in enumerate(val_entry["doping_sentences"]):
if not s["relevant"]:
continue
test_entry_tot = test[i]["doping_sentences"][j]["entity_graph_raw"]
test_entry = {k: test_entry_tot[k] for k in ent_categories + ["dopants2basemats"]}
gold_entry = {k: s[k] for k in ent_categories + ["dopants2basemats"]}
if test_entry["dopants2basemats"] == []:
test_entry["dopants2basemats"] = {}
sentence_text = s["sentence_text"]
gold_completion = s.get("completion", "")
if lowercase:
# Adjust the sequence-level scoring for seq2rel
# lowercase the gold entries if we need to account for things in lowercase
for k in ("dopants", "basemats"):
for ent_id, ent_val in gold_entry[k].items():
gold_entry[k][ent_id] = ent_val.lower()
# seq2rel needs some adjustment for this
gold_completion = json.dumps({k: gold_entry[k] for k in ["dopants", "basemats", "dopants2basemats"]})
test_completion = json.dumps({k: test_entry[k] for k in ["dopants", "basemats", "dopants2basemats"]})
gold_completion = gold_completion.lower()\
.replace("\n", "") \
.replace(" ", " ") \
.replace("{ ", "{") \
.replace(" }", "}") \
.replace(" .", ".") \
.replace("[ ", "[") \
.replace(" ]", "]") \
.strip()
else:
try:
test_completion = test[i]["doping_sentences"][j][
"llm_completion"]
except KeyError:
print(
"WARNING: Could not find completion key for test completion. Sequence-level results will be incorrect.")
test_completion = " "
parsability_valid = False
if loud:
print(s["sentence_text"])
pprint.pprint(gold_entry)
pprint.pprint(test_entry)
# this is a proxy to find the unparsable sequences,
# since by default the processing script will either throw error
# for unparsable sequences or will pass them and return empty decoded entry
if not test_completion[-1] in ["}", ".", "\n"]:
if loud:
print("Sequence from LLM was likely not parsable.")
else:
sequences_parsable += 1
for ent_type in ent_categories:
gold_ents = gold_entry[ent_type]
# correcting a relic of a previous annotation scheme
if ent_type == "doping_modifiers":
gold_ents_words = " ".join(gold_entry[ent_type]).split(" ")
else:
gold_ents_words = " ".join(list(gold_entry[ent_type].values())).split(" ")
support["words"][ent_type] += len(gold_ents_words)
support["ents"][ent_type] += 1 if isinstance(gold_ents, str) else len(gold_ents)
test_ents_words = " ".join(list(test_entry[ent_type].values())).split(" ")
gold_ents_words = [w for w in gold_ents_words if w]
test_ents_words = [w for w in test_ents_words if w]
if loud:
print(ent_type, test_entry)
# print(f"GOLD: {gold_ents_words}")
# print(f"TEST: {test_ents_words}")
TP = 0
TN = 0
FP = 0
FN = 0
for w in gold_ents_words:
if w in test_ents_words:
TP += 1
else:
if loud:
print(f"FALSE NEGATIVE: {w}")
FN += 1
for w in test_ents_words:
if w not in gold_ents_words:
if loud:
print(f"FALSE POSITIVE: {w}")
FP += 1
TN = len(sentence_text.split(" ")) - TP - FN - FP
scores[ent_type]["tp"] += TP
scores[ent_type]["tn"] += TN
scores[ent_type]["fp"] += FP
scores[ent_type]["fn"] += FN
gold_entry["triplets"] = []
test_entry["triplets"] = []
# assemble triplets
for is_test, rel_entry in enumerate((gold_entry, test_entry)):
for did, bids in rel_entry["dopants2basemats"].items():
for bid in bids:
bmat_words = rel_entry["basemats"][bid]
dop_words = rel_entry["dopants"][did]
if not is_test:
support["links_ents"] += 1
for bmat_word in bmat_words.split(" "):
for dop_word in dop_words.split(" "):
if bmat_word and dop_word:
rel_entry["triplets"].append(f"{bmat_word} {dop_word}")
gold_triplets = gold_entry["triplets"]
test_triplets = test_entry["triplets"]
n_correct_triplets = 0
for triplet in gold_triplets:
if triplet in test_triplets:
n_correct_triplets += 1
scores["dopants2basemats"]["n_correct"] += n_correct_triplets
scores["dopants2basemats"]["test_retrieved"] += len(test_triplets)
scores["dopants2basemats"]["gold_retrieved"] += len(gold_triplets)
support["links_words"] += len(gold_triplets)
# Jaro winkler sequence accuracies
dist = jellyfish.jaro_winkler_similarity(gold_completion, test_completion)
sequences_distances.append(dist)
sequences_total += 1
if test_completion == gold_completion:
if loud:
print("Sequences are identical")
sequences_correct += 1
elif loud:
print("Sequences differ:")
print(test_completion)
print(gold_completion)
if loud:
print("-"*50 + "\n")
if loud:
pprint.pprint(scores)
scores_computed = {k: {} for k in ent_categories}
for k in ent_categories:
tp = scores[k]["tp"]
tn = scores[k]["tn"]
fp = scores[k]["fp"]
fn = scores[k]["fn"]
if tp + fp == 0:
prec = 0
else:
prec = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = tp / (tp + 0.5 * (fp + fn))
print(f"{k}: prec={prec}, recall={recall}, f1={f1}")
scores_computed[k]["precision"] = prec
scores_computed[k]["recall"] = recall
scores_computed[k]["f1"] = f1
# Precision = Number of correct triples/Number of triples retrieved
# Recall = Number of correct triples/Number of correct triples that exist in Gold set.
# F-Measure = Harmonic mean of Precision and Recall.
triplet_scores = scores["dopants2basemats"]
if triplet_scores["test_retrieved"] == 0:
triplet_prec = 0
else:
triplet_prec = triplet_scores["n_correct"]/triplet_scores["test_retrieved"]
triplet_recall = triplet_scores["n_correct"]/triplet_scores["gold_retrieved"]
if triplet_recall == 0 or triplet_prec == 0:
triplet_f1 = 0
else:
triplet_f1 = (2 * triplet_prec * triplet_recall)/(triplet_prec + triplet_recall)
print(f"triplets: prec={triplet_prec}, recall={triplet_recall}, f1={triplet_f1}")
scores_computed["link triplets"] = {"precision": triplet_prec, "recall": triplet_recall, "f1": triplet_f1}
return (
scores_computed,
ent_categories,
sequences_distances,
sequences_correct,
sequences_parsable,
sequences_total,
support,
parsability_valid
)
if __name__ == "__main__":
p = argparse.ArgumentParser(fromfile_prefix_chars='@')
p.add_argument(
"schema_type",
choices=["eng", "engextra", "json"],
help="The schema to use for similarity scores.",
)
p.add_argument(
'--test_file',
help="The test file with correct answers. ",
default=os.path.join(DATADIR, "test.json")
)
p.add_argument(
"--pred_file",
help="The file predicted by LLM-NERRE. Default is using the data from publication using the ENG schema.",
default=os.path.join(DATADIR, "inference_decoded_eng.json"),
required=False
)
p.add_argument(
"--plot",
action='store_true',
help="If flag is present, show a simple bar chart of results.",
required=False
)
p.add_argument(
"--loud",
action='store_true',
help="If true, show a summary of each evaluated sentence w/ FP and FNs.",
required=False
)
p.add_argument(
"--enforce-lowercase",
action='store_true',
help="If true, lowercase all words for evaluation. Should only be used with seq2rel results."
)
args = p.parse_args()
gold = loadfn(args.test_file)
test = loadfn(args.pred_file)
plot = args.plot
loud = args.loud
schema_type = args.schema_type
lowercase = args.enforce_lowercase
kwargs = {"write_results": False, "write_modifiers": False}
if schema_type == "eng":
fmt = "eng"
elif schema_type == "engextra":
fmt = "eng"
kwargs = {"write_results": True, "write_modifiers": True}
EVALUATE_MODIFIERS_AND_RESULTS = True
else:
fmt = "json"
for gj in gold:
for sjson in gj["doping_sentences"]:
c = llm_completion_from_sentence_json(sjson, stop_token="", fmt=fmt, **kwargs)
sjson["completion"] = c
print(f"Scoring outputs using \n\ttest file: {args.test_file}\n\tpred file: {args.pred_file}")
(
scores_computed,
ent_categories,
sequences_distances,
sequences_correct,
sequences_parsable,
sequences_total,
support,
parsability_valid
) = evaluate(gold, test, loud=loud, lowercase=lowercase)
# FOR PLOTTING ONLY
if not parsability_valid:
print("Sequence-level formats invalid. Skipping sequence-level metrics.")
ents_rows = []
for entc in ent_categories:
ents_rows += [entc] * 3
df = pd.DataFrame(
{
"metric": ["precision", "recall", "f1"] * (len(ent_categories) + 1),
"entity": ents_rows + ["link triplets"] * 3,
}
)
scores_df = []
for i, r in df.iterrows():
scores_df.append(scores_computed[r["entity"]][r["metric"]])
df["score"] = scores_df
print(df)
print("Total sequences was:", sequences_total)
print("Frac. Sequences parsable: ", sequences_parsable/sequences_total)
print("Avg sequence similarity: ", np.mean(sequences_distances))
print("Frac. of sequences exactly correct: ", sequences_correct/sequences_total)
print("Support was: ", pprint.pformat(support))
if plot:
ax = sns.barplot(x="entity", y="score", hue="metric", data=df)
for container in ax.containers:
ax.bar_label(container)
plt.show()