Skip to content

Commit

Permalink
Add mised code from #1191
Browse files Browse the repository at this point in the history
  • Loading branch information
Czaki committed Sep 22, 2024
1 parent c417134 commit f174e95
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
50 changes: 24 additions & 26 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
10 changes: 9 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,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")

0 comments on commit f174e95

Please sign in to comment.