def test_sequence_of_iterables_raises(): with pytest.raises(ValueError): # the length argument asserts a specific length ensure_sequence_of_iterables(((0, 1), ), length=4) # BEWARE: only the first element of a nested sequence is checked. with pytest.raises(AssertionError): iterable = (None, (0, 1), (0, 2)) result = iter(ensure_sequence_of_iterables(iterable)) assert next(result) is None
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_sequence_of_iterables(input, expected): """Test ensure_sequence_of_iterables returns a sequence of iterables.""" zipped = zip( range(3), ensure_sequence_of_iterables(input, repeat_empty=True), expected, ) for i, result, expectation in zipped: assert result == expectation
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_sequence_of_iterables_no_repeat_empty(): assert ensure_sequence_of_iterables([], repeat_empty=False) == [] with pytest.raises(ValueError): ensure_sequence_of_iterables([], repeat_empty=False, length=3)
def test_sequence_of_iterables_allow_none(): input = [(1, 2), None] assert ensure_sequence_of_iterables(input, allow_none=True) == input