.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_add_functionality/4_add_output_system.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_4_add_output_system.py: ============================== Add a Custom Output System ============================== This example demonstrates how to build and register a new output system in **MyoGestic**. By creating a subclass of the generic :ref:`output_system_template`, you can channel model predictions into any destination you like—such as a virtual environment or hardware device. .. 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. **Create** a custom output system class by inheriting from ``OutputSystemTemplate``. 2. **Implement** the required methods for processing and sending predictions. 3. **Register** the output system into ``CONFIG_REGISTRY``. 4. (Optional) **Test** your new output system by selecting it in your MyoGestic workflow. Notes on Implementation ----------------------- The class design follows the interface defined by :ref:`output_system_template`, requiring: - A constructor that takes in the ``main_window`` (as all QObject do) and a boolean flag for classification vs. regression. - Definitions for: - ``_process_prediction__classification(prediction: Any) -> Any`` - ``_process_prediction__regression(prediction: Any) -> Any`` - ``send_prediction(prediction: Any) -> None`` - ``closeEvent(event) -> None`` Below, we provide a simplified demonstration that mirrors how you might adapt a neural rehabilitation device interface or a virtual hand interface. For further examples of fully featured implementations, see references in MyoGestic's existing output systems. .. GENERATED FROM PYTHON SOURCE LINES 53-56 -------------------------------------- Step 1: Define Your Custom Output System -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-117 .. code-block:: Python from typing import Any from myogestic.gui.widgets.templates.output_system import OutputSystemTemplate class MyCustomOutputSystem(OutputSystemTemplate): """ A simple example output system for demonstration purposes. This class shows how to inherit from OutputSystemTemplate and implement the required abstract methods. It can send predictions to any desired interface or hardware, provided you adapt the methods below. """ def __init__(self, main_window, prediction_is_classification: bool) -> None: """ Initialize the custom output system. Parameters ---------- main_window : Any The main window object containing a logger or other references. prediction_is_classification : bool Set to True for classification tasks, False for regression. """ super().__init__(main_window, prediction_is_classification) # (Optional) Initialize sockets, signals, or other resources here def _process_prediction__classification(self, prediction: Any) -> Any: """ Convert a classification prediction into a format suitable for your output system or interface. """ # Replace with your own logic or mappings return f"Classification: {prediction}".encode("utf-8") def _process_prediction__regression(self, prediction: Any) -> Any: """ Convert a regression prediction into a format suitable for your output system or interface. """ # Replace with your own logic or mappings return f"Regression: {prediction}".encode("utf-8") def send_prediction(self, prediction: Any) -> None: """ Send the processed prediction to your output destination. """ processed = self.process_prediction(prediction) # For demonstration, we simply print the processed data self._main_window.logger.print( f"Sending: {processed}", ) def close_event(self, event) -> None: """ Handle any cleanup needed when your system closes. """ # Close sockets, timers, or other resources here pass .. GENERATED FROM PYTHON SOURCE LINES 118-121 ------------------------------------------------- Step 2: Register the Output System in CONFIG_REGISTRY ------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 121-127 .. code-block:: Python from myogestic.utils.config import CONFIG_REGISTRY CONFIG_REGISTRY.register_output_system( name="MyCustomOutputSystem", output_system=MyCustomOutputSystem ) .. GENERATED FROM PYTHON SOURCE LINES 128-134 ------------------------------- Example: Virtual Hand Interface ------------------------------- .. literalinclude:: /../../myogestic/gui/widgets/visual_interfaces/virtual_hand_interface/output_interface.py :language: python :lineno-match: .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.390 seconds) **Estimated memory usage:** 623 MB .. _sphx_glr_download_auto_examples_01_add_functionality_4_add_output_system.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 4_add_output_system.ipynb <4_add_output_system.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 4_add_output_system.py <4_add_output_system.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 4_add_output_system.zip <4_add_output_system.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_