From f174e954af4c4dda9edd34d5c5823083a6068b39 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Sun, 22 Sep 2024 22:15:37 +0200 Subject: [PATCH 1/5] Add mised code from #1191 --- package/PartSegImage/image.py | 50 +++++++++---------- package/tests/test_PartSegImage/test_image.py | 10 +++- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/package/PartSegImage/image.py b/package/PartSegImage/image.py index 5bbd94926..7a11ca8e8 100644 --- a/package/PartSegImage/image.py +++ b/package/PartSegImage/image.py @@ -250,9 +250,11 @@ def __init__( @staticmethod def _adjust_channel_info( - channel_info: list[ChannelInfo | ChannelInfoFull] | None, channel_array: list[np.ndarray] + channel_info: list[ChannelInfo | ChannelInfoFull] | None, + channel_array: list[np.ndarray], + default_colors=("red", "blue", "green", "yellow", "magenta", "cyan"), ) -> list[ChannelInfoFull]: - default_colors = cycle(["red", "blue", "green", "yellow", "magenta", "cyan"]) + default_colors = cycle(default_colors) if channel_info is None: ranges = [(np.min(x), np.max(x)) for x in channel_array] return [ @@ -262,32 +264,28 @@ def _adjust_channel_info( channel_info = channel_info[: len(channel_array)] - res = [] - - for i, ch_inf in enumerate(channel_info): - res.append( - ChannelInfoFull( - name=ch_inf.name or f"channel {i+1}", - color_map=( - ch_inf.color_map if ch_inf.color_map is not None else next(default_colors) # skipcq: PTC-W0063 - ), - contrast_limits=( - ch_inf.contrast_limits - if ch_inf.contrast_limits is not None - else (np.min(channel_array[i]), np.max(channel_array[i])) - ), - ) + res = [ + ChannelInfoFull( + name=ch_inf.name or f"channel {i+1}", + color_map=( + ch_inf.color_map if ch_inf.color_map is not None else next(default_colors) # skipcq: PTC-W0063 + ), + contrast_limits=( + ch_inf.contrast_limits + if ch_inf.contrast_limits is not None + else (np.min(channel_array[i]), np.max(channel_array[i])) + ), ) - - for i, arr in enumerate(channel_array[len(res) :], start=len(channel_info)): - res.append( - ChannelInfoFull( - name=f"channel {i+1}", - color_map=next(default_colors), # skipcq: PTC-W0063 - contrast_limits=(np.min(arr), np.max(arr)), - ) + for i, ch_inf in enumerate(channel_info) + ] + res.extend( + ChannelInfoFull( + name=f"channel {i+1}", + color_map=next(default_colors), # skipcq: PTC-W0063 + contrast_limits=(np.min(arr), np.max(arr)), ) - + for i, arr in enumerate(channel_array[len(res) :], start=len(channel_info)) + ) return res @staticmethod diff --git a/package/tests/test_PartSegImage/test_image.py b/package/tests/test_PartSegImage/test_image.py index 596701508..b19d63f15 100644 --- a/package/tests/test_PartSegImage/test_image.py +++ b/package/tests/test_PartSegImage/test_image.py @@ -6,7 +6,7 @@ from skimage.morphology import diamond from PartSegImage import Channel, ChannelInfo, Image, ImageWriter, TiffImageReader -from PartSegImage.image import FRAME_THICKNESS, _hex_to_rgb +from PartSegImage.image import FRAME_THICKNESS, _hex_to_rgb, _name_to_rgb class TestImageBase: @@ -705,3 +705,11 @@ def test_hex_to_rgb(): assert _hex_to_rgb("#B00") == (187, 0, 0) with pytest.raises(ValueError, match="Invalid hex code format"): _hex_to_rgb("#b000") + + +def test_name_to_rgb(): + assert _name_to_rgb("red") == (255, 0, 0) + assert _name_to_rgb("blue") == (0, 0, 255) + assert _name_to_rgb("green") == (0, 255, 0) + with pytest.raises(ValueError, match="Unknown color name"): + _name_to_rgb("strange") From f9ff1f24f07d5283f353a9d33827eab559d66112 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Mon, 23 Sep 2024 11:30:48 +0200 Subject: [PATCH 2/5] improve code --- package/PartSegImage/image.py | 28 +++++++++++-------- package/tests/test_PartSegImage/test_image.py | 14 +++++++++- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/package/PartSegImage/image.py b/package/PartSegImage/image.py index 7a11ca8e8..1981a63dd 100644 --- a/package/PartSegImage/image.py +++ b/package/PartSegImage/image.py @@ -988,19 +988,25 @@ def _name_to_rgb(name: str) -> tuple[int, int, int]: :param str name: The color name :return: A tuple containing the RGB values (R, G, B) """ + name = name.lower() if name not in _NAMED_COLORS: raise ValueError(f"Unknown color name: {name}") return _hex_to_rgb(_NAMED_COLORS[name]) -_NAMED_COLORS = { - "red": "#FF0000", - "green": "#00FF00", - "blue": "#0000FF", - "yellow": "#FFFF00", - "cyan": "#00FFFF", - "magenta": "#FF00FF", - "white": "#FFFFFF", - "black": "#000000", - "orange": "#FFA500", -} +try: + from vispy.color import get_color_dict +except ImportError: # pragma: no cover + _NAMED_COLORS = { + "red": "#FF0000", + "green": "#008000", + "blue": "#0000FF", + "yellow": "#FFFF00", + "cyan": "#00FFFF", + "magenta": "#FF00FF", + "white": "#FFFFFF", + "black": "#000000", + "orange": "#FFA500", + } +else: + _NAMED_COLORS = get_color_dict() diff --git a/package/tests/test_PartSegImage/test_image.py b/package/tests/test_PartSegImage/test_image.py index b19d63f15..43955bd30 100644 --- a/package/tests/test_PartSegImage/test_image.py +++ b/package/tests/test_PartSegImage/test_image.py @@ -709,7 +709,19 @@ def test_hex_to_rgb(): def test_name_to_rgb(): assert _name_to_rgb("red") == (255, 0, 0) + assert _name_to_rgb("Red") == (255, 0, 0) + assert _name_to_rgb("RED") == (255, 0, 0) assert _name_to_rgb("blue") == (0, 0, 255) - assert _name_to_rgb("green") == (0, 255, 0) + assert _name_to_rgb("green") == (0, 128, 0) + assert _name_to_rgb("white") == (255, 255, 255) + assert _name_to_rgb("black") == (0, 0, 0) + assert _name_to_rgb("yellow") == (255, 255, 0) with pytest.raises(ValueError, match="Unknown color name"): _name_to_rgb("strange") + with pytest.raises(ValueError, match="Unknown color name"): + _name_to_rgb("") + + +def test_name_to_rgb_vispy(): + # This test fail if vispy is not installed + assert _name_to_rgb("lime") == (0, 255, 0) From 60f536d58afd9e350077c9f7d0e6c2a6daf1f788 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Mon, 23 Sep 2024 14:55:59 +0200 Subject: [PATCH 3/5] update tests --- package/tests/test_PartSegImage/test_image.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package/tests/test_PartSegImage/test_image.py b/package/tests/test_PartSegImage/test_image.py index 43955bd30..f3895527d 100644 --- a/package/tests/test_PartSegImage/test_image.py +++ b/package/tests/test_PartSegImage/test_image.py @@ -722,6 +722,7 @@ def test_name_to_rgb(): _name_to_rgb("") +@pytest.mark.skipif(pytest.importorskip("vispy", reason="vispy not installed")) def test_name_to_rgb_vispy(): - # This test fail if vispy is not installed + # This test check mapping not defined in fallback dictionary assert _name_to_rgb("lime") == (0, 255, 0) From a6355d6a7090a9e50dcd0d8b73009cae5dd62e11 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Mon, 23 Sep 2024 15:06:46 +0200 Subject: [PATCH 4/5] fix skipif --- package/tests/test_PartSegImage/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/tests/test_PartSegImage/test_image.py b/package/tests/test_PartSegImage/test_image.py index f3895527d..368f4e7e3 100644 --- a/package/tests/test_PartSegImage/test_image.py +++ b/package/tests/test_PartSegImage/test_image.py @@ -722,7 +722,7 @@ def test_name_to_rgb(): _name_to_rgb("") -@pytest.mark.skipif(pytest.importorskip("vispy", reason="vispy not installed")) +@pytest.mark.skipif(pytest.importorskip("vispy"), reason="vispy not installed") def test_name_to_rgb_vispy(): # This test check mapping not defined in fallback dictionary assert _name_to_rgb("lime") == (0, 255, 0) From 51def5b44f115a14557fbe97c7ce06d78ef72c7e Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Mon, 23 Sep 2024 15:08:12 +0200 Subject: [PATCH 5/5] change to proper importskip --- package/tests/test_PartSegImage/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/tests/test_PartSegImage/test_image.py b/package/tests/test_PartSegImage/test_image.py index 368f4e7e3..8ac148624 100644 --- a/package/tests/test_PartSegImage/test_image.py +++ b/package/tests/test_PartSegImage/test_image.py @@ -722,7 +722,7 @@ def test_name_to_rgb(): _name_to_rgb("") -@pytest.mark.skipif(pytest.importorskip("vispy"), reason="vispy not installed") def test_name_to_rgb_vispy(): # This test check mapping not defined in fallback dictionary + pytest.importorskip("vispy", reason="vispy not installed") assert _name_to_rgb("lime") == (0, 255, 0)