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 1 commit
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
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")

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("blue") == (0, 0, 255)
assert _name_to_rgb("green") == (0, 255, 0)
with pytest.raises(ValueError, match="Unknown color name"):
_name_to_rgb("strange")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): New test added for _name_to_rgb function

The test covers basic color conversions and an error case. Consider adding more edge cases or boundary conditions to ensure comprehensive coverage.

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)
    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("")

Loading