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).
Parametrization class#
- class Parametrization[source]#
Bases:
LockableMixin,JsonSerializerMixin,Hdf5SerializerMixinAbstract 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 apysdic.ReparametrizedTransformation). A locked parametrization refuses the modifications of its constants (for the subclasses with modifiable constants):is_lockedandlocked_messagetell 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
encodemethod, which returns apysdic.ParametrizationResultobject 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
decodemethod, which returns apysdic.ParametrizationResultobject 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
Class
pysdic.ParametrizationResultfor the result returned by theencodeanddecodemethods.
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_invertibleandis_right_invertiblebelow).Note
Only single parameter vectors are supported: shape \((n_i,)\) for
encodeand \((n_o,)\) fordecode. 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.ComposedParametrizationto allow multiple parametrizations to be composed together.The syntax
P2 @ P1which is equivalent toComposedParametrization([P1, P2]).
See also
To change the parametric space of the transformation (ie \(\vec{p_o} = P(\vec{p_i})\)), use the following classes:
Class
pysdic.Transformation: the base class for transformations.Class
pysdic.ReparametrizedTransformationto allow a transformation to be associated with a parametrization to encode its parameters.
Capability flags#
Each parametrization declares its capabilities with the following boolean class/instance attributes. All flags default to
False._has_encode: the method_encodeis implemented (i.e. \(P\) can be evaluated)._has_encode_dp: the method_encodecan compute \(\frac{\partial P}{\partial \vec{p_i}}\)._has_decode: the method_decodeis implemented (i.e. \(P^{-1}\) can be evaluated)._has_decode_dp: the method_decodecan compute \(\frac{\partial P^{-1}}{\partial \vec{p_o}}\)._is_left_invertible:decodeis a left inverse ofencode, i.e. \(P^{-1}(P(\vec{p_i})) = \vec{p_i}\) for all \(\vec{p_i} \in \mathbb{R}^{n_i}\)._is_right_invertible:decodeis a right inverse ofencode, 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_dprequires_has_encode._has_decode_dprequires_has_decode._is_left_invertibleand_is_right_invertiblerequire both_has_encodeand_has_decode.
The consistency is checked at initialization by
Parametrization.__init__. The flags_is_left_invertibleand_is_right_invertibleare declarations that can be verified numerically with the methodscheck_left_invertibleandcheck_right_invertible.Implementation details#
All subclasses of
pysdic.Parametrizationmust callsuper().__init__()at the end of their__init__method to check the consistency of the flags.Subclasses with
_has_encode=Truemust implement the method_encode, and subclasses with_has_decode=Truemust implement the method_decode. These methods are called by the public methodsencodeanddecoderespectively, which handle the validation of the inputs and outputs of the parametrization with optional Jacobian computation. If a flag isFalse, the corresponding method does not need to be implemented.All subclasses must implement the properties
input_dimandoutput_dimto return the input and output dimensions of the parametrization.All subclasses must implement the method
_to_dict,_from_dict, and_copycalled by the public methods,from_dict,to_dictandcopywill 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.Parametrizationrepresenting 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:
[Get] the input dimension \(n_i\) of the parametrization. |
|
[Get] the output dimension \(n_o\) of the parametrization. |
The capabilities of a parametrization are exposed by the following read-only flags:
[Get] whether the parametrization implements the encode method. |
|
[Get] whether the parametrization supports Jacobian computation in the encode method. |
|
[Get] whether the parametrization implements the decode method. |
|
[Get] whether the parametrization supports Jacobian computation in the decode method. |
|
[Get] whether the parametrization is invertible (i.e. both left and right invertible). |
|
[Get] whether the decode method is a left inverse of the encode method. |
|
[Get] whether the decode method is a right inverse of the encode method. |
Methods#
The pysdic.Parametrization defines the following methods:
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:
|
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}\). |
|
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:
|
Check the Jacobian of the encode method against central finite differences at the given input parameters \(\vec{p_i}\). |
|
Check the Jacobian of the decode method against central finite differences at the given output parameters \(\vec{p_o}\). |
Check numerically that the decode method is a left inverse of the encode method at the given input parameters \(\vec{p_i}\). |
|
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.
Create a parametrization instance from a dictionary. |
|
Serialize the parametrization to a dictionary. |