Skip to content

uqregressors.utils.logging

Logger

Base Logging class.

Parameters:

Name Type Description Default
use_wandb bool

Whether to use weights and biases for logging (Experimental feature, not validated yet).

False
project_name str

The logger project name.

None
run_name str

The logger run name for a given training run.

None
config dict

Dictionary of relevant training parameters, only used if weights and biases is used.

None
name str

Name of the logger.

None
Source code in uqregressors\utils\logging.py
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
class Logger:
    """
    Base Logging class.

    Args: 
        use_wandb (bool): Whether to use weights and biases for logging (Experimental feature, not validated yet).
        project_name (str): The logger project name.
        run_name (str): The logger run name for a given training run. 
        config (dict): Dictionary of relevant training parameters, only used if weights and biases is used.
        name (str): Name of the logger.
    """
    def __init__(self, use_wandb=False, project_name=None, run_name=None, config=None, name=None):
        self.use_wandb = use_wandb and _wandb_available
        self.logs = []

        if self.use_wandb:
            wandb.init(
                project=project_name or "default_project",
                name=run_name,
                config=config or {},
            )
            self.wandb = wandb
        else:
            self.logger = logging.getLogger(name or f"Logger-{os.getpid()}")
            self.logger.setLevel(logging.INFO)

            if LOGGING_CONFIG["print"] and not self.logger.handlers:
                ch = logging.StreamHandler()
                formatter = logging.Formatter("[%(name)s] %(message)s")
                ch.setFormatter(formatter)
                self.logger.addHandler(ch)

    def log(self, data: dict):
        """
        Writes a dictionary to a stored internal log.
        """
        if self.use_wandb:
            self.wandb.log(data)
        else:
            msg = ", ".join(f"{k}={v}" for k, v in data.items())
            self.logs.append(msg)
            self.logger.info(msg)

    def save_to_file(self, path, subdir="logs", idx=0, name=""): 
        """
        Saves logs to the logs subdirectory when model.save is called.
        """
        log_dir = Path(path) / subdir 
        log_dir.mkdir(parents=True, exist_ok=True)
        with open(log_dir / f"{name}_{str(idx)}.log", "w", encoding="utf-8") as f: 
            f.write("\n".join(self.logs))


    def finish(self):
        """
        Finish method for weights and biases logging.
        """
        if self.use_wandb:
            self.wandb.finish()

finish()

Finish method for weights and biases logging.

Source code in uqregressors\utils\logging.py
79
80
81
82
83
84
def finish(self):
    """
    Finish method for weights and biases logging.
    """
    if self.use_wandb:
        self.wandb.finish()

log(data)

Writes a dictionary to a stored internal log.

Source code in uqregressors\utils\logging.py
58
59
60
61
62
63
64
65
66
67
def log(self, data: dict):
    """
    Writes a dictionary to a stored internal log.
    """
    if self.use_wandb:
        self.wandb.log(data)
    else:
        msg = ", ".join(f"{k}={v}" for k, v in data.items())
        self.logs.append(msg)
        self.logger.info(msg)

save_to_file(path, subdir='logs', idx=0, name='')

Saves logs to the logs subdirectory when model.save is called.

Source code in uqregressors\utils\logging.py
69
70
71
72
73
74
75
76
def save_to_file(self, path, subdir="logs", idx=0, name=""): 
    """
    Saves logs to the logs subdirectory when model.save is called.
    """
    log_dir = Path(path) / subdir 
    log_dir.mkdir(parents=True, exist_ok=True)
    with open(log_dir / f"{name}_{str(idx)}.log", "w", encoding="utf-8") as f: 
        f.write("\n".join(self.logs))

set_logging_config(print=True)

Sets global logging printing configuration.

Parameters:

Name Type Description Default
print bool

If False, disables printing to the terminal for all future Logger instances

True
Source code in uqregressors\utils\logging.py
 9
10
11
12
13
14
15
16
def set_logging_config(print=True): 
    """
    Sets global logging printing configuration. 

    Args: 
        print (bool): If False, disables printing to the terminal for all future Logger instances
    """
    LOGGING_CONFIG["print"] = print