.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/1_understanding_basics.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_1_understanding_basics.py: Package basics ============== This example is a brief introduction to the basic functionalities of the package. .. GENERATED FROM PYTHON SOURCE LINES 10-14 Loading data ------------ First we load the EMG example data and convert it to a DocOctoPy Data object. The Data object is the main object in the package and is used to store the data and apply filters to it. .. GENERATED FROM PYTHON SOURCE LINES 14-24 .. code-block:: Python import pickle as pkl from doc_octopy.datatypes import EMGData emg_data = {} with open("data/emg.pkl", "rb") as f: for k, v in pkl.load(f).items(): emg_data[k] = EMGData(v, sampling_frequency=2044) print(emg_data) .. rst-class:: sphx-glr-script-out .. code-block:: none {'1': EMGData; Sampling frequency: 2044 Hz; (0) Input (320, 20440), '2': EMGData; Sampling frequency: 2044 Hz; (0) Input (320, 20440)} .. GENERATED FROM PYTHON SOURCE LINES 25-29 Looking at one specific task for simplicity ------------------------------------------- The example data contains EMG from two different tasks labeled as "1" and "2". In the following we will only look at task one to explain the filtering functionalities. .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python task_one_data = emg_data["1"] print(task_one_data) .. rst-class:: sphx-glr-script-out .. code-block:: none -- EMGData Sampling frequency: 2044 Hz (0) Input (320, 20440) -- .. GENERATED FROM PYTHON SOURCE LINES 34-39 Understanding the saving format ------------------------------- The EMGData object has a input_data attribute that stores the raw data. .. note:: The raw data is stored as a dictionary where the keys are "data" and "filter_sequence". The "data" key stores the raw data and the "filter_sequence" key stores the filter sequence applied to the data. For the raw data the filter sequence is always "Raw". However, when filters are applied the filter sequence is updated. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python print(task_one_data.input_data) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 53 63 74 ... -160 -173 -116] [ -20 -8 12 ... -194 -198 -128] [ -17 -4 14 ... -180 -187 -116] ... [ -89 -100 -133 ... -200 -234 -212] [ -27 -25 -53 ... -156 -203 -210] [ -11 -7 -27 ... -143 -200 -216]] .. GENERATED FROM PYTHON SOURCE LINES 42-45 Plotting the raw data --------------------- We can plot the raw data using matplotlib. .. GENERATED FROM PYTHON SOURCE LINES 45-67 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np raw_emg = task_one_data.input_data # set plt font size plt.rcParams.update({"font.size": 14}) for channel in range(raw_emg.shape[0]): plt.plot(raw_emg[channel], color="black", alpha=0.1) plt.title("Raw EMG data") plt.ylabel("Amplitude (a. u.)") plt.xticks( np.arange(0, raw_emg.shape[-1] + 1, 2044).astype(int), np.arange(0, raw_emg.shape[-1] / 2044 + 1, 1).astype(int), ) plt.xlabel("Time (s)") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_1_understanding_basics_001.png :alt: Raw EMG data :srcset: /auto_examples/images/sphx_glr_1_understanding_basics_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-75 Attributes of the EMGData object -------------------------------- Any Data object, of which EMGData is inheriting from, posses a processed_representations attribute where filtered data will be stored. .. note :: We refer to a filtered data as a representation. At the beginning this attribute is empty. .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python print(task_one_data.processed_representations) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Input': array([[ 53, 63, 74, ..., -160, -173, -116], [ -20, -8, 12, ..., -194, -198, -128], [ -17, -4, 14, ..., -180, -187, -116], ..., [ -89, -100, -133, ..., -200, -234, -212], [ -27, -25, -53, ..., -156, -203, -210], [ -11, -7, -27, ..., -143, -200, -216]], dtype=int16)} .. GENERATED FROM PYTHON SOURCE LINES 78-82 Applying a filter ----------------- The EMGData object has a method called apply_filter that applies a filter to the data. For example, we can apply a 4th order 20 HZ lowpass filter to the data. .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: Python from scipy.signal import butter from doc_octopy.datasets.filters.temporal import SOSFrequencyFilter sos_filter_coefficients = butter(4, 20, "lowpass", output="sos", fs=2044) .. GENERATED FROM PYTHON SOURCE LINES 88-93 Creating the filter ------------------- Each filter has a parameter input_is_chunked that specifies if the input data is chunked or not. This must be set explicitly as some filters can only be used on either chunked or non-chunked data. Further having the user specify this parameter forces them to think about the data they are working with. .. GENERATED FROM PYTHON SOURCE LINES 93-98 .. code-block:: Python lowpass_filter = SOSFrequencyFilter( sos_filter_coefficients, is_output=True, name="Lowpass" ) print(lowpass_filter) .. rst-class:: sphx-glr-script-out .. code-block:: none Lowpass (SOSFrequencyFilter) .. GENERATED FROM PYTHON SOURCE LINES 99-102 Applying the filter ------------------- To apply the filter we call the apply_filter method on the EMGData object. .. GENERATED FROM PYTHON SOURCE LINES 102-108 .. code-block:: Python task_one_data.apply_filter( lowpass_filter, representation_to_filter="Last" ) print(task_one_data.processed_representations) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Input': array([[ 53, 63, 74, ..., -160, -173, -116], [ -20, -8, 12, ..., -194, -198, -128], [ -17, -4, 14, ..., -180, -187, -116], ..., [ -89, -100, -133, ..., -200, -234, -212], [ -27, -25, -53, ..., -156, -203, -210], [ -11, -7, -27, ..., -143, -200, -216]], dtype=int16), 'Lowpass': array([[ 30.41515398, 28.55359452, 26.67929727, ..., -21.69369799, -21.72851447, -21.75680579], [ -45.92706148, -45.11724327, -44.3360931 , ..., -31.30362556, -31.32936372, -31.35041695], [ -43.78353654, -42.16849119, -40.58739862, ..., -5.6463695 , -5.66186106, -5.67475859], ..., [-110.89048115, -104.30581101, -97.80368913, ..., -15.93949333, -15.93448988, -15.93147574], [ -56.62476067, -49.13379821, -41.74402497, ..., 42.30042609, 42.27745456, 42.25766496], [ -39.179818 , -32.0000522 , -24.91885117, ..., 51.63322883, 51.606299 , 51.58321195]])} .. GENERATED FROM PYTHON SOURCE LINES 109-120 Accessing the filtered data --------------------------- The filtered data is saved in the processed_data attribute of the EMGData object. Processed_data is a dictionary where the keys are the names of the filters sequence applied to the data. The key of the last filter sequence always is marked with "(Output)". This is the data that will be outputted by the dataset pipeline. In our example the key is (Output) Raw->SOSFrequencyFilter. The data can be accessed by indexing the processed_data attribute or by indexing the EMGData directly. In case you do not want to index using the filter sequence name, you can retrieve the last processed data by indexing with "Last". .. GENERATED FROM PYTHON SOURCE LINES 120-129 .. code-block:: Python print(task_one_data) print( np.allclose( task_one_data.processed_representations["Lowpass"], task_one_data["Last"], ) ) .. rst-class:: sphx-glr-script-out .. code-block:: none -- EMGData Sampling frequency: 2044 Hz (0) Input (320, 20440) Filter(s): (1 | 1) (Output) Lowpass (320, 20440) -- True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.183 seconds) **Estimated memory usage:** 553 MB .. _sphx_glr_download_auto_examples_1_understanding_basics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 1_understanding_basics.ipynb <1_understanding_basics.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 1_understanding_basics.py <1_understanding_basics.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 1_understanding_basics.zip <1_understanding_basics.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_