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", colormaps.Colormap(['g', 'm', 'y'])) assert viewer.add_image(data, colormap=cmap_tuple) # as dict cmap_dict = {"your_colormap": colormaps.Colormap(['g', 'r', 'y'])} assert viewer.add_image(data, colormap=cmap_dict) # as Colormap instance fire = colormaps.AVAILABLE_COLORMAPS['fire'] assert viewer.add_image(data, colormap=fire) # 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)
import numpy as np import dask.array as da from napari.components import ViewerModel from napari.utils.colormaps import colormaps, ensure_colormap_tuple from napari.utils.misc import ensure_sequence_of_iterables, ensure_iterable import pytest base_colormaps = colormaps.CYMRGB two_colormaps = colormaps.MAGENTA_GREEN green_cmap = colormaps.simple_colormaps['green'] red_cmap = colormaps.simple_colormaps['red'] fire = colormaps.AVAILABLE_COLORMAPS['fire'] cmap_tuple = ("my_colormap", colormaps.Colormap(['g', 'm', 'y'])) cmap_dict = {"your_colormap": colormaps.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 }), # split single RGB image