Part 3: Output System#

This implementation demonstrates the creation of an Output System for the Virtual Hand Interface. It processes predictions (classification or regression) and communicates them to the visual interface.

Steps:#

  1. Step 1: Class Initialization and Validation
    • Ensures the correct visual interface is selected and initializes helper objects.

  2. Step 2: Processing Predictions
    • Converts classification and regression predictions into an appropriate format.

  3. Step 3: Sending Predictions
    • Sends processed predictions to the visual interface via output signals.

  4. Step 4: Handling Close Events
    • Cleans up resources and handles the exit process gracefully.

Predefined Mapping for Classification Predictions#

This dictionary maps classification labels to their respective predefined output strings for the Virtual Hand Interface.

PREDICTION2INTERFACE_MAP = {
    -1: "Rejected Sample",
    0: "[0, 0, 0, 0, 0, 0, 0, 0, 0]",
    1: "[0, 0, 1, 0, 0, 0, 0, 0, 0]",
    2: "[1, 0, 0, 0, 0, 0, 0, 0, 0]",
    3: "[0, 0, 0, 1, 0, 0, 0, 0, 0]",
    4: "[0, 0, 0, 0, 1, 0, 0, 0, 0]",
    5: "[0, 0, 0, 0, 0, 1, 0, 0, 0]",
    6: "[0.67, 1, 1, 1, 1, 1, 0, 0, 0]",
    7: "[0.45, 1, 0.6, 0, 0, 0, 0, 0, 0]",
    8: "[0.55, 1, 0.65, 0.65, 0, 0, 0, 0, 0]",
}

Step 1: Class Initialization#

This step ensures that the selected visual interface is the Virtual Hand Interface. It also establishes the connection for outgoing signals using the main window.

Output System Initialization#
 1from typing import Any
 2
 3from myogestic.gui.widgets.logger import LoggerLevel
 4from myogestic.gui.widgets.templates.output_system import OutputSystemTemplate
 5from myogestic.gui.widgets.visual_interfaces.virtual_hand_interface.setup_interface import (
 6    VirtualHandInterface_SetupInterface,
 7)
 8
 9PREDICTION2INTERFACE_MAP = {
10    -1: "Rejected Sample",
11    0: "[0, 0, 0, 0, 0, 0, 0, 0, 0]",
12    1: "[0, 0, 1, 0, 0, 0, 0, 0, 0]",
13    2: "[1, 0, 0, 0, 0, 0, 0, 0, 0]",
14    3: "[0, 0, 0, 1, 0, 0, 0, 0, 0]",
15    4: "[0, 0, 0, 0, 1, 0, 0, 0, 0]",
16    5: "[0, 0, 0, 0, 0, 1, 0, 0, 0]",
17    6: "[0.67, 1, 1, 1, 1, 1, 0, 0, 0]",
18    7: "[0.45, 1, 0.6, 0, 0, 0, 0, 0, 0]",
19    8: "[0.55, 1, 0.65, 0.65, 0, 0, 0, 0, 0]",
20}
21
22
23class VirtualHandInterface_OutputSystem(OutputSystemTemplate):
24    """Output system for the Virtual Hand Interface.
25
26    Parameters
27    ----------
28    main_window : MainWindow
29        The main window object.
30    prediction_is_classification : bool
31        Whether the prediction is a classification or regression.
32    """
33
34    def __init__(self, main_window, prediction_is_classification: bool) -> None:
35        super().__init__(main_window, prediction_is_classification)
36
37        if self._main_window.selected_visual_interface is None:
38            self._main_window.logger.print(
39                "No visual interface selected.", level=LoggerLevel.ERROR
40            )
41            raise ValueError("No visual interface selected.")
42
43        if not isinstance(
44            self._main_window.selected_visual_interface.setup_interface_ui,
45            VirtualHandInterface_SetupInterface,
46        ):
47            raise ValueError(
48                "The virtual interface must be the Virtual Hand Interface."
49                f"Got {type(self._main_window.selected_visual_interface)}."
50            )
51
52        self._outgoing_message_signal = (
53            self._main_window.selected_visual_interface.outgoing_message_signal
54        )
55

Step 2: Processing Predictions#

This step defines how predictions (either classification or regression) are processed into the appropriate format that can be used by the Virtual Hand Interface.

Processing Classification Predictions - Convert classification predictions into the required format to send them to the visual interface.#
56    def _process_prediction__classification(self, prediction: Any) -> bytes:
57        """Process the prediction for classification."""
58        return PREDICTION2INTERFACE_MAP[prediction].encode("utf-8")
Processing Regression Predictions - Convert regression predictions into the required format to send them to the visual interface.#
60    def _process_prediction__regression(self, prediction: Any) -> bytes:
61        """Process the prediction for regression."""
62        return str(prediction).encode("utf-8")

Step 3: Sending Predictions#

This step sends the processed predictions (after formatting them into bytes) to the connected visual interface via the outgoing signal.

Sending Predictions - Send the processed prediction to the visual interface.#
64    def send_prediction(self, prediction: Any) -> None:
65        """Send the prediction to the visual interface."""
66        self._outgoing_message_signal.emit(self.process_prediction(prediction))

Step 4: Handling Close Events#

This step handles the cleanup process when the Output System is closed. It logs the closure and ensures that resources are properly released.

Handling Close Events - Clean up resources and handle the exit process gracefully. If needed, this method can be extended to include additional cleanup steps.#
68    def close_event(self, event):
69        """Close the output system."""
70        pass

Total running time of the script: (0 minutes 0.131 seconds)

Estimated memory usage: 528 MB

Gallery generated by Sphinx-Gallery