Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/navigate/model/features/feature_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# Third Party Imports

# Local Imports
from navigate.model.utils.exceptions import UserVisibleException

p = __name__.split(".")[1]

Expand Down Expand Up @@ -488,7 +489,7 @@ class SignalContainer(Container):
track of the remaining executions.
"""

def __init__(self, root=None, cleanup_list=[], number_of_execution=1):
def __init__(self, root=None, cleanup_list=[], warning_queue=None, number_of_execution=1):
"""Initialize the SignalContainer object.

Parameters:
Expand All @@ -498,6 +499,8 @@ def __init__(self, root=None, cleanup_list=[], number_of_execution=1):
cleanup_list : list of TreeNode, optional
A list of nodes containing 'cleanup' functions to be executed when the
container is closed. Default is an empty list.
warning_queue : Queue, optional
A queue for warning messages. Default is None.
number_of_execution : int, optional
The number of times the control sequence should be executed. Default is 1.
"""
Expand All @@ -509,6 +512,9 @@ def __init__(self, root=None, cleanup_list=[], number_of_execution=1):
#: int: The remaining number of executions of the control sequence.
self.remaining_number_of_execution = number_of_execution

#: Queue: A queue for warning messages related to the control sequence.
self.warning_queue = warning_queue

def reset(self):
"""Reset the container's state, including the current node and end flag.

Expand Down Expand Up @@ -556,8 +562,12 @@ def run(self, *args, wait_response=False):
while self.curr_node:
try:
result, is_end = self.curr_node.run(*args, wait_response=wait_response)
except Exception:
except Exception as e:
logger.debug(f"SignalContainer - {traceback.format_exc()}")
if self.warning_queue and isinstance(e, UserVisibleException):
self.warning_queue.put(
("warning", f"Warning: review details below.\n{str(e)}")
)
self.end_flag = True
self.cleanup()
return
Expand Down Expand Up @@ -1031,7 +1041,7 @@ def build_feature_tree(feature_list, continue_list, break_list):
for node in break_list:
if node[0] == "child":
node[1].child, node[2].child = create_node({"name": DummyFeature})
return SignalContainer(signal_root, signal_cleanup_list), DataContainer(
return SignalContainer(signal_root, signal_cleanup_list, getattr(model, "event_queue", None)), DataContainer(
data_root, data_cleanup_list
)

Expand Down
13 changes: 10 additions & 3 deletions src/navigate/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
SharedList,
load_dynamic_parameter_functions,
)
from navigate.model.utils.threads import ThreadWithWarning
from navigate.log_files.log_functions import log_setup
from navigate.tools.common_dict_tools import update_stage_dict
from navigate.tools.common_functions import load_module_from_file, VariableWithLock
Expand Down Expand Up @@ -587,9 +588,13 @@ def run_command(
self.data_buffer_saving_flags = None

if self.imaging_mode == "live":
self.signal_thread = threading.Thread(target=self.run_live_acquisition)
self.signal_thread = ThreadWithWarning(target=self.run_live_acquisition,
warning_queue=self.event_queue,
logger=self.logger)
else:
self.signal_thread = threading.Thread(target=self.run_acquisition)
self.signal_thread = ThreadWithWarning(target=self.run_acquisition,
warning_queue=self.event_queue,
logger=self.logger)

self.signal_thread.name = f"{self.imaging_mode} signal"

Expand Down Expand Up @@ -685,7 +690,9 @@ def run_command(
self, self.acquisition_modes_feature_setting[self.imaging_mode]
)
self.stop_send_signal = False
self.signal_thread = threading.Thread(target=self.run_live_acquisition)
self.signal_thread = ThreadWithWarning(target=self.run_live_acquisition,
warning_queue=self.event_queue,
logger=self.logger)
self.signal_thread.name = "Waveform Popup Signal"
self.signal_thread.start()

Expand Down
41 changes: 41 additions & 0 deletions src/navigate/model/utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2021-2025 The University of Texas Southwestern Medical Center.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted for academic and research use only (subject to the
# limitations in the disclaimer below) provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.

# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.

# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

class UserVisibleException(Exception):
"""Base class for exceptions that should be visible to the user."""
def __init__(self, message: str):
"""Initialize the exception with a user-friendly message.
Parameters
----------
message : str
A message that describes the exception in a user-friendly way.
"""
super().__init__(message)
65 changes: 65 additions & 0 deletions src/navigate/model/utils/threads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) 2021-2025 The University of Texas Southwestern Medical Center.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted for academic and research use only (subject to the
# limitations in the disclaimer below) provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.

# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.

# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# Standard Library Imports
import threading
import logging
# Local Imports
from navigate.model.utils.exceptions import UserVisibleException

# Logger Setup
p = __name__.split(".")[1]
logger = logging.getLogger(p)

class ThreadWithWarning(threading.Thread):
"""A custom thread class that raises a warning to the user if any error is raised."""

def __init__(self, *args, **kwargs):
"""Initialize the ThreadWithWarning."""
if "warning_queue" in kwargs:
self._warning_queue = kwargs["warning_queue"]
del kwargs["warning_queue"]
self._logger = logger
if "logger" in kwargs:
self._logger = kwargs["logger"]
del kwargs["logger"]
super().__init__(*args, **kwargs)

def run(self):
"""Run the thread and handle warnings."""
try:
super().run()
except Exception as e:
self._logger.error(f"Error in thread {self.name}: {e}")
if hasattr(self, "_warning_queue") and isinstance(e, UserVisibleException):
self._warning_queue.put(("warning", str(e)))
raise e