.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/preprocessing/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_preprocessing_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-14 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 15-21 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 21-32 .. code-block:: Python import matplotlib.pyplot as plt from skfda.datasets import make_multimodal_samples fd = make_multimodal_samples(random_state=1) fd.extrapolation = "bounds" # See extrapolation for a detailed explanation. fd.plot() plt.show() .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_001.png :alt: plot landmark shift :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 33-49 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\ :footcite:p:`ramsay+silverman_2005`. 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 49-61 .. code-block:: Python import numpy as np from skfda.datasets import make_multimodal_landmarks landmarks = make_multimodal_landmarks(random_state=1).squeeze() fig, ax = plt.subplots() ax.scatter(landmarks, np.repeat(1, fd.n_samples)) fd.plot(fig=fig) plt.show() .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_002.png :alt: plot landmark shift :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-63 Location of the landmarks: .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. 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 67-69 The following figure shows the result of shifting the curves to align their landmarks at 0. .. GENERATED FROM PYTHON SOURCE LINES 69-82 .. code-block:: Python from skfda.preprocessing.registration import landmark_shift_registration fd_registered = landmark_shift_registration( fd, landmarks, location=0, ) fig = fd_registered.plot() fig.axes[0].scatter(0, 1) plt.show() .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_003.png :alt: plot landmark shift :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-89 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 89-108 .. code-block:: Python # Curves aligned restricting the domain fd_restricted = landmark_shift_registration( fd, landmarks, restrict_domain=True, ) # Curves aligned to default point without restrict domain fd_extrapolated = landmark_shift_registration( fd, landmarks, ) fig = fd_extrapolated.plot(linestyle="dashed", label="Extrapolated samples") fd_restricted.plot(fig=fig, label="Restricted samples") plt.show() .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_004.png :alt: plot landmark shift :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-113 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 113-124 .. code-block:: Python fd = make_multimodal_samples( n_samples=3, points_per_dim=30, dim_domain=2, random_state=1, ) fd.plot() plt.show() .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_005.png :alt: plot landmark shift :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 125-126 In this case the landmarks will be defined by tuples with 2 coordinates. .. GENERATED FROM PYTHON SOURCE LINES 126-134 .. code-block:: Python landmarks = 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 135-138 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 138-147 .. code-block:: Python fd_registered = landmark_shift_registration( fd, landmarks, ) fd_registered.plot() plt.show() .. image-sg:: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_006.png :alt: plot landmark shift :srcset: /auto_examples/preprocessing/images/sphx_glr_plot_landmark_shift_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 148-149 .. footbibliography:: .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.492 seconds) .. _sphx_glr_download_auto_examples_preprocessing_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/preprocessing/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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_landmark_shift.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_