Skip to content

Commit 1c5c925

Browse files
committed
Add tests
1 parent 1be161d commit 1c5c925

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

package/PartSegImage/image.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ class ChannelInfoFull:
4747
def __post_init__(self):
4848
if not isinstance(self.color_map, (str, np.ndarray)):
4949
self.color_map = np.array(self.color_map, dtype=np.uint8)
50+
if isinstance(self.color_map, np.ndarray):
51+
if self.color_map.dtype != np.uint8:
52+
message = f"Colormap as array need to be uint8, not {self.color_map.dtype}"
53+
raise ValueError(message)
5054
if self.color_map.ndim in {1, 2}:
5155
if self.color_map.shape[0] not in {3, 4}:
52-
message = f"1d color map need to have 3 or 4 elements (RGB or RGBA), not {self.color_map.shape}"
56+
message = f"Color map need to have 3 or 4 elements (RGB or RGBA), not {self.color_map.shape}"
5357
raise ValueError(message)
5458
else:
5559
message = f"Colormap as sequence need to be 1d or 2d array, not {self.color_map.shape}"

package/tests/test_PartSegImage/test_image.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import os
33

44
import numpy as np
5+
import numpy.testing as npt
56
import pytest
67
from skimage.morphology import diamond
78

8-
from PartSegImage import Channel, ChannelInfo, Image, ImageWriter, TiffImageReader
9+
from PartSegImage import Channel, ChannelInfo, ChannelInfoFull, Image, ImageWriter, TiffImageReader
910
from PartSegImage.image import FRAME_THICKNESS, _hex_to_rgb, _name_to_rgb
1011

1112

@@ -728,3 +729,44 @@ def test_name_to_rgb_vispy():
728729
# This test check mapping not defined in fallback dictionary
729730
pytest.importorskip("vispy", reason="vispy not installed")
730731
assert _name_to_rgb("lime") == (0, 255, 0)
732+
733+
734+
class TestChannelInfoFull:
735+
RED_DEF = (255, 0, 0)
736+
RED_2D_REF = [[0, 255], [0, 0], [0, 0]]
737+
738+
def test_str(self):
739+
obj = ChannelInfoFull(name="test", color_map="gray", contrast_limits=(0, 20))
740+
assert obj.name == "test"
741+
assert obj.color_map == "gray"
742+
assert obj.contrast_limits == (0, 20)
743+
744+
@pytest.mark.parametrize("color_map", [tuple(RED_DEF), np.array(RED_DEF, dtype=np.uint8), list(RED_DEF)])
745+
def test_1d_colormap(self, color_map):
746+
obj = ChannelInfoFull(name="test", color_map=color_map, contrast_limits=(0, 20))
747+
assert obj.name == "test"
748+
assert np.all(obj.contrast_limits == (0, 20))
749+
assert isinstance(obj.color_map, np.ndarray)
750+
assert obj.color_map.shape == (3,)
751+
npt.assert_array_equal(obj.color_map, self.RED_DEF)
752+
753+
@pytest.mark.parametrize("color_map", [tuple(RED_2D_REF), np.array(RED_2D_REF, dtype=np.uint8), list(RED_2D_REF)])
754+
def test_2d_colormap(self, color_map):
755+
obj = ChannelInfoFull(name="test", color_map=color_map, contrast_limits=(0, 20))
756+
assert obj.name == "test"
757+
assert np.all(obj.contrast_limits == (0, 20))
758+
assert isinstance(obj.color_map, np.ndarray)
759+
assert obj.color_map.shape == (3, 2)
760+
npt.assert_array_equal(obj.color_map, self.RED_2D_REF)
761+
762+
def test_wrong_dtype_colormap(self):
763+
with pytest.raises(ValueError, match="Colormap as array need to be uint8"):
764+
ChannelInfoFull(name="test", color_map=np.array(self.RED_DEF, dtype=np.float32), contrast_limits=(0, 20))
765+
766+
def test_wrong_colors_colormap(self):
767+
with pytest.raises(ValueError, match="Color map need to have 3 or 4 elements"):
768+
ChannelInfoFull(name="test", color_map=np.array([[0, 0], [0, 0]], dtype=np.uint8), contrast_limits=(0, 20))
769+
770+
def test_wrong_dimesnsion_colormap(self):
771+
with pytest.raises(ValueError, match="Colormap as sequence need to"):
772+
ChannelInfoFull(name="test", color_map=np.array([[[0]], [[0]]], dtype=np.uint8), contrast_limits=(0, 20))

0 commit comments

Comments
 (0)