Pydantic utilities

Functions and classes to handle the validation of hyperparameters using Pydantic.

exception metatrain.utils.pydantic.MetatrainValidationError(model: Any, errors: list[dict])[source]

Bases: Exception

This class transforms Pydantic validation errors into a more user-friendly format.

Parameters:
  • model (Any) – The Pydantic model class or TypedDict that was used for validation.

  • errors (list[dict]) – The list of Pydantic error dictionaries.

get_error_string(error: dict) str[source]

Given an individual error from Pydantic, return a user-friendly string.

Parameters:

error (dict) – The Pydantic error dictionary.

Returns:

The formatted error string to display to the user.

Return type:

str

default_pydantic(err: dict) str[source]

Default Pydantic error formatting.

Parameters:

err (dict) – The Pydantic error dictionary.

Returns:

The formatted error string to display to the user.

Return type:

str

get_loc_path(error_loc: tuple) str[source]

Convert the error location tuple into a dot-separated string path.

Parameters:

error_loc (tuple) – The ‘loc’ field from a Pydantic error dictionary, which is a tuple representing the location of the error in the input data.

Returns:

A string representing the path to the error location, with certain internal pydantic function calls filtered out for readability.

Return type:

str

metatrain.utils.pydantic.validate(model_cls: ~typing.Any, data: dict, error_cls: type[~metatrain.utils.pydantic.MetatrainValidationError] = <class 'metatrain.utils.pydantic.MetatrainValidationError'>, **kwargs: ~typing.Any) dict[source]

Validate with pydantic, raising custom metatrain errors.

Parameters:
  • model_cls (Any) – The Pydantic model class to use for validation. If it is not a pydantic model, it will be adapted to pydantic using pydantic.TypeAdapter.

  • data (dict) – The data to validate.

  • error_cls (type[MetatrainValidationError]) – The custom error class to raise if validation fails.

  • **kwargs (Any) – Additional keyword arguments to pass to the validation method.

Returns:

The validated options, which have been sanitized.

Raises:

MetatrainValidationError – If validation fails.

Return type:

dict

exception metatrain.utils.pydantic.MetatrainArchitectureValidationError(model: Any, errors: list[dict])[source]

Bases: MetatrainValidationError

Custom validation error for architecture options.

Parameters:
classmethod for_architecture(name: str | None) type[MetatrainArchitectureValidationError][source]
Parameters:

name (str | None)

Return type:

type[MetatrainArchitectureValidationError]

Parameters:

cls (str)

Return type:

str

get_error_string(error: dict) str[source]

Given an individual error from Pydantic, return a user-friendly string.

Parameters:

error (dict) – The Pydantic error dictionary.

Returns:

The formatted error string to display to the user.

Return type:

str

metatrain.utils.pydantic.validate_architecture_options(options: dict, model_hypers: type, trainer_hypers: type, architecture_name: str | None = None) dict[source]

Validate architecture-specific options using Pydantic.

Parameters:
  • options (dict) – The architecture options to validate.

  • model_hypers (type) – The ModelHypers class of the architecture.

  • trainer_hypers (type) – The TrainerHypers class of the architecture.

  • architecture_name (str | None) – The name of the architecture. If provided, it is used to give more specific error messages with links to the architecture documentation.

Returns:

The validated options, which have been sanitized.

Raises:

MetatrainValidationError – If validation fails.

Return type:

dict

exception metatrain.utils.pydantic.MetatrainBaseValidationError(model: Any, errors: list[dict])[source]

Bases: MetatrainValidationError

Custom validation error for base options.

Parameters:
get_error_string(error: dict) str[source]

Given an individual error from Pydantic, return a user-friendly string.

Parameters:

error (dict) – The Pydantic error dictionary.

Returns:

The formatted error string to display to the user.

Return type:

str

metatrain.utils.pydantic.validate_base_options(options: dict) dict[source]

Validate base options using Pydantic.

Parameters:

options (dict) – The base options to validate.

Returns:

The validated options, which have been sanitized.

Raises:

MetatrainValidationError – If the options are invalid.

Return type:

dict

metatrain.utils.pydantic.validate_eval_options(options: dict) dict[source]

Validate evaluation options using Pydantic.

Parameters:

options (dict) – The evaluation options to validate.

Returns:

The validated options, which have been sanitized.

Raises:

MetatrainValidationError – If the options are invalid.

Return type:

dict

metatrain.utils.pydantic.get_train_json_schema(allow_missing_hypers: bool) dict[source]

Generate a JSON schema for the training options.

This JSON schema is a full specification for the input yaml files of mtt train. Therefore, it includes all possible architectures.

Parameters:

allow_missing_hypers (bool) – Whether to allow missing hyperparameters. If you want to use the JSON schema for validating user input, you should set this to True, as it will allow users to omit fields that have default values. If you want to use the JSON schema for validating the input once filled in with defaults, you should set this to False.

Returns:

The JSON schema as a dictionary.

Return type:

dict