hydronaut package

Subpackages

Submodules

hydronaut.decorator module

hydronaut.experiment module

Experiment base class.

class hydronaut.experiment.Experiment(config: omegaconf.DictConfig)[source]

Bases: object

Base class for experiments.

TODO * Add methods for logging parameters, artefacts, etc.

__init__(config: omegaconf.DictConfig) None[source]
Parameters:

config – The Hydra configuration object.

log_artifact(local_path: str | Path, artifact_path: str | Path | None = None) None[source]

Wrapper around mlflow.log_artifact: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_artifact

Parameters:
  • local_path – A path to an existing artifact. The artitfact must already be saved to this path.

  • artifact_path – An optional path to a directory under artifact_uri in which to log the artifact in MLflow.

log_artifacts(local_dir: str | Path, artifact_path: str | Path | None = None) None[source]

Wrapper around mlflow.log_artifacts: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_artifacts

Parameters:
  • local_dir – A path to a directory with artifacts.

  • artifact_path – An optional path to a directory under artifact_uri in which to log the artifact in MLflow.

log_dict(dictionary: Any, artifact_file: str | Path) None[source]

Wrapper around mlflow.log_dict: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_dict

Parameters:
  • dictionary – A dict object to log.

  • artifact_file – The run-relative subpath to which to save the data as a JSON or YAML file. The file type will be determined from the extension (“.yaml” or “.json”).

log_figure(figure: Any, artifact_file: str | Path) None[source]

Wrapper around mlflow.log_figure: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_figure

Parameters:
  • figure – A figure to log. Check the documentation of mlflow.log_figure for supported figure types (e.g. Matplotlib and Plotly.py figures).

  • artifact_file – The run-relative subpath to which to save the figure.

log_image(image: Any, artifact_file: str | Path) None[source]

Wrapper around mlflow.log_image: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_image

Parameters:
  • image – An image to log. Check the documentation of mlflow.log_image for supported image types (e.g. numpy.ndarray, Pillow images).

  • artifact_file – The run-relative subpath to which to save the image.

log_metric(key: str, value: int | float, step: int | None = None) None[source]

Wrapper around mlflow.log_metric: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_metric

Parameters:
  • key – The metric name.

  • value – The metric value.

  • step – The metric step. Defaults to zero if unspecified.

log_metrics(metrics: dict[str, Union[int, float]], step: int | None = None) None[source]

Wrapper around mlflow.log_metrics: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_metrics

Parameters:
  • metrics – A dict mapping metric names to their values.

  • step – The metric step. Defaults to zero if unspecified.

log_param(key: str, value: Any) None[source]

Wrapper around mlflow.log_param: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_param

Parameters:
  • key – The parameter name.

  • value – The parameter value.

log_params(params: dict[str, Any]) None[source]

Wrapper around mlflow.log_params: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_params

Parameters:

params – A dict mapping parameter names to their values.

log_text(text: str, artifact_file: str | Path) None[source]

Wrapper around mlflow.log_text: https://mlflow.org/docs/latest/python_api/mlflow.html#mlflow.log_text

Parameters:
  • text – The text to log.

  • artifact_file – The run-relative subpath to which to save the text.

setup() None[source]

Set up the experiment before running it (e.g. download data, generate files, configure databases).

hydronaut.file module

File functions.

hydronaut.file.backup_copy(path: Path) None[source]

Attempt to backup a copy to a timestamped path in the same directory.

Parameters:

path – The path to back up. If it does not exist then nothing will be done.

hydronaut.file.safe_open(path: Path, *args: Any, backup: bool = False, **kwargs: Any) TextIOBase[source]

Context manager for safely opening a path by first creating any missing parent directories and then moving any existing file to a backup path.

Parameters:
  • path – The path to open.

  • backup – If True, backup the file at the path if it exists.

  • *args – Positional and keyword arguments passed through to pathlib.Path.open.

  • **kwargs

    Positional and keyword arguments passed through to pathlib.Path.open.

Returns:

The open file handle.

hydronaut.init module

Initialize a directory with default files for Hydronaut.

class hydronaut.init.Initializer[source]

Bases: object

Initialize a directory with default files.

__init__() None[source]
create_config() None[source]

Create the configuration file.

create_experiment() None[source]

Create a placeholder experiment subclass.

parse_args(args: list[str] = None) None[source]

Parse the command-line arguments with argparse.

Parameters:

args – The list of arguments to parse. If None, the arguments are taken from the current system invocation.

Returns:

The parsed arguments.

run(args: list[str] = None) None[source]

Process command-line arguments and generate the requested files.

Parameters:

args – Passed through to parse_args().

hydronaut.init.main(args: list[str] = None) None[source]

Initialize a directory with default files.

hydronaut.log module

Logging helper functions.

hydronaut.log.configure_logging(level: int = 20) None[source]

Configure logging.

Parameters:

level – The logging level.

hydronaut.mlflow module

MLflow utility functions.

class hydronaut.mlflow.MLflowRunner(config: omegaconf.DictConfig, base_dir: str | Path = None)[source]

Bases: object

Context manager to set up an MLflow run from an Omegaconf configuration object. When used as a context manager, it will configure the tracking URI, experiment, artifact location and run parameters from the configurat object. The context will return the MLflow active run.

__init__(config: omegaconf.DictConfig, base_dir: str | Path = None) None[source]
Parameters:

config – The Omegaconf configuration object.

get_metric(name)[source]

Return the value of a metric logged during the current run.

get_param(name)[source]

Return the value of a parameter logged during the current run.

log_parameters() None[source]

Log all parameters with MLflow.

hydronaut.paths module

Path management.

class hydronaut.paths.PathManager(base_dir: str | Path = None, config_path: str | Path = None)[source]

Bases: object

Path manager. This provides methods to retrieve common paths.

DEFAULT_CONFIG_SUBPATH = 'conf/config.yaml'[source]
HYDRONAUT_CONFIG_ENV_VAR = 'HYDRONAUT_CONFIG'[source]
__init__(base_dir: str | Path = None, config_path: str | Path = None) None[source]
Parameters:

base_dir – The directory relative to which to interpret paths. If None, the current working directory is used.

add_python_paths(paths: list[Union[str, pathlib.Path]]) None[source]

Add Paths to the Python system paths list. The resulting list will be the equivalent of concatenating the input paths with the current system paths.

Parameters:

paths – The paths to add. If relative, they are interpreted relative to the path given by relative_to.

property config_dir: Path[source]

The resolved path to the current configuration directory as a pathlib.Path object.

property config_path: Path[source]

The resolved path to the main configuration file as a pathlib.Path object. If the path was not specified during initialization and the environment variable is not set, it returns the default path.

get_config_path(subpath: str | Path = None) Path[source]

Get the path to a configuration file.

subpath:

The subpath relative to the configuration directory. If None, defaults to config.yaml.

Returns:

The resolved path to the configuration file as a pathlib.Path object.

get_src_path(subpath: str | Path) Path[source]

Get the path to a Python source file in the default source directory.

subpath:

The subpath relative to the source directory.

Returns:

The resolved path to the source file as a pathlib.Path object.

hydronaut.run module

hydronaut.types module

Type definitions and functions.

hydronaut.types.ensure_numeric(value: Any) Generator[Real][source]

Convert values to numeric values. If the value is iterable, then the function will be recursively applied to its elements.

Parameters:

value – The value to check.

Returns:

A generator over numeric values (instances of numbers.Real). Values that could not be converted will be returned as NaN values.

Module contents

File stub.