def test_multichannel(shape, kwargs): """Test adding multichannel image.""" viewer = ViewerModel() np.random.seed(0) data = np.random.random(shape or (15, 10, 5)) channel_axis = kwargs.pop('channel_axis', -1) viewer.add_image(data, channel_axis=channel_axis, **kwargs) # make sure the right number of layers got added n_channels = data.shape[channel_axis] assert len(viewer.layers) == n_channels for i in range(n_channels): # make sure that the data has been divided into layers assert np.all(viewer.layers[i].data == data.take(i, axis=channel_axis)) # make sure colors have been assigned properly if 'colormap' not in kwargs: if n_channels == 1: assert viewer.layers[i].colormap.name == 'gray' elif n_channels == 2: assert viewer.layers[i].colormap.name == two_colormaps[i] else: assert viewer.layers[i].colormap.name == base_colormaps[i] if 'blending' not in kwargs: assert (viewer.layers[i].blending == 'translucent_no_depth' if i == 0 else 'additive') for key, expectation in kwargs.items(): # broadcast exceptions if key in { 'scale', 'translate', 'rotate', 'shear', 'contrast_limits', 'metadata', 'experimental_clipping_planes', }: expectation = ensure_sequence_of_iterables(expectation, repeat_empty=True) elif key == 'colormap' and expectation is not None: if isinstance(expectation, list): exp = [ensure_colormap(c).name for c in expectation] else: exp = ensure_colormap(expectation).name expectation = ensure_iterable(exp) else: expectation = ensure_iterable(expectation) expectation = [v for i, v in zip(range(i + 1), expectation)] result = getattr(viewer.layers[i], key) if key == 'colormap': # colormaps are tuples of (name, cmap) result = result.name if isinstance(result, np.ndarray): np.testing.assert_almost_equal(result, expectation[i]) else: assert result == expectation[i]
def test_multichannel(shape, kwargs): """Test adding multichannel image.""" viewer = ViewerModel() np.random.seed(0) data = np.random.random(shape or (15, 10, 5)) channel_axis = kwargs.pop('channel_axis', -1) viewer.add_image(data, channel_axis=channel_axis, **kwargs) # make sure the right number of layers got added n_channels = data.shape[channel_axis] assert len(viewer.layers) == n_channels for i in range(n_channels): # make sure that the data has been divided into layers assert np.all(viewer.layers[i].data == data.take(i, axis=channel_axis)) # make sure colors have been assigned properly if 'colormap' not in kwargs: if n_channels < 3: assert viewer.layers[i].colormap[0] == two_colormaps[i] else: assert viewer.layers[i].colormap[0] == base_colormaps[i] if 'blending' not in kwargs: assert viewer.layers[i].blending == 'additive' for key, expectation in kwargs.items(): # broadcast expections if key in {'scale', 'translate', 'contrast_limits', 'metadata'}: expectation = ensure_sequence_of_iterables(expectation) else: expectation = ensure_iterable(expectation) expectation = [v for i, v in zip(range(i + 1), expectation)] result = getattr(viewer.layers[i], key) if key == 'colormap': # colormaps are tuples of (name, cmap) result = result[0] assert np.all(result == expectation[i])
def test_ensure_iterable(input, expected): """Test test_ensure_iterable returns an iterable.""" zipped = zip(range(3), ensure_iterable(input), expected) for i, result, expectation in zipped: assert result == expectation