pysdic.ReparametrizedTransformation#

The pysdic.ReparametrizedTransformation provides a standardized way to manage reparametrized transformations, allowing for the application of a parametrization \(P\) to the parameters of a transformation \(T\):

\[\vec{x_o} = \tilde{T}(\vec{x_i}; \vec{q}) = T(\vec{x_i}; P(\vec{q}))\]

The reparametrized transformation stores the new parameters \(\vec{q}\). The underlying transformation and parametrization are copied, and the parameters \(\vec{p} = P(\vec{q})\) of the underlying transformation are locked: they can only be modified through the reparametrized transformation.

ReparametrizedTransformation class#

class ReparametrizedTransformation(*args, **kwargs)[source]#

Bases: Transformation

A pysdic.Transformation whose parameters are described by a pysdic.Parametrization.

Lets \(T\) be a transformation that maps input points \(\vec{x_i}\) to output points \(\vec{x_o}\) with parameters \(\vec{p}\):

\[\vec{x_o} = T(\vec{x_i}; \vec{p})\]

In some cases, it can be useful to express the parameters \(\vec{p}\) with another representation \(\vec{q}\) related to the original parameters by a parametrization \(P\):

\[\vec{p} = P(\vec{q})\]

The reparametrized transformation \(\tilde{T}\) has the parameters \(\vec{q}\):

\[\vec{x_o} = \tilde{T}(\vec{x_i}; \vec{q}) = T(\vec{x_i}; P(\vec{q}))\]

For example, a pysdic.SubsetParametrization allows to optimize only some parameters of a transformation while the others are kept fixed.

The Jacobian with respect to the input points is the one of \(T\), and the Jacobian with respect to the parameters is obtained with the chain rule:

\[\mathbf{J}_q = \frac{\partial \tilde{T}}{\partial \vec{q}} = \frac{\partial T}{\partial \vec{p}} \cdot \frac{\partial P}{\partial \vec{q}}\]

See also

Note

The reparametrized transformation stores the parameters \(\vec{q}\). Each time they are set, the parameters \(\vec{p} = P(\vec{q})\) of the underlying transformation are updated.

Important

The transformation and the parametrization are not copied: the reparametrized transformation uses the given objects and locks them permanently (see is_locked and locked_message), with messages explaining why:

  • the parameters \(\vec{p}\) of the transformation can only be modified through the parameters \(\vec{q}\) of the reparametrized transformation, so that they are always equal to \(P(\vec{q})\);

  • the parametrization cannot be modified (for the parametrizations with modifiable constants), since \(\vec{p}\) and the cached Jacobian \(\frac{\partial P}{\partial \vec{q}}\) depend on it.

To keep a modifiable transformation (or parametrization), give a copy: ReparametrizedTransformation(transformation.copy(), parametrization).

A transformation or a parametrization that is already locked (for example by another reparametrized transformation) is refused. If one of them is unlocked from outside (with the internal method _set_locked), the reparametrized transformation refuses any later modification of \(\vec{q}\).

Note

Locking the reparametrized transformation itself (by an owner of the package) only locks its parameters \(\vec{q}\): the transformation and the parametrization stay locked in any case. A copy (copy) is unlocked and owns copies of the transformation and of the parametrization.

Note

The capability flags are derived from the flags of the transformation and the parametrization. The Jacobians with respect to the parameters are only available if the parametrization supports encode with Jacobian (has_encode_dp).

Parameters:
  • transformation (Transformation) – The transformation \(T\) to reparametrize. The number of parameters of the transformation must match the output dimension of the parametrization. The transformation is not copied: it must not be locked, and it is locked permanently (see above).

  • parametrization (Parametrization) – The parametrization \(P\) mapping the new parameters \(\vec{q}\) to the parameters \(\vec{p}\) of the transformation. The parametrization must implement encode. The parametrization is not copied: it must not be locked, and it is locked permanently (see above).

  • parameters (Optional[ArrayLike], optional (default: None)) – A 1D array of shape \((n_q,)\) containing the new parameters \(\vec{q}\), with \(n_q\) the input dimension of the parametrization. The parameters of the transformation are then replaced by \(P(\vec{q})\). If None, the parameters \(\vec{q}\) are obtained from the current parameters of the transformation (see below).

Initialization of the parameters#

  • If parameters is given, \(\vec{q}\) is set to it and the parameters of the transformation are replaced by \(\vec{p} = P(\vec{q})\).

  • If parameters is None and the parameters \(\vec{p}\) of the transformation are set, the parameters \(\vec{q} = P^{-1}(\vec{p})\) are obtained with the decode method of the parametrization. The parameters \(\vec{p}\) must be in the image of \(P\), i.e. \(P(P^{-1}(\vec{p})) = \vec{p}\), otherwise a ValueError is raised to avoid silently modifying the parameters of the transformation. If the parametrization does not implement decode, a ValueError is raised too: the parameters of the transformation are never discarded silently.

  • If parameters is None and the parameters of the transformation are not set, the reparametrized transformation is not set up either (set its parameters later).

Examples

Optimize only the translation along \(x\) of a transformation with parameters \((t_x, t_y)\), the translation along \(y\) being fixed:

transformation = ScaledTranslation2D(parameters=[1.0, 2.0])
parametrization = SubsetParametrization(
    mask=[True, False],
    fixed_values=[0.0, 2.0],  # The value of t_y, and 0.0 for the optimized t_x
)

reparametrized = ReparametrizedTransformation(transformation, parametrization)
reparametrized.parameters                     # array([1.])
reparametrized.parameters = [5.0]
transformation.parameters                     # array([5., 2.]) (the same object)
transformation.parameters = [0.0, 0.0]        # RuntimeError: locked by the reparametrized transformation

Implemented methods and attributes#

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

Attributes#

The pysdic.ReparametrizedTransformation defines or redefines the following attributes:

ReparametrizedTransformation.input_dim

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

ReparametrizedTransformation.n_parameters

[Get] the number of parameters \(n_q\) of the reparametrized transformation, i.e. the input dimension of the parametrization.

ReparametrizedTransformation.output_dim

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

ReparametrizedTransformation.parametrization

[Get] the parametrization \(P\) mapping the new parameters \(\vec{q}\) to the parameters \(\vec{p}\) of the transformation.

ReparametrizedTransformation.transformation

[Get] the underlying transformation \(T\).

Methods#

The pysdic.ReparametrizedTransformation redefines the following method to update the parameters \(\vec{p} = P(\vec{q})\) of the underlying transformation:

ReparametrizedTransformation.set_parameters(...)

Set the parameters \(\vec{q}\) of the reparametrized transformation, with an option to override the lock.

The pysdic.ReparametrizedTransformation implements the following backend methods to apply and invert the reparametrized transformation:

ReparametrizedTransformation._apply(x_i, *)

Apply the transformation \(T\) to the input points \(\vec{x_i}\) with the parameters \(\vec{p} = P(\vec{q})\).

ReparametrizedTransformation._invert(x_o, *)

Apply the inverse map \(T^{-1}\) to the output points \(\vec{x_o}\) with the parameters \(\vec{p} = P(\vec{q})\).