-
Notifications
You must be signed in to change notification settings - Fork 2
/
analysis_utils.py
197 lines (173 loc) · 6.05 KB
/
analysis_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
"""Utilities for analyzing ELIZA errors."""
from collections import deque
import json
from pathlib import Path
import pandas as pd
from src import simple_tokenizers, data_utils
def annotate_predictions(predictions):
predictions["template_length"] = [len(t.split()) for t in predictions["template"]]
predictions["num_wildcards"] = [t.count("0") for t in predictions["template"]]
predictions["transformation_length"] = [
len(r.split()) for r in predictions["transformation"]
]
predictions["num_copies"] = [
sum(g.isnumeric() for g in r.split()) for r in predictions["transformation"]
]
predictions["input_length"] = [len(s.split()) for s in predictions["input"]]
predictions["sentence"] = [s[: s.find(" E")] for s in predictions["input"]]
return predictions
def annotate_copy_length(predictions):
predictions["output_length"] = [len(t.split()) + 1 for t in predictions["want"]]
predictions["copy_length"] = [
o_len - (t_len - n_copies)
for o_len, t_len, n_copies in zip(
predictions["output_length"],
predictions["transformation_length"],
predictions["num_copies"],
)
]
return predictions
def get_predictions(output_dir, epoch=None):
output_dir = Path(output_dir)
with open(output_dir / "args.json", "r") as f:
args = json.load(f)
if epoch is None:
path = sorted(list(output_dir.glob("predictions_*")))[-1]
elif type(epoch) == str:
path = output_dir / f"predictions_{epoch}.csv"
else:
path = output_dir / f"predictions_{epoch:02d}.csv"
predictions = pd.read_csv(path)
return annotate_predictions(predictions), args
def get_all_predictions(output_dir):
output_dir = Path(output_dir)
with open(output_dir / "args.json", "r") as f:
args = json.load(f)
lst = []
for path in output_dir.glob("predictions_*"):
lst.append(pd.read_csv(path))
predictions = pd.concat(lst)
return annotate_predictions(predictions), args
def get_output_dirs(base):
return list(base.glob("*/*/*"))
def parse_data_dir(s):
parts = s.split("/")[-1].split("_")
d = {}
for part in parts:
idxs = [i for i, c in enumerate(part) if c.isnumeric()]
if not idxs:
continue
i = idxs[0]
k, v = part[:i], part[i:]
v = float(v) if "." in v else int(v)
d[k] = v
return d
def get_data_args(data_dir):
fn = Path(data_dir) / "args.json"
with open(fn, "r") as f:
args = json.load(f)
return args
def add_data_args(data_dir, df):
data_args = get_data_args(data_dir)
for k, v in data_args.items():
df[k] = v
return df
def get_metrics(paths):
rows = []
for i, path in enumerate(paths):
if not (path / "args.json").exists() or not (path / "metrics.csv").exists():
continue
with open(path / "args.json", "r") as f:
args = json.load(f)
metrics = pd.read_csv(path / "metrics.csv")
for k, v in args.items():
metrics[k] = v
data_args = get_data_args(args["data_dir"])
for k, v in data_args.items():
metrics[k] = v
rows.append(metrics)
print(f"Got metrics for {len(rows)} paths")
return pd.concat(rows)
def get_memory_stats(path):
script = pd.read_csv(path / "script.csv")
memory_template = script.query("type == 'memory'").iloc[0]["template"]
memory_template_id = str(
script.query(f"template == '{memory_template}'").iloc[0]["template_id"]
)
null_id = str(script.query(f"type == 'none'").iloc[0]["template_id"])
tokenizer, idx_w = simple_tokenizers.get_tokenizer(path)
val_df = data_utils.load_dataset(path / "validation.csv", tokenizer)
rows = []
for conv_id, templates, conv in zip(
val_df["conv_id"], val_df["template_ids"], val_df["input"]
):
turns = conv.split(". U")
turns = [turns[0]] + [". U" + s for s in turns[1:]]
queue = deque()
num_enqueues = 0
num_dequeues = 0
position = 0
for i, (template_id, turn) in enumerate(zip(templates.split(";"), turns)):
row = {
"conv_id": conv_id,
"turn": i,
"position": position,
"enqueues": num_enqueues,
"dequeues": num_dequeues,
}
row.update(
{
k: 0
for k in (
"tgt_turn",
"tgt_pos",
"queue_size",
"num_enqueues",
"num_dequeues",
)
}
)
if template_id == memory_template_id:
queue.append((i, position))
num_enqueues += 1
row["kind"] = "enqueue"
elif template_id == null_id and len(queue):
tgt_turn, tgt_pos = queue.popleft()
row.update(
{
"kind": "dequeue",
"tgt_turn": tgt_turn,
"tgt_pos": tgt_pos,
"queue_size": len(queue) + 1,
"num_enqueues": num_enqueues,
"num_dequeues": num_dequeues,
}
)
num_dequeues += 1
elif template_id == null_id:
row.update(
{
"kind": "none",
"num_enqueues": num_enqueues,
"num_dequeues": num_dequeues,
}
)
else:
row["kind"] = "-"
rows.append(row)
position += turn.find(" ") + 1
return pd.DataFrame(rows)
def add_memory_stats(preds, memory_df):
for col in [
"position",
"enqueues",
"dequeues",
"tgt_turn",
"tgt_pos",
"queue_size",
"num_enqueues",
"num_dequeues",
"kind",
]:
preds[col] = memory_df[col]
return preds