def test_viewer_open_no_plugin(tmp_path): viewer = ViewerModel() fname = tmp_path / 'gibberish.gbrsh' fname.touch() with pytest.raises(ValueError, match='No plugin found capable of reading'): # will default to builtins viewer.open(fname)
def test_viewer_open_no_plugin(tmp_path): viewer = ViewerModel() fname = tmp_path / 'gibberish.gbrsh' fname.touch() with pytest.raises(ValueError) as e: viewer.open(fname) assert 'No plugin found capable of reading' in str(e.value)
def test_dask_2D(): """Test adding 2D dask image.""" viewer = ViewerModel() da.random.seed(0) data = da.random.random((10, 15)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data)
def test_viewer_open_no_plugin(tmp_path): viewer = ViewerModel() fname = tmp_path / 'gibberish.gbrsh' fname.touch() with pytest.raises(ValueError) as e: # will default to builtins viewer.open(fname) assert "Plugin 'builtins' does not support file(s)" in str(e.value)
def test_xarray_nD(): """Test adding nD xarray image.""" viewer = ViewerModel() np.random.seed(0) data = np.random.random((10, 15, 6, 16)) xdata = xr.DataArray(data, dims=['t', 'z', 'y', 'x']) viewer.add_image(xdata) assert np.all(viewer.layers[0].data == xdata)
def test_zarr_nD(): """Test adding nD zarr image.""" viewer = ViewerModel() data = zarr.zeros((200, 100, 50), chunks=(40, 20, 10)) data[53:63, 10:20, :] = 1 # If passing a zarr file directly, must pass contrast_limits viewer.add_image(data, contrast_limits=[0, 1]) assert np.all(viewer.layers[0].data == data)
def test_zarr_dask_nD(): """Test adding nD zarr image.""" viewer = ViewerModel() data = zarr.zeros((200, 100, 50), chunks=(40, 20, 10)) data[53:63, 10:20, :] = 1 zdata = da.from_zarr(data) viewer.add_image(zdata) assert np.all(viewer.layers[0].data == zdata)
def test_add_layers_with_plugins(layer_datum): """Test that add_layers_with_plugins adds the expected layer types.""" with patch( "napari.components.add_layers_mixin.read_data_with_plugins", MagicMock(return_value=[layer_datum]), ): v = ViewerModel() v._add_layers_with_plugins('mock_path') layertypes = [l._type_string for l in v.layers] assert layertypes == [layer_datum[2]]
def test_add_layers_with_plugins(layer_datum): """Test that add_layers_with_plugins adds the expected layer types.""" with patch( "napari.plugins.io.read_data_with_plugins", MagicMock(return_value=([layer_datum], _testimpl)), ): v = ViewerModel() v._add_layers_with_plugins('mock_path') layertypes = [layer._type_string for layer in v.layers] assert layertypes == [layer_datum[2]] expected_source = Source(path='mock_path', reader_plugin='testimpl') assert all(lay.source == expected_source for lay in v.layers)
def test_add_layers_with_plugins_and_kwargs(layer_data, kwargs): """Test that _add_layers_with_plugins kwargs override plugin kwargs. see also: napari.components._test.test_prune_kwargs """ with patch( "napari.components.add_layers_mixin.read_data_with_plugins", MagicMock(return_value=layer_data), ): v = ViewerModel() v._add_layers_with_plugins('mock_path', kwargs=kwargs) for layer in v.layers: for key, val in kwargs.items(): assert getattr(layer, key) == val
def test_receive_return_proxy_object(): """Test that an""" viewer = ViewerModel() viewer.add_image(np.random.random((20, 20))) pv = PublicOnlyProxy(viewer) # simulates behavior from outside of napari with patch('napari.utils.misc.ROOT_DIR', new='/some/other/package'): # the recursion means this layer will be a Proxy Object on __getitem__ layer = pv.layers[-1] assert isinstance(layer, PublicOnlyProxy) # remove and add it back, should be fine add_layer = getattr(pv, 'add_layer') add_layer(layer) assert len(viewer.layers) == 2
def test_dims_sorter_with_reordered_init(qtbot): viewer = ViewerModel() viewer.dims.order = (1, 0) dim_sorter = QtDimsSorter(viewer) qtbot.addWidget(dim_sorter) assert tuple(dim_sorter.axes_list) == tuple(viewer.dims.order)
def test_add_layers_with_plugins_and_kwargs(layer_data, kwargs): """Test that _add_layers_with_plugins kwargs override plugin kwargs. see also: napari.components._test.test_prune_kwargs """ with patch( "napari.components.add_layers_mixin.read_data_with_plugins", MagicMock(return_value=layer_data), ): v = ViewerModel() v._add_layers_with_plugins('mock_path', kwargs=kwargs) for layer in v.layers: for key, val in kwargs.items(): assert getattr(layer, key) == val # if plugins don't provide "name", it falls back to path name if 'name' not in kwargs: assert layer.name.startswith('mock_path')
def test_add_layers_with_plugins_and_kwargs(layer_data, kwargs): """Test that _add_layers_with_plugins kwargs override plugin kwargs. see also: napari.components._test.test_prune_kwargs """ with patch( "napari.plugins.io.read_data_with_plugins", MagicMock(return_value=(layer_data, _testimpl)), ): v = ViewerModel() v._add_layers_with_plugins(['mock_path'], kwargs=kwargs, stack=False) expected_source = Source(path='mock_path', reader_plugin='testimpl') for layer in v.layers: for key, val in kwargs.items(): assert getattr(layer, key) == val # if plugins don't provide "name", it falls back to path name if 'name' not in kwargs: assert layer.name.startswith('mock_path') assert layer.source == expected_source
def test_dims_sorter(qtbot): viewer = ViewerModel() dim_sorter = QtDimsSorter(viewer) qtbot.addWidget(dim_sorter) assert tuple(dim_sorter.axes_list) == (0, 1) viewer.dims.axis_labels = ('y', 'x') assert tuple(dim_sorter.axes_list) == ('y', 'x') dim_sorter.axes_list.move(1, 0) assert tuple(dim_sorter.axes_list) == ('x', 'y') assert tuple(viewer.dims.order) == (1, 0)
def test_viewer_open(): """Test that a plugin to returning nothing adds nothing to the Viewer.""" viewer = ViewerModel() assert len(viewer.layers) == 0 viewer.open('mock_path') assert len(viewer.layers) == 1 viewer.open('mock_path', stack=True) assert len(viewer.layers) == 2
def test_viewer_open(): """Test that a plugin to returning an image adds stuff to the viewer.""" viewer = ViewerModel() assert len(viewer.layers) == 0 viewer.open('mock_path.tif') assert len(viewer.layers) == 1 # The name should be taken from the path name, stripped of extension assert viewer.layers[0].name == 'mock_path' # stack=True also works... and very long names are truncated viewer.open('mock_path.tif', stack=True) assert len(viewer.layers) == 2 assert viewer.layers[1].name.startswith('mock_path')
def test_image_dytpes(dtype): """Test different dtype images.""" np.random.seed(0) viewer = ViewerModel() # add dtype image data data = np.random.randint(20, size=(30, 40)).astype(dtype) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) # add dtype multiscale data data = [ np.random.randint(20, size=(30, 40)).astype(dtype), np.random.randint(20, size=(15, 20)).astype(dtype), ] viewer.add_image(data, multiscale=True) assert np.all(viewer.layers[1].data == data)
def test_viewer_method(): viewer = PublicOnlyProxy(ViewerModel()) assert viewer.add_points() is not None
def test_public_proxy_limited_to_napari(patched_root_dir): """Test that the recursive public proxy goes no farther than napari.""" viewer = ViewerModel() viewer.add_points(None) pv = PublicOnlyProxy(viewer) assert not isinstance(pv.layers[0].data, PublicOnlyProxy)
def test_array_from_proxy_objects(patched_root_dir): """Test that the recursive public proxy goes no farther than napari.""" viewer = ViewerModel() viewer.add_points(None) pv = PublicOnlyProxy(viewer) assert isinstance(np.array(pv.dims.displayed, dtype=int), np.ndarray)
def test_plugin_returns_nothing(): """Test that a plugin to returning nothing adds nothing to the Viewer.""" v = ViewerModel() v._add_layers_with_plugins('mock_path') assert not v.layers