.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_landmark_shift.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_shift.py: Landmark shift ============== This example shows how to shift functional data objects to align its samples with a particular reference point. .. GENERATED FROM PYTHON SOURCE LINES 8-20 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 2 import matplotlib.pyplot as plt import numpy as np import skfda .. GENERATED FROM PYTHON SOURCE LINES 21-27 We will use an example dataset synthetically generated by :func:`~skfda.datasets.make_multimodal_samples`, which in this case will be used to generate gaussian-like samples with a mode near to 0. Each sample will be shifted to align their modes to a reference point using the function :func:`~skfda.preprocessing.registration.landmark_shift_registration`. .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. code-block:: Python fd = skfda.datasets.make_multimodal_samples(random_state=1) fd.extrapolation = 'bounds' #  See extrapolation for a detailed explanation. fd.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_shift_001.png :alt: plot landmark shift :srcset: /auto_examples/images/sphx_glr_plot_landmark_shift_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 34-50 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 [RaSi2005-2]_. For alignment we need to know in advance the location of the landmark of each of the samples, in our case it will correspond to the maxima 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 50-58 .. code-block:: Python landmarks = skfda.datasets.make_multimodal_landmarks(random_state=1).squeeze() fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(landmarks, np.repeat(1, fd.n_samples)) fd.plot(fig=fig) .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_shift_002.png :alt: plot landmark shift :srcset: /auto_examples/images/sphx_glr_plot_landmark_shift_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 59-60 Location of the landmarks: .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python print(landmarks) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0.36321467 -0.13679289 -0.11810279 -0.23992308 0.19351103 -0.5146397 0.39015177 -0.17021104 0.07133931 -0.05576091 0.32693727 -0.46066147 -0.07209468 -0.08587716 0.25351855] .. GENERATED FROM PYTHON SOURCE LINES 64-66 The following figure shows the result of shifting the curves to align their landmarks at 0. .. GENERATED FROM PYTHON SOURCE LINES 66-76 .. code-block:: Python fd_registered = skfda.preprocessing.registration.landmark_shift_registration( fd, landmarks, location=0, ) fig = fd_registered.plot() fig.axes[0].scatter(0, 1) .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_shift_003.png :alt: plot landmark shift :srcset: /auto_examples/images/sphx_glr_plot_landmark_shift_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 77-83 In many circumstances it is possible that we could not apply extrapolation, in these cases it is possible to restrict the domain to avoid evaluating points outside where our curves are defined. If the location of the new reference point is not specified it is choosen the point that minimizes the maximum amount of shift. .. GENERATED FROM PYTHON SOURCE LINES 83-101 .. code-block:: Python # Curves aligned restricting the domain fd_restricted = skfda.preprocessing.registration.landmark_shift_registration( fd, landmarks, restrict_domain=True, ) # Curves aligned to default point without restrict domain fd_extrapolated = skfda.preprocessing.registration.landmark_shift_registration( fd, landmarks, ) fig = fd_extrapolated.plot(linestyle='dashed', label='Extrapolated samples') fd_restricted.plot(fig=fig, label="Restricted samples") .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_shift_004.png :alt: plot landmark shift :srcset: /auto_examples/images/sphx_glr_plot_landmark_shift_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 102-106 The previous method is also applicable for multidimensional objects, without limitation of the domain or image dimension. As an example we are going to create a datset with surfaces, in a similar way to the previous case. .. GENERATED FROM PYTHON SOURCE LINES 106-116 .. code-block:: Python fd = skfda.datasets.make_multimodal_samples( n_samples=3, points_per_dim=30, dim_domain=2, random_state=1, ) fd.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_shift_005.png :alt: plot landmark shift :srcset: /auto_examples/images/sphx_glr_plot_landmark_shift_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 117-118 In this case the landmarks will be defined by tuples with 2 coordinates. .. GENERATED FROM PYTHON SOURCE LINES 118-126 .. code-block:: Python landmarks = skfda.datasets.make_multimodal_landmarks( n_samples=3, dim_domain=2, random_state=1, ).squeeze() print(landmarks) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 0.36321467 -0.13679289] [-0.11810279 -0.23992308] [ 0.19351103 -0.5146397 ]] .. GENERATED FROM PYTHON SOURCE LINES 127-130 As in the previous case, we can align the curves to a specific point, or by default will be chosen the point that minimizes the maximum amount of displacement. .. GENERATED FROM PYTHON SOURCE LINES 130-140 .. code-block:: Python fd_registered = skfda.preprocessing.registration.landmark_shift_registration( fd, landmarks, ) fd_registered.plot() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_landmark_shift_006.png :alt: plot landmark shift :srcset: /auto_examples/images/sphx_glr_plot_landmark_shift_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-143 .. [RaSi2005-2] Ramsay, J., Silverman, B. W. (2005). Functional Data Analysis. Springer. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.216 seconds) .. _sphx_glr_download_auto_examples_plot_landmark_shift.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_shift.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_landmark_shift.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_landmark_shift.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_