Skip to content

Logging

logging

Custom Logging.

This module provides extensions of Pythons's logging capabilities for run and debug logs within MLDA runs. The more elaborate logging routines take Statistics objects, which makes their evaluation and formatted output more convenient.

Classes:

Name Description
LoggerSettings

Data class storing settings for the logger

Statistic

Basic statistics object containing information for logging

RunningStatistic

Statistic for computing batch averages

AccumulativeStatistic

Statistic for computing overall averages

MTMLDALogger

Logger for the MLDA sampler

DebugFileHandler

Custom file handler for debug logging

mtmlda.core.logging.LoggerSettings dataclass

Data class storing settings for the logger.

Attributes:

Name Type Description
do_printing bool

Decides if the default logger prints to the console, default is True

logfile_path str

Directory to log run statistics to, default is None

debugfile_path str

Directory to log debug statistics to, default is None

write_mode str

Write mode for the log files (append, overwrite), default is 'w'

mtmlda.core.logging.Statistic

Basic statistics object containing information for logging.

Every statistics object has a string identifier and a format string for the value it stores. The value attribute is not accessed directly, but through getter and setter methods. The idea behind is that more sophisticated logic can be implemented in subclasses, still adhering to the generic interface.

Attributes:

Name Type Description
str_id str

Identifier for the statistic

str_format str

Format string for the value

Methods:

Name Description
set_value

Set the value of the statistic

get_value

Get the value of the statistic

__init__

__init__(str_id: str, str_format: str) -> None

Constructor of the statistic.

Parameters:

Name Type Description Default
str_id _type_

Identifier for the statistic

required
str_format _type_

Format string for the value

required

set_value

set_value(value: float | np.ndarray) -> None

Set the value of the statistic.

get_value

get_value() -> int | float | np.ndarray

Get the value of the statistic.

mtmlda.core.logging.RunningStatistic

Bases: Statistic

Statistic for computing batch averages.

The running statistics stores all values provided by the set_value method and computes their average when the get_value method is called.

__init__

__init__(str_id: str, str_format: str) -> None

Constructor, see base class for details.

set_value

set_value(new_value: float | np.ndarray) -> None

Set the value of the statistic.

get_value

get_value() -> np.ndarray

Get the value of the statistic.

mtmlda.core.logging.AccumulativeStatistic

Bases: Statistic

Statistic for computing overall averages.

The mean value is computed from all provided values.

__init__

__init__(str_id: str, str_format: str) -> None

Constructor, see base class for details.

set_value

set_value(new_value: float | np.ndarray) -> None

Set the value of the statistic.

get_value

get_value() -> np.ndarray

Get the value of the statistic's running average.

mtmlda.core.logging.MTMLDALogger

Logger for the MLDA sampler.

This custom logger wraps the standard Python logger, adding some convenience methods for logging on different levels to different files and the console.

Methods:

Name Description
log_run_statistics

Log run statistics

log_debug_statistics

Log debug statistics

log_header

Log the header of the run statistics table

log_debug_new_samples

Log the start of a new chain segment in the debug log

log_debug_tree_export

Log the export of a tree in the debug log

info

Log an info message to the run logger

debug

Log a debug message to the debug logger

exception

Log an exception to all loggers

__init__

__init__(logger_settings: LoggerSettings) -> None

Constructor of the logger.

Initializes the run and debug log handles, depending on the user settings.

Parameters:

Name Type Description Default
logger_settings LoggerSettings

User settings

required

log_run_statistics

log_run_statistics(statistics: dict[str, Statistic]) -> None

Log statistics into run log table.

Parameters:

Name Type Description Default
statistics dict[str, Statistic]

Run statistics object to log

required

log_debug_statistics

log_debug_statistics(info: str, statistics: dict[str, Statistic]) -> None

Log statistics into debug file.

Parameters:

Name Type Description Default
info str

Event to log statistics for

required
statistics dict[str, Statistic]

Statistics to log

required

log_header

log_header(statistics: dict[str, Statistic]) -> None

Print our the header for the run statistics table.

Parameters:

Name Type Description Default
statistics dict[str, Statistic]

Statistics to print header for

required

log_debug_new_samples

log_debug_new_samples(sample: int) -> None

Log divider into debug file, indicating new fine-level sample.

Parameters:

Name Type Description Default
sample int

Number of the new sample

required

log_debug_tree_export

log_debug_tree_export(tree_id: int) -> None

Note in debug file that Markov tree with given id has been exported.

Parameters:

Name Type Description Default
tree_id int

ID of the tree that has been exported

required

info

info(message: str) -> None

Wrapper for Python logger info call.

Parameters:

Name Type Description Default
message str

Info message to log

required

debug

debug(message: str) -> None

Wrapper for Python logger debug call.

Parameters:

Name Type Description Default
message str

Debug message to log

required

exception

exception(message: str) -> None

Wrapper for Python logger exception call.

Parameters:

Name Type Description Default
message str

Exception message to log

required

_process_value_str

_process_value_str(value: Any, str_format: str) -> str

Format a numerical value as string, given a suitable format.

if the provided value is None, it is formatted as np.nan. If the value is iterable, all values are concatenated as comma-separated list, each with the provided format.

Parameters:

Name Type Description Default
value Any

Value to format as string

required
str_format str

format to use

required

Raises:

Type Description
TypeError

If the value is of an unsupported type

Returns:

Name Type Description
str str

Value as formatted string

mtmlda.core.logging.DebugFileHandler

Bases: logging.FileHandler

Custom file handler for Logger.

This file handler only transfers messages on the DEBUGlevel of python logging.

__init__

__init__(filename: Path, mode: str = 'a', encoding: str = None, delay: bool = False)

Constructor of the file handler.

Parameters:

Name Type Description Default
filename Path

File to log to

required
mode str

Write mode for log messages. Defaults to "a".

'a'
encoding str

Special encoding for messages. Defaults to None.

None
delay bool

Determines if file opening is deferred until first emit call. Defaults to False.

False

emit

emit(record: logging.LogRecord) -> None

Transfer a log message.

Parameters:

Name Type Description Default
record logging.LogRecord

Log message object

required