Logging

Logging.

class metatrain.utils.logging.CSVFileHandler(*args, **kwargs)[source]

Bases: FileHandler

A custom FileHandler for logging data in CSV format.

Open the specified file and use it as the stream for logging.

emit(record: LogRecord)[source]

Override the default behavior preventing any output to the default log.

Parameters:

record (LogRecord)

emit_data(keys: List[str], values: List[str], units: List[str])[source]

Write structured data to the CSV file.

keys and values are written only the first time this methods is called.

Parameters:
  • keys (List[str]) – Column header names

  • values (List[str]) – Data values to write

  • units (List[str]) – Units for each column

class metatrain.utils.logging.WandbHandler(run: None)[source]

Bases: Handler

A custom logging handler that pushes structured logs to Weights & Biases.

Parameters:

run (None) – Weights & Biases run object.

Initializes the instance - basically setting the formatter to None and the filter list to empty.

emit(record: LogRecord)[source]

Override default behavior to ignore standard log records.

Parameters:

record (LogRecord)

emit_data(keys: List[str], values: List[str], units: List[str])[source]

Log structured data to Weights & Biases.

Parameters:
  • keys (List[str]) – Column header names

  • values (List[str]) – Data values to write

  • units (List[str]) – Units for each column

class metatrain.utils.logging.CustomLogger(name, level=0)[source]

Bases: Logger

Custom logger to log structured data.

Initialize the logger with a name and an optional level.

data(keys: List[str], values: List[str], units: List[str])[source]

Logs data entries to handlers that support an emit_data method.

Parameters:
  • keys (List[str]) – Column header names

  • values (List[str]) – Data values to write

  • units (List[str]) – Units for each column

class metatrain.utils.logging.MetricLogger(log_obj: Logger | CustomLogger, dataset_info: ModelCapabilities | DatasetInfo, initial_metrics: Dict[str, float] | List[Dict[str, float]], names: str | List[str] = '', scales: Dict[str, float] | None = None)[source]

Bases: object

Simple interface to log training metrics logging instance.

Initialize the metric logger. The logger is initialized with the initial metrics and names relative to the metrics (e.g., “train”, “validation”).

In this way, and by assuming that these metrics never increase, the logger can align the output to make it easier to read.

Parameters:
log(metrics: Dict[str, float] | List[Dict[str, float]], epoch: int | None = None, rank: int | None = None)[source]

Log the metrics.

The metrics are automatically aligned to make them easier to read, based on the order of magnitude of each metric given to the class at initialization.

Parameters:
  • metrics (Dict[str, float] | List[Dict[str, float]]) – The current metrics to be logged.

  • epoch (int | None) – The current epoch (optional). If None, the epoch will not be printed, and the logging string will start with the first metric in the metrics dictionary.

  • rank (int | None) – The rank of the process, if the training is distributed. In that case, the logger will only print the metrics for the process with rank 0.

metatrain.utils.logging.setup_logging(log_obj: Logger, log_file: str | Path | None = None, level: int = 30)[source]

Create a logging environment for a given log_obj.

Extracted and adjusted from github.com/MDAnalysis/mdacli/blob/main/src/mdacli/logger.py

Parameters:
  • log_obj (Logger) – A logging instance

  • log_file (str | Path | None) – Name of the log file

  • level (int) – Set the root logger level to the specified level. If for example set to logging.DEBUG detailed debug logs including filename and function name are displayed. For logging.INFO only the message logged from errors, warnings and infos will be displayed.

metatrain.utils.logging.get_cli_input(argv: List[str] | None = None) str[source]

Proper formatted string of the command line input.

Parameters:

argv (List[str] | None) – List of strings to parse. If None taken from sys.argv.

Return type:

str