helios.core.logging

Classes

RootLogger

Logger used to log while training to a file.

TensorboardWriter

Wrapper for the Tensorboard writer.

Functions

get_default_log_name(→ str)

Generate the default name used for log files or Tensorboard runs.

create_default_loggers(→ None)

Construct the RootLogger and TensorboardWriter instances.

restore_default_loggers(→ None)

Restore the default loggers from a previous run.

setup_default_loggers(→ None)

Call the setup functions on the default loggers.

flush_default_loggers(→ None)

Flushes the default loggers.

close_default_loggers(→ None)

Close the default loggers and clears them.

get_root_logger(→ RootLogger)

Get the root logger instance.

is_root_logger_active(→ bool)

Check if the root logger has been created.

get_tensorboard_writer(→ TensorboardWriter | None)

Return the Tensorboard writter.

Module Contents

class helios.core.logging.RootLogger

Logger used to log while training to a file.

The log file will be placed in the logs folder as determined by the given path.

Parameters:

log_file – path to the log file.

setup(log_file: pathlib.Path | None = None) None

Finish configuring the root logger.

In particular, this function will create the file logger provided that the input path is not None. If the path points to a file that already exists, then the logger will automatically append to the file, otherwise a new file will be created.

Parameters:

log_file – the (optional) path to the log file.

property logger: logging.Logger

Return the logger instance.

property log_file: pathlib.Path | None

Return the path to the current log file.

If the path for the log file was originally None, this will return None as well.

info(msg: str, **kwargs: Any) None

Log using the INFO tag.

Only available for the main process in distributed training.

Parameters:
  • msg – the message to log

  • kwargs – keyword arguments to logging.info

warning(msg: str, **kwargs: Any) None

Log using the WARNING tag.

Parameters:
  • msg – the message to log

  • kwargs – keyword arguments to logging.warning

error(msg: str, **kwargs: Any) None

Log using the ERROR tag.

Parameters:
  • msg (str) – the message to log

  • kwargs (Any) – keyword arguments to logging.error

exception(msg: str, **kwargs: Any) None

Log an exception.

Parameters:
  • msg – the message to log

  • kwargs – keyword arguments to logging.exception

flush() None

Flush the log.

close() None

Close the log.

class helios.core.logging.TensorboardWriter

Wrapper for the Tensorboard writer.

Parameters:

run_path – path to the directory for the run.

setup(run_path: pathlib.Path) None

Finish configuring the TensorboardWriter.

In particular, this function will create the writer instance and assign it to the given path. If the path already exists, Tensorboard will automatically append to the previous run. In distributed training, the writer will only be created on rank 0.

Parameters:

run_path: – the path to the run folder.:

property run_path: pathlib.Path

Return the path to the current run folder.

add_scalar(tag: str, scalar_value: float | str | torch.Tensor, global_step: int | None = None) None

Add scalar data to the log.

Parameters:
  • tag – name of the scalar to plot.

  • scalar_value – the scalar to plot.

  • global_step – the (optional) step for the given scalar.

add_scalars(main_tag: str, tag_scalar_dict: dict[str, float | torch.Tensor], global_step: int | None) None

Add multiple scalars to the log.

Parameters:
  • main_tag – the parent name for the tags

  • tag_scalar_dict – key-value pair storing tag and corresponding values.

  • global_step – (optional) global step value to record.

add_image(tag: str, img_tensor: torch.Tensor | numpy.typing.NDArray, global_step: int | None = None, dataformats: str = 'CHW') None

Add image data to log.

Parameters:
  • tag – data identifier.

  • img_tensor – image data.

  • global_step – (optional) global step value to record

  • dataformats – image data format specification in the form CHW, HWC, HW, WH, etc.

add_images(tag: str, img_tensor: torch.Tensor | numpy.typing.NDArray, global_step: int | None = None, dataformats: str = 'NCHW') None

Add batched image data to log.

Parameters:
  • tag – data identifier.

  • img_tensor – image data.

  • global_step – (optional) global step value to record.

  • dataformats – image data format specification in the form NCHW, NHWC, CHW, HWC, HW, WH, etc.

add_figure(tag: str, figure: matplotlib.pyplot.Figure | list[matplotlib.pyplot.Figure], global_step: int | None = None, close: bool = True) None

Render matplotlib figure into an image and add it to a summary.

Parameters:
  • tag – data identifier.

  • figure – figure or a list of figures.

  • global_step – (optional) global step value to record.

  • close – flag to automatically close the figure.

add_text(tag: str, text_string: str, global_step: int | None = None)

Add text data to log.

Parameters:
  • tag – data identifier.

  • text_string – string to save.

  • global_step – (optional) global step value to record.

add_graph(model: torch.nn.Module, input_to_model: torch.Tensor | list[torch.Tensor] | None = None, verbose: bool = False, use_strict_trace: bool = True) None

Add graph data to log.

PyTorch currently supports CPU tracing only, so the model and its input(s) will be automatically moved to the CPU prior to logging. Note that the model will be copied prior to moving to the CPU to ensure the input model is not affected.

Parameters:
  • model – model to draw.

  • input_to_mode – a variable or tuple of variables to be fed.

  • verbose – whether to print graph structure in console.

  • use_strict_trace – whether to pass keyword argument strict to torch.jit.trace.

add_pr_curve(tag: str, labels: torch.Tensor | numpy.typing.NDArray, predictions: torch.Tensor | numpy.typing.NDArray, global_step: int | None = None, num_thresholds: int = 127) None

Add a precision recall curve to the log.

Plotting a precision-recall curve lets you understand your model’s performance under different threshold settings. With this function, you provide the ground truth labelling (T/F) and prediction confidence (usually the output of your model) for each target. The TensorBoard UI will let you choose the threshold interactively.

Parameters:
  • tag – data identifier.

  • labels – ground truth data. Binary label for each element.

  • predictions – the probability that an element be classified as true. Value should be in [0, 1].

  • global_step – (optional) global step value to record.

  • num_thresholds – number of thresholds used to draw the curve. Defaults to 127.

add_hparams(hparam_dict: dict[str, Any], metric_dict: dict[str, Any], hparam_domain_discrete: dict[str, list[Any]] | None = None, run_name: str | None = None, global_step: int | None = None) None

Add a set of hyper-parameters to be compared in the log.

Parameters:
  • hparam_dict – each key-value pair in the dictionary is the name of the hyper-parameter and it’s corresponding value. The type of the value can be one of bool, string, float, int, or None.

  • metric_dict – each key-value pair in the dictionary is the name of the metric and it’s corresponding value. Note that the key used here should be unique in the Tensorboard record.

  • hparam_domain_discrete: – a dictionary that contains names of hyper-parameters: and all the discrete values they can hold.

  • run_name – name of the run, to be included as part of the logdir.

  • global_step – (optional) global step value to record.

flush() None

Flush any cached values to Tensorboard.

close() None

Close the Tensorboard writer.

helios.core.logging.get_default_log_name(run_name: str) str

Generate the default name used for log files or Tensorboard runs.

Parameters:

run_name – the name of the run.

Returns:

The string with the default name.

helios.core.logging.create_default_loggers(enable_tensorboard: bool = True) None

Construct the RootLogger and TensorboardWriter instances.

In distributed training, this function should be called AFTER the processes have been created to ensure each process gets a copy of the loggers.

Parameters:

enable_tensorboard – (optional) enables the tensorboard writer. Defaults to true.

helios.core.logging.restore_default_loggers(log_path: pathlib.Path | None = None, run_path: pathlib.Path | None = None) None

Restore the default loggers from a previous run.

This function should be called whenever the loggers need to continue logging to the same file/folder as a previous run.

Parameters:
  • log_path – (optional) path to the log file.

  • run_path – (optional) path to the Tensorboard run folder.

Raises:

RuntimeError – if the root logger hasn’t been created.

helios.core.logging.setup_default_loggers(run_name: str, log_root: pathlib.Path | None = None, runs_root: pathlib.Path | None = None) None

Call the setup functions on the default loggers.

This function should be called when the loggers don’t need to continue from a previous run. If you need that, call restore_default_loggers instead.

Parameters:
  • run_name – the name of the current run.

  • log_root – (optional) path to the logs folder.

  • runs_root – (optional) path to the Tensorboard runs folder.

helios.core.logging.flush_default_loggers() None

Flushes the default loggers.

helios.core.logging.close_default_loggers() None

Close the default loggers and clears them.

helios.core.logging.get_root_logger() RootLogger

Get the root logger instance.

Returns:

The root logger.

helios.core.logging.is_root_logger_active() bool

Check if the root logger has been created.

helios.core.logging.get_tensorboard_writer() TensorboardWriter | None

Return the Tensorboard writter.

If Tensorboard is disabled, this function will return None

Returns:

The Tensorboard logger or None if it doesn’t exist.