MCPcopy Create free account
hub / github.com/M-Nauta/ProtoTree / Log

Class Log

util/log.py:6–77  ·  view source on GitHub ↗

Object for managing the log directory

Source from the content-addressed store, hash-verified

4from util.args import save_args, load_args
5
6class Log:
7
8 """
9 Object for managing the log directory
10 """
11
12 def __init__(self, log_dir: str): # Store log in log_dir
13
14 self._log_dir = log_dir
15 self._logs = dict()
16
17 # Ensure the directories exist
18 if not os.path.isdir(self.log_dir):
19 os.mkdir(self.log_dir)
20 if not os.path.isdir(self.metadata_dir):
21 os.mkdir(self.metadata_dir)
22 if not os.path.isdir(self.checkpoint_dir):
23 os.mkdir(self.checkpoint_dir)
24 open(self.log_dir + '/log.txt', 'w').close() #make log file empty if it already exists
25
26 @property
27 def log_dir(self):
28 return self._log_dir
29
30 @property
31 def checkpoint_dir(self):
32 return self._log_dir + '/checkpoints'
33
34 @property
35 def metadata_dir(self):
36 return self._log_dir + '/metadata'
37
38 def log_message(self, msg: str):
39 """
40 Write a message to the log file
41 :param msg: the message string to be written to the log file
42 """
43 with open(self.log_dir + '/log.txt', 'a') as f:
44 f.write(msg+"\n")
45
46 def create_log(self, log_name: str, key_name: str, *value_names):
47 """
48 Create a csv for logging information
49 :param log_name: The name of the log. The log filename will be <log_name>.csv.
50 :param key_name: The name of the attribute that is used as key (e.g. epoch number)
51 :param value_names: The names of the attributes that are logged
52 """
53 if log_name in self._logs.keys():
54 raise Exception('Log already exists!')
55 # Add to existing logs
56 self._logs[log_name] = (key_name, value_names)
57 # Create log file. Create columns
58 with open(self.log_dir + f'/{log_name}.csv', 'w') as f:
59 f.write(','.join((key_name,) + value_names) + '\n')
60
61 def log_values(self, log_name, key, *values):
62 """
63 Log values in an existent log file

Callers 2

run_ensembleFunction · 0.90
run_treeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected