.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_landmark_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_landmark_registration.py: Landmark registration ===================== This example shows the basic usage of the landmark registration. .. GENERATED FROM PYTHON SOURCE LINES 7-16 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT import matplotlib.pyplot as plt import numpy as np import skfda .. GENERATED FROM PYTHON SOURCE LINES 17-31 The simplest curve alignment procedure is landmark registration. This method only takes into account a discrete ammount of features of the curves which will be registered. A landmark or a feature of a curve is some characteristic that one can associate with a specific argument value t. These are typically maxima, minima, or zero crossings of curves, and may be identified at the level of some derivatives as well as at the level of the curves themselves. We align the curves by transforming t for each curve so that landmark locations are the same for all curves ( [RaSi2005]_ , [RaHoGr2009]_ ). We will use a dataset synthetically generated by :func:`~skfda.datasets.make_multimodal_samples`, which in this case will be used to generate bimodal curves. .. GENERATED FROM PYTHON SOURCE LINES 31-41 .. code-block:: Python fd = skfda.datasets.make_multimodal_samples( n_samples=4, n_modes=2, std=0.002, mode_std=0.005, random_state=1, ) fd.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_registration_001.png :alt: plot landmark registration :srcset: /auto_examples/images/sphx_glr_plot_landmark_registration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 42-53 For this type of alignment we need to know in advance the location of the landmarks of each of the samples, in our case it will correspond to the two maximum points of each sample. Because our dataset has been generated synthetically we can obtain the value of the landmarks using the function :func:`~skfda.datasets.make_multimodal_landmarks`, which is used by :func:`~skfda.datasets.make_multimodal_samples` to set the location of the modes. In general it will be necessary to use numerical or other methods to determine the location of the landmarks. .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: Python landmarks = skfda.datasets.make_multimodal_landmarks( n_samples=4, n_modes=2, std=0.002, random_state=1, ).squeeze() print(landmarks) .. rst-class:: sphx-glr-script-out .. code-block:: none [[-0.2606904 0.30597475] [-0.35695389 0.28534872] [-0.29463113 0.23040539] [-0.25530298 0.29929113]] .. GENERATED FROM PYTHON SOURCE LINES 64-81 The transformation will not be linear, and will be the result of applying a warping function to the time of our curves. After the identification of the landmarks asociated with the features of each of our curves we can construct the warping function with the function :func:`~skfda.preprocessing.registration.landmark_elastic_registration_warping`. Let :math:`h_i` be the warping function corresponding with the curve :math:`i`, :math:`t_{ij}` the time where the curve :math:`i` has their feature :math:`j` and :math:`t^*_j` the new location of the feature :math:`j`. The warping functions will transform the new time in the original time of the curve, i.e., :math:`h_i(t^*_j) = t_{ij}`. These functions will be defined between landmarks using monotone cubic interpolation (see the example of interpolation for more details). In this case we will place the landmarks at -0.5 and 0.5. .. GENERATED FROM PYTHON SOURCE LINES 81-96 .. code-block:: Python warping = skfda.preprocessing.registration.landmark_elastic_registration_warping( fd, landmarks, location=[-0.5, 0.5], ) # Plots warping fig = warping.plot() # Plot landmarks for i in range(fd.n_samples): fig.axes[0].scatter([-0.5, 0.5], landmarks[i]) .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_registration_002.png :alt: plot landmark registration :srcset: /auto_examples/images/sphx_glr_plot_landmark_registration_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-100 Once we have the warping functions, the registered curves can be obtained using function composition. Let :math:`x_i` a curve, we can obtain the corresponding registered curve as :math:`x^*_i(t) = x_i(h_i(t))`. .. GENERATED FROM PYTHON SOURCE LINES 101-108 .. code-block:: Python fd_registered = fd.compose(warping) fig = fd_registered.plot() fig.axes[0].scatter([-0.5, 0.5], [1, 1]) .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_registration_003.png :alt: plot landmark registration :srcset: /auto_examples/images/sphx_glr_plot_landmark_registration_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 109-116 If we do not need the warping function we can obtain the registered curves directly using the function :func:`~skfda.preprocessing.registration.landmark_elastic_registration`. If the position of the new location of the landmarks is not specified the mean position is taken. .. GENERATED FROM PYTHON SOURCE LINES 117-129 .. code-block:: Python fd_registered = skfda.preprocessing.registration.landmark_elastic_registration( fd, landmarks, ) fd_registered.plot() plt.scatter(np.mean(landmarks, axis=0), [1, 1]) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_registration_004.png :alt: plot landmark registration :srcset: /auto_examples/images/sphx_glr_plot_landmark_registration_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 130-135 .. [RaSi2005] Ramsay, J., Silverman, B. W. (2005). Functional Data Analysis. Springer. .. [RaHoGr2009] Ramsay, J., Hooker, G. & Graves S. (2009). Functional Data Analysis with R and Matlab. Springer. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.580 seconds) .. _sphx_glr_download_auto_examples_plot_landmark_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_landmark_registration.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_landmark_registration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_landmark_registration.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_