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

metatrain.utils.pydantic.validate(model_cls: Any, data: dict, **kwargs: 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.

  • **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

metatrain.utils.pydantic.validate_architecture_options(options: dict, model_hypers: type, trainer_hypers: type) 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.

Returns:

The validated options, which have been sanitized.

Raises:

MetatrainValidationError – If validation fails.

Return type:

dict

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