LeastSquaresShiftRegistration#

class skfda.preprocessing.registration.LeastSquaresShiftRegistration(max_iter=5, tol=0.01, template='mean', extrapolation=None, step_size=1, restrict_domain=False, initial='zeros', grid_points=None)[source]#

Register data using shift alignment by least squares criterion.

Realizes the registration of a set of curves using a shift aligment [1]. Let \(\{x_i(t)\}_{i=1}^{N}\) be a functional dataset, calculates \(\delta_{i}\) for each sample such that \(x_i(t + \delta_{i})\) minimizes the least squares criterion:

\[\text{REGSSE} = \sum_{i=1}^{N} \int_{\mathcal{T}} [x_i(t + \delta_i) - \hat\mu(t)]^2 ds\]

Estimates each shift parameter \(\delta_i\) iteratively by using a modified Newton-Raphson algorithm, updating the template \(\mu\) in each iteration as is described in detail in [1].

Method only implemented for univariate functional data.

Parameters:
  • max_iter (int) – Maximun number of iterations. Defaults sets to 5. Generally 2 or 3 iterations are sufficient to obtain a good alignment.

  • tol (float) – Tolerance allowable. The process will stop if \(\max_{i}|\delta_{i}^{(\nu)}-\delta_{i}^{(\nu-1)}|<tol\). Default sets to 1e-2.

  • template (Union[Literal['mean'], FData, TemplateFunction]) – Template to use in the least squares criterion. If template=”mean” it is use the functional mean as in the original paper. The template can be a callable that will receive an FDataGrid with the samples and will return another FDataGrid as a template, such as any of the means or medians of the module skfda.explotatory.stats. If the template is an FData is used directly as the final template to the registration, if it is a callable or “mean” the template is computed iteratively constructing a temporal template in each iteration. In [1] is described in detail this procedure. Defaults to “mean”.

  • extrapolation (Optional[ExtrapolationLike]) – Controls the extrapolation mode for points outside the domain range. By default uses the method defined in the data to be transformed. See the extrapolation documentation to obtain more information.

  • step_size (float) – Parameter to adjust the rate of convergence in the Newton-Raphson algorithm, see [1]. Defaults to 1.

  • restrict_domain (bool) – If True restricts the domain to avoid the need of using extrapolation, in which case only the fit_transform method will be available, as training and transformation must be done together. Defaults to False.

  • initial (Union[Literal['zeros'], ArrayLike]) – Array with an initial estimation of shifts. Default uses a list of zeros for the initial shifts.

  • grid_points (Optional[GridPointsLike]) – Set of points where the functions are evaluated to obtain the discrete representation of the object to integrate. If None is passed it calls numpy.linspace in FDataBasis and uses the grid_points in FDataGrids.

Attributes:
  • template_ – Template \(\mu\) learned during the fitting used to the transformation.

  • deltas_ – List of shifts \(\delta_i\) applied during the last transformation.

  • n_iter_ – Number of iterations performed during the last transformation.

Note

Due to the use of derivatives for the estimation of the shifts, the samples to be registered may be smooth for the correct convergence of the method.

Examples

>>> from skfda.preprocessing.registration import (
...     LeastSquaresShiftRegistration,
... )
>>> from skfda.datasets import make_sinusoidal_process
>>> from skfda.representation.basis import FourierBasis

Registration and creation of dataset in discretized form:

>>> fd = make_sinusoidal_process(n_samples=10, error_std=0,
...                              random_state=1)
>>> reg = LeastSquaresShiftRegistration(extrapolation="periodic")
>>> fd_registered = reg.fit_transform(fd)
>>> fd_registered
FDataGrid(...)

Shifts applied during the transformation

>>> reg.deltas_.round(3)
array([-0.131,  0.188,  0.026,  0.033, -0.109,  0.115, ..., -0.062])

Registration of a dataset in basis form using the transformation previosly fitted. The result is a dataset in discretized form, as it is not possible to express shifted functions exactly as a basis expansion:

>>> fd = make_sinusoidal_process(n_samples=2, error_std=0,
...                              random_state=2)
>>> fd_basis = fd.to_basis(FourierBasis())
>>> reg.transform(fd_basis)
FDataGrid(...)

References

Methods

fit(X[, y])

Fit the registration model.

fit_transform(X[, y])

Fit the registration model and return the registered data.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

inverse_transform(X[, y])

Apply the inverse transformation.

score(X[, y])

Return the percentage of total variation removed.

set_output(*[, transform])

Set output container.

set_params(**params)

Set the parameters of this estimator.

transform(X[, y])

Register new data.

fit(X, y=None)[source]#

Fit the registration model.

Parameters:
  • X (FData) – Original (unregistered) training data.

  • y (object | None) – Ignored.

  • self (SelfType) –

Returns:

Returns the instance itself.

Return type:

SelfType

fit_transform(X, y=None)[source]#

Fit the registration model and return the registered data.

Parameters:
  • X (T) – Original (unregistered) training data.

  • y (object) – Ignored.

  • fit_params – Additional fit parameters.

Returns:

Registered training data.

Return type:

T

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)#

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

inverse_transform(X, y=None)[source]#

Apply the inverse transformation.

Applies the opossite shift used in the last call to transform.

Parameters:
  • X (FData) – Functional dataset to be transformed.

  • y (object | None) – not used, present for API consistency by convention.

Returns:

Functional data registered.

Return type:

FDataGrid

Examples

Creates a synthetic functional dataset.

>>> from skfda.preprocessing.registration import (
...     LeastSquaresShiftRegistration,
... )
>>> from skfda.datasets import make_sinusoidal_process
>>> fd = make_sinusoidal_process(error_std=0, random_state=1)
>>> fd.extrapolation = 'periodic'

Dataset registration and centering.

>>> reg = LeastSquaresShiftRegistration()
>>> fd_registered = reg.fit_transform(fd)
>>> fd_centered = fd_registered - fd_registered.mean()

Reverse the translation applied during the registration.

>>> reg.inverse_transform(fd_centered)
FDataGrid(...)
score(X, y=None)[source]#

Return the percentage of total variation removed.

Computes the squared multiple correlation index of the proportion of the total variation due to phase, defined as:

\[R^2 = \frac{\text{MSE}_{phase}}{\text{MSE}_{total}},\]

where \(\text{MSE}_{total}\) is the mean squared error and \(\text{MSE}_{phase}\) is the mean squared error due to the phase explained by the registration procedure. See AmplitudePhaseDecomposition for a detailed explanation.

Parameters:
  • X (Input) – Functional data to be registered

  • y (object | None) – Ignored, only for API conventions.

Returns:

Registration score.

Return type:

float

set_output(*, transform=None)#

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:

transform ({"default", "pandas"}, default=None) –

Configure output of transform and fit_transform.

  • ”default”: Default output format of a transformer

  • ”pandas”: DataFrame output

  • ”polars”: Polars output

  • None: Transform configuration is unchanged

New in version 1.4: “polars” option was added.

Returns:

self – Estimator instance.

Return type:

estimator instance

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

transform(X, y=None)[source]#

Register new data.

Parameters:
  • X (FData) – Original (unregistered) data.

  • y (object) –

Returns:

Registered data.

Return type:

FDataGrid

Examples using skfda.preprocessing.registration.LeastSquaresShiftRegistration#

Shift Registration

Shift Registration

Scikit-fda and scikit-learn

Scikit-fda and scikit-learn