-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_logging_helper.py
51 lines (37 loc) · 1.31 KB
/
dl_logging_helper.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
from timeit import default_timer as timer
class Logger:
def __init__(self, log_filename):
logger_path = './' + log_filename
with open(logger_path, "r") as log_file:
headers = log_file.readline().strip()
headers = headers.split(',')
self.headers = headers[2:]
self.loggerOut = open(logger_path, "a")
self.time = 0.0
self.begin = 0.0
self.iter = 0.0
self.metric_dic = {}
def beginLoop(self, iter):
self.begin = timer()
self.iter = iter
def endLoop(self):
if self.metric_dic:
end = timer()
self.time = self.time + (end - self.begin)
self.loggerOut.write("%s, %.5f" % (str(self.iter), self.time))
self.time = 0.0
self.iter = 0.0
for header in self.headers:
if header in self.metric_dic:
self.loggerOut.write(",%.5f" % self.metric_dic[header])
else:
self.loggerOut.write(",")
self.loggerOut.write("\n")
# self.loggerOut.flush()
self.metric_dic = {}
def log(self, key, value):
self.metric_dic[key] = value
def endLogger(self):
self.loggerOut.close()
def init(log_filename):
return Logger(log_filename)