From 63cc3132cffe6dd0d46e58fdf52142af066413bb Mon Sep 17 00:00:00 2001 From: "brian.kim" Date: Thu, 30 Jan 2025 13:08:19 -0800 Subject: [PATCH] only threshold selected idx --- .../core/file_input_model.py | 7 +++ .../core/prediction_result_input_widget.py | 8 +-- .../thresholding/thresholding_service.py | 59 +++++++++---------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/allencell_ml_segmenter/core/file_input_model.py b/src/allencell_ml_segmenter/core/file_input_model.py index c0cef960..a1293bb5 100644 --- a/src/allencell_ml_segmenter/core/file_input_model.py +++ b/src/allencell_ml_segmenter/core/file_input_model.py @@ -24,6 +24,7 @@ def __init__(self) -> None: self._input_mode: Optional[InputMode] = None self._selected_paths: Optional[list[Path]] = None self._max_channels: Optional[int] = None + self._selected_idx: Optional[list[int]] = None def get_output_seg_directory(self) -> Optional[Path]: """ @@ -92,3 +93,9 @@ def get_input_files_as_list(self) -> List[Path]: if selected_paths is not None: return selected_paths return [] + + def set_selected_idx(self, selected_idx: list[int]) -> None: + self._selected_idx = selected_idx + + def get_selected_idx(self) -> Optional[list[int]]: + return self._selected_idx diff --git a/src/allencell_ml_segmenter/core/prediction_result_input_widget.py b/src/allencell_ml_segmenter/core/prediction_result_input_widget.py index 31ae4d6f..d6fd25f1 100644 --- a/src/allencell_ml_segmenter/core/prediction_result_input_widget.py +++ b/src/allencell_ml_segmenter/core/prediction_result_input_widget.py @@ -36,10 +36,4 @@ def _update_layer_list(self) -> None: def process_checked_signal(self, row: int, state: Qt.CheckState) -> None: if self._model.get_input_mode() == InputMode.FROM_NAPARI_LAYERS: - selected_indices: list[int] = self._image_list.get_checked_rows() - if state == Qt.CheckState.Checked: - # paths of images to be segmented, which will not be opened again because already in memory - # but satisfies file_input_model state which determines if we have anything selected - self._model.set_selected_paths( - [x.path for x in self._prediction_layers] - ) + self._model.set_selected_idx(self._image_list.get_checked_rows()) diff --git a/src/allencell_ml_segmenter/thresholding/thresholding_service.py b/src/allencell_ml_segmenter/thresholding/thresholding_service.py index bf6ad064..d100383c 100644 --- a/src/allencell_ml_segmenter/thresholding/thresholding_service.py +++ b/src/allencell_ml_segmenter/thresholding/thresholding_service.py @@ -80,40 +80,39 @@ def _on_threshold_changed(self, _: Event) -> None: ) else: thresh_function = self._threshold_image - for layer in segmentation_labels: - # Creating helper functions for mypy strict typing - def thresholding_task() -> np.ndarray: - # INVARIANT: a segmentation layer must have prob_map in its metadata if it came from our plugin - # so we are only supporting thresholding images that are from the plugin itself. - if ( - not isinstance(layer.metadata, dict) - or "prob_map" not in layer.metadata - ): - raise ValueError( - "Layer metadata must be a dictionary containing the 'prob_map' key in order to threshold." + for idx, layer in enumerate(segmentation_labels): + if idx in self._file_input_model.get_selected_idx(): + # Creating helper functions for mypy strict typing + def thresholding_task() -> np.ndarray: + # INVARIANT: a segmentation layer must have prob_map in its metadata if it came from our plugin + # so we are only supporting thresholding images that are from the plugin itself. + if ( + not isinstance(layer.metadata, dict) + or "prob_map" not in layer.metadata + ): + raise ValueError( + "Layer metadata must be a dictionary containing the 'prob_map' key in order to threshold." + ) + + return thresh_function(layer.metadata["prob_map"]) + + def on_return( + threshold_output: np.ndarray, + layer_to_change: LabelsLayer = layer, + ) -> None: + self._viewer.insert_threshold( + layer_to_change, + threshold_output, + self._main_model.are_predictions_in_viewer(), ) - return thresh_function(layer.metadata["prob_map"]) - - layer_instance: LabelsLayer = layer - - def on_return( - threshold_output: np.ndarray, - layer_to_change: LabelsLayer = layer_instance, - ) -> None: - self._viewer.insert_threshold( - layer_to_change, - threshold_output, - self._main_model.are_predictions_in_viewer(), + self._task_executor.exec( + task=thresholding_task, + # lambda functions capture variables by reference so need to pass layer as a default argument + on_return=on_return, + on_error=self._handle_thresholding_error, ) - self._task_executor.exec( - task=thresholding_task, - # lambda functions capture variables by reference so need to pass layer as a default argument - on_return=on_return, - on_error=self._handle_thresholding_error, - ) - def _save_thresholded_images(self, _: Event) -> None: images_to_threshold: list[Path] = ( self._file_input_model.get_input_files_as_list()