Skip to content
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

chore: Add missed code from #1191 #1197

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions package/PartSegImage/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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
Expand Down Expand Up @@ -990,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()
23 changes: 22 additions & 1 deletion package/tests/test_PartSegImage/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -705,3 +705,24 @@ 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")

Czaki marked this conversation as resolved.
Show resolved Hide resolved

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, 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 check mapping not defined in fallback dictionary
pytest.importorskip("vispy", reason="vispy not installed")
assert _name_to_rgb("lime") == (0, 255, 0)
Loading