-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpprint.py
105 lines (83 loc) · 3.16 KB
/
pprint.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
"""=============================================================================
Utility functions for easy and pretty file logging.
============================================================================="""
import logging
import numpy as np
import types
import torch
# ------------------------------------------------------------------------------
MAIN_LOGGER = 'logger.main'
DIRECTORY = None
# ------------------------------------------------------------------------------
def set_logfiles(directory, level=logging.INFO):
"""Function setup as many loggers as you want.
"""
global DIRECTORY
DIRECTORY = directory
handler = logging.FileHandler('%s/out.txt' % directory)
logger = logging.getLogger(MAIN_LOGGER)
logger.setLevel(level)
logger.addHandler(handler)
# ------------------------------------------------------------------------------
def log(msg):
"""Print message.
"""
_log(msg, MAIN_LOGGER)
# ------------------------------------------------------------------------------
def _log(msg, logger_name):
"""Print message to appropriate logger if on cluster, otherwise print to
stdout.
"""
if torch.cuda.is_available():
logger = logging.getLogger(logger_name)
logger.info(msg)
else:
print(msg)
# ------------------------------------------------------------------------------
def log_line(epoch, train_msgs, test_msgs):
"""Print main line in log file, including current epoch and train and test
data.
"""
train = '\t'.join(['{:6f}' for _ in train_msgs]).format(*train_msgs)
test = '\t'.join(['{:6f}' for _ in test_msgs]).format(*test_msgs)
msg = '\t|\t'.join([str(epoch), train, test])
log(msg)
# ------------------------------------------------------------------------------
def log_section(msg, delim='='):
"""Print message with header.
"""
log(delim * 80)
log(msg)
log(delim * 80)
# ------------------------------------------------------------------------------
def log_args(args):
"""Print arguments passed to script.
"""
fields = [f for f in vars(args)]
longest = max(fields, key=len)
format_str = '{:>%s} {:}' % len(longest)
for f in fields:
msg = format_str.format(f, getattr(args, f))
log(msg)
# ------------------------------------------------------------------------------
def log_config(cfg):
"""Print settings in the configuration object.
"""
fields = [f for f in dir(cfg) if not f.startswith('__') and
type(getattr(cfg, f)) != types.MethodType]
longest = max(fields, key=len)
format_str = '{:>%s} {:}' % len(longest)
for f in fields:
if type(getattr(cfg, f)) != types.MethodType:
msg = format_str.format(f, getattr(cfg, f))
log(msg)
# ------------------------------------------------------------------------------
def log_model(model):
"""Print model specifications.
"""
log(model)
# ------------------------------------------------------------------------------
def save_test_indices(indices):
"""Save a Python list so we know our random split of test indices.
"""
np.save('%s/testset_indices' % DIRECTORY, indices)