-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkqa_evaluate.py
More file actions
266 lines (223 loc) · 8.88 KB
/
mkqa_evaluate.py
File metadata and controls
266 lines (223 loc) · 8.88 KB
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
""" Parsing the jsonl reranking prediction file to gather reranking results (top@n)"""
from argparse import ArgumentParser, RawTextHelpFormatter
import ast
import json
import os
from tqdm.auto import tqdm
import pandas as pd
from datasets import load_dataset
from pywikidata.utils import get_wd_search_results
DESCRIPTION = """Evaluation script for MKQA ranked predictions
Evaluate ranked predictions. If AnswerEntityID not provided,
try to link AnswerString to Entity and compare with GT.
"""
EXAMPLE_OF_DATA_FORMAT = """
Example of data format in predictions_path:
{
"QuestionID": "ID1",
"RankedAnswers": [
{
"AnswerEntityID": null,
"AnswerString": "String of prediction",
"Score": null
},
{
"AnswerEntityID": "Q90",
"AnswerString": "Paris",
"Score": 0.99
},
...
]
},
"""
parser = ArgumentParser(
description=DESCRIPTION,
formatter_class=RawTextHelpFormatter,
)
# pylint: disable=line-too-long
parser.add_argument(
"--predictions_path",
help="Path to JSONL file with predictions" + EXAMPLE_OF_DATA_FORMAT,
default="/workspace/storage/misc/subgraphs_reranking_runs/reranking_model_results/t5_large_ssm/mpnet_highlighted_t5_sequence_reranking_seq2seq_large_2_results.jsonl",
)
parser.add_argument(
"--split",
default="test",
type=str,
help="MKQA dataset split.\ntest by default",
)
parser.add_argument(
"--force",
action="store_true",
help="Force evaluation even if output file already exists",
)
def label_to_entity(label: str, top_k: int = 1) -> list:
"""label_to_entity method to linking label to WikiData entity ID
by using elasticsearch Wikimedia public API
Supported only English language (en)
Parameters
----------
label : str
label of entity to search
top_k : int, optional
top K results from WikiData, by default 1
Returns
-------
list[str] | None
list of entity IDs or None if not found
"""
try:
elastic_results = get_wd_search_results(label, top_k, language="en")[:top_k]
except: # pylint: disable=bare-except
elastic_results = []
try:
elastic_results.extend(
get_wd_search_results(
label.replace('"', "").replace("'", "").strip(), top_k, language="en"
)[:top_k]
)
except: # pylint: disable=bare-except
return [None]
if len(elastic_results) == 0:
return [None]
return list(dict.fromkeys(elastic_results).keys())[:top_k]
class EvalMKQA:
"""EvalMKQA Evaluation class for MKQA ranked predictions"""
def __init__(self):
mkqa_ds = load_dataset("Dms12/mkqa_mintaka_format_with_question_entities")
self.dataset = {
"train": mkqa_ds["train"].to_pandas(),
"validation": mkqa_ds["validation"].to_pandas(),
"test": mkqa_ds["test"].to_pandas(),
}
# Extract Entities Names (Ids) from dataset records
for _, df in self.dataset.items():
df["answerEntityNames"] = df["answerEntity"].apply(
self._get_list_of_entity_ids
)
def _get_list_of_entity_ids(self, answer_entities):
return [e["name"] for e in answer_entities]
def is_answer_correct(self, mkqa_record: pd.Series, answer: dict) -> bool:
"""to check whether an answer is correct or not
Args:
mkqa_record (pd.Series): row in the MKQA dataset
answer (dict): answer dict; comprising of the answer entity and/or answer str
Returns:
bool: correct or not
"""
answer_entity_id = answer.get("AnswerEntityID")
# Parse AnswerEntityID if it's a string representation of a list
if answer_entity_id is not None and isinstance(answer_entity_id, str):
if answer_entity_id.startswith("[") and answer_entity_id.endswith("]"):
try:
parsed = ast.literal_eval(answer_entity_id)
if isinstance(parsed, list) and len(parsed) > 0:
answer_entity_id = parsed[0]
else:
answer_entity_id = None
except (ValueError, SyntaxError):
answer_entity_id = None
if answer_entity_id is None:
if answer.get("AnswerString") is not None:
answer_entity_id = label_to_entity(answer["AnswerString"])[0]
else:
answer_entity_id = None
if (
answer_entity_id is None
and mkqa_record["answerText"] is not None
and answer.get("AnswerString") is not None
):
return answer["AnswerString"] == mkqa_record["answerText"]
if answer_entity_id is None:
return False
return answer_entity_id in mkqa_record["answerEntityNames"]
def evaluate(self, predictions, split: str = "test", top_n: int = 10):
"""evaluate _summary_
Parameters
----------
predictions : List[Dict]
Predictions in the following format:
[
{
"QuestionID": "ID1",
"RankedAnswers": [
{
"AnswerEntityID": None,
"AnswerString": "String of prediction",
"Score": None
},
...
]
},
...
]
"""
_df = self.dataset[split]
import concurrent.futures
def process_prediction(prediction):
question_idx = int(prediction["QuestionID"])
matching_records = _df[_df["id"] == question_idx]
if len(matching_records) == 0:
raise ValueError(f"QuestionID {question_idx} not found in dataset")
mkqa_record = matching_records.iloc[0]
is_answer_correct_results = [
self.is_answer_correct(mkqa_record, answer)
for answer in prediction["RankedAnswers"]
]
return is_answer_correct_results
is_correct = []
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
results = list(
tqdm(
executor.map(process_prediction, predictions),
total=len(predictions),
desc="Process predictions.."
)
)
is_correct.extend(results)
is_correct_df = pd.DataFrame(is_correct)
is_correct_df["id"] = [int(p["QuestionID"]) for p in predictions]
is_correct_df = _df.merge(is_correct_df, on="id")
if len(set(is_correct_df["id"]).symmetric_difference(_df["id"])) != 0:
print(
"WARNING: Not all questions have predictions, "
"the results will be calculated only for the provided predictions "
"without taking into account the unworthy ones."
)
# Format metrics based on is_correct matrix
results = {
"FULL Dataset": self._calculate_hits(is_correct_df, top_n),
}
return results
def _calculate_hits(self, is_correct_df: pd.DataFrame, top_n: int = 10) -> dict:
hits = {}
numeric_cols = [col for col in is_correct_df.columns if isinstance(col, int)]
for top in range(1, top_n + 1):
cols_to_use = [col for col in range(top) if col in numeric_cols]
if not cols_to_use:
hits[f"Hit@{top}"] = 0.0
else:
hits[f"Hit@{top}"] = (
is_correct_df[cols_to_use].apply(any, axis=1).mean()
)
return hits
if __name__ == "__main__":
args = parser.parse_args()
OUTPUT_DIR = "/".join(args.predictions_path.split("/")[:-1])
run_name = args.predictions_path.split("/")[-1]
output_path = f"{OUTPUT_DIR}/reranking_result_{run_name}.txt"
if os.path.exists(output_path) and not args.force:
print(f"Output file already exists: {output_path}")
print("Skipping evaluation. Use --force to re-evaluate.")
else:
with open(args.predictions_path, "r", encoding="utf-8") as f:
reranking_predictions = [json.loads(line) for line in f.readlines()]
eval_mkqa = EvalMKQA()
reranking_results = eval_mkqa.evaluate(reranking_predictions, args.split, 5)
with open(output_path, "w+", encoding="utf-8") as file_output:
file_output.write("Hit scores:\n")
for key, val in reranking_results.items():
file_output.write(f"{key}")
for hitkey, hitval in sorted(val.items(), key=lambda x: int(x[0].split("@")[1])):
file_output.write(f"\t{hitkey:6} = {hitval:.6f}")
file_output.write("\n")