.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_shift_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_shift_registration.py: Shift Registration ================== Shows the use of shift registration applied to a sinusoidal process represented in a Fourier basis. .. GENERATED FROM PYTHON SOURCE LINES 8-20 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 3 import matplotlib.pyplot as plt from skfda.datasets import make_sinusoidal_process from skfda.preprocessing.registration import LeastSquaresShiftRegistration from skfda.representation.basis import FourierBasis .. GENERATED FROM PYTHON SOURCE LINES 21-29 In this example we will use a :func:`sinusoidal process ` synthetically generated. This dataset consists in a sinusoidal wave with fixed period which contanis phase and amplitude variation with gaussian noise. In this example we want to register the curves using a translation and remove the phase variation to perform further analysis. .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python fd = make_sinusoidal_process(random_state=1) fd.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_shift_registration_001.png :alt: plot shift registration :srcset: /auto_examples/images/sphx_glr_plot_shift_registration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 34-38 We will smooth the curves using a basis representation, which will help us to remove the gaussian noise. Smoothing before registration is essential due to the use of derivatives in the optimization process. Because of their sinusoidal nature we will use a Fourier basis. .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python fd_basis = fd.to_basis(FourierBasis(n_basis=11)) fd_basis.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_shift_registration_002.png :alt: plot shift registration :srcset: /auto_examples/images/sphx_glr_plot_shift_registration_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 43-50 We will use the :func:`~skfda.preprocessing.registration.LeastSquaresShiftRegistration` transformer, which is suitable due to the periodicity of the dataset and the small amount of amplitude variation. We can observe how the sinusoidal pattern is easily distinguishable once the alignment has been made. .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. code-block:: Python shift_registration = LeastSquaresShiftRegistration() fd_registered = shift_registration.fit_transform(fd_basis) fd_registered.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_shift_registration_003.png :alt: plot shift registration :srcset: /auto_examples/images/sphx_glr_plot_shift_registration_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 57-64 We will plot the mean of the original smoothed curves and the registered ones, and we will compare with the original sinusoidal process without noise. We can see how the phase variation affects to the mean of the original curves varying their amplitude with respect to the original process, however, this effect is mitigated after the registration. .. GENERATED FROM PYTHON SOURCE LINES 64-79 .. code-block:: Python # sinusoidal process without variation and noise sine = make_sinusoidal_process( n_samples=1, phase_std=0, amplitude_std=0, error_std=0, ) fig = fd_basis.mean().plot() fd_registered.mean().plot(fig) sine.plot(fig, linestyle='dashed') fig.axes[0].legend(['original mean', 'registered mean', 'sine']) .. image-sg:: /auto_examples/images/sphx_glr_plot_shift_registration_004.png :alt: plot shift registration :srcset: /auto_examples/images/sphx_glr_plot_shift_registration_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 80-83 The values of the shifts :math:`\delta_i`, stored in the attribute `deltas_` may be relevant for further analysis, as they may be considered as nuisance or random effects. .. GENERATED FROM PYTHON SOURCE LINES 83-88 .. code-block:: Python print(shift_registration.deltas_) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0.09004943 0.01808744 0.08732826 -0.00013559 -0.04950421 0.11984576 -0.09723283 -0.09330286 -0.04398832 -0.08389279 0.0583045 0.00503724 0.08788296 0.0214795 -0.042531 ] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.591 seconds) .. _sphx_glr_download_auto_examples_plot_shift_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_shift_registration.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_shift_registration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_shift_registration.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_