-
Notifications
You must be signed in to change notification settings - Fork 26
/
log.py
55 lines (45 loc) · 1.75 KB
/
log.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
import logging
import time
import os
import socket
from multiprocessing import Process, Lock
def set_up_log(args, sys_argv):
log_dir = args.log_dir
dataset_log_dir = os.path.join(log_dir, args.dataset)
if not os.path.exists(log_dir):
os.mkdir(log_dir)
if not os.path.exists(dataset_log_dir):
os.mkdir(dataset_log_dir)
file_path = os.path.join(dataset_log_dir, '{}.log'.format(str(time.time())))
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(file_path)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.WARN)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.info('Create log file at {}'.format(file_path))
logger.info('Command line executed: python ' + ' '.join(sys_argv))
logger.info('Full args parsed:')
logger.info(args)
return logger
def save_performance_result(args, logger, metrics):
summary_file = args.summary_file
if summary_file != 'test':
summary_file = os.path.join(args.log_dir, summary_file)
else:
return
dataset = args.dataset
val_metric, no_val_metric = metrics
model_name = '-'.join([args.model, args.feature, str(args.prop_depth)])
seed = args.seed
log_name = os.path.split(logger.handlers[1].baseFilename)[-1]
server = socket.gethostname()
line = '\t'.join([dataset, model_name, str(seed), str(round(val_metric, 4)), str(round(no_val_metric, 4)), log_name, server]) + '\n'
with open(summary_file, 'a') as f:
f.write(line) # WARNING: process unsafe!