gluonts.core.component module#
- class gluonts.core.component.BaseValidatedInitializerModel[source]#
Bases:
pydantic.main.BaseModelBase Pydantic model for components with
validated()initializers.See also
validatedDecorates an initializer methods with argument validation logic.
- class Config[source]#
Bases:
pydantic.config.BaseConfigConfig for the Pydantic model inherited by all
validated()initializers.Allows the use of arbitrary type annotations in initializer parameters.
- arbitrary_types_allowed = True#
- gluonts.core.component.equals(this: Any, that: Any) bool[source]#
Structural equality check between two objects of arbitrary type.
By default, this function delegates to
equals_default_impl().In addition, the function dispatches to specialized implementations based on the type of the first argument, so the above conditions might be sticter for certain types.
- Parameters
this – Objects to compare.
that – Objects to compare.
- Returns
A boolean value indicating whether
thisandthatare structurally equal.- Return type
bool
See also
equals_default_implDefault semantics of a structural equality check between two objects of arbitrary type.
equals_representable_blockSpecialization for Gluon
HybridBlockinput arguments.equals_parameter_dictSpecialization for Gluon
ParameterDictinput arguments.
- gluonts.core.component.equals_default_impl(this: Any, that: Any) bool[source]#
Default semantics of a structural equality check between two objects of arbitrary type.
Two objects
thisandthatare defined to be structurally equal if and only if the following criteria are satisfied:Their types match.
If their initializer are
validated(), their initializer arguments are pairlise structurally equal.If their initializer are not
validated(), they are referentially equal (i.e.this == that).
- Parameters
this – Objects to compare.
that – Objects to compare.
- Returns
A boolean value indicating whether
thisandthatare structurally equal.- Return type
bool
- gluonts.core.component.from_hyperparameters(cls: Type[gluonts.core.component.A], **hyperparameters) gluonts.core.component.A[source]#
Reflectively create an instance of a class with a
validated()initializer.- Parameters
cls – The type
Aof the component to be instantiated.hyperparameters – A dictionary of key-value pairs to be used as parameters to the component initializer.
- Returns
An instance of the given class.
- Return type
A
- Raises
GluonTSHyperparametersError – Wraps a
ValidationErrorthrown when validating the initializer parameters.
- gluonts.core.component.skip_encoding(v: Any) bool[source]#
Tells whether the input value v should be encoded using the
encode()function.This is used by
validated()to determine which values need to be skipped when recording the initializer arguments for later serialization.This is the fallback implementation, and can be specialized for specific types by registering handler functions.
- gluonts.core.component.validated(base_model=None)[source]#
Decorates an
__init__method with typed parameters with validation and auto-conversion logic.>>> class ComplexNumber: ... @validated() ... def __init__(self, x: float = 0.0, y: float = 0.0) -> None: ... self.x = x ... self.y = y
Classes with decorated initializers can be instantiated using arguments of another type (e.g. an
yargument of typestr). The decorator handles the type conversion logic.>>> c = ComplexNumber(y='42') >>> (c.x, c.y) (0.0, 42.0)
If the bound argument cannot be converted, the decorator throws an error.
>>> c = ComplexNumber(y=None) Traceback (most recent call last): ... pydantic.error_wrappers.ValidationError: 1 validation error for ComplexNumberModel y none is not an allowed value (type=type_error.none.not_allowed)
Internally, the decorator delegates all validation and conversion logic to a Pydantic model, which can be accessed through the
Modelattribute of the decorated initializer.>>> ComplexNumber.__init__.Model <class 'pydantic.main.ComplexNumberModel'>
The Pydantic model is synthesized automatically from on the parameter names and types of the decorated initializer. In the
ComplexNumberexample, the synthesized Pydantic model corresponds to the following definition.>>> class ComplexNumberModel(BaseValidatedInitializerModel): ... x: float = 0.0 ... y: float = 0.0
Clients can optionally customize the base class of the synthesized Pydantic model using the
base_modeldecorator parameter. The default behavior usesBaseValidatedInitializerModeland its model config.See also
BaseValidatedInitializerModelDefault base class for all synthesized Pydantic models.