.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_correlation_warping/plot_07_episodes.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_06_correlation_warping_plot_07_episodes.py: Comparing qualitative trends: episode alignment =============================================== Balaskó, Bankó and Abonyi (2007) describe a signal by its sequence of triangular episodes (concave/convex increase and decrease, steady and linear parts) and compare two signals by aligning their episode strings with the Needleman--Wunsch algorithm and a primitive substitution table. Weighting the episodes by their size makes small wiggles count less. The alignment works on a handful of symbols, so it is fast and insensitive to the time scale. .. GENERATED FROM PYTHON SOURCE LINES 13-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import representation as rp from mvtsa import viz from mvtsa import warping as wp t = np.linspace(0, 1, 300) rng = np.random.default_rng(4) base = np.tanh(8 * (t - 0.3)) - 0.6 * np.exp(-(((t - 0.75) / 0.07) ** 2)) signals = { "reference": base, "slower and scaled": 2.0 * np.interp(t**1.3, t, base) + 1, "noisy": base + 0.04 * rng.normal(size=300), "different": -base, } .. GENERATED FROM PYTHON SOURCE LINES 32-34 Episode strings --------------- .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python eps = {k: rp.triangular_episodes(v, sigma=8) for k, v in signals.items()} for k, e in eps.items(): print(f"{k:18s} {rp.episode_string(e)}") viz.plot_episodes(signals["noisy"], eps["noisy"], smooth=8) .. image-sg:: /auto_examples/06_correlation_warping/images/sphx_glr_plot_07_episodes_001.png :alt: plot 07 episodes :srcset: /auto_examples/06_correlation_warping/images/sphx_glr_plot_07_episodes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none reference DEAGBCDAG slower and scaled GEDEAGBCDAG noisy EDABCDAED different BFCGDABCG .. GENERATED FROM PYTHON SOURCE LINES 40-42 Alignment scores ---------------- .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: Python for k in ("slower and scaled", "noisy", "different"): score, pairs = wp.align_episodes(eps["reference"], eps[k]) print( f"reference vs {k:18s}: score {score:5.2f} / 10, " f"episode DTW {wp.episode_dtw(eps['reference'], eps[k]):.3f}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none reference vs slower and scaled : score 9.47 / 10, episode DTW 6.321 reference vs noisy : score 7.94 / 10, episode DTW 8.603 reference vs different : score 5.14 / 10, episode DTW 11.500 .. GENERATED FROM PYTHON SOURCE LINES 49-51 Parameter study: smoothing before the episode extraction -------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: Python for s in (2, 8, 20): e_ref = rp.triangular_episodes(signals["reference"], sigma=s) e_noisy = rp.triangular_episodes(signals["noisy"], sigma=s) print( f"sigma {s:2d}: {len(e_noisy):3d} episodes, " f"score {wp.align_episodes(e_ref, e_noisy)[0]:.2f}" ) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none sigma 2: 57 episodes, score 7.54 sigma 8: 9 episodes, score 7.94 sigma 20: 9 episodes, score 9.98 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.175 seconds) .. _sphx_glr_download_auto_examples_06_correlation_warping_plot_07_episodes.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/ComplexSystemsIntelligence/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/06_correlation_warping/plot_07_episodes.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_07_episodes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_07_episodes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_07_episodes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_