.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_elastic_registration.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_elastic_registration.py: Elastic registration ==================== Shows the usage of the elastic registration to perform a groupwise alignment. .. GENERATED FROM PYTHON SOURCE LINES 7-20 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 5 import numpy as np import skfda from skfda.datasets import fetch_growth, make_multimodal_samples from skfda.exploratory.stats import fisher_rao_karcher_mean from skfda.preprocessing.registration import FisherRaoElasticRegistration .. GENERATED FROM PYTHON SOURCE LINES 21-34 In the example of pairwise alignment was shown the usage of :class:`~skfda.preprocessing.registration.FisherRaoElasticRegistration` to align a set of functional observations to a given template or a set of templates. In the groupwise alignment all the samples are aligned to the same template, constructed to minimise some distance, generally a mean or a median. In the case of the elastic registration, due to the use of the elastic distance in the alignment, one of the most suitable templates is the karcher mean under this metric. We will create a synthetic dataset to show the basic usage of the registration. .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python fd = make_multimodal_samples(n_modes=2, stop=4, random_state=1) fd.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_elastic_registration_001.png :alt: plot elastic registration :srcset: /auto_examples/images/sphx_glr_plot_elastic_registration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 39-47 The following figure shows the :func:`~skfda.exploratory.stats.fisher_rao_karcher_mean` of the dataset and the cross-sectional mean, which correspond to the karcher-mean under the :math:`\mathbb{L}^2` distance. It can be seen how the elastic mean better captures the geometry of the curves compared to the standard mean, since it is not affected by the deformations of the curves. .. GENERATED FROM PYTHON SOURCE LINES 47-53 .. code-block:: Python fig = fd.mean().plot(label="L2 mean") fisher_rao_karcher_mean(fd).plot(fig=fig, label="Elastic mean") fig.legend() .. image-sg:: /auto_examples/images/sphx_glr_plot_elastic_registration_002.png :alt: plot elastic registration :srcset: /auto_examples/images/sphx_glr_plot_elastic_registration_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 54-56 In this case, the alignment completely reduces the amplitude variability between the samples, aligning the maximum points correctly. .. GENERATED FROM PYTHON SOURCE LINES 56-64 .. code-block:: Python elastic_registration = FisherRaoElasticRegistration() fd_align = elastic_registration.fit_transform(fd) fd_align.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_elastic_registration_003.png :alt: plot elastic registration :srcset: /auto_examples/images/sphx_glr_plot_elastic_registration_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 65-73 In general these type of alignments are not possible, in the following figure it is shown how it works with a real dataset. The :func:`berkeley growth dataset` contains the growth curves of a set children, in this case will be used only the males. The growth curves will be resampled using cubic interpolation and derived to obtain the velocity curves. First we show the original curves: .. GENERATED FROM PYTHON SOURCE LINES 73-86 .. code-block:: Python growth = fetch_growth() # Select only one sex fd = growth['data'][growth['target'] == 0] # Obtain velocity curves fd.interpolation = skfda.representation.interpolation.SplineInterpolation(3) fd_derivative = fd.to_grid(np.linspace(*fd.domain_range[0], 200)).derivative() fd_derivative = fd_derivative.to_grid(np.linspace(*fd.domain_range[0], 50)) fd_derivative.dataset_name = f"{fd.dataset_name} - derivative" fd_derivative.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_elastic_registration_004.png :alt: Berkeley Growth Study - derivative :srcset: /auto_examples/images/sphx_glr_plot_elastic_registration_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 87-88 We now show the aligned curves: .. GENERATED FROM PYTHON SOURCE LINES 88-94 .. code-block:: Python fd_align = elastic_registration.fit_transform(fd_derivative) fd_align.dataset_name = f"{fd.dataset_name} - derivative aligned" fd_align.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_elastic_registration_005.png :alt: Berkeley Growth Study - derivative aligned :srcset: /auto_examples/images/sphx_glr_plot_elastic_registration_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 95-106 * Srivastava, Anuj & Klassen, Eric P. (2016). Functional and shape data analysis. In *Functional Data and Elastic Registration* (pp. 73-122). Springer. * Tucker, J. D., Wu, W. and Srivastava, A. (2013). Generative Models for Functional Data using Phase and Amplitude Separation. Computational Statistics and Data Analysis, Vol. 61, 50-66. * J. S. Marron, James O. Ramsay, Laura M. Sangalli and Anuj Srivastava (2015). Functional Data Analysis of Amplitude and Phase Variation. Statistical Science 2015, Vol. 30, No. 4 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.361 seconds) .. _sphx_glr_download_auto_examples_plot_elastic_registration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/GAA-UAM/scikit-fda/develop?filepath=examples/plot_elastic_registration.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_elastic_registration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_elastic_registration.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_