from typing import Any
from myogestic.gui.widgets.logger import LoggerLevel
from myogestic.gui.widgets.templates.output_system import OutputSystemTemplate
from myogestic.gui.widgets.visual_interfaces.virtual_hand_interface.setup_interface import (
VirtualHandInterface_SetupInterface,
)
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]",
}
[docs]
class VirtualHandInterface_OutputSystem(OutputSystemTemplate):
"""Output system for the Virtual Hand Interface.
Parameters
----------
main_window : MainWindow
The main window object.
prediction_is_classification : bool
Whether the prediction is a classification or regression.
"""
[docs]
def __init__(self, main_window, prediction_is_classification: bool) -> None:
super().__init__(main_window, prediction_is_classification)
# Check if VHI is among the active VIs
active_vis = self._main_window.active_visual_interfaces
vi = active_vis.get("VHI")
if vi is None:
self._main_window.logger.print(
"VHI (Virtual Hand Interface) is not active.", level=LoggerLevel.ERROR
)
raise ValueError("VHI (Virtual Hand Interface) is not active.")
if not isinstance(
vi.setup_interface_ui,
VirtualHandInterface_SetupInterface,
):
raise ValueError(
"The visual interface must be the Virtual Hand Interface."
f"Got {type(vi)}."
)
self._outgoing_message_signal = vi.outgoing_message_signal
[docs]
def _process_prediction__classification(self, prediction: Any) -> bytes:
"""Process the prediction for classification."""
return PREDICTION2INTERFACE_MAP[prediction].encode("utf-8")
[docs]
def _process_prediction__regression(self, prediction: Any) -> bytes:
"""Process the prediction for regression."""
return str([float(x) for x in prediction]).encode("utf-8")
[docs]
def send_prediction(self, prediction: Any) -> None:
"""Send the prediction to the visual interface."""
processed = self.process_prediction(prediction)
self._main_window.logger.print(f"VHI sending: {processed[:50]}...") # Debug
self._outgoing_message_signal.emit(processed)
[docs]
def close_event(self, event):
"""Close the output system."""
pass