-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
78 lines (64 loc) · 2.6 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
#!/usr/bin/env python
"""
module with functions to enable logging
"""
import time,os,re,csv,sys,uuid,joblib
from datetime import date
if not os.path.exists(os.path.join(".","logs")):
os.mkdir("logs")
def update_train_log(tag,period,rmse,runtime,MODEL_VERSION,MODEL_VERSION_NOTE,test=False):
"""
update train log file
"""
## name the logfile using something that cycles with date (day, month, year)
today = date.today()
if test:
logfile = os.path.join("logs","train-test.log")
else:
logfile = os.path.join("logs","train-{}-{}.log".format(today.year, today.month))
## write the data to a csv file
header = ['unique_id','timestamp','tag','period','rmse','model_version',
'model_version_note','runtime']
write_header = False
if not os.path.exists(logfile):
write_header = True
with open(logfile,'a') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
if write_header:
writer.writerow(header)
to_write = map(str,[uuid.uuid4(),time.time(),tag,period,rmse,
MODEL_VERSION,MODEL_VERSION_NOTE,runtime])
writer.writerow(to_write)
def update_predict_log(country, y_pred,y_proba,target_date,runtime,MODEL_VERSION,test=False):
"""
update predict log file
"""
## name the logfile using something that cycles with date (day, month, year)
today = date.today()
if test:
logfile = os.path.join("logs","predict-test.log")
else:
logfile = os.path.join("logs","predict-{}-{}.log".format(today.year, today.month))
## write the data to a csv file
header = ['unique_id','timestamp','country', 'y_pred','y_proba','target_date','model_version','runtime']
write_header = False
if not os.path.exists(logfile):
write_header = True
with open(logfile,'a') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
if write_header:
writer.writerow(header)
to_write = map(str,[uuid.uuid4(),time.time(),country,y_pred,y_proba,target_date,
MODEL_VERSION,runtime])
writer.writerow(to_write)
if __name__ == "__main__":
"""
basic test procedure for logger.py
"""
from model import MODEL_VERSION, MODEL_VERSION_NOTE
## train logger
update_train_log(str((100,10)),"{'rmse':0.5}","00:00:01",
MODEL_VERSION, MODEL_VERSION_NOTE,test=True)
## predict logger
update_predict_log("[0]","[0.6,0.4]","['united_states', 24, 'aavail_basic', 8]",
"00:00:01",MODEL_VERSION, test=True)