.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/generic_algorithms/base_dtw_generic.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_generic_algorithms_base_dtw_generic.py: BaseDtw simple segmentation =========================== This example illustrates how subsequent DTW implemented by the :class:`~gaitmap.stride_segmentation.BaseDtw` can be used to find multiple matches of a sequence in a longer sequence. This can be used to segment the larger signal into smaller pieces for further processing. *This example is adapted based on the sDTW example of tslearn* .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: default import matplotlib.pyplot as plt import numpy import numpy as np numpy.random.seed(0) .. GENERATED FROM PYTHON SOURCE LINES 19-25 Creating some example data -------------------------- As this is just a simple example, we will generate some example data. The short sequence (used as template) will be repeated 5 times to form the large sequence For this example we assume that the sampling rate of all signals is 100 Hz .. GENERATED FROM PYTHON SOURCE LINES 25-44 .. code-block:: default sampling_rate_hz = 100 n_repeat = 5 sequence_length = 200 dataset = (np.sin(np.linspace(0, 2 * np.pi, sequence_length)) + np.random.normal(size=200) * 0.1).reshape(-1, 1) dataset_scaled = dataset / np.std(dataset) # We repeat the long sequence multiple times to generate multiple possible # matches long_sequence = numpy.tile(dataset_scaled, (n_repeat, 1)) short_sequence = dataset_scaled sz1 = len(long_sequence) sz2 = len(short_sequence) print(f"Shape long sequence: {long_sequence.shape}") print(f"Shape short sequence: {short_sequence.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Shape long sequence: (1000, 1) Shape short sequence: (200, 1) .. GENERATED FROM PYTHON SOURCE LINES 45-46 Plot the sequences .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: default plt.figure(1, figsize=(6, 3)) plt.plot(long_sequence, label="Long Sequence") plt.plot(short_sequence, label="Short Sequence") plt.legend() plt.show() .. image-sg:: /auto_examples/generic_algorithms/images/sphx_glr_base_dtw_generic_001.png :alt: base dtw generic :srcset: /auto_examples/generic_algorithms/images/sphx_glr_base_dtw_generic_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-59 Creating a template ------------------- To use `BaseDtw` we first need to create a template. The easiest way is to use the `create_dtw_template` helper function. We pass the data of the short sequence as the template data. .. GENERATED FROM PYTHON SOURCE LINES 59-67 .. code-block:: default from gaitmap.stride_segmentation import DtwTemplate template = DtwTemplate(data=short_sequence, sampling_rate_hz=sampling_rate_hz) print(template.get_data().shape) print(template.sampling_rate_hz) .. rst-class:: sphx-glr-script-out .. code-block:: none (200, 1) 100 .. GENERATED FROM PYTHON SOURCE LINES 68-73 Using BaseDtw ------------- With the created template we can initialize the BaseDtw. Additionally, we set a set of thresholds that help to prevent false positive matches. Note that these thresholds are adapted for this specific example and need to be modified for a different dataset. .. GENERATED FROM PYTHON SOURCE LINES 73-78 .. code-block:: default from gaitmap.stride_segmentation import BaseDtw dtw = BaseDtw(template, min_match_length_s=0.6 * sequence_length / sampling_rate_hz, max_cost=3) .. GENERATED FROM PYTHON SOURCE LINES 79-81 In a second step we apply the dtw to the long sequence Afterwards a set of results are available on the dtw object .. GENERATED FROM PYTHON SOURCE LINES 81-86 .. code-block:: default dtw = dtw.segment(long_sequence, sampling_rate_hz=sampling_rate_hz) print(f"{len(dtw.matches_start_end_)} matches were found") print(dtw.matches_start_end_) .. rst-class:: sphx-glr-script-out .. code-block:: none 5 matches were found [[ 0 200] [200 400] [400 600] [600 800] [800 995]] .. GENERATED FROM PYTHON SOURCE LINES 87-89 Finally we can plot the results. This plot shows the cost matrix and the individual match paths .. GENERATED FROM PYTHON SOURCE LINES 89-128 .. code-block:: default cost_matrix = dtw.acc_cost_mat_ paths = dtw.paths_ plt.figure(1, figsize=(6 * n_repeat, 6)) # definitions for the axes left, bottom = 0.01, 0.1 h_ts = 0.2 w_ts = h_ts / n_repeat left_h = left + w_ts + 0.02 width = height = 0.65 bottom_h = bottom + height + 0.02 rect_s_y = [left, bottom, w_ts, height] rect_gram = [left_h, bottom, width, height] rect_s_x = [left_h, bottom_h, width, h_ts] ax_gram = plt.axes(rect_gram) ax_s_x = plt.axes(rect_s_x) ax_s_y = plt.axes(rect_s_y) ax_gram.imshow(numpy.sqrt(cost_matrix)) ax_gram.axis("off") ax_gram.autoscale(False) # Plot the paths for path in paths: ax_gram.plot([j for (i, j) in path], [i for (i, j) in path], "w-", linewidth=3.0) ax_s_x.plot(numpy.arange(sz1), long_sequence, "b-", linewidth=3.0) ax_s_x.axis("off") ax_s_x.set_xlim((0, sz1 - 1)) ax_s_y.plot(-short_sequence, numpy.arange(sz2)[::-1], "b-", linewidth=3.0) ax_s_y.axis("off") ax_s_y.set_ylim((0, sz2 - 1)) plt.show() .. image-sg:: /auto_examples/generic_algorithms/images/sphx_glr_base_dtw_generic_002.png :alt: base dtw generic :srcset: /auto_examples/generic_algorithms/images/sphx_glr_base_dtw_generic_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.428 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_generic_algorithms_base_dtw_generic.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: base_dtw_generic.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: base_dtw_generic.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_