.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_add_functionality/3_add_biosignal_feature.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_01_add_functionality_3_add_biosignal_feature.py: ==================================== Adding a Custom Biosignal Feature ==================================== Below is an example of how to create and register a new feature by inheriting from a base filter class from `MyoVerse `_. This custom feature calculates the variance of the input signal. .. admonition:: We Recommend Implementing any Additions in *user_config.py* The *user_config.py* module is specifically designed for end-users to register and configure their own custom components such as models, features, and filters. This keeps your modifications modular, reduces conflicts with core MyoGestic settings, and simplifies upgrades in the future. .. important:: By registering your addition in ``user_config.py``, you ensure that your custom configuration stays separate from core MyoGestic functionality and remains compatible with future updates. Example Overview ---------------- 1. **Define** a new feature class inheriting from the base class. 2. **Implement** the core logic in the ``_filter`` method. 3. **Register** the feature so it becomes available globally. .. GENERATED FROM PYTHON SOURCE LINES 31-38 .. code-block:: Python import numpy as np from myogestic.utils.config import CONFIG_REGISTRY # Ensure this import references your actual base class location: from myoverse.datasets.filters._template import FilterBaseClass .. GENERATED FROM PYTHON SOURCE LINES 39-42 -------------------------------- Step 1: Inherit and define logic -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-73 .. code-block:: Python class MyVarianceFeature(FilterBaseClass): """ A feature that computes the variance of the input signal. Parameters ---------- input_is_chunked : bool, optional Indicates whether the input data is chunked. allowed_input_type : Literal["both", "chunked", "not chunked"], optional Whether the filter accepts chunked, unchunked, or both. is_output : bool, optional If True, this feature's output is set as a final output in the pipeline. """ def _filter(self, input_array: np.ndarray) -> np.ndarray: """ Compute the variance of the input signal. Parameters ---------- input_array : np.ndarray The data on which variance is computed. Returns ------- np.ndarray The variance output as a single-element array, for consistency. """ return np.array([np.var(input_array)], dtype=float) .. GENERATED FROM PYTHON SOURCE LINES 74-77 ----------------------------------------------- Step 2: Register the custom feature ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python CONFIG_REGISTRY.register_feature("My Variance Feature", MyVarianceFeature) .. GENERATED FROM PYTHON SOURCE LINES 81-84 ----------------------------------------------- Example Usage ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 84-96 .. code-block:: Python if __name__ == "__main__": # Create a small signal for demonstration sample_data = np.array([1.2, 2.5, 2.7, 2.8, 3.1]) # Instantiate and apply the feature feature_instance = MyVarianceFeature( input_is_chunked=False, allowed_input_type="not chunked" ) variance_value = feature_instance(sample_data) print(f"Variance of {sample_data} = {variance_value[0]:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Variance of [1.2 2.5 2.7 2.8 3.1] = 0.4344 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.574 seconds) **Estimated memory usage:** 570 MB .. _sphx_glr_download_auto_examples_01_add_functionality_3_add_biosignal_feature.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 3_add_biosignal_feature.ipynb <3_add_biosignal_feature.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 3_add_biosignal_feature.py <3_add_biosignal_feature.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 3_add_biosignal_feature.zip <3_add_biosignal_feature.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_