leaspy.models.utils.noise_struct module

class NoiseStruct(distribution_factory: ~typing.Optional[~typing.Callable[[...], ~torch.distributions.distribution.Distribution]] = None, model_kws_to_dist_kws: ~typing.Dict[str, str] = <factory>, dist_kws_validators: ~typing.Tuple[~typing.Callable[[~typing.Dict[str, ~typing.Any]], ~typing.Dict[str, ~typing.Any]], ...] = (), contextual_dist_kws_validators: ~typing.Tuple[~typing.Callable[[...], ~typing.Optional[~typing.Callable[[~typing.Dict[str, ~typing.Any]], ~typing.Dict[str, ~typing.Any]]]], ...] = ())

Bases: object

Class storing all metadata of a noise structure (read-only).

This class is not intended to be used directly, it serves as configuration for NoiseModel helper class.

TODO? really have everything related to noise here, including stuff that is currently hardcoded in models (model log-likelihood…)?

Parameters
distribution_factoryfunction [torch.Tensor, **kws] -> torch.distributions.Distribution (or None)

A function taking a torch.Tensor of values first, possible keyword arguments and returning a noise generator (instance of class torch.distributions.Distribution), which can sample around these values with respect to noise structure.

model_kws_to_dist_kwsdict[str, str]

Mapping from naming of noise parameters in Leaspy model to the related torch distribution parameters.

dist_kws_validatorstuple[ValidationFunc (kwargs -> kwargs)]

Tuple of functions that sequentially (FIFO) check (& possibly clean) distribution parameters (input). It may raise (LeaspyAlgoInputError) if those are not appropriate for the noise structure. Those validators are the ones that we already may define without any need for a context (e.g. a ‘gaussian_scalar’ noise will need the scale to be of dimension 1, always)

contextual_dist_kws_validatorstuple[**context -> ValidationFunc or None]

Tuple of functions which are factory of validators functions, based on context parameters. Indeed, sometimes we may want to enforce some conditions, but we cannot enforce them without having extra contextual information (e.g. the scale of ‘gaussian_diagonal’ can be of any length in general, but if we already know the model dimension,

then we want to make sure that the scale parameter will be of the same dimension)

Note: if a given context is not sufficient to build a validator, factory should return None instead of a ValidationFunc. cf. NoiseStruct.with_contextual_validators() for more details.

Attributes
dist_kws_to_model_kwsdict[str, str] (read-only property)

Shortcut for reciprocal mapping of model_kws_to_dist_kws

All the previous parameters are also attributes (dataclass)

Methods

validate_dist_kws(dist_kws)

Sequentially compose all validators to validate input.

with_contextual_validators(**context_kws)

Clone the current noise structure but with the additional contextual dist_kws_validators.

contextual_dist_kws_validators: Tuple[Callable[[...], Optional[Callable[[Dict[str, Any]], Dict[str, Any]]]], ...] = ()
property dist_kws_to_model_kws

Shortcut for reciprocal mapping of model_kws_to_dist_kws

dist_kws_validators: Tuple[Callable[[Dict[str, Any]], Dict[str, Any]], ...] = ()
distribution_factory: Optional[Callable[[...], Distribution]] = None
model_kws_to_dist_kws: Dict[str, str]
validate_dist_kws(dist_kws: Dict[str, Any]) Dict[str, Any]

Sequentially compose all validators to validate input.

with_contextual_validators(**context_kws)

Clone the current noise structure but with the additional contextual dist_kws_validators.

Note: the contextual validators will be appended, in FIFO order, to the already existing dist_kws_validators (so in particular they will be executed after them).

Parameters
**context_kws

Any relevant keyword argument which may help to define additional contextual dist_kws_validators.

Returns
NoiseStruct

A cloned version of the current noise structure with relevant extra contextual validators set (they are now “static”, i.e. regular validators)

check_scale_is_compat_with_model_dimension(*, model: AbstractModel, **unused_extra_kws)

Check that scale parameter is compatible with model dimension.

check_scale_is_positive(d: Dict[str, Any])

Checks scale of noise is positive (component-wise if not scalar).

check_scale_is_univariate(d: Dict[str, Any])
convert_input_to_1D_float_tensors(d: Dict[str, Any]) Dict[str, FloatTensor]

Helper function to convert all input values into 1D torch float tensors.

validate_dimension_of_scale_factory(error_tpl: str, expected_dim: int, *, klass=<class 'leaspy.exceptions.LeaspyInputError'>)

Helper to produce a validator function that check dimension of scale among parameters.