leaspy.io.realizations.realization module

class AbstractRealization(name: str, shape: Tuple[int, ...], *, tensor: Tensor | None = None, tensor_copy: bool = True, **kwargs)

Bases: object

Abstract class for Realization.

Parameters:
nameParamType

The name of the variable associated with the realization.

shapeTuple[int, …]

The shape of the tensor realization.

tensortorch.Tensor, optional

If not None, the tensor realization to be stored.

tensor_copybool (default True)

Whether the tensor provided is copied or not.

**kwargsdict

Additional parameters.

Attributes:
nameParamType

The name of the variable associated with the realization.

shapeTuple[int, …]

The shape of the tensor realization.

tensortorch.Tensor

The tensor realization.

Methods

initialize(model, **kwargs)

Initialize realization from a given model.

set_autograd()

Set autograd for tensor of realizations.

set_tensor_realizations_element(element, dim)

Manually change the value (in-place) of tensor_realizations at dimension dim.

to_dict()

Return a serialized dictionary of realization attributes.

unset_autograd()

Unset autograd for tensor of realizations

abstract initialize(model: AbstractModel, **kwargs: KwargsType)

Initialize realization from a given model.

Parameters:
modelAbstractModel

The model you want realizations for.

**kwargsKwargsType

Additional parameters for initialization.

Raises:
LeaspyModelInputError

if unknown variable type

set_autograd() None

Set autograd for tensor of realizations.

TODO remove? only in legacy code

Raises:
ValueError

if inconsistent internal request

set_tensor_realizations_element(element: Tensor, dim: tuple[int, ...]) None

Manually change the value (in-place) of tensor_realizations at dimension dim.

Parameters:
elementtorch.Tensor

The element to put in the tensor realization.

dimTuple[int, …]

The dimension where to put the element.

property tensor: Tensor
to_dict() Dict[str, Any]

Return a serialized dictionary of realization attributes.

unset_autograd() None

Unset autograd for tensor of realizations

TODO remove? only in legacy code

Raises:
ValueError

if inconsistent internal request

class IndividualRealization(name: str, shape: Tuple[int, ...], *, n_individuals: int, **kwargs)

Bases: AbstractRealization

Class for realizations of individual variables.

Parameters:
nameParamType

The name of the variable associated with the realization.

shapeTuple[int, …]

The shape of the tensor realization.

n_individualsint

The number of individuals related to this realization.

**kwargsdict

Additional parameters (including tensor and tensor_copy).

Attributes:
tensor

Methods

initialize(model, *[, init_at_mean])

Initialize the realization from a model instance.

initialize_around_mean(mean, std)

Initialize the realization around the provided mean value.

initialize_at_mean(mean)

Initialize the realization at provided mean value.

set_autograd()

Set autograd for tensor of realizations.

set_tensor_realizations_element(element, dim)

Manually change the value (in-place) of tensor_realizations at dimension dim.

to_dict()

Return a serialized dictionary of realization attributes.

unset_autograd()

Unset autograd for tensor of realizations

initialize(model: AbstractModel, *, init_at_mean: bool = False, **kwargs: KwargsType)

Initialize the realization from a model instance.

Parameters:
modelAbstractModel

The model from which to initialize the realization.

init_at_meanbool, optional

If True, the realization is initialized at the corresponding variable mean value, otherwise it the initial value is sampled around its mean value with a normal distribution.

**kwargsKwargsType

Additional parameters for initialization.

initialize_around_mean(mean: Tensor, std: Tensor) None

Initialize the realization around the provided mean value.

The initial value is sampled according to a normal distribution with provided mean and std parameters.

Parameters:
meantorch.Tensor

Mean value around which to sample the initial value.

stdtorch.Tensor

Standard deviation for the normal distribution used to sample the initial value.

initialize_at_mean(mean: Tensor) None

Initialize the realization at provided mean value.

Parameters:
meantorch.Tensor

The mean at which to initialize the realization.

set_autograd() None

Set autograd for tensor of realizations.

TODO remove? only in legacy code

Raises:
ValueError

if inconsistent internal request

set_tensor_realizations_element(element: Tensor, dim: tuple[int, ...]) None

Manually change the value (in-place) of tensor_realizations at dimension dim.

Parameters:
elementtorch.Tensor

The element to put in the tensor realization.

dimTuple[int, …]

The dimension where to put the element.

property tensor: Tensor
to_dict()

Return a serialized dictionary of realization attributes.

unset_autograd() None

Unset autograd for tensor of realizations

TODO remove? only in legacy code

Raises:
ValueError

if inconsistent internal request

class PopulationRealization(name: str, shape: Tuple[int, ...], *, tensor: Tensor | None = None, tensor_copy: bool = True, **kwargs)

Bases: AbstractRealization

Class for realizations of population variables.

Parameters:
nameParamType

The name of the variable associated with the realization.

shapeTuple[int, …]

The shape of the tensor realization.

tensortorch.Tensor, optional

If not None, the tensor realization to be stored.

tensor_copybool (default True)

Whether the tensor provided is copied or not.

**kwargsdict

Additional parameters.

Attributes:
tensor

Methods

initialize(model, **kwargs)

Initialize the realization from a model instance.

set_autograd()

Set autograd for tensor of realizations.

set_tensor_realizations_element(element, dim)

Manually change the value (in-place) of tensor_realizations at dimension dim.

to_dict()

Return a serialized dictionary of realization attributes.

unset_autograd()

Unset autograd for tensor of realizations

initialize(model: AbstractModel, **kwargs: KwargsType) None

Initialize the realization from a model instance.

Parameters:
modelAbstractModel

The model from which to initialize the realization.

**kwargsKwargsType

Additional parameters for initialization.

set_autograd() None

Set autograd for tensor of realizations.

TODO remove? only in legacy code

Raises:
ValueError

if inconsistent internal request

set_tensor_realizations_element(element: Tensor, dim: tuple[int, ...]) None

Manually change the value (in-place) of tensor_realizations at dimension dim.

Parameters:
elementtorch.Tensor

The element to put in the tensor realization.

dimTuple[int, …]

The dimension where to put the element.

property tensor: Tensor
to_dict() Dict[str, Any]

Return a serialized dictionary of realization attributes.

unset_autograd() None

Unset autograd for tensor of realizations

TODO remove? only in legacy code

Raises:
ValueError

if inconsistent internal request