Skip to content

Commit

Permalink
only threshold selected idx
Browse files Browse the repository at this point in the history
  • Loading branch information
yrkim98 committed Jan 30, 2025
1 parent c22520e commit 63cc313
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
7 changes: 7 additions & 0 deletions src/allencell_ml_segmenter/core/file_input_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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())
59 changes: 29 additions & 30 deletions src/allencell_ml_segmenter/thresholding/thresholding_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 63cc313

Please sign in to comment.