-
Notifications
You must be signed in to change notification settings - Fork 1
/
experiment_recorder.py
51 lines (39 loc) · 1.3 KB
/
experiment_recorder.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
import json
import os
import numpy as np
class ExperimentRecorder:
def __init__(self, problem=None, label=None):
# self.problem = problem
# self.label = label
# self.n = len(problem.texts)
self.output_dir = None
self.iteration = 0
def record_propose(self, descriptions, name):
with open(
os.path.join(self.output_dir, f"iteration-{self.iteration}", f"{name}.json"),
"w",
) as f:
json.dump(
{
"descriptions": descriptions,
},
f,
)
def next_iteration(self):
self.iteration += 1
os.makedirs(
os.path.join(self.output_dir, f"iteration-{self.iteration}"), exist_ok=True
)
def set_output_dir(self, output_dir):
self.output_dir = output_dir
os.makedirs(
os.path.join(self.output_dir, f"iteration-{self.iteration}"), exist_ok=True
)
def save_np(path, filename, data):
np.save(os.path.join(path, filename), data)
def save_list(path, filename, data):
with open(os.path.join(path, filename), "w") as f:
f.writelines("\n".join(data))
def save_json(path, filename, data):
with open(os.path.join(path, filename), "w") as f:
json.dump(data, f)