-
Notifications
You must be signed in to change notification settings - Fork 24
feat(segmentation): add island segmentation effect #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
examples/viewer_lib/logic/segmentation/islands_effect_logic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from trame_server import Server | ||
|
|
||
| from trame_slicer.core import SlicerApp | ||
| from trame_slicer.segmentation import SegmentationEffectIslands | ||
|
|
||
| from ...ui import ( | ||
| IslandsEffectUI, | ||
| IslandsSegmentationMode, | ||
| IslandsState, | ||
| SegmentEditorUI, | ||
| ) | ||
| from .base_segmentation_logic import BaseEffectLogic | ||
|
|
||
|
|
||
| class IslandsEffectLogic(BaseEffectLogic[IslandsState, SegmentationEffectIslands]): | ||
| def __init__(self, server: Server, slicer_app: SlicerApp): | ||
| super().__init__(server, slicer_app, IslandsState, SegmentationEffectIslands) | ||
|
|
||
| def set_ui(self, ui: SegmentEditorUI): | ||
| self.set_effect_ui(ui.get_effect_ui(SegmentationEffectIslands)) | ||
|
|
||
| def set_effect_ui(self, islands_ui: IslandsEffectUI): | ||
| islands_ui.apply_clicked.connect(self._on_apply_clicked) | ||
|
|
||
| def _on_apply_clicked(self): | ||
| if not self.is_active(): | ||
| return | ||
| if self._typed_state.data.mode == IslandsSegmentationMode.KEEP_LARGEST_ISLAND: | ||
| self.effect.keep_largest_island() | ||
| elif self._typed_state.data.mode == IslandsSegmentationMode.REMOVE_SMALL_ISLANDS: | ||
| self.effect.remove_small_islands(self._typed_state.data.minimum_size) | ||
| elif self._typed_state.data.mode == IslandsSegmentationMode.SPLIT_TO_SEGMENTS: | ||
| self.effect.split_islands_to_segments() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum, auto | ||
|
|
||
| from trame_server.utils.typed_state import TypedState | ||
| from trame_vuetify.widgets.vuetify3 import VBtn, VContainer, VNumberInput, VSelect | ||
| from undo_stack import Signal | ||
|
|
||
|
|
||
| class IslandsSegmentationMode(Enum): | ||
| KEEP_LARGEST_ISLAND = auto() | ||
| REMOVE_SMALL_ISLANDS = auto() | ||
| SPLIT_TO_SEGMENTS = auto() | ||
|
|
||
|
|
||
| @dataclass | ||
| class IslandsState: | ||
| mode: IslandsSegmentationMode = IslandsSegmentationMode.KEEP_LARGEST_ISLAND | ||
| minimum_size: int = 1000 | ||
|
|
||
|
|
||
| class IslandsEffectUI(VContainer): | ||
| apply_clicked = Signal() | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(classes="fill-width", **kwargs) | ||
| self._typed_state = TypedState(self.state, IslandsState) | ||
|
|
||
| self.labels = { | ||
| IslandsSegmentationMode.KEEP_LARGEST_ISLAND: "Keep largest island", | ||
| IslandsSegmentationMode.REMOVE_SMALL_ISLANDS: "Remove small islands", | ||
| IslandsSegmentationMode.SPLIT_TO_SEGMENTS: "Split to segments", | ||
| } | ||
|
|
||
| with self: | ||
| VSelect( | ||
| v_model=self._typed_state.name.mode, | ||
| items=( | ||
| [ | ||
| {"title": self.labels[mode], "value": self._typed_state.encode(mode)} | ||
| for mode in IslandsSegmentationMode | ||
| ], | ||
| ), | ||
| hide_details=True, | ||
| density="compact", | ||
| label="Mode", | ||
| ) | ||
| VNumberInput( | ||
| v_model=self._typed_state.name.minimum_size, | ||
| label="Minimum size", | ||
| disabled=( | ||
| f"{self._typed_state.name.mode} !== {self._typed_state.encode(IslandsSegmentationMode.REMOVE_SMALL_ISLANDS)}", | ||
| ), | ||
| min=(0,), | ||
| hide_details=True, | ||
| density="compact", | ||
| classes="mt-5", | ||
| ) | ||
| VBtn("Apply", prepend_icon="mdi-check-outline", block=True, click=self.apply_clicked, classes="mt-5") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import pytest | ||
|
|
||
| from examples.viewer_lib.logic import IslandsEffectLogic | ||
| from examples.viewer_lib.ui import ( | ||
| IslandsEffectUI, | ||
| IslandsSegmentationMode, | ||
| MedicalViewerLayout, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def effect_ui(a_server): | ||
| with MedicalViewerLayout(a_server, is_drawer_visible=True) as ui, ui.drawer: | ||
| return IslandsEffectUI() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def effect_logic(a_server, a_slicer_app, effect_ui): | ||
| logic = IslandsEffectLogic(a_server, a_slicer_app) | ||
| logic.set_effect_ui(effect_ui) | ||
| return logic | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("island_mode", list(IslandsSegmentationMode)) | ||
| def test_can_apply_island_effect( | ||
| effect_logic, | ||
| effect_ui, | ||
| a_segmentation_nifti_file_path, | ||
| a_segmentation_editor, | ||
| a_slicer_app, | ||
| a_volume_node, | ||
| island_mode, | ||
| ): | ||
| segmentation_node = a_slicer_app.io_manager.load_segmentation(a_segmentation_nifti_file_path) | ||
| a_segmentation_editor.set_active_segmentation(segmentation_node, a_volume_node) | ||
| effect_logic.set_active() | ||
| effect_ui._typed_state.data.mode = island_mode | ||
| effect_ui.apply_clicked() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from undo_stack import UndoStack | ||
|
|
||
| from trame_slicer.segmentation import SegmentationEffectIslands | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def a_segmentation_spheres_file_path(a_data_folder) -> Path: | ||
| return a_data_folder.joinpath("segmentation_spheres.nii.gz") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def effect(a_segmentation_editor): | ||
| effect: SegmentationEffectIslands = a_segmentation_editor.set_active_effect_type(SegmentationEffectIslands) | ||
| return effect | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def segment_id(a_segmentation_editor): | ||
| return a_segmentation_editor.get_nth_segment_id(0) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def set_up(a_slicer_app, a_volume_node, a_segmentation_editor, a_segmentation_spheres_file_path): | ||
| a_slicer_app.display_manager.show_volume(a_volume_node, vr_preset="MR-Default") | ||
| segmentation_node = a_slicer_app.io_manager.load_segmentation(a_segmentation_spheres_file_path) | ||
| a_segmentation_editor.set_active_segmentation(segmentation_node, a_volume_node) | ||
| undo_stack = UndoStack() | ||
| a_segmentation_editor.set_undo_stack(undo_stack) | ||
|
|
||
|
|
||
| def get_segment_array(a_segmentation_editor, segment_id): | ||
| return a_segmentation_editor.get_segment_labelmap(segment_id, as_numpy_array=True) | ||
|
|
||
|
|
||
| def test_keep_biggest_island(a_segmentation_editor, effect, segment_id): | ||
| assert effect.is_active | ||
| source_array = get_segment_array(a_segmentation_editor, segment_id) | ||
| effect.keep_largest_island() | ||
| segment_array = get_segment_array(a_segmentation_editor, segment_id) | ||
| # Assert that application created new zeros | ||
| assert np.count_nonzero(source_array) > np.count_nonzero(segment_array) | ||
|
|
||
|
|
||
| def test_split_islands_to_segments(a_segmentation_editor, effect): | ||
| assert effect.is_active | ||
| effect.split_islands_to_segments() | ||
| assert len(a_segmentation_editor.get_segment_ids()) == 3 | ||
|
|
||
|
|
||
| def test_with_0_min_voxel_size_remove_small_islands_does_nothing(a_segmentation_editor, effect, segment_id): | ||
| assert effect.is_active | ||
| source_array = get_segment_array(a_segmentation_editor, segment_id) | ||
| effect.remove_small_islands(1) | ||
| segment_array = get_segment_array(a_segmentation_editor, segment_id) | ||
| assert np.array_equal(source_array, segment_array) | ||
|
|
||
|
|
||
| def test_with_max_min_voxel_size_remove_small_islands_removes_all_islands(a_segmentation_editor, effect, segment_id): | ||
| assert effect.is_active | ||
| effect.remove_small_islands(int(1e15)) | ||
| segment_array = get_segment_array(a_segmentation_editor, segment_id) | ||
| assert np.array_equal(segment_array, np.zeros_like(segment_array)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of set_up, it's better to give fixtures explicit names and to return / yield something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I don't need it to return anything, I just need to setup the scene with the necessary content
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still better to have a more explicit name and have fixtures that return something (and not have autouse fixtures).
The main advantage is to be able to factorize the different fixtures in different tests to avoid repetition.
You can ignore for now, but I will probably have a go at the different segment effects test to factorize common initialization to simplify the tests.