-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxp_metrics_over_degradation.py
180 lines (156 loc) · 5.7 KB
/
xp_metrics_over_degradation.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
from __future__ import annotations
import json, pickle
import pathlib as pl
import functools as ft
import matplotlib.pyplot as plt
from sacred import Experiment
from sacred.run import Run
from sacred.observers import FileStorageObserver
from sacred.commands import print_config
from splice.analysis import (
compute_metrics_over_ner_degradation,
compute_metrics_over_coref_degradation,
plot_task_and_graph_metrics,
)
from splice.corefs import (
add_wrong_link,
add_wrong_mention,
deteriorate_coref,
remove_correct_link,
remove_correct_mention,
)
from splice.ner import add_wrong_entity, remove_correct_entity
from splice.sacred_utils import archive_fig, log_series
from splice.utils import mean_noNaN
DEGRADATION_ACTIONS = {
"add_wrong_entity": ft.partial(add_wrong_entity, max_size=3),
"remove_correct_entity": remove_correct_entity,
"add_wrong_mention": ft.partial(add_wrong_mention, max_span_size=10),
"remove_correct_mention": remove_correct_mention,
"add_wrong_link": add_wrong_link,
"remove_correct_link": remove_correct_link,
"coref_all": ft.partial(
deteriorate_coref,
actions=[
ft.partial(add_wrong_mention, max_span_size=10),
remove_correct_mention,
add_wrong_link,
remove_correct_link,
],
),
}
ex = Experiment("metrics_over_degradation")
ex.observers.append(FileStorageObserver("./runs"))
@ex.config
def config():
input_dir: str
task_name: str
degradation_name: str
degradation_steps: int
degradation_report_frequency: float
@ex.automain
def main(
_run: Run,
input_dir: str,
task_name: str,
degradation_name: str,
degradation_steps: int,
degradation_report_frequency: float,
):
print_config(_run)
RUN_PATH = pl.Path(input_dir)
with open(RUN_PATH / "info.json") as f:
info = json.load(f)
with open(RUN_PATH / "run.json") as f:
run_dict = json.load(f)
co_occurrences_dist = run_dict["meta"]["config_updates"]["co_occurrences_dist"]
analysis_novels = info["analysis_novels"]
book_states = {}
book_states_gold = {}
for book in analysis_novels:
with open(RUN_PATH / f"{book}_state.pickle", "rb") as f:
book_states[book] = pickle.load(f)
with open(RUN_PATH / f"{book}_state_gold.pickle", "rb") as f:
book_states_gold[book] = pickle.load(f)
print(
f"starting evaluation of {task_name} task under {degradation_name} degradation"
)
all_task_metrics_dict: list[dict[str, list[float]]] = []
all_graph_metrics_dict: list[dict[str, list[float]]] = []
degradation_action = DEGRADATION_ACTIONS[degradation_name]
for book, book_state_gold in book_states_gold.items():
if task_name == "NER":
(
task_metrics_dict,
graph_metrics_dict,
) = compute_metrics_over_ner_degradation(
book_state_gold,
"rules",
{"link_corefs_mentions": True},
degradation_report_frequency,
degradation_steps,
(co_occurrences_dist, "tokens"),
degradation_actions=[degradation_action],
)
elif task_name == "coref":
(
task_metrics_dict,
graph_metrics_dict,
) = compute_metrics_over_coref_degradation(
book_state_gold,
"rules",
{"link_corefs_mentions": True},
degradation_report_frequency,
degradation_steps,
(co_occurrences_dist, "tokens"),
degradation_actions=[degradation_action],
)
else:
raise ValueError()
for metric, values in task_metrics_dict.items():
steps = [int(x / degradation_report_frequency) for x in range(len(values))]
log_series(_run, f"{book}_{metric}", values, steps)
for metric, values in graph_metrics_dict.items():
steps = [int(x / degradation_report_frequency) for x in range(len(values))]
log_series(_run, f"{book}_{metric}", values, steps)
fig = plot_task_and_graph_metrics(
task_metrics_dict,
task_name,
graph_metrics_dict,
report_frequency=degradation_report_frequency,
steps=degradation_steps,
)
archive_fig(_run, fig, book)
all_task_metrics_dict.append(task_metrics_dict)
all_graph_metrics_dict.append(graph_metrics_dict)
task_metrics_keys = all_task_metrics_dict[0].keys()
mean_task_metrics_dict = {
k: [
mean_noNaN([d[k][i] for d in all_task_metrics_dict])
for i in range(len(all_task_metrics_dict[0][k]))
]
for k in task_metrics_keys
}
graph_metrics_keys = all_graph_metrics_dict[0].keys()
mean_graph_metrics_dict = {
k: [
mean_noNaN([d[k][i] for d in all_graph_metrics_dict])
for i in range(len(all_graph_metrics_dict[0][k]))
]
for k in graph_metrics_keys
}
for metric, values in mean_task_metrics_dict.items():
steps = [int(x / degradation_report_frequency) for x in range(len(values))]
log_series(_run, f"mean_{metric}", values, steps)
for metric, values in mean_graph_metrics_dict.items():
steps = [int(x / degradation_report_frequency) for x in range(len(values))]
log_series(_run, f"mean_{metric}", values, steps)
fig = plot_task_and_graph_metrics(
mean_task_metrics_dict,
task_name,
mean_graph_metrics_dict,
report_frequency=degradation_report_frequency,
steps=degradation_steps,
)
plt.tight_layout()
archive_fig(_run, fig, "MEAN")