Skip to content

Commit

Permalink
fix: Fix selection of custom label colors for napari 0.5.0 (#1138)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Improved handling of colormap properties for ROI and mask
visualization based on different Napari versions.
  
- **Tests**
- Added a new function `get_color_dict` to retrieve color information in
test cases, ensuring compatibility across Napari versions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
Czaki authored Jul 12, 2024
1 parent ef25753 commit 4bd99b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions package/PartSeg/common_gui/napari_image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ def get_roi_view_parameters(self, image_info: ImageInfo) -> ColorInfo:
return res

def set_roi_colormap(self, image_info) -> None:
if _napari_ge_5:
image_info.roi.colormap = self.get_roi_view_parameters(image_info)
return
if _napari_ge_4_13:
image_info.roi.color = self.get_roi_view_parameters(image_info)
return
Expand Down Expand Up @@ -583,7 +586,10 @@ def set_mask(self, mask: Optional[np.ndarray] = None, image: Optional[Image] = N
else:
image_info.mask.data = mask_marker
image_info.mask.metadata["valid"] = True
image_info.mask.color = self.mask_color()
if _napari_ge_5:
image_info.mask.colormap = self.mask_color()
else:
image_info.mask.color = self.mask_color()
image_info.mask.opacity = self.mask_opacity()
image_info.mask.visible = self.mask_chk.isChecked()
self._toggle_mask_chk_visibility()
Expand All @@ -602,7 +608,10 @@ def update_mask_parameters(self):
for image_info in self.image_info.values():
if image_info.mask is not None:
image_info.mask.opacity = opacity
image_info.mask.color = colormap
if _napari_ge_5:
image_info.mask.colormap = colormap
else:
image_info.mask.color = colormap

def set_image(self, image: Optional[Image] = None):
self.image_info = {}
Expand Down
13 changes: 11 additions & 2 deletions package/tests/test_PartSeg/test_napari_image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@

if NAPARI_GE_5_0:
EXPECTED_RANGE = (0, 0, 1)

def get_color_dict(layer):
return layer.colormap.color_dict

else:
EXPECTED_RANGE = (0, 1, 1)

def get_color_dict(layer):
return layer.color


def test_image_info():
image_info = ImageInfo(Image(np.zeros((10, 10)), image_spacing=(1, 1), axes_order="XY"), [])
Expand Down Expand Up @@ -159,9 +166,11 @@ def test_mask_rendering(self, base_settings, image_view, qtbot, tmp_path):
base_settings.set_in_profile("mask_presentation_opacity", 0.5)
assert image_view.image_info[str(tmp_path / "test2.tiff")].mask.opacity == 0.5
base_settings.set_in_profile("mask_presentation_color", (255, 0, 0))
assert np.all(image_view.image_info[str(tmp_path / "test2.tiff")].mask.color[1] == (1, 0, 0, 1))
assert np.all(get_color_dict(image_view.image_info[str(tmp_path / "test2.tiff")].mask)[1] == (1, 0, 0, 1))
base_settings.set_in_profile("mask_presentation_color", (128, 0, 0))
assert np.allclose(image_view.image_info[str(tmp_path / "test2.tiff")].mask.color[1], (128 / 255, 0, 0, 1))
assert np.allclose(
get_color_dict(image_view.image_info[str(tmp_path / "test2.tiff")].mask)[1], (128 / 255, 0, 0, 1)
)

assert not image_view.image_info[str(tmp_path / "test2.tiff")].mask.visible
with qtbot.waitSignal(image_view.mask_chk.stateChanged):
Expand Down

0 comments on commit 4bd99b8

Please sign in to comment.