diff --git a/.gitignore b/.gitignore index 5bf1196..162185b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ fixed_wheels/ .vscode/ .DS_Store *.so -/**/*_ui.py Visualization_Results/ +/**/*_ui.py diff --git a/engines/ceus b/engines/ceus index 97d3d3a..08fd56b 160000 --- a/engines/ceus +++ b/engines/ceus @@ -1 +1 @@ -Subproject commit 97d3d3a8b03ee02bc5bda8dca2f1f316151d8210 +Subproject commit 08fd56b17af818edb4b63db9713f0a53c20926ab diff --git a/engines/qus b/engines/qus index d5531ed..ac6e2f0 160000 --- a/engines/qus +++ b/engines/qus @@ -1 +1 @@ -Subproject commit d5531ed6f48149931fb83cd33960b273517dd4c8 +Subproject commit ac6e2f0b8d13b6065a4cda9e889f3d48c5f070c9 diff --git a/src/ceus/analysis_loading/analysis_loading_controller.py b/src/ceus/analysis_loading/analysis_loading_controller.py index 8ddbc27..adf7ccc 100644 --- a/src/ceus/analysis_loading/analysis_loading_controller.py +++ b/src/ceus/analysis_loading/analysis_loading_controller.py @@ -10,7 +10,8 @@ from ..mvc.base_controller import BaseController from .analysis_loading_view_coordinator import AnalysisLoadingViewCoordinator -from engines.ceus.src.data_objs import UltrasoundImage, CeusSeg +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg from engines.ceus.src.time_series_analysis.curves.framework import CurvesAnalysis @@ -64,20 +65,31 @@ def _connect_signals(self) -> None: def _setup_analysis_options(self) -> None: """Setup available analysis types and functions in the view.""" analysis_types, analysis_functions = self._model.get_analysis_types() + print(f"DEBUG: Available analysis types: {list(analysis_types.keys())}") - # Automatically select "Paramap" as the analysis type - paramap_type = "paramap" - if paramap_type in analysis_types: - self._selected_analysis_type = paramap_type - if self._model.set_analysis_type(paramap_type): - # Get available functions for Paramap analysis - available_functions = self._model.get_analysis_functions(paramap_type) + # Automatically select the best available analysis type + # Prefer curves_paramap, then curves, or just the first available one + # For CEUS, 'curves' is the standard non-parametric analysis. + selected_type = None + for preferred in ["curves", "curves_paramap", "paramap"]: + if preferred in analysis_types: + selected_type = preferred + break + + if not selected_type and analysis_types: + selected_type = list(analysis_types.keys())[0] + + if selected_type: + self._selected_analysis_type = selected_type + if self._model.set_analysis_type(selected_type): + # Get available functions for selected analysis type + available_functions = self._model.get_analysis_functions(selected_type) # Skip analysis type selection and go directly to function selection self._view_coordinator.show_function_selection(available_functions) else: - self._view_coordinator.show_error("Failed to set Paramap analysis type") + self._view_coordinator.show_error(f"Failed to set {selected_type} analysis type") else: - self._view_coordinator.show_error("Paramap analysis type not available") + self._view_coordinator.show_error("No analysis types available") def _on_user_action(self, action_name: str, action_data: Any) -> None: """ @@ -98,7 +110,7 @@ def _on_user_action(self, action_name: str, action_data: Any) -> None: print(f"DEBUG: Controller received analysis_execution_started action") print(f"DEBUG: action_data = {action_data}") self._handle_analysis_execution(action_data) - elif action_name == "analysis_completed": + elif action_name == "analysis_loading_completed": self._handle_analysis_completion(action_data) else: # Forward unknown actions to application controller diff --git a/src/ceus/analysis_loading/analysis_loading_view_coordinator.py b/src/ceus/analysis_loading/analysis_loading_view_coordinator.py index a87ade9..7e969ff 100644 --- a/src/ceus/analysis_loading/analysis_loading_view_coordinator.py +++ b/src/ceus/analysis_loading/analysis_loading_view_coordinator.py @@ -10,12 +10,13 @@ from PyQt6.QtWidgets import QWidget, QStackedWidget from PyQt6.QtCore import pyqtSignal -from quantus.gui.mvc.base_view import BaseViewMixin +from ..mvc.base_view import BaseViewMixin from .views.analysis_function_selection_widget import AnalysisFunctionSelectionWidget -from quantus.gui.config_loading.views.analysis_params_widget import AnalysisParamsWidget +from .views.analysis_params_widget import AnalysisParamsWidget from .views.analysis_execution_widget import AnalysisExecutionWidget -from quantus.data_objs import UltrasoundRfImage, BmodeSeg, RfAnalysisConfig -from quantus.analysis.paramap.framework import ParamapAnalysis +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg +from engines.ceus.src.time_series_analysis.curves.framework import CurvesAnalysis class AnalysisLoadingViewCoordinator(QStackedWidget, BaseViewMixin): @@ -40,7 +41,7 @@ class AnalysisLoadingViewCoordinator(QStackedWidget, BaseViewMixin): # ============================================================================ - def __init__(self, image_data: UltrasoundRfImage, seg_data: BmodeSeg, config_data: RfAnalysisConfig, parent: Optional[QWidget] = None): + def __init__(self, image_data: UltrasoundImage, seg_data: CeusSeg, config_data, parent: Optional[QWidget] = None): super().__init__(parent) self.__init_base_view__(parent) self._image_data = image_data @@ -48,11 +49,6 @@ def __init__(self, image_data: UltrasoundRfImage, seg_data: BmodeSeg, config_dat self._config_data = config_data print(f"DEBUG: AnalysisLoadingViewCoordinator - image_data = {image_data is not None}") - if image_data is not None: - print(f"DEBUG: AnalysisLoadingViewCoordinator - scan_name = {image_data.scan_name}") - print(f"DEBUG: AnalysisLoadingViewCoordinator - phantom_name = {image_data.phantom_name}") - else: - print(f"DEBUG: AnalysisLoadingViewCoordinator - image_data is None!") # Widget instances self._function_selection_widget: Optional[AnalysisFunctionSelectionWidget] = None @@ -63,7 +59,7 @@ def __init__(self, image_data: UltrasoundRfImage, seg_data: BmodeSeg, config_dat self._selected_analysis_type: Optional[str] = None self._selected_functions: List[str] = [] self._analysis_params: dict = {} - self._analysis_data: Optional[ParamapAnalysis] = None + self._analysis_data: Optional[CurvesAnalysis] = None # Note: Analysis type selection is now skipped - Paramap is automatically selected # The controller will call show_function_selection directly @@ -106,12 +102,16 @@ def show_error(self, error_message: str) -> None: error_message: Error message to display """ current_widget: BaseViewMixin = self.currentWidget() - current_widget.show_error(error_message) + if current_widget: + current_widget.show_error(error_message) + else: + print(f"ERROR (no active widget): {error_message}") def clear_error(self) -> None: """Clear error message in the current widget.""" current_widget: BaseViewMixin = self.currentWidget() - current_widget.clear_error() + if current_widget: + current_widget.clear_error() # ============================================================================ # NAVIGATION METHODS - Methods to show different widgets @@ -160,13 +160,19 @@ def show_params_configuration(self, required_params: List[str], selected_functio print(f"DEBUG: Creating AnalysisParamsWidget with image_data = {self._image_data is not None}") if self._image_data is not None: print(f"DEBUG: Passing scan_name = {self._image_data.scan_name}") - print(f"DEBUG: Passing phantom_name = {self._image_data.phantom_name}") + if hasattr(self._image_data, 'phantom_name'): + print(f"DEBUG: Passing phantom_name = {self._image_data.phantom_name}") self._params_widget = AnalysisParamsWidget(self._image_data, self._seg_data, self._config_data) self._params_widget.setup_ui() self._params_widget.connect_signals() self._params_widget.params_configured.connect(self._on_params_configured) self._params_widget.back_requested.connect(self._on_params_back) self.addWidget(self._params_widget) + else: + # Update data in existing widget + self._params_widget._image_data = self._image_data + self._params_widget._seg_data = self._seg_data + self._params_widget._config_data = self._config_data print(f"DEBUG: Calling set_required_params...") self._params_widget.set_required_params(required_params, selected_functions) @@ -198,6 +204,10 @@ def show_analysis_execution(self, execution_summary: Dict) -> None: print(f"DEBUG: AnalysisExecutionWidget created and added to stack") else: print(f"DEBUG: Using existing AnalysisExecutionWidget") + # Update data in existing widget + self._execution_widget._image_data = self._image_data + self._execution_widget._seg_data = self._seg_data + self._execution_widget._config_data = self._config_data print(f"DEBUG: Setting execution summary...") self._execution_widget.set_execution_summary(execution_summary) @@ -208,7 +218,7 @@ def show_analysis_execution(self, execution_summary: Dict) -> None: self._execution_widget.clear_error() print(f"DEBUG: show_analysis_execution completed - execution screen should be visible") - def show_analysis_results(self, analysis_data: ParamapAnalysis) -> None: + def show_analysis_results(self, analysis_data: CurvesAnalysis) -> None: """ Show analysis results in the execution widget. @@ -260,7 +270,7 @@ def _on_execution_started(self, execution_data: dict) -> None: self._emit_user_action("analysis_execution_started", execution_data) print(f"DEBUG: user_action signal emitted") - def _on_analysis_confirmed(self, analysis_data: ParamapAnalysis) -> None: + def _on_analysis_confirmed(self, analysis_data: CurvesAnalysis) -> None: """ Handle analysis completion confirmation. diff --git a/src/ceus/analysis_loading/ui/analysis_params.ui b/src/ceus/analysis_loading/ui/analysis_params.ui new file mode 100644 index 0000000..c373b58 --- /dev/null +++ b/src/ceus/analysis_loading/ui/analysis_params.ui @@ -0,0 +1,688 @@ + + + analysisParams + + + + 0 + 0 + 1284 + 803 + + + + + 0 + 0 + + + + Analysis Parameters Configuration + + + QWidget { + background: rgb(42, 42, 42); +} + + + + + 60 + 20 + 951 + 731 + + + + + + + 0 + + + QLayout::SizeConstraint::SetMaximumSize + + + + + + 341 + 601 + + + + + 241 + 601 + + + + <html><head/><body><p><br/></p></body></html> + + + QWidget { + background-color: rgb(28, 0, 101); +} + + + + + 0 + 0 + 341 + 121 + + + + + 341 + 121 + + + + + 341 + 121 + + + + QFrame { + background-color: rgb(99, 0, 174); + border: 1px solid black; +} + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 70 + 0 + 191 + 51 + + + + QLabel { + font-size: 21px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight: bold; +} + + + Image Selection: + + + Qt::AlignmentFlag::AlignCenter + + + + + + -60 + 40 + 191 + 51 + + + + QLabel { + font-size: 16px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight: bold; +} + + + Image: + + + Qt::AlignmentFlag::AlignCenter + + + + + + -50 + 70 + 191 + 51 + + + + QLabel { + font-size: 16px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight: bold +} + + + Phantom: + + + Qt::AlignmentFlag::AlignCenter + + + + + + 100 + 40 + 241 + 51 + + + + QLabel { + font-size: 14px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; +} + + + Sample filename + + + Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter + + + + + + 100 + 70 + 241 + 51 + + + + QLabel { + font-size: 14px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; +} + + + Sample filename + + + Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter + + + + + + + 0 + 120 + 341 + 121 + + + + + 341 + 121 + + + + QFrame { + background-color: rgb(99, 0, 174); + border: 1px solid black; +} + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 0 + 40 + 341 + 51 + + + + QLabel { + font-size: 21px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight: bold; +} + + + Segmentation Selection + + + Qt::AlignmentFlag::AlignCenter + + + + + + + 0 + 240 + 341 + 121 + + + + + 341 + 121 + + + + QFrame { + background-color: rgb(99, 0, 174); + border: 1px solid black; +} + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 0 + 30 + 341 + 51 + + + + QLabel { + font-size: 21px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight:bold; +} + + + Analysis Parameter Selection + + + Qt::AlignmentFlag::AlignCenter + + + + + + + 0 + 360 + 341 + 121 + + + + + 341 + 121 + + + + + 341 + 121 + + + + QFrame { + background-color: rgb(99, 0, 174); + border: 1px solid black; +} + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 0 + 30 + 341 + 51 + + + + QLabel { + font-size: 21px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight: bold; +} + + + CEUS Analysis + + + Qt::AlignmentFlag::AlignCenter + + + + + + + 0 + 480 + 341 + 121 + + + + + 341 + 121 + + + + + 341 + 121 + + + + QFrame { + background-color: rgb(49, 0, 124); + border: 1px solid black; +} + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + 20 + 30 + 301 + 51 + + + + QLabel { + font-size: 21px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); + border: 0px; + font-weight: bold; +} + + + Visualization / Export + + + Qt::AlignmentFlag::AlignCenter + + + + + + + + + + 341 + 16777215 + + + + QFrame { + background-color: rgb(28, 0, 101); +} + + + + QLayout::SizeConstraint::SetMinAndMaxSize + + + 10 + + + 10 + + + 10 + + + 10 + + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + + + + + Qt::Orientation::Horizontal + + + + 40 + 20 + + + + + + + + + 131 + 41 + + + + + 131 + 41 + + + + QPushButton { + color: white; + font-size: 16px; + background: rgb(90, 37, 255); + border-radius: 15px; +} + + + Back + + + + + + + + + + + + 50 + + + 30 + + + 10 + + + 30 + + + 10 + + + + + QLabel { + font-size: 29px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); +} + + + Analysis in Progress... + + + Qt::TextFormat::AutoText + + + false + + + Qt::AlignmentFlag::AlignCenter + + + true + + + + + + + QLabel { + font-size: 29px; + color: rgb(255, 255, 255); + background-color: rgba(255, 255, 255, 0); +} + + + Configure Analysis Parameters: + + + Qt::TextFormat::AutoText + + + false + + + Qt::AlignmentFlag::AlignCenter + + + true + + + + + + + true + + + + + 0 + 0 + 409 + 284 + + + + + + + + + + QLabel { + color: rgb(0, 255, 0); + font-size: 20px; + background-color: rgba(255, 255, 255, 0); +} + + + Running Analysis.... + + + Qt::AlignmentFlag::AlignCenter + + + + + + + + 131 + 41 + + + + + 131 + 41 + + + + QPushButton { + color: white; + font-size: 16px; + background: rgb(90, 37, 255); + border-radius: 15px; +} + + + Run Analysis + + + + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + + + + + + + + + diff --git a/src/ceus/analysis_loading/views/analysis_execution_widget.py b/src/ceus/analysis_loading/views/analysis_execution_widget.py index 08d5b76..c5a408c 100644 --- a/src/ceus/analysis_loading/views/analysis_execution_widget.py +++ b/src/ceus/analysis_loading/views/analysis_execution_widget.py @@ -10,10 +10,11 @@ from PyQt6.QtCore import pyqtSignal, Qt, QTimer from PyQt6.QtGui import QFont -from quantus.gui.mvc.base_view import BaseViewMixin -from quantus.gui.analysis_loading.ui.analysis_execution_ui import Ui_analysisExecution -from quantus.data_objs import UltrasoundRfImage, BmodeSeg, RfAnalysisConfig -from quantus.analysis.paramap.framework import ParamapAnalysis +from ...mvc.base_view import BaseViewMixin +from ..ui.analysis_execution_ui import Ui_analysisExecution +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg +from engines.ceus.src.time_series_analysis.curves.framework import CurvesAnalysis class AnalysisExecutionWidget(QWidget, BaseViewMixin): @@ -26,11 +27,11 @@ class AnalysisExecutionWidget(QWidget, BaseViewMixin): # Signals for communicating with controller execution_started = pyqtSignal(dict) # execution_data - analysis_confirmed = pyqtSignal(object) # analysis_data (ParamapAnalysis) + analysis_confirmed = pyqtSignal(object) # analysis_data (CurvesAnalysis) close_requested = pyqtSignal() back_requested = pyqtSignal() - def __init__(self, image_data: UltrasoundRfImage, seg_data: BmodeSeg, config_data: RfAnalysisConfig, parent: Optional[QWidget] = None): + def __init__(self, image_data: UltrasoundImage, seg_data: CeusSeg, config_data, parent: Optional[QWidget] = None): QWidget.__init__(self, parent) self.__init_base_view__(parent) self._ui = Ui_analysisExecution() @@ -40,7 +41,7 @@ def __init__(self, image_data: UltrasoundRfImage, seg_data: BmodeSeg, config_dat # Current state self._execution_summary: Dict = {} - self._analysis_data: Optional[ParamapAnalysis] = None + self._analysis_data: Optional[CurvesAnalysis] = None self._is_executing = False self._results_shown = False # Track if results have been shown @@ -78,8 +79,8 @@ def setup_ui(self) -> None: # Update labels to reflect inputted image and phantom if self._image_data is not None: - self._ui.image_path_input.setText(self._image_data.scan_name or "No image loaded") - self._ui.phantom_path_input.setText(self._image_data.phantom_name or "No phantom loaded") + self._ui.image_path_input.setText(getattr(self._image_data, 'scan_name', "No image loaded")) + self._ui.phantom_path_input.setText(getattr(self._image_data, 'phantom_name', "No phantom loaded")) else: self._ui.image_path_input.setText("No image loaded") self._ui.phantom_path_input.setText("No phantom loaded") @@ -219,7 +220,7 @@ def _clear_summary_layout(self) -> None: if child.widget(): child.widget().deleteLater() - def show_results(self, analysis_data: ParamapAnalysis) -> None: + def show_results(self, analysis_data: CurvesAnalysis) -> None: """ Show analysis results. @@ -233,6 +234,12 @@ def show_results(self, analysis_data: ParamapAnalysis) -> None: self._ui.progress_bar.setValue(100) self._ui.progress_label.setText("Analysis completed successfully!") + # Add a message that the rest of the pipeline needs to be finished + info_label = QLabel("Note: The rest of the pipeline still needs to be finished.") + info_label.setStyleSheet("color: #FFD700; font-style: italic; font-size: 10px; margin-top: 5px;") + info_label.setAlignment(Qt.AlignmentFlag.AlignCenter) + self._ui.analysis_execution_layout.addWidget(info_label) + # Show finish button, hide execute button self._ui.execute_button.setVisible(False) self._ui.finish_button.setVisible(True) diff --git a/src/ceus/analysis_loading/views/analysis_function_selection_widget.py b/src/ceus/analysis_loading/views/analysis_function_selection_widget.py index f36643d..318544a 100644 --- a/src/ceus/analysis_loading/views/analysis_function_selection_widget.py +++ b/src/ceus/analysis_loading/views/analysis_function_selection_widget.py @@ -9,9 +9,10 @@ from PyQt6.QtWidgets import QWidget, QComboBox, QVBoxLayout, QHBoxLayout, QLabel, QSizePolicy from PyQt6.QtCore import pyqtSignal, Qt -from quantus.gui.mvc.base_view import BaseViewMixin -from quantus.gui.analysis_loading.ui.analysis_function_selection_ui import Ui_analysisFunctionSelection -from quantus.data_objs import UltrasoundRfImage, BmodeSeg, RfAnalysisConfig +from ...mvc.base_view import BaseViewMixin +from ..ui.analysis_function_selection_ui import Ui_analysisFunctionSelection +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg class AnalysisFunctionSelectionWidget(QWidget, BaseViewMixin): @@ -27,7 +28,7 @@ class AnalysisFunctionSelectionWidget(QWidget, BaseViewMixin): close_requested = pyqtSignal() back_requested = pyqtSignal() - def __init__(self, image_data: UltrasoundRfImage, seg_data: BmodeSeg, config_data: RfAnalysisConfig, parent: Optional[QWidget] = None): + def __init__(self, image_data: UltrasoundImage, seg_data: CeusSeg, config_data, parent: Optional[QWidget] = None): QWidget.__init__(self, parent) self.__init_base_view__(parent) self._ui = Ui_analysisFunctionSelection() @@ -56,8 +57,8 @@ def setup_ui(self) -> None: # Update labels to reflect inputted image and phantom if self._image_data is not None: - self._ui.image_path_input.setText(self._image_data.scan_name or "No image loaded") - self._ui.phantom_path_input.setText(self._image_data.phantom_name or "No phantom loaded") + self._ui.image_path_input.setText(getattr(self._image_data, 'scan_name', "No image loaded")) + self._ui.phantom_path_input.setText(getattr(self._image_data, 'phantom_name', "No phantom loaded")) else: self._ui.image_path_input.setText("No image loaded") self._ui.phantom_path_input.setText("No phantom loaded") diff --git a/src/ceus/analysis_loading/views/analysis_params_widget.py b/src/ceus/analysis_loading/views/analysis_params_widget.py new file mode 100644 index 0000000..aea4c51 --- /dev/null +++ b/src/ceus/analysis_loading/views/analysis_params_widget.py @@ -0,0 +1,92 @@ +""" +Analysis Parameters Widget for Analysis Loading + +This widget allows users to configure parameters required for the selected analysis functions. +It dynamically creates input fields based on the required parameters. +""" + +from typing import List, Optional, Dict, Any +from PyQt6.QtWidgets import (QWidget, QLabel, QLineEdit, QDoubleSpinBox, QSpinBox, + QCheckBox, QComboBox, QFormLayout, + QGroupBox, QTextEdit) +from PyQt6.QtCore import pyqtSignal, Qt, QTimer + +from ...mvc.base_view import BaseViewMixin +from ..ui.analysis_params_ui import Ui_analysisParams +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg + + +class AnalysisParamsWidget(QWidget, BaseViewMixin): + """ + Widget for configuring analysis parameters. + + This widget dynamically creates input fields based on the required parameters + for the selected analysis functions. + """ + + # Signals for communicating with controller + params_configured = pyqtSignal(dict) # analysis_params + close_requested = pyqtSignal() + back_requested = pyqtSignal() + + def __init__(self, image_data: UltrasoundImage, seg_data: CeusSeg, config_data, parent: Optional[QWidget] = None): + QWidget.__init__(self, parent) + self.__init_base_view__(parent) + self._ui = Ui_analysisParams() + self._image_data = image_data + self._seg_data = seg_data + self._config_data = config_data + + # Track parameter inputs + self._param_inputs: Dict[str, QWidget] = {} + self._required_params: List[str] = [] + self._selected_functions: List[str] = [] + + def setup_ui(self) -> None: + """Setup the user interface.""" + self._ui.setupUi(self) + + # Configure layout for parameters configuration (assuming similar structure to QUS) + if hasattr(self._ui, 'full_screen_layout'): + self.setLayout(self._ui.full_screen_layout) + + # Update labels to reflect inputted image + if hasattr(self._ui, 'image_path_input') and self._image_data: + scan_name = getattr(self._image_data, 'scan_name', 'Unknown') + self._ui.image_path_input.setText(scan_name) + + def connect_signals(self) -> None: + """Connect UI signals to internal handlers.""" + if hasattr(self._ui, 'run_analysis_button'): + self._ui.run_analysis_button.clicked.connect(self._on_run_analysis_clicked) + if hasattr(self._ui, 'back_button'): + self._ui.back_button.clicked.connect(self._on_back_clicked) + + def set_required_params(self, required_params: List[str], selected_functions: List[str]) -> None: + """ + Set required parameters and create input fields. + + Args: + required_params: List of required parameter names + selected_functions: List of selected function names + """ + self._required_params = required_params + self._selected_functions = selected_functions + self._create_parameter_inputs() + + def _create_parameter_inputs(self) -> None: + """Create input fields for each required parameter.""" + # This implementation is simplified compared to QUS for now + # Ideally would dynamically create inputs based on CEUS requirements + pass + + def _on_run_analysis_clicked(self) -> None: + """Handle run analysis button click.""" + # Collect parameters (simplified) + params = {} + self.params_configured.emit(params) + + def _on_back_clicked(self) -> None: + """Handle back button click.""" + self.back_requested.emit() diff --git a/src/ceus/application_controller.py b/src/ceus/application_controller.py index 86f6cbf..3e2fca6 100644 --- a/src/ceus/application_controller.py +++ b/src/ceus/application_controller.py @@ -12,7 +12,9 @@ from .image_loading.image_loading_view_coordinator import ImageLoadingViewCoordinator from .image_loading.image_loading_controller import ImageLoadingController from .seg_loading.seg_loading_controller import SegmentationLoadingController -from engines.ceus.src.data_objs import UltrasoundImage, CeusSeg +from .analysis_loading.analysis_loading_controller import AnalysisLoadingController +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg class ApplicationController(QObject): @@ -37,9 +39,14 @@ def __init__(self, app: QApplication): # Unified application model self._model = ApplicationModel() + # Current data + self._image_data: Optional[UltrasoundImage] = None + self._seg_data: Optional[CeusSeg] = None + # Controllers for different screens (using the same model) self._image_loading_controller: Optional[ImageLoadingController] = None self._segmentation_controller: Optional[SegmentationLoadingController] = None + self._analysis_loading_controller: Optional[AnalysisLoadingController] = None # Setup main widget self._setup_main_widget() @@ -58,8 +65,13 @@ def _setup_main_widget(self) -> None: def _connect_model_signals(self) -> None: """Connect unified model signals to application controller.""" self._model.image_loaded.connect(self._initialize_segmentation_loading) + self._model.segmentation_loaded.connect(self._on_segmentation_loaded) self._model.error_occurred.connect(self._on_model_error) + def _on_segmentation_loaded(self, seg_data: CeusSeg) -> None: + """Handle segmentation loading completion.""" + self._on_segmentation_action('segmentation_confirmed', seg_data) + def _initialize_image_loading(self) -> None: """Initialize the image loading screen.""" if self._image_loading_controller: @@ -82,6 +94,8 @@ def _initialize_segmentation_loading(self, image_data: UltrasoundImage) -> None: Args: image_data: Loaded image data from previous screen """ + self._image_data = image_data + if self._segmentation_controller: self._cleanup_segmentation_loading() @@ -128,10 +142,58 @@ def _on_segmentation_action(self, action_name: str, action_data) -> None: """ if action_name == 'segmentation_confirmed': self._seg_data = self._segmentation_controller.get_loaded_segmentation() - # TODO: Navigate to analysis screen when implemented - print("Analysis screen coming soon...") - self._app.quit() + + # Use model data as source of truth + image_data = self._model.image_data if self._model.image_data else self._image_data + + self._initialize_analysis_loading(image_data, self._seg_data) + def _initialize_analysis_loading(self, image_data: UltrasoundImage, seg_data: CeusSeg) -> None: + """ + Initialize the analysis loading screen. + + Args: + image_data: Loaded image data + seg_data: Loaded segmentation data + """ + if self._analysis_loading_controller: + self._cleanup_analysis_loading() + + # Create controller with unified model + # Note: CEUS might need a config object, passing None for now if not available + self._analysis_loading_controller = AnalysisLoadingController(self._model, image_data, seg_data, None) + + # Connect signals + self._analysis_loading_controller.view.user_action.connect(self._on_analysis_action) + self._analysis_loading_controller.view.back_requested.connect(self._navigate_to_segmentation_loading) + + # Add to stack and show + self._widget_stack.addWidget(self._analysis_loading_controller.view) + self._widget_stack.setCurrentWidget(self._analysis_loading_controller.view) + + def _on_analysis_action(self, action_name: str, action_data) -> None: + """ + Handle actions from the analysis loading screen. + + Args: + action_name: Name of the action + action_data: Data associated with the action + """ + if action_name == 'analysis_loading_completed': + print("Analysis completed successfully!") + # Future: Navigate to visualization screen + self._app.quit() + + def _navigate_to_segmentation_loading(self) -> None: + """Navigate back to segmentation loading.""" + if self._analysis_loading_controller: + self._cleanup_analysis_loading() + + if self._segmentation_controller: + self._widget_stack.setCurrentWidget(self._segmentation_controller.view) + else: + self._initialize_segmentation_loading(self._image_data) + def _navigate_to_image_loading(self) -> None: """Navigate to image loading screen.""" # Reset image loading controller to initial state @@ -192,6 +254,15 @@ def _cleanup(self) -> None: """Clean up all resources before application exit.""" self._cleanup_image_loading() self._cleanup_segmentation_loading() + self._cleanup_analysis_loading() + + def _cleanup_analysis_loading(self) -> None: + """Clean up analysis loading controller resources.""" + if self._analysis_loading_controller: + self._widget_stack.removeWidget(self._analysis_loading_controller.view) + self._analysis_loading_controller.cleanup() + self._analysis_loading_controller.view.deleteLater() + self._analysis_loading_controller = None @property def image_data(self) -> Optional[UltrasoundImage]: diff --git a/src/ceus/application_model.py b/src/ceus/application_model.py index 2ae47fc..85f95fb 100644 --- a/src/ceus/application_model.py +++ b/src/ceus/application_model.py @@ -12,8 +12,11 @@ from .mvc.base_model import BaseModel from engines.ceus.src.image_loading.options import get_scan_loaders from engines.ceus.src.seg_loading.options import get_seg_loaders +from engines.ceus.src.time_series_analysis.options import get_analysis_types from engines.ceus.src.entrypoints import scan_loading_step, seg_loading_step -from engines.ceus.src.data_objs import UltrasoundImage, CeusSeg +from engines.ceus.src.data_objs.image import UltrasoundImage +from engines.ceus.src.data_objs.seg import CeusSeg +from engines.ceus.src.time_series_analysis.curves.framework import CurvesAnalysis class ScanLoadingWorker(QThread): @@ -35,6 +38,11 @@ def run(self): self.image_path, **self.scan_loader_kwargs ) + + if isinstance(image_data, int): + self.error_msg.emit(f"Error loading scan: Loader error code {image_data}") + return + self.finished.emit(image_data) except Exception as e: @@ -66,6 +74,10 @@ def run(self): **self.seg_loader_kwargs ) + if isinstance(seg_data, int): + self.error_msg.emit(f"Error loading segmentation: Loader error code {seg_data}") + return + self.finished.emit(seg_data) except Exception as e: @@ -75,6 +87,56 @@ def run(self): self.error_msg.emit(f"Error loading segmentation: {e}") +class AnalysisWorker(QThread): + """Worker thread for time-consuming analysis operations.""" + finished = pyqtSignal(object) + error_msg = pyqtSignal(str) + + def __init__(self, analysis_type: str, image_data: UltrasoundImage, + config_data: Any, seg_data: CeusSeg, + selected_functions: List[str], analysis_kwargs: Dict[str, Any]): + super().__init__() + self.analysis_type = analysis_type + self.image_data = image_data + self.config_data = config_data + self.seg_data = seg_data + self.selected_functions = selected_functions + self.analysis_kwargs = analysis_kwargs + + def run(self): + """Execute the analysis in background thread.""" + try: + from engines.ceus.src.time_series_analysis.options import get_analysis_types + all_types, _ = get_analysis_types() + + if self.analysis_type not in all_types: + self.error_msg.emit(f"Invalid analysis type: {self.analysis_type}") + return + + analysis_cls = all_types[self.analysis_type] + + # Initialize analysis + analysis_obj = analysis_cls( + self.image_data, + self.seg_data, + self.selected_functions, + **self.analysis_kwargs + ) + + # Execute analysis + if hasattr(analysis_obj, 'compute_curves'): + analysis_obj.compute_curves() + elif hasattr(analysis_obj, 'run'): + analysis_obj.run() + + self.finished.emit(analysis_obj) + + except Exception as e: + import traceback + traceback.print_exc() + self.error_msg.emit(f"Error during analysis: {e}") + + class ApplicationModel(BaseModel): """ Unified application model that manages all data and business logic for the QuantUS GUI. @@ -89,6 +151,7 @@ class ApplicationModel(BaseModel): # Additional signals for application-specific events image_loaded = pyqtSignal(UltrasoundImage) segmentation_loaded = pyqtSignal(CeusSeg) + analysis_completed = pyqtSignal(object) # Emits CurvesAnalysis def __init__(self): super().__init__() @@ -105,9 +168,17 @@ def __init__(self): self._seg_data: Optional[CeusSeg] = None self._seg_worker: Optional[SegLoadingWorker] = None + # Analysis state + self._analysis_data: Optional[CurvesAnalysis] = None + self._analysis_types: Dict[str, Any] = {} + self._analysis_functions: Dict[str, Any] = {} + self._selected_analysis_type: Optional[str] = None + self._analysis_worker: Optional[AnalysisWorker] = None + # Initialize loaders self._load_scan_loaders() self._load_seg_loaders() + self._load_analysis_types() def _load_scan_loaders(self) -> None: """Load available scan loaders from backend.""" @@ -122,6 +193,15 @@ def _load_seg_loaders(self) -> None: self._seg_loaders = get_seg_loaders() except Exception as e: self._emit_error(f"Failed to load seg loaders: {e}") + + def _load_analysis_types(self) -> None: + """Load available analysis types from backend.""" + try: + self._analysis_types, self._analysis_functions = get_analysis_types() + except Exception as e: + print(f"Error loading analysis types: {e}") + self._analysis_types = {} + self._analysis_functions = {} # Image Loading Properties and Methods @property @@ -438,7 +518,7 @@ def set_seg_type(self, seg_type_display_name: str) -> bool: """ try: if seg_type_display_name == "Manual Segmentation": - self._selected_seg_type = "pkl_roi" + self._selected_seg_type = "nifti" return True # Convert display name back to internal key @@ -541,6 +621,8 @@ def _on_segmentation_loading_complete(self, seg_data: CeusSeg) -> None: print(f"-----------------------------------------------\n") self.segmentation_loaded.emit(seg_data) + # Automatically confirm if this was loaded (either from file or manual save) + # This allows the app controller to catch the completion else: print(f"DEBUG: Segmentation loading failed - invalid seg data") self._emit_error("Failed to load segmentation data") @@ -556,3 +638,101 @@ def cleanup(self) -> None: self._seg_worker.quit() self._seg_worker.wait() self._seg_worker = None + + # ============================================================================ + # ANALYSIS METHODS + # ============================================================================ + + def get_analysis_types(self) -> tuple: + """Get available analysis types and functions.""" + return self._analysis_types, self._analysis_functions + + def set_analysis_type(self, analysis_type: str) -> bool: + """ + Set the selected analysis type. + + Args: + analysis_type: Analysis type to select + + Returns: + bool: True if successful + """ + if analysis_type in self._analysis_types: + self._selected_analysis_type = analysis_type + return True + else: + print(f"DEBUG: Invalid analysis type: {analysis_type}") + return False + + def get_analysis_functions(self, analysis_type: str) -> dict: + """ + Get available functions for an analysis type. + + Args: + analysis_type: Analysis type + + Returns: + dict: Available functions for the analysis type + """ + # In CEUS engine, analysis_functions is a flat dict of all available curve functions + # that are applicable to both 'curves' and 'curves_paramap' analysis types. + if analysis_type in self._analysis_functions and isinstance(self._analysis_functions[analysis_type], dict): + return self._analysis_functions[analysis_type] + + return self._analysis_functions + + def get_required_params(self, analysis_type: str, selected_functions: list) -> list: + """ + Get required parameters for the selected analysis. + + Args: + analysis_type: Key for the analysis type + selected_functions: List of selected function names + + Returns: + list: List of parameter names required + """ + try: + from engines.ceus.src.time_series_analysis.options import get_required_kwargs + return get_required_kwargs(analysis_type, selected_functions) + except Exception as e: + print(f"Error getting required params: {e}") + return [] + + def set_analysis_data(self, analysis_data: CurvesAnalysis) -> None: + """ + Store completed analysis data. + + Args: + analysis_data: Completed analysis data + """ + self._analysis_data = analysis_data + # Signal that analysis is complete + self.analysis_completed.emit(analysis_data) + + def run_analysis(self, analysis_type: str, image_data: UltrasoundImage, + config_data: Any, seg_data: CeusSeg, + selected_functions: List[str], **kwargs) -> None: + """ + Run the analysis in a background thread. + """ + # Stop existing worker if running + if self._analysis_worker and self._analysis_worker.isRunning(): + self._analysis_worker.quit() + self._analysis_worker.wait() + + self._analysis_worker = AnalysisWorker( + analysis_type, image_data, config_data, seg_data, selected_functions, kwargs + ) + + self._analysis_worker.finished.connect(self._on_analysis_worker_finished) + self._analysis_worker.error_msg.connect(self._emit_error) + + self._set_loading(True) + self._analysis_worker.start() + + def _on_analysis_worker_finished(self, analysis_obj: Any) -> None: + """Handle analysis completion.""" + self._set_loading(False) + self._analysis_data = analysis_obj + self.analysis_completed.emit(analysis_obj) diff --git a/src/ceus/image_loading/views/file_selection_widget.py b/src/ceus/image_loading/views/file_selection_widget.py index 5b095c6..23f8d09 100644 --- a/src/ceus/image_loading/views/file_selection_widget.py +++ b/src/ceus/image_loading/views/file_selection_widget.py @@ -116,7 +116,8 @@ def _show_loading_message(self) -> None: def _on_choose_image_path(self) -> None: """Handle image file selection.""" - if self._file_extensions == ["FOLDER"]: + is_folder = any(ext.upper() == "FOLDER" for ext in self._file_extensions) + if is_folder: dir_name = QFileDialog.getExistingDirectory(self, "Select Directory") if dir_name: self._ui.image_path_input.setText(dir_name) @@ -133,12 +134,16 @@ def _on_generate_image(self) -> None: if not os.path.exists(image_path): self.show_error(f"Image file does not exist: {os.path.basename(image_path)}") return - if not image_path.endswith(tuple(self._file_extensions)) and self._file_extensions != ['FOLDER']: - self.show_error(f"Image file must have one of the following extensions: {', '.join(self._file_extensions)}") - return - if self._file_extensions == ["FOLDER"] and not os.path.isdir(image_path): - self.show_error("Input path must be a folder!") - return + + is_folder = any(ext.upper() == "FOLDER" for ext in self._file_extensions) + if not is_folder: + if not image_path.endswith(tuple(self._file_extensions)): + self.show_error(f"Image file must have one of the following extensions: {', '.join(self._file_extensions)}") + return + else: + if not os.path.isdir(image_path): + self.show_error("Input path must be a folder!") + return self.clear_error() diff --git a/src/ceus/seg_loading/seg_loading_controller.py b/src/ceus/seg_loading/seg_loading_controller.py index eab66f6..3b1b7a0 100644 --- a/src/ceus/seg_loading/seg_loading_controller.py +++ b/src/ceus/seg_loading/seg_loading_controller.py @@ -35,15 +35,15 @@ def __init__(self, model: Optional[ApplicationModel] = None, custom_view=None): super().__init__(model, view) - # # Connect to model signals for automatic view updates - # self._connect_model_signals() + # Connect to model signals for automatic view updates + self._connect_model_signals() # Initialize view with segmentation loaders self._initialize_view() - # def _connect_model_signals(self) -> None: - # """Connect to model signals for automatic view updates.""" - # self.model.segmentation_loaded.connect(self.view.show_segmentation_preview) + def _connect_model_signals(self) -> None: + """Connect to model signals for automatic view updates.""" + self.model.segmentation_loaded.connect(self.view.show_segmentation_preview) def _initialize_view(self) -> None: """Initialize the view with data from the model.""" diff --git a/src/ceus/seg_loading/seg_loading_view_coordinator.py b/src/ceus/seg_loading/seg_loading_view_coordinator.py index 4f6d9d5..3151755 100644 --- a/src/ceus/seg_loading/seg_loading_view_coordinator.py +++ b/src/ceus/seg_loading/seg_loading_view_coordinator.py @@ -181,6 +181,7 @@ def show_voi_drawing(self) -> None: self._voi_drawing_widget = DrawVOIWidget(self._image_data) # Connect signals to handle user actions + self._voi_drawing_widget.segmentation_saved.connect(self._on_segmentation_saved) self._voi_drawing_widget.back_requested.connect(self.reset_to_seg_type_selection) self._voi_drawing_widget.close_requested.connect(self.close_requested.emit) self._voi_drawing_widget.apply_preprocs_preview.connect(self._on_preprocs_preview_requested) @@ -193,6 +194,8 @@ def preview_modified_image(self, modified_image: UltrasoundImage, frame: int) -> """Show the preprocessed data in the VOI drawing widget.""" if self._voi_drawing_widget: self._voi_drawing_widget.update_enhancement_cache(modified_image.pixel_data, frame) + elif self._roi_drawing_widget: + self._roi_drawing_widget.update_enhancement_cache(modified_image.pixel_data, frame) else: raise RuntimeError("VOI drawing widget not initialized") @@ -201,17 +204,47 @@ def show_roi_drawing(self) -> None: self._roi_drawing_widget = DrawROIWidget(self._image_data) # Connect signals to handle user actions + self._roi_drawing_widget.segmentation_saved.connect(self._on_segmentation_saved) self._roi_drawing_widget.back_requested.connect(self.reset_to_seg_type_selection) self._roi_drawing_widget.close_requested.connect(self.close_requested.emit) + self._roi_drawing_widget.apply_preprocs_preview.connect(self._on_preprocs_preview_requested) # Add to stack and show self.addWidget(self._roi_drawing_widget) self.setCurrentWidget(self._roi_drawing_widget) + def show_segmentation_preview(self, seg_data: CeusSeg) -> None: + """ + Show the segmentation preview widget. + + Args: + seg_data: Loaded segmentation data + """ + self._seg_data = seg_data + + # For now, since CEUS specific preview widgets are not yet implemented, + # we automatically confirm the segmentation to allow the workflow to continue. + # This prevents the application from getting "stuck" after loading. + print(f"DEBUG: Segmentation loaded, automatically confirming (Preview not yet implemented for CEUS)") + self.user_action.emit('segmentation_confirmed', seg_data) + # ============================================================================ # USER ACTION HANDLING - Process user interactions and communicate with controller # ============================================================================ + def _on_segmentation_saved(self, file_path: str) -> None: + """ + Handle segmentation saved from the manual drawing widget. + + Args: + file_path: Path to the saved segmentation file + """ + file_data = { + 'seg_path': file_path, + 'seg_type': self._selected_seg_type + } + self._emit_user_action('load_segmentation', file_data) + def _on_seg_type_selected(self, seg_type_name: str) -> None: """ Handle segmentation type selection from the seg type widget. diff --git a/src/ceus/seg_loading/views/draw_roi_widget.py b/src/ceus/seg_loading/views/draw_roi_widget.py index f72738f..95ee444 100644 --- a/src/ceus/seg_loading/views/draw_roi_widget.py +++ b/src/ceus/seg_loading/views/draw_roi_widget.py @@ -11,14 +11,24 @@ from scipy import interpolate from PIL import Image, ImageDraw from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas +from matplotlib.colors import LinearSegmentedColormap -from PyQt6.QtWidgets import QWidget, QHBoxLayout, QFileDialog +from PyQt6.QtWidgets import QWidget, QHBoxLayout, QFileDialog, QSlider, QVBoxLayout, QFrame, QCheckBox, QLabel from PyQt6.QtCore import pyqtSignal, Qt from ...mvc.base_view import BaseViewMixin from ..ui.draw_roi_ui import Ui_constructRoi from engines.ceus.src.data_objs import UltrasoundImage +# Philips CEUS Colormap: Grayscale -> Red -> Yellow +philips_colors = [ + (0.0, 0.0, 0.0), # 0% - Black + (0.4, 0.4, 0.4), # 40% - Gray + (0.8, 0.0, 0.0), # 80% - Red + (1.0, 1.0, 0.0) # 100% - Yellow +] +philips_cmap = LinearSegmentedColormap.from_list("philips_ceus", philips_colors) + class DrawROIWidget(QWidget, BaseViewMixin): """ @@ -33,6 +43,7 @@ class DrawROIWidget(QWidget, BaseViewMixin): segmentation_saved = pyqtSignal(str) # emit with saved file path back_requested = pyqtSignal() close_requested = pyqtSignal() + apply_preprocs_preview = pyqtSignal(list) # List of dicts with 'name' and 'kwargs' keys def __init__(self, image_data: UltrasoundImage, parent: Optional[QWidget] = None): QWidget.__init__(self, parent) @@ -56,11 +67,20 @@ def __init__(self, image_data: UltrasoundImage, parent: Optional[QWidget] = None self._im_artist = None # The image artist for fast updates self._roi_plot_artist = None # The ROI artist for fast updates self._roi_scatter_artist = None # The ROI scatter artist for fast updates - self._target_frame = 0 # Target frame for smooth transitions self._frame_update_pending = False + # Enhancement parameters + self._clahe_clip_limit = 1.2 + self._gamma = 1.5 self._width_scale = 1.0 + # Enhancement parameters + self._clahe_clip_limit = 1.2 + self._gamma = 1.5 + self._use_philips_ceus = False + self._enhanced_cache = None # Cache for enhanced current frame + self._enhanced_cache_idx = -1 + self._setup_ui() self._connect_signals() self._show_draw_type_selection() @@ -206,10 +226,7 @@ def _update_frame_animated(self, frame_num) -> list: if not self._frame_update_pending: return [self._im_artist, self._roi_plot_artist[0], self._roi_scatter_artist] - # Update to target frame - if self._frame != self._target_frame: - self._frame = self._target_frame - self._update_frame_display(self._frame) + self._update_frame_display(self._frame) self._update_roi_plot() self._update_roi_scatter() @@ -256,70 +273,156 @@ def _update_aspect_ratio(self) -> None: self._matplotlib_canvas.draw_idle() def _setup_enhancement_controls(self) -> None: - """Add enhancement sliders to the sidebar.""" - from PyQt6.QtWidgets import QVBoxLayout, QLabel, QSlider, QFrame - + """Add enhancement sliders beside the frame slider in a single horizontal line.""" + # Container frame for enhancement controls enh_group = QFrame() enh_group.setStyleSheet("background-color: rgba(255, 255, 255, 0); border: none;") - container_layout = QVBoxLayout(enh_group) - container_layout.setContentsMargins(0, 10, 0, 10) + + # Main horizontal layout for the enhancement section + container_layout = QHBoxLayout(enh_group) + container_layout.setContentsMargins(0, 0, 15, 0) container_layout.setSpacing(15) - def create_enh_column(label_text, min_val, max_val, current_val, callback): - col_widget = QWidget() - col_layout = QVBoxLayout(col_widget) - col_layout.setContentsMargins(0, 0, 0, 0) - col_layout.setSpacing(5) + def create_compact_control(label_text, min_val, max_val, current_val, callback): + # Widget to hold label, slider, and value in ONE line + ctrl_widget = QWidget() + ctrl_layout = QHBoxLayout(ctrl_widget) + ctrl_layout.setContentsMargins(0, 0, 0, 0) + ctrl_layout.setSpacing(5) lbl = QLabel(label_text) - lbl.setStyleSheet("font-size: 14px; color: white; font-weight: bold;") - lbl.setAlignment(Qt.AlignmentFlag.AlignCenter) - col_layout.addWidget(lbl) + lbl.setStyleSheet("font-size: 10px; color: white; font-weight: bold;") + ctrl_layout.addWidget(lbl) - row_layout = QHBoxLayout() + # Slider slider = QSlider(Qt.Orientation.Horizontal) slider.setRange(min_val, max_val) slider.setValue(current_val) - slider.setMinimumWidth(100) - slider.setMaximumWidth(120) + slider.setStyleSheet(self._ui.frame_slider.styleSheet()) + slider.setFixedWidth(70) + slider.setFixedHeight(12) slider.valueChanged.connect(callback) - + ctrl_layout.addWidget(slider) + val_lbl = QLabel(f"{current_val/10.0:.1f}") - val_lbl.setMinimumWidth(40) - val_lbl.setStyleSheet("color: #3498db; font-weight: bold; font-size: 14px;") + val_lbl.setStyleSheet("color: #3498db; font-weight: bold; font-size: 10px;") + val_lbl.setMinimumWidth(22) + val_lbl.setAlignment(Qt.AlignmentFlag.AlignLeft) + ctrl_layout.addWidget(val_lbl) - row_layout.addWidget(slider) - row_layout.addWidget(val_lbl) - col_layout.addLayout(row_layout) - - return col_widget, slider, val_lbl + return ctrl_widget, slider, val_lbl - width_col, self.width_slider, self.width_val_lbl = create_enh_column( + # Create controls + clahe_w, self.clahe_slider, self.clahe_val_lbl = create_compact_control( + "CLAHE", 1, 100, int(self._clahe_clip_limit * 10), self._on_clahe_changed + ) + gamma_w, self.gamma_slider, self.gamma_val_lbl = create_compact_control( + "GAMMA", 1, 40, int(self._gamma * 10), self._on_gamma_changed + ) + width_w, self.width_slider, self.width_val_lbl = create_compact_control( "WIDTH", 1, 50, int(self._width_scale * 10), self._on_width_changed ) - container_layout.addWidget(width_col) - - # Add to the layout below the frame slider - self._ui.side_bar_layout.addWidget(enh_group) + # Add to horizontal layout + container_layout.addWidget(clahe_w) + container_layout.addWidget(gamma_w) + container_layout.addWidget(width_w) + + # Pseudo coloring toggle nicely aligned + if not (self._image_data.pixel_data.ndim == 4 and self._image_data.pixel_data.shape[3] > 1): + # For RGB images, disable the Philips colormap option since it doesn't apply + self.philips_check = QCheckBox("Pseudo coloring") + self.philips_check.setStyleSheet("color: white; font-weight: bold; font-size: 11px;") + self.philips_check.stateChanged.connect(self._on_philips_toggled) + container_layout.addWidget(self.philips_check) + + # Add to the layout beside the frame slider (below the image) + self._ui.frameControlsLayout.insertWidget(0, enh_group) + + def _on_clahe_changed(self, value: int) -> None: + """Handle CLAHE clip limit change.""" + self._clahe_clip_limit = value / 10.0 + if hasattr(self, 'clahe_val_lbl'): + self.clahe_val_lbl.setText(f"{self._clahe_clip_limit:.1f}") + self._invalidate_enhancement_cache() + + def _on_gamma_changed(self, value: int) -> None: + """Handle gamma change.""" + self._gamma = value / 10.0 + if hasattr(self, 'gamma_val_lbl'): + self.gamma_val_lbl.setText(f"{self._gamma:.1f}") + self._invalidate_enhancement_cache() + + def _invalidate_enhancement_cache(self) -> None: + """Invalidate the enhancement cache (e.g. when parameters change).""" + self._enhanced_cache = None + self._enhanced_cache_idx = -1 + self._frame_update_pending = True # Trigger update to request new enhanced frame + + def _on_philips_toggled(self, state: int) -> None: + self._use_philips_ceus = state == Qt.CheckState.Checked.value + if self._im_artist: + new_cmap = philips_cmap if self._use_philips_ceus else 'gray' + self._im_artist.set_cmap(new_cmap) + + # # Force a call to set_array() to dirty the artist for the blitter + # self._update_frame_display(self._frame) + + # Flag the animation loop to blit the newly dirtied image on its next tick + self._frame_update_pending = True + + def _request_enhanced_frame(self, frame_2d: np.ndarray) -> np.ndarray: + """Enhance a 2D image frame using backend engine functions.""" + # Create a temporary UltrasoundImage for the current frame + temp_im = UltrasoundImage(self._image_data.scan_path) + temp_im.pixel_data = frame_2d.T[None].T.copy() # Add back time dimension for processing + temp_im.pixdim = self._image_data.pixdim + temp_im.frame_rate = self._image_data.frame_rate + + clahe_preproc_dict = { + 'name': 'enhance_clahe', + 'image_data': temp_im, + 'frame_ix': self._frame, + 'kwargs': { + 'clip_limit': self._clahe_clip_limit, + 'tile_grid_size': (8, 8), + } + } + + gamma_preproc_dict = { + 'name': 'enhance_gamma', + 'image_data': None, # signal to reuse the already CLAHE-enhanced image (all preprocs in the same batch share the same image input) + 'frame_ix': self._frame, + 'kwargs': { + 'gamma': self._gamma, + } + } + + preproc_dicts = [clahe_preproc_dict, gamma_preproc_dict] + self.apply_preprocs_preview.emit(preproc_dicts) # synchronous call to apply the enhancements and update the cache via the connected slot def _on_frame_changed(self, value: int) -> None: """Handle frame slider change with optimized performance.""" - self._target_frame = value + self._frame = value self._frame_update_pending = True - # Animation will handle the actual update efficiently + + def update_enhancement_cache(self, enhanced_frame: np.ndarray, frame: int) -> None: + """Receives enhanced frame from controller and stores it for display.""" + self._enhanced_cache = enhanced_frame.T[0].T # shape is (1, H, W) from the temp_im — take the single frame + self._enhanced_cache_idx = frame + self._frame_update_pending = True # Flag to update display on next animation tick def _update_frame_display(self, frame_index: int) -> None: - """Update the frame display with consistent parameters.""" if self._im_artist: - self._displayed_im = self._all_frames[frame_index] - self._im_artist.set_array(self._displayed_im) - self._ui.cur_frame_label.setText(str(np.round(frame_index*self._image_data.frame_rate, decimals=2))) - - def _force_frame_update(self) -> None: - """Force immediate frame update without animation (for initialization).""" - self._update_frame_display(self._frame) - self._matplotlib_canvas.draw_idle() + if self._enhanced_cache is None or self._enhanced_cache_idx != frame_index: + # synchronously update self._enhanced_cache with the new enhanced frame + # for the current index + self._request_enhanced_frame(self._all_frames[frame_index]) + self._im_artist.set_array(self._enhanced_cache) + + self._ui.cur_frame_label.setText( + str(np.round(frame_index * self._image_data.frame_rate, decimals=2)) + ) def _cleanup_animation(self): """Stop and clean up animation safely.""" @@ -351,13 +454,6 @@ def __del__(self): self._cleanup_animation() except: pass # Ignore errors during cleanup - - def _on_frame_selected(self) -> None: - """Handle frame selection confirmation.""" - # Make sure we're on the correct frame before confirming - if self._frame != self._target_frame: - self._frame = self._target_frame - self._force_frame_update() def _on_back_clicked(self) -> None: """Handle back button click.""" diff --git a/src/ceus/seg_loading/views/draw_voi_widget.py b/src/ceus/seg_loading/views/draw_voi_widget.py index 858a869..15126b1 100644 --- a/src/ceus/seg_loading/views/draw_voi_widget.py +++ b/src/ceus/seg_loading/views/draw_voi_widget.py @@ -128,7 +128,7 @@ class DrawVOIWidget(QWidget, BaseViewMixin): """ # Signals for communicating with controller - file_selected = pyqtSignal(dict) # {'seg_path': str, 'seg_type': str} + segmentation_saved = pyqtSignal(str) # emit with saved file path back_requested = pyqtSignal() close_requested = pyqtSignal() apply_preprocs_preview = pyqtSignal(list) # List of dicts with 'name' and 'kwargs' keys @@ -196,8 +196,8 @@ def __init__(self, image_data: UltrasoundImage, parent: Optional[QWidget] = None self._connect_signals() self._connect_matplotlib_events() self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) - self._update_scan_display() # Initial UI update - self._refresh_frames() # Mark all planes for first update + self._update_scan_display() # Initial UI update + self._refresh_frames() # Mark all planes for first update def update_enhancement_cache(self, enhanced_frame: np.ndarray, frame: int) -> None: """Update the displayed image data, e.g. after preprocessing.""" @@ -1044,6 +1044,8 @@ def _on_save_voi_finished(self, msg): self._ui.saving_voi_label.hide() self._show_widget_lists([self._save_voi_widgets]) print(msg) + if hasattr(self, '_last_saved_path'): + self.segmentation_saved.emit(str(self._last_saved_path)) def _on_save_voi_error(self, err): self._ui.saving_voi_label.hide() @@ -1272,6 +1274,7 @@ def _save_voi(self): out_name = out_name + '.nii.gz' if not out_name.endswith('.nii.gz') else out_name out_path = Path(self._ui.save_folder_input.text()) / out_name + self._last_saved_path = out_path affine = np.eye(4) for i, res in enumerate(self._image_data.pixdim[:3]):