pycvcam.build_optimize_parameters_RJ_functions#

build_optimize_parameters_RJ_functions(seq_transforms, seq_chains, seq_inputs, seq_outputs, seq_parameters=None, seq_kwargs=None, seq_masks=None, filter_nans=False, sparse=False, _save_length=1)[source]#

Build the residual and Jacobian functions for the least squares optimization. The residual function computes the residuals between the transformed input points and the output points for each chain, while respecting the structure of the chains and the masks for the parameters.

Lets \((T_0, T_1, ..., T_{N_T-1})\) be a tuple of \(N_T\) Transform objects, and \((C_0, C_1, ..., C_{N_C-1})\) be a tuple of \(N_C\) chains of transformations.

A chain \(C_i\) is defined as a tuple of indices corresponding to the transformations in the chain. For example:

C_0 = (1, 4, 8) -----> C_0(X) = T_8 ∘ T_4 ∘ T_1(X)

For each chain \(C_i\), we have a set of input points \(X_i\) and output points \(Y_i\) we can build a residual function \(R_i\) and a Jacobian function \(J_i\) as follows:

\[R_i(p) = Y_i - C_i(X_i; p)\]
\[J_i(p) = -\frac{\partial C_i(X_i; p)}{\partial p} = \frac{\partial R_i(p)}{\partial p}\]

where \(p\) is the vector of parameters of the transformations in the chain, which is a subset of the parameters of all the transformations in \((T_0, T_1, ..., T_{N_T-1})\) defined by the masks.

Then we can build the full residual function \(R\) and Jacobian function \(J\) for the least squares optimization by assembling the residuals and Jacobians of each chain, while respecting the structure of the chains and the masks for the parameters.

Note

To create the functions for an unique transformation, simply give a single transformation in seq_transforms and a single chain with the index 0 in seq_chains, with the corresponding input and output points in seq_inputs and seq_outputs.

After creating the functions, simply pass them to the least squares optimization of the pysolvegn package, or to any other least squares optimization method that accepts residual and Jacobian functions.

Important

The initial parameters for solve_least_squares_gauss_newton() should be the concatenation of the parameters of the transformations in seq_transforms masked by seq_masks and ordered according to the order of the transformations.

parameters = numpy.concatenate(
    [
        seq_parameters[i][seq_masks[i]] for i in range(len(seq_transforms))
    ]
)
Parameters:
  • seq_transforms (Sequence[Transform]) –

    A sequence of Transform objects to be optimized.

  • seq_chains (Sequence[Sequence[int]]) – A sequence of chains, where each chain is a sequence of indices corresponding to the transformations in seq_transforms that are part of that chain. The order of the indices in each chain defines the order of the transformations in the chain.

  • seq_inputs (Sequence[ArrayLike]) – A sequence of input points for each chain, where each element is a numpy array of shape (n_points, input_dim) containing the input points for that chain.

  • seq_outputs (Sequence[ArrayLike]) – A sequence of output points for each chain, where each element is a numpy array of shape (n_points, output_dim) containing the output points for that chain.

  • seq_parameters (Optional[Sequence[ArrayLike]]) – A sequence of initial guess arrays for each transformation, where each element is a numpy array of shape (n_params,) containing the initial guess for the parameters of that transformation. If None, the initial parameters of the transformations will be used as the initial guess.

  • seq_masks (Optional[Sequence[ArrayLike]]) – A sequence of mask arrays for each chain, where each element is a numpy array of shape (n_params,) containing boolean values indicating which parameters of the transformations in that chain are being optimized (True) and which are kept fixed (False). If None, all parameters of the transformations will be optimized.

  • seq_kwargs (Sequence[Dict]) – A sequence of dictionaries containing additional keyword arguments to be passed to the _transform method of each transformation during the computation of the residuals and Jacobian. Each element in the sequence corresponds to a transformation in seq_transforms, and the dictionary contains the keyword arguments for that transformation. If None, no additional keyword arguments will be passed to the _transform method of the transformations.

  • filter_nans (bool) – If True, the function will filter out any NaN or infinite values in the residuals and Jacobian by setting them to zero. This can help to prevent the optimization from diverging due to invalid values, but it may also affect the convergence of the optimization if there are many NaN or infinite values.

  • sparse (bool, optional) – If True, the Jacobian is treated as a sparse matrix for the computation and assembly. Default is False.

  • _save_length (int, optional) – The number of previous parameter sets, residuals, and Jacobians to save for quick retrieval if the same parameters are encountered again. This can help to speed up the optimization if the optimization process encounters the same parameters multiple times, but it also increases the memory usage. Default is 1, which means only the last set of parameters, residuals, and Jacobian is saved.

Returns:

  • R_function (Callable) – A function that takes a parameter vector as input and returns the residuals vector.

  • J_function (Callable) – A function that takes a parameter vector as input and returns the Jacobian matrix.

Return type:

Tuple[ndarray, Callable[[ndarray], ndarray], Callable[[ndarray], ndarray | spmatrix]]