pysdic.Parametrization#

The pysdic.Parametrization provides a standardized way to manage parametrizations \(P\) mapping input parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\) to output parameters \(\vec{p_o} \in \mathbb{R}^{n_o}\), allowing for the modification of the parametric representation of a transformation without changing the underlying transformation (see pysdic.ReparametrizedTransformation).

\[\vec{p_o} = P(\vec{p_i})\]

Parametrization class#

class Parametrization[source]#

Bases: LockableMixin, JsonSerializerMixin, Hdf5SerializerMixin

Abstract base class representing a parametric transformation \(P\) that maps parameters \(\vec{p_i}\) from an input parametric space \(\mathbb{R}^{n_i}\) to an output parametric space \(\mathbb{R}^{n_o}\).

\[\vec{p_o} = P(\vec{p_i})\]

All parametrizations can depend on constants, which are fixed values that influence the parametrization.

Note

A parametrization is locked (see pysdic.core.lockable.LockableMixin) when it is owned by another object of the package that assumes it does not change (for example a pysdic.ReparametrizedTransformation). A locked parametrization refuses the modifications of its constants (for the subclasses with modifiable constants): is_locked and locked_message tell whether it is locked and why. A copy is always unlocked.

A parametrization can be applied to input parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\) using the encode method, which returns a pysdic.ParametrizationResult object containing the encoded parameters \(\vec{p_o} \in \mathbb{R}^{n_o}\) and the Jacobian matrix \(\mathbf{J}_p = \frac{\partial P}{\partial \vec{p_i}}\) (if requested).

A parametrization can also define a decoding map \(P^{-1}\) using the decode method, which returns a pysdic.ParametrizationResult object containing the decoded parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\) and the Jacobian matrix \(\mathbf{J}_p = \frac{\partial P^{-1}}{\partial \vec{p_o}}\) (if requested).

See also

Note

When \(n_i \neq n_o\), the map \(P\) cannot be bijective and \(P^{-1}\) denotes a left inverse or a right inverse of \(P\) (see the flags is_left_invertible and is_right_invertible below).

Note

Only single parameter vectors are supported: shape \((n_i,)\) for encode and \((n_o,)\) for decode. Batched parameters with shape \((\dots, n_i)\) or \((\dots, n_o)\) are not supported.

See also

To compose parametrizations (ie \(P = P_2 \circ P_1\)), use the following classes:

  • Class pysdic.ComposedParametrization to allow multiple parametrizations to be composed together.

  • The syntax P2 @ P1 which is equivalent to ComposedParametrization([P1, P2]).

See also

To change the parametric space of the transformation (ie \(\vec{p_o} = P(\vec{p_i})\)), use the following classes:

Capability flags#

Each parametrization declares its capabilities with the following boolean class/instance attributes. All flags default to False.

  • _has_encode: the method _encode is implemented (i.e. \(P\) can be evaluated).

  • _has_encode_dp: the method _encode can compute \(\frac{\partial P}{\partial \vec{p_i}}\).

  • _has_decode: the method _decode is implemented (i.e. \(P^{-1}\) can be evaluated).

  • _has_decode_dp: the method _decode can compute \(\frac{\partial P^{-1}}{\partial \vec{p_o}}\).

  • _is_left_invertible: decode is a left inverse of encode, i.e. \(P^{-1}(P(\vec{p_i})) = \vec{p_i}\) for all \(\vec{p_i} \in \mathbb{R}^{n_i}\).

  • _is_right_invertible: decode is a right inverse of encode, i.e. \(P(P^{-1}(\vec{p_o})) = \vec{p_o}\) for all \(\vec{p_o} \in \mathbb{R}^{n_o}\).

The flags must be consistent:

  • _has_encode_dp requires _has_encode.

  • _has_decode_dp requires _has_decode.

  • _is_left_invertible and _is_right_invertible require both _has_encode and _has_decode.

The consistency is checked at initialization by Parametrization.__init__. The flags _is_left_invertible and _is_right_invertible are declarations that can be verified numerically with the methods check_left_invertible and check_right_invertible.

Implementation details#

  • All subclasses of pysdic.Parametrization must call super().__init__() at the end of their __init__ method to check the consistency of the flags.

  • Subclasses with _has_encode=True must implement the method _encode, and subclasses with _has_decode=True must implement the method _decode. These methods are called by the public methods encode and decode respectively, which handle the validation of the inputs and outputs of the parametrization with optional Jacobian computation. If a flag is False, the corresponding method does not need to be implemented.

  • All subclasses must implement the properties input_dim and output_dim to return the input and output dimensions of the parametrization.

  • All subclasses must implement the method _to_dict, _from_dict, and _copy called by the public methods, from_dict, to_dict and copy will handle copying and the serialization and deserialization of the constants of the parametrization.

  • All subclasses must set the capability flags described above, either as class attributes (if they do not depend on the constants) or as instance attributes before calling super().__init__() (if they depend on the constants).

  • The constants should be fixed at the initialization (read-only properties). If a subclass allows to modify a constant after the initialization, each method that modifies it must call self._check_unlocked("set the <name>") before any modification, so that a locked parametrization cannot be modified.

Here is a minimal example of how to implement a simple subclass of pysdic.Parametrization representing a scaling of parameters \(p_o = s \cdot p_i\) with a fixed scale factor \(s\) as a constant:

class ScalingParametrization(Parametrization):

    _has_encode = True
    _has_encode_dp = True
    _has_decode = True
    _has_decode_dp = True
    _is_left_invertible = True
    _is_right_invertible = True

    def __init__(
        self,
        scale_factor: float = 1.0
    ):
        scale_factor = float(scale_factor)
        if scale_factor == 0.0:
            raise ValueError("scale_factor must be non-zero to be invertible.")
        self._scale_factor = scale_factor
        super().__init__()  # Check the consistency of the flags

    @property
    def scale_factor(self) -> float:
        return self._scale_factor  # Read-only constant

    @property
    def input_dim(self) -> int:
        return 1  # 1D input parameters

    @property
    def output_dim(self) -> int:
        return 1  # 1D output parameters

    def _to_dict(self) -> Dict[str, Any]:
        return {"scale_factor": self.scale_factor}

    @classmethod
    def _from_dict(cls, data: Dict[str, Any]) -> ScalingParametrization:
        return cls(scale_factor=data["scale_factor"])

    def _copy(self) -> ScalingParametrization:
        return self.__class__(scale_factor=self.scale_factor)

    def _encode(self, p_i: numpy.ndarray, *, dp: bool = False, **kwargs):
        p_o = self.scale_factor * p_i  # (1,)
        jacobian_dp = numpy.array([[self.scale_factor]]) if dp else None  # (1, 1)
        return p_o, jacobian_dp

    def _decode(self, p_o: numpy.ndarray, *, dp: bool = False, **kwargs):
        p_i = p_o / self.scale_factor  # (1,)
        jacobian_dp = numpy.array([[1.0 / self.scale_factor]]) if dp else None  # (1, 1)
        return p_i, jacobian_dp

The implementation can then be verified numerically:

P = ScalingParametrization(scale_factor=2.0)
P.encode([3.0]).p                              # array([6.])
P.check_encode_dp([3.0], raise_error=True)     # True
P.check_left_invertible([3.0], check_dp=True)  # True

Implemented methods and attributes#

The following sections provide an overview of the attributes and methods defined in the pysdic.Parametrization class.

Attributes#

The pysdic.Parametrization defines the following attributes:

Parametrization.input_dim

[Get] the input dimension \(n_i\) of the parametrization.

Parametrization.output_dim

[Get] the output dimension \(n_o\) of the parametrization.

The capabilities of a parametrization are exposed by the following read-only flags:

Parametrization.has_encode

[Get] whether the parametrization implements the encode method.

Parametrization.has_encode_dp

[Get] whether the parametrization supports Jacobian computation in the encode method.

Parametrization.has_decode

[Get] whether the parametrization implements the decode method.

Parametrization.has_decode_dp

[Get] whether the parametrization supports Jacobian computation in the decode method.

Parametrization.is_invertible

[Get] whether the parametrization is invertible (i.e. both left and right invertible).

Parametrization.is_left_invertible

[Get] whether the decode method is a left inverse of the encode method.

Parametrization.is_right_invertible

[Get] whether the decode method is a right inverse of the encode method.

Methods#

The pysdic.Parametrization defines the following methods:

Parametrization.copy()

Create a copy of the parametrization.

Applying the parametrization methods#

To perform the parametrization from input parameters \(\vec{p_i}\) to output parameters \(\vec{p_o}\) (encode) or the decoding map from \(\vec{p_o}\) to \(\vec{p_i}\) (decode), use the following methods:

Parametrization.encode(p_i, *[, dp])

Apply the parametrization to the given parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\) to return the encoded parameters \(\vec{p_o} \in \mathbb{R}^{n_o}\).

Parametrization.decode(p_o, *[, dp])

Apply the decoding map \(P^{-1}\) to the output parameters \(\vec{p_o} \in \mathbb{R}^{n_o}\) to return the decoded parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\).

The output of the parametrization is a pysdic.ParametrizationResult object, which contains the encoded or decoded parameters (p) and the Jacobian matrix (jacobian_dp) if requested.

Note

Only single parameter vectors are supported: shape \((n_i,)\) for encode and \((n_o,)\) for decode (no batch dimension).

Note

You can also use the __call__ method to apply the parametrization, which is an alias for the encode method: parametrization(p_i).

Note

Two parametrizations can be composed with the syntax P2 @ P1, which is equivalent to ComposedParametrization([P1, P2]) (see pysdic.ComposedParametrization).

Numerical checks#

The implementation of a parametrization (Jacobians and decoding map) can be verified numerically with the following methods:

Parametrization.check_encode_dp(p_i, *[, ...])

Check the Jacobian of the encode method against central finite differences at the given input parameters \(\vec{p_i}\).

Parametrization.check_decode_dp(p_o, *[, ...])

Check the Jacobian of the decode method against central finite differences at the given output parameters \(\vec{p_o}\).

Parametrization.check_left_invertible(p_i, *)

Check numerically that the decode method is a left inverse of the encode method at the given input parameters \(\vec{p_i}\).

Parametrization.check_right_invertible(p_o, *)

Check numerically that the decode method is a right inverse of the encode method at the given output parameters \(\vec{p_o}\).

Serialization methods#

The pysdic.Parametrization inherits serialization methods from the following mixin classes:

See also

The documentation Serialization Mixins for more details on the available serialization methods and formats.

Parametrization.from_dict(data)

Create a parametrization instance from a dictionary.

Parametrization.to_dict()

Serialize the parametrization to a dictionary.