pysdic.SubsetParametrization#

SubsetParametrization class#

class SubsetParametrization(mask, fixed_values=None)[source]#

Bases: Parametrization

A parametrization \(P\) that selects a subset of the output parameters to be driven by the input parameters, the other output parameters being fixed.

Lets \(\mathcal{S} \subset \{1, \dots, n_o\}\) be the set of the selected indices defined by a boolean mask of shape \((n_o,)\), with \(n_i = |\mathcal{S}|\) the number of selected parameters. The parametrization maps the input parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\) to the output parameters \(\vec{p_o} \in \mathbb{R}^{n_o}\) according to:

\[\vec{p_o}[\mathcal{S}] = \vec{p_i} \quad \text{and} \quad \vec{p_o}[\overline{\mathcal{S}}] = \vec{v}[\overline{\mathcal{S}}]\]

where \(\vec{v}\) are the fixed values of the non-selected parameters.

This is typically used to optimize only some parameters of a transformation while the others are kept fixed (see pysdic.ReparametrizedTransformation).

The Jacobian of the parametrization is the constant selection matrix \(\mathbf{E} \in \mathbb{R}^{n_o \times n_i}\) whose columns are the columns of the identity matrix at the selected indices:

\[\mathbf{J}_p = \frac{\partial P}{\partial \vec{p_i}} = \mathbf{E} = \mathbf{I}_{n_o}[:, \mathcal{S}]\]

The parametrization can be decoded by extracting the selected parameters:

\[\vec{p_i} = P^{-1}(\vec{p_o}) = \vec{p_o}[\mathcal{S}]\]

The decoding map is a left inverse of \(P\). It is also a right inverse only if all the parameters are selected (i.e. \(P\) is the identity).

See also

Note

pysdic.SubsetParametrization is equivalent to an pysdic.AffineParametrization with modes \(\mathbf{E}\) and offset \(\vec{v}\) (with zeros at the selected indices), but it avoids dense matrix products. In particular, for any matrix \(\mathbf{J}\) with \(n_o\) columns (e.g. the Jacobian of a transformation with respect to its parameters), the chain rule reduces to a column selection:

\[\mathbf{J} \cdot \mathbf{E} = \mathbf{J}[:, \mathcal{S}]\]

which can be computed with jacobian[..., parametrization.mask].

Note

The mask and fixed_values are constants of the parametrization. They are stored as read-only copies and cannot be modified after initialization.

Parameters:
  • mask (ArrayLike) – A boolean array of shape \((n_o,)\) indicating the selected output parameters (True) driven by the input parameters. At least one parameter must be selected.

  • fixed_values (Optional[ArrayLike], optional (default: None)) – The values \(\vec{v}\) of shape \((n_o,)\) used for the non-selected output parameters. The values at the selected indices must be 0.0 (a ValueError is raised otherwise, to avoid ignoring values given by mistake). If None, the non-selected parameters are fixed to zero.

Examples

Optimize only the first and third parameters of a vector of four parameters, the second and fourth parameters being fixed:

parametrization = SubsetParametrization(
    mask=[True, False, True, False],
    fixed_values=[0.0, 5.0, 0.0, 7.0],
)

parametrization.encode([1.0, 2.0]).p              # array([1., 5., 2., 7.])
parametrization.decode([1.0, 5.0, 2.0, 7.0]).p    # array([1., 2.])

A convenient way to freeze the current values of the parameters of a transformation is to pass them as fixed_values, with zeros at the selected indices:

parametrization = SubsetParametrization(
    mask=mask,
    fixed_values=numpy.where(mask, 0.0, transformation.parameters),
)

Instantiation#

The SubsetParametrization class is instantiated directly with its constructor:

  • With a boolean mask of shape \((n_o,)\): SubsetParametrization(mask) (the non-selected parameters are fixed to zero).

  • With a boolean mask and fixed values \(\vec{v}\) of shape \((n_o,)\): SubsetParametrization(mask, fixed_values) (the fixed values must be 0.0 at the selected indices, e.g. numpy.where(mask, 0.0, transformation.parameters)).

Implemented methods and attributes#

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

Attributes#

The pysdic.SubsetParametrization defines the following attributes (the constants mask, indices and fixed_values are read-only):

SubsetParametrization.fixed_values

[Get] the values \(\vec{v}\) of the non-selected output parameters.

SubsetParametrization.indices

[Get] the indices \(\mathcal{S}\) of the selected output parameters, in increasing order.

SubsetParametrization.input_dim

[Get] the input dimension \(n_i\) of the parametrization, i.e. the number of selected output parameters.

SubsetParametrization.mask

[Get] the boolean mask of the selected output parameters.

SubsetParametrization.output_dim

[Get] the output dimension \(n_o\) of the parametrization, i.e. the size of the mask.

Encoding and decoding#

Use the encode and decode methods from pysdic.Parametrization to perform the parametrization:

  • encode: maps p_i → p_o (p_i inserted at the selected indices, the fixed values elsewhere).

  • decode: maps p_o → p_i (extraction of the selected parameters, left inverse of encode).

The Jacobian with respect to the parameters can be computed by enabling the dp flag:

  • dp=True: compute ∂p_o / ∂p_i (constant selection matrix \(\mathbf{E} = \mathbf{I}_{n_o}[:, \mathcal{S}]\))

SubsetParametrization._encode(p_i, *[, dp])

Insert the input parameters \(\vec{p_i}\) at the selected indices of the output parameters.

SubsetParametrization._decode(p_o, *[, dp])

Extract the selected parameters from the output parameters \(\vec{p_o}\).