-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
179 lines (145 loc) · 6.07 KB
/
metrics.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
import os
import yaml
with open(r"./configs/metrics.yml") as file:
params_list = yaml.load(file, Loader=yaml.FullLoader)
import sys
import pandas as pd
import numpy as np
import pickle
from pipeline_src.metrics.metrics import Metric
from pipeline_src.dataset.dataset import HypernymDataset
from transformers import AutoTokenizer
from pipeline_src.dataset.prompt_schemas import (
hypo_term_hyper,
predict_child_from_2_parents,
predict_child_from_parent,
predict_child_with_parent_and_grandparent,
predict_children_with_parent_and_brothers,
predict_parent_from_child_granparent,
predict_parent_from_child,
predict_multiple_parents_from_child,
)
def get_intersect(gold, pred):
return list(set(pred).intersection(set(gold)))
if __name__ == "__main__":
test_path = params_list["TEST_DATA_PATH"][0]
saving_path = params_list["OUTPUT_NAME"][0]
save_examples = params_list["SAVE_EXAMPLES"][0]
save_examples_path = params_list["SAVE_EXAMPLES_PATH"][0]
ids_to_use = params_list["IDS_TO_USE"][0]
hard_ids_path = params_list["HARD_IDS_PATH"][0]
df = pd.read_pickle(test_path)
transforms = {
"only_child_leaf": predict_child_with_parent_and_grandparent,
"only_leafs_all": predict_child_from_parent,
"only_leafs_divided": predict_children_with_parent_and_brothers,
"leafs_and_no_leafs": predict_child_from_parent,
"simple_triplet_grandparent": predict_parent_from_child_granparent,
"simple_triplet_2parent": predict_child_from_2_parents,
"predict_hypernym": predict_parent_from_child,
"predict_multiple_hypernyms": predict_multiple_parents_from_child,
}
with open(saving_path, "rb") as fp:
all_preds = pickle.load(fp)
with open(hard_ids_path, "rb") as fp:
hard_ids = pickle.load(fp)
if ids_to_use == "easy":
all_preds = [elem for i, elem in enumerate(all_preds) if i not in hard_ids]
df = [elem for i, elem in enumerate(df) if i not in hard_ids]
write_log_path = save_examples_path + "/easy_metrics.txt"
elif ids_to_use == "hard":
all_preds = [elem for i, elem in enumerate(all_preds) if i in hard_ids]
df = [elem for i, elem in enumerate(df) if i in hard_ids]
write_log_path = save_examples_path + "/hard_metrics.txt"
else:
write_log_path = save_examples_path + "/metrics.txt"
if isinstance(all_preds[0][0], list):
flat_list = [item for sublist in all_preds for item in sublist]
all_preds = flat_list
all_labels = []
all_terms = []
cased = {"all_hyponyms": {"pred": [], "label": [], "term": []}}
for i, elem in enumerate(df):
try:
all_preds[i]
except IndexError:
continue
case = elem["case"]
processed_term, target = transforms[case](elem)
all_labels.append(target)
all_terms.append(processed_term)
if not case in cased.keys():
cased[case] = {"pred": [], "label": [], "term": []}
cased[case]["pred"].append(all_preds[i])
cased[case]["label"].append(target)
cased[case]["term"].append(processed_term)
if case in ("leafs_and_no_leafs", "only_leafs_all", "only_child_leaf"):
cur_case = "all_hyponyms"
cased[cur_case]["pred"].append(all_preds[i])
cased[cur_case]["label"].append(target)
cased[cur_case]["term"].append(processed_term)
print("total preds:" + str(len(all_preds)))
print("total labels:" + str(len(all_labels)))
metric_counter = Metric(all_labels, all_preds, decoding="mean")
mean_cased = metric_counter.get_metrics()
cased_metrics = {}
for key in cased.keys():
metric_counter = Metric(cased[key]["label"], cased[key]["pred"])
res = metric_counter.get_metrics()
cased_metrics[key] = res
if not os.path.exists(save_examples_path):
print("path {} do not exist".format(save_examples_path))
os.mkdir(save_examples_path)
with open(write_log_path, "w") as f:
df = pd.concat(
[pd.DataFrame(cased_metrics), (pd.DataFrame(mean_cased, index=["mean"]).T)],
axis=1,
)
f.write(df.to_string())
print("metrics written in file")
if save_examples:
if not os.path.exists(save_examples_path):
print("path {} do not exist".format(save_examples_path))
os.mkdir(save_examples_path)
metric_counter = Metric(all_labels, all_preds)
for key in cased.keys():
n = len(cased[key]["pred"])
total_str = ""
for i in range(n):
preds = cased[key]["pred"][i]
m = len(preds)
for j in range(m):
pred = preds[j]
gold = cased[key]["label"][i]
term = cased[key]["term"][i]
res = metric_counter.get_one_prediction(
gold, pred, metric_counter.default_metrics(), limit=50
)
intersect = get_intersect(
metric_counter.get_hypernyms(gold),
metric_counter.get_hypernyms(pred),
)
res_str = ""
for metric_key in res.keys():
res_str += " " + str(metric_key) + " " + str(res[metric_key])
total_str += (
term
+ "\n\n"
+ "predicted: "
+ pred
+ "\n \n"
+ "true: "
+ gold
+ "\n \n"
+ "intersection: "
+ ",".join(intersect)
+ "\n\n"
+ "metrics: "
+ res_str
+ " \n\n"
+ "=" * 10
+ "\n"
)
file_name = save_examples_path + "/" + str(key) + ".txt"
with open(file_name, "w") as f:
f.write(total_str)