def test_add_image_colormap_variants(): """Test adding image with all valid colormap argument types.""" viewer = ViewerModel() np.random.seed(0) data = np.random.random((10, 15)) # as string assert viewer.add_image(data, colormap='green') # as string that is valid, but not a default colormap assert viewer.add_image(data, colormap='cubehelix') # as tuple cmap_tuple = ("my_colormap", Colormap(['g', 'm', 'y'])) assert viewer.add_image(data, colormap=cmap_tuple) # as dict cmap_dict = {"your_colormap": Colormap(['g', 'r', 'y'])} assert viewer.add_image(data, colormap=cmap_dict) # as Colormap instance blue_cmap = AVAILABLE_COLORMAPS['blue'] assert viewer.add_image(data, colormap=blue_cmap) # string values must be known colormap types with pytest.raises(KeyError) as err: viewer.add_image(data, colormap='nonsense') assert 'Colormap "nonsense" not found' in str(err.value) # lists are only valid with channel_axis with pytest.raises(TypeError) as err: viewer.add_image(data, colormap=['green', 'red']) assert "did you mean to specify a 'channel_axis'" in str(err.value)
def test_colormap_equality(): colors = np.array([[0, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) cmap_1 = Colormap(colors, name='testing', controls=[0, 0.75, 1]) cmap_2 = Colormap(colors, name='testing', controls=[0, 0.75, 1]) cmap_3 = Colormap(colors, name='testing', controls=[0, 0.25, 1]) assert cmap_1 == cmap_2 assert cmap_1 != cmap_3
def test_can_accept_napari_colormaps_in_dict(): """Test that we can accept vispy colormaps in a dictionary""" colors_a = np.array([[0, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) colors_b = np.array([[0, 0, 0, 1], [1, 0, 0, 1], [0, 0, 1, 1]]) napari_cmap_a = Colormap(colors_a) napari_cmap_b = Colormap(colors_b) cmap = ensure_colormap({'a': napari_cmap_a, 'b': napari_cmap_b}) assert isinstance(cmap, Colormap) np.testing.assert_almost_equal(cmap.colors, colors_a) assert cmap.name == 'a'
def test_linear_colormap(): """Test a linear colormap.""" colors = np.array([[0, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) cmap = Colormap(colors, name='testing') assert cmap.name == 'testing' assert cmap.interpolation == 'linear' assert len(cmap.controls) == len(colors) np.testing.assert_almost_equal(cmap.colors, colors) np.testing.assert_almost_equal(cmap.map([0.75]), [[0, 0.5, 0.5, 1]])
def test_binned_colormap(): """Test a binned colormap.""" colors = np.array([[0, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) cmap = Colormap(colors, name='testing', interpolation='zero') assert cmap.name == 'testing' assert cmap.interpolation == 'zero' assert len(cmap.controls) == len(colors) + 1 np.testing.assert_almost_equal(cmap.colors, colors) np.testing.assert_almost_equal(cmap.map([0.4]), [[0, 1, 0, 1]])
def test_can_accept_napari_colormaps(): """Test that we can accept napari colormaps.""" colors = np.array([[0, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) napari_cmap = Colormap(colors) cmap = ensure_colormap(napari_cmap) assert isinstance(cmap, Colormap) np.testing.assert_almost_equal(cmap.colors, colors)
def test_wrong_number_control_points(): """Test wrong number of control points raises an error.""" colors = np.array( [[0, 0, 0, 1], [0, 0.5, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]] ) with pytest.raises(ValueError): Colormap(colors, name='testing', controls=[0, 0.75, 1])
def test_non_ascending_control_points(): """Test non ascending control points raises an error.""" colors = np.array( [[0, 0, 0, 1], [0, 0.5, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]] ) with pytest.raises(ValueError): Colormap(colors, name='testing', controls=[0, 0.75, 0.25, 1])
def test_can_accept_napari_colormap_name_tuple(): """Test that we can accept napari colormap named type.""" colors = np.array([[0, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) napari_cmap = Colormap(colors) cmap = ensure_colormap(('special_name', napari_cmap)) assert isinstance(cmap, Colormap) np.testing.assert_almost_equal(cmap.colors, colors) assert cmap.name == 'special_name'
from napari.utils.colormaps import ( AVAILABLE_COLORMAPS, CYMRGB, MAGENTA_GREEN, SIMPLE_COLORMAPS, Colormap, ensure_colormap, ) from napari.utils.misc import ensure_iterable, ensure_sequence_of_iterables base_colormaps = CYMRGB two_colormaps = MAGENTA_GREEN green_cmap = SIMPLE_COLORMAPS['green'] red_cmap = SIMPLE_COLORMAPS['red'] blue_cmap = AVAILABLE_COLORMAPS['blue'] cmap_tuple = ("my_colormap", Colormap(['g', 'm', 'y'])) cmap_dict = {"your_colormap": Colormap(['g', 'r', 'y'])} MULTI_TUPLES = [[0.3, 0.7], [0.1, 0.9], [0.3, 0.9], [0.4, 0.9], [0.2, 0.9]] # data shape is (15, 10, 5) unless otherwise set # channel_axis = -1 is implied unless otherwise set multi_channel_test_data = [ # basic multichannel image ((), {}), # single channel ((15, 10, 1), {}), # two channels ((15, 10, 2), {}), # Test adding multichannel image with color channel set. ((5, 10, 15), {'channel_axis': 0}),
# Survivor -> cyan # Deceased -> red SURVIVOR_COLORMAP = np.array([[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.8, 1.0]]) # steal some colors, mwah ha ha... ID_COLORMAP = AVAILABLE_COLORMAPS['turbo'].colors ID_COLORMAP = ID_COLORMAP[::4, :] id_colormap = ModuloColormap(ID_COLORMAP, controls=colormap_bins(ID_COLORMAP), interpolation='zero', name='tracking_id') state_colormap = Colormap(STATE_COLORMAP, controls=colormap_bins(STATE_COLORMAP), interpolation='zero', name='tracking_state') survivor_colormap = Colormap(SURVIVOR_COLORMAP, controls=colormap_bins(SURVIVOR_COLORMAP), interpolation='zero', name='tracking_survivors') colormaps = { 'ID': id_colormap, 'parent': id_colormap, 'root': id_colormap, 'states': state_colormap, 'survivor': survivor_colormap }
def test_colormap_recreate(): c_map = Colormap("black") Colormap(**c_map.dict())