-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.py
335 lines (264 loc) · 9.74 KB
/
logger.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# This code is taken from rllab
from enum import Enum
from tabulate import tabulate
from console import mkdir_p, colorize
from contextlib import contextmanager
import numpy as np
import os
import os.path as osp
import sys
import datetime
import dateutil.tz
import csv
import joblib
import json
import pickle
import base64
_prefixes = []
_prefix_str = ''
_tabular_prefixes = []
_tabular_prefix_str = ''
_tabular = []
_text_outputs = []
_tabular_outputs = []
_text_fds = {}
_tabular_fds = {}
_tabular_header_written = set()
_snapshot_dir = None
_snapshot_mode = 'all'
_snapshot_gap = 1
_log_tabular_only = False
_header_printed = False
def _add_output(file_name, arr, fds, mode='a'):
if file_name not in arr:
mkdir_p(os.path.dirname(file_name))
arr.append(file_name)
fds[file_name] = open(file_name, mode)
def _remove_output(file_name, arr, fds):
if file_name in arr:
fds[file_name].close()
del fds[file_name]
arr.remove(file_name)
def push_prefix(prefix):
_prefixes.append(prefix)
global _prefix_str
_prefix_str = ''.join(_prefixes)
def add_text_output(file_name):
_add_output(file_name, _text_outputs, _text_fds, mode='a')
def remove_text_output(file_name):
_remove_output(file_name, _text_outputs, _text_fds)
def add_tabular_output(file_name):
_add_output(file_name, _tabular_outputs, _tabular_fds, mode='w')
def remove_tabular_output(file_name):
if _tabular_fds[file_name] in _tabular_header_written:
_tabular_header_written.remove(_tabular_fds[file_name])
_remove_output(file_name, _tabular_outputs, _tabular_fds)
def set_snapshot_dir(dir_name):
global _snapshot_dir
_snapshot_dir = dir_name
def get_snapshot_dir():
return _snapshot_dir
def get_snapshot_mode():
return _snapshot_mode
def set_snapshot_mode(mode):
global _snapshot_mode
_snapshot_mode = mode
def get_snapshot_gap():
return _snapshot_gap
def set_snapshot_gap(gap):
global _snapshot_gap
_snapshot_gap = gap
def set_log_tabular_only(log_tabular_only):
global _log_tabular_only
_log_tabular_only = log_tabular_only
def get_log_tabular_only():
return _log_tabular_only
def log(s, with_prefix=True, with_timestamp=True, color=None):
out = s
if with_prefix:
out = _prefix_str + out
if with_timestamp:
now = datetime.datetime.now(dateutil.tz.tzlocal())
timestamp = now.strftime('%Y-%m-%d %H:%M:%S.%f %Z')
out = "%s | %s" % (timestamp, out)
if color is not None:
out = colorize(out, color)
if not _log_tabular_only:
# Also log to stdout
print(out)
for fd in list(_text_fds.values()):
fd.write(out + '\n')
fd.flush()
sys.stdout.flush()
def record_tabular(key, val):
_tabular.append((_tabular_prefix_str + str(key), str(val)))
def push_tabular_prefix(key):
_tabular_prefixes.append(key)
global _tabular_prefix_str
_tabular_prefix_str = ''.join(_tabular_prefixes)
def pop_tabular_prefix():
del _tabular_prefixes[-1]
global _tabular_prefix_str
_tabular_prefix_str = ''.join(_tabular_prefixes)
@contextmanager
def prefix(key):
push_prefix(key)
try:
yield
finally:
pop_prefix()
@contextmanager
def tabular_prefix(key):
push_tabular_prefix(key)
yield
pop_tabular_prefix()
class TerminalTablePrinter(object):
def __init__(self):
self.headers = None
self.tabulars = []
def print_tabular(self, new_tabular):
if self.headers is None:
self.headers = [x[0] for x in new_tabular]
else:
assert len(self.headers) == len(new_tabular)
self.tabulars.append([x[1] for x in new_tabular])
self.refresh()
def refresh(self):
import os
rows, columns = os.popen('stty size', 'r').read().split()
tabulars = self.tabulars[-(int(rows) - 3):]
sys.stdout.write("\x1b[2J\x1b[H")
sys.stdout.write(tabulate(tabulars, self.headers))
sys.stdout.write("\n")
table_printer = TerminalTablePrinter()
def dump_tabular(*args, **kwargs):
wh = kwargs.pop("write_header", None)
if len(_tabular) > 0:
if _log_tabular_only:
table_printer.print_tabular(_tabular)
else:
for line in tabulate(_tabular).split('\n'):
log(line, *args, **kwargs)
tabular_dict = dict(_tabular)
# Also write to the csv files
# This assumes that the keys in each iteration won't change!
for tabular_fd in list(_tabular_fds.values()):
writer = csv.DictWriter(tabular_fd, fieldnames=list(tabular_dict.keys()))
if wh or (wh is None and tabular_fd not in _tabular_header_written):
writer.writeheader()
_tabular_header_written.add(tabular_fd)
writer.writerow(tabular_dict)
tabular_fd.flush()
del _tabular[:]
def pop_prefix():
del _prefixes[-1]
global _prefix_str
_prefix_str = ''.join(_prefixes)
def save_itr_params(itr, params):
if _snapshot_dir:
if _snapshot_mode == 'all':
file_name = osp.join(_snapshot_dir, 'itr_%d.pkl' % itr)
joblib.dump(params, file_name, compress=3)
elif _snapshot_mode == 'last':
# override previous params
file_name = osp.join(_snapshot_dir, 'params.pkl')
joblib.dump(params, file_name, compress=3)
elif _snapshot_mode == "gap":
if itr % _snapshot_gap == 0:
file_name = osp.join(_snapshot_dir, 'itr_%d.pkl' % itr)
joblib.dump(params, file_name, compress=3)
elif _snapshot_mode == 'none':
pass
else:
raise NotImplementedError
def log_parameters(log_file, args, classes):
log_params = {}
for param_name, param_value in args.__dict__.items():
if any([param_name.startswith(x) for x in list(classes.keys())]):
continue
log_params[param_name] = param_value
for name, cls in classes.items():
if isinstance(cls, type):
params = get_all_parameters(cls, args)
params["_name"] = getattr(args, name)
log_params[name] = params
else:
log_params[name] = getattr(cls, "__kwargs", dict())
log_params[name]["_name"] = cls.__module__ + "." + cls.__class__.__name__
mkdir_p(os.path.dirname(log_file))
with open(log_file, "w") as f:
json.dump(log_params, f, indent=2, sort_keys=True)
def stub_to_json(stub_sth):
from rllab.misc import instrument
if isinstance(stub_sth, instrument.StubObject):
assert len(stub_sth.args) == 0
data = dict()
for k, v in stub_sth.kwargs.items():
data[k] = stub_to_json(v)
data["_name"] = stub_sth.proxy_class.__module__ + "." + stub_sth.proxy_class.__name__
return data
elif isinstance(stub_sth, instrument.StubAttr):
return dict(
obj=stub_to_json(stub_sth.obj),
attr=stub_to_json(stub_sth.attr_name)
)
elif isinstance(stub_sth, instrument.StubMethodCall):
return dict(
obj=stub_to_json(stub_sth.obj),
method_name=stub_to_json(stub_sth.method_name),
args=stub_to_json(stub_sth.args),
kwargs=stub_to_json(stub_sth.kwargs),
)
elif isinstance(stub_sth, instrument.BinaryOp):
return "binary_op"
elif isinstance(stub_sth, instrument.StubClass):
return stub_sth.proxy_class.__module__ + "." + stub_sth.proxy_class.__name__
elif isinstance(stub_sth, dict):
return {stub_to_json(k): stub_to_json(v) for k, v in stub_sth.items()}
elif isinstance(stub_sth, (list, tuple)):
return list(map(stub_to_json, stub_sth))
elif type(stub_sth) == type(lambda: None):
if stub_sth.__module__ is not None:
return stub_sth.__module__ + "." + stub_sth.__name__
return stub_sth.__name__
elif "theano" in str(type(stub_sth)):
return repr(stub_sth)
return stub_sth
class MyEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, type):
return {'$class': o.__module__ + "." + o.__name__}
elif isinstance(o, Enum):
return {'$enum': o.__module__ + "." + o.__class__.__name__ + '.' + o.name}
return json.JSONEncoder.default(self, o)
def log_parameters_lite(log_file, args):
log_params = {}
for param_name, param_value in args.__dict__.items():
log_params[param_name] = param_value
#if args.args_data is not None:
# stub_method = pickle.loads(base64.b64decode(args.args_data))
# method_args = stub_method.kwargs
# log_params["json_args"] = dict()
# for k, v in list(method_args.items()):
# log_params["json_args"][k] = stub_to_json(v)
# kwargs = stub_method.obj.kwargs
# for k in ["baseline", "env", "policy"]:
# if k in kwargs:
# log_params["json_args"][k] = stub_to_json(kwargs.pop(k))
# log_params["json_args"]["algo"] = stub_to_json(stub_method.obj)
mkdir_p(os.path.dirname(log_file))
with open(log_file, "w") as f:
json.dump(log_params, f, indent=2, sort_keys=True, cls=MyEncoder)
def log_variant(log_file, variant_data):
mkdir_p(os.path.dirname(log_file))
if hasattr(variant_data, "dump"):
variant_data = variant_data.dump()
variant_json = stub_to_json(variant_data)
with open(log_file, "w") as f:
json.dump(variant_json, f, indent=2, sort_keys=True, cls=MyEncoder)
def record_tabular_misc_stat(key, values):
record_tabular(key + "Average", np.average(values))
record_tabular(key + "Std", np.std(values))
record_tabular(key + "Median", np.median(values))
record_tabular(key + "Min", np.amin(values))
record_tabular(key + "Max", np.amax(values))