minerva.utils.instantiators

Attributes

T

Exceptions

ParserException

Custom exception for parser errors.

Functions

_instantiate_cls(cls, config_dict[, additional_kwargs])

Instantiate a class from a configuration dictionary.

instantiate_cls(cls, config[, additional_kwargs])

Instantiate a class from a configuration dictionary.

Module Contents

exception minerva.utils.instantiators.ParserException[source]

Bases: Exception

Custom exception for parser errors.

Initialize self. See help(type(self)) for accurate signature.

minerva.utils.instantiators.T
minerva.utils.instantiators._instantiate_cls(cls, config_dict, additional_kwargs=None)[source]

Instantiate a class from a configuration dictionary.

This function uses the ArgumentParser from jsonargparse to parse the configuration dictionary and instantiate the class with the provided arguments. It also allows for additional keyword arguments to be passed.

Parameters

clsType[T]

The class to instantiate.

config_dictdict

The configuration dictionary containing the parameters for the class, following the structure expected by jsonargparse.

additional_kwargsOptional[dict], optional

Additional arguments that override or extend the configuration dictionary. It uses dot notation for nested parameters, e.g., {“nested.param”: “value”}.

Returns

T

An instance of the class cls initialized with the parameters from config_dict and additional_kwargs.

Raises

ParserException

If there is an error during parsing or instantiation, a ParserException is raised.

Parameters:
  • cls (Type[T])

  • config_dict (dict)

  • additional_kwargs (Optional[dict])

Return type:

T

minerva.utils.instantiators.instantiate_cls(cls, config, additional_kwargs=None)[source]

Instantiate a class from a configuration dictionary.

This function uses the ArgumentParser from jsonargparse to parse the configuration dictionary and instantiate the class with the provided arguments. It also allows for additional keyword arguments to be passed.

Parameters

clsType[T]

The class to instantiate.

configUnion[dict, PathLike]

The configuration dictionary containing the parameters for the class, following the structure expected by jsonargparse. It can also be a path to a JSON or YAML file containing the configuration. If a path is provided, it will read the file and parse its contents into a dictionary. Supported formats are JSON (.json) and YAML (.yaml, .yml).

additional_kwargsOptional[dict], optional

Additional arguments that override or extend the configuration dictionary. It uses dot notation for nested parameters, e.g., {“nested.param”: “value”}.

Returns

T

An instance of the class cls initialized with the parameters from config_dict and additional_kwargs.

Raises

ParserException

If there is an error during parsing or instantiation, a ParserException is raised.

FileNotFoundError

If the provided path does not exist when a path-like object is given.

ValueError

If the provided configuration is neither a dictionary nor a valid path-like object, or if the file format is unsupported.

Examples

>>> from minerva.utils.instantiators import instantiate_cls
>>> from minerva.models.nets.base import SimpleSupervisedModel
>>> model_config = {
        "class_path": "minerva.models.nets.base.SimpleSupervisedModel",
        "init_args": {
            "backbone": {
                "class_path": "minerva.models.nets.time_series.cnns.CNN_PF_Backbone",
                "init_args": {"include_middle": True},
            },
            "fc": {
                "class_path": "minerva.models.nets.mlp.MLP",
                "init_args": {"layer_sizes": [768, 128, 6]},
            },
            "loss_fn": {"class_path": "torch.nn.CrossEntropyLoss"},
            "flatten": True,
        },
    }
>>> model = instantiate_cls(SimpleSupervisedModel, model_config)
Parameters:
  • cls (Type[T])

  • config (Union[dict, minerva.utils.typing.PathLike])

  • additional_kwargs (Optional[dict])

Return type:

T