-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheval_utils.py
223 lines (187 loc) · 7.06 KB
/
eval_utils.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
import json
import pathlib
from typing import Dict
import numpy as np
import spacy
from rouge_score import rouge_scorer
import re
import pandas as pd
import tqdm
from multiprocessing import Pool, Queue
def namedEntityRatio(nes_ori, nes_cand):
nes_ori = set(nes_ori)
if len(nes_ori) == 0:
return 0
if len(nes_cand) == 0:
return 0
nes_cand = set(nes_cand)
recall = len(nes_ori.intersection(nes_cand)) / len(nes_ori)
precision = len(nes_ori.intersection(nes_cand)) / len(nes_cand)
if recall + precision == 0:
return 0
return 2 * (recall * precision) / (recall + precision)
def nerScore(original, generated, nlp):
cost = np.zeros((len(original), len(generated)))
named_entities_original = [
list(map(lambda x: x.text, nlp(o).ents)) for o in original
]
named_entities_candidate = [
list(map(lambda x: x.text, nlp(o).ents)) for o in generated
]
for i, o in enumerate(named_entities_original):
for j, g in enumerate(named_entities_candidate):
cost[i, j] = namedEntityRatio(o, g)
return cost, named_entities_original, named_entities_candidate
def rougeScores(original, generated, scorer):
rouge_scores = {}
for statistic in ["fmeasure", "recall", "precision"]:
cost_1 = np.zeros((len(original), len(generated)))
cost_2 = np.zeros((len(original), len(generated)))
cost_L = np.zeros((len(original), len(generated)))
for i, o in enumerate(original):
for j, g in enumerate(generated):
scores = scorer.score(o, g)
if statistic == "fmeasure":
cost_1[i, j] = scores["rouge1"].fmeasure
cost_2[i, j] = scores["rouge2"].fmeasure
cost_L[i, j] = scores["rougeL"].fmeasure
elif statistic == "recall":
cost_1[i, j] = scores["rouge1"].recall
cost_2[i, j] = scores["rouge2"].recall
cost_L[i, j] = scores["rougeL"].recall
elif statistic == "precision":
cost_1[i, j] = scores["rouge1"].precision
cost_2[i, j] = scores["rouge2"].precision
cost_L[i, j] = scores["rougeL"].precision
rouge_scores[statistic] = {"rouge1": cost_1, "rouge2": cost_2, "rougeL": cost_L}
return rouge_scores
def createScorers():
nlp = spacy.load("en_core_web_sm")
rouge = rouge_scorer.RougeScorer(["rouge1", "rouge2", "rougeL"], use_stemmer=True)
return {"nlp": nlp, "rouge": rouge}
def calcMetrics(ref, cand, scorers, strip=True):
if strip:
cand = ["".join(re.split(r'( *[\.\?!][\'"\)\]]* *)', c)[:2]) for c in cand]
ner, ent_orig, ent_cand = nerScore(ref, cand, scorers["nlp"])
df = pd.DataFrame(
{
"original": [ref],
"candidate": [cand],
"ner": [ner],
"ent_orig": [ent_orig],
"ent_cand": [ent_cand],
}
)
rouge_scores = rougeScores(ref, cand, scorers["rouge"])
for statistic in ["fmeasure", "recall", "precision"]:
for rouge in ["1", "2", "L"]:
df[f"rouge_{rouge}_{statistic}"] = [
rouge_scores[statistic][f"rouge{rouge}"]
]
return df
def getTopkMetrics(result, params, scorers, k=1):
topk_data = [
calcMetrics(result["original"], result["results"][i]["result"], scorers)
for i in range(0, k)
if len(result["results"]) > i
]
for idx, metrics in enumerate(topk_data):
for (k, v) in params.items():
metrics[k] = v
metrics["topk"] = idx
topk_data = pd.concat(topk_data, ignore_index=True)
return topk_data
def getAllResultsJson(dir: pathlib.Path):
for child in dir.iterdir():
if child.name == "result.json":
params = child.parent / "params.json"
params = json.load(params.open())
results = json.load(child.open())
if params["no_repeat_ngram_size"] is None:
params["no_repeat_ngram_size"] = 0
yield {"results": results, "params": params}
elif child.is_dir():
yield from getAllResultsJson(child)
def isNewRow(dataframe: pd.DataFrame, parameters: Dict):
dataframe_columns = set(dataframe.columns)
for (k, v) in parameters.items():
if k not in dataframe_columns:
return True
dataframe = dataframe[dataframe[k] == v]
return len(dataframe) == 0
def calculateAllNewMetrics(
directory: pathlib.Path, existing: pd.DataFrame = None, topk=1
):
all_results = getAllResultsJson(directory)
if existing is not None:
all_results = filter(lambda r: isNewRow(existing, r["params"]), all_results)
else:
existing = pd.DataFrame()
scorers = createScorers()
dataframes = []
for result in tqdm.tqdm(list(all_results)):
dataframes.append(
getTopkMetrics(result["results"], result["params"], scorers, topk)
)
return dataframes
def getBestScores(dataframe: pd.DataFrame, metric: str):
best_index = [np.unravel_index(np.argmax(v), v.shape) for v in dataframe[metric]]
best_value = [np.max(v) for v in dataframe[metric]]
return best_index, best_value
hyperparams = [
"beamscore-method",
"num_beams",
"batchsize",
"train_iteration",
"beamgroups",
"sample_num",
"beamheight",
"diversity",
"randinit",
"train_lr",
"train_lrsched",
"freqknown",
"no_repeat_ngram_size",
"dataset_size",
"n_repetitions",
]
def keepOnlyBestTopk(
dataframe: pd.DataFrame,
metric="rouge_1_fmeasure_best_value",
ascending=False,
topk=1000,
):
filtered_hyperparams = [h for h in hyperparams if h in dataframe.columns]
split_df = dataframe.groupby(filtered_hyperparams)
rows = []
for (_, group) in split_df:
group = group[group["topk"] <= topk]
rows.append(group.sort_values(metric, ascending=ascending).head(1))
return pd.concat(rows)
def keepOnlyBestTopkUniqueSentences(
dataframe: pd.DataFrame,
metric="rouge_1_fmeasure",
ascending=False,
topk=1000,
):
filtered_hyperparams = [h for h in hyperparams if h in dataframe.columns]
split_df = dataframe.groupby(filtered_hyperparams)
rows = []
for (_, group) in split_df:
group = group[group["topk"] <= topk]
split_group = group.groupby(f"{metric}_best_idx")
for (_, subgroup) in split_group:
rows.append(subgroup.sort_values(f"{metric}_best_value", ascending=ascending).head(1))
return pd.concat(rows)
if __name__ == "__main__":
print(calcMetrics(["The fat cat sits at home."], ["The cat"], createScorers()))
a, b, c = nerScore(
[
"It is variable in form and may be fused dorsally with some of the thoracic segments or occasionally be in two parts, hinged dorsally."
],
[
"What is known about her is that she had three children: one of whom is unknown;the other two are known not to have been born;and one is not known to be living at all."
],
createScorers()["nlp"],
)
print(a, b, c)