def test_reader_plugin_can_return_null_layer_sentinel(test_plugin_manager, add_implementation): from napari_plugin_engine import napari_hook_implementation from napari.plugins import hook_specifications test_plugin_manager.project_name = 'napari' test_plugin_manager.add_hookspecs(hook_specifications) with pytest.raises(ValueError) as e: read_data_with_plugins('') assert 'No plugin found capable of reading' in str(e) @napari_hook_implementation(tryfirst=True) def napari_get_reader(path): def _reader(path): return [(None, )] return _reader add_implementation(napari_get_reader) layer_data, _ = read_data_with_plugins('', plugin_manager=test_plugin_manager) assert layer_data is not None assert len(layer_data) == 0
def test_iter_reader_plugins(plugin_manager): """Test safe iteration through reader plugins even with errors. `napari_bad_plugin2` is a plugin that loads fine but throws an error during file-reading. this tests that we can gracefully handle that. """ # the plugin loads fine, so there should be no exceptions yet. assert 'napari_bad_plugin2' not in plugin_manager._exceptions # we want 'napari_bad_plugin2' to be the first plugin called. But # `napari_test_plugin` will be in line first... Until we can # reorder the call-order of plugins, (#1023), this line serves to prevent # that good plugin from running. plugin_manager.set_blocked('napari_test_plugin') # but when we try to read an image path, it will raise an IOError. # we want to catch and store that IOError, and then move on to give other # plugins chance to return layer_data layer_data = read_data_with_plugins('image.ext', plugin_manager) # the good plugins (like "napari_test_plugin") should return layer_data assert layer_data # but the exception from `bad_plugin2` should now be stored. assert 'napari_bad_plugin2' in plugin_manager._exceptions # we can print out a string that should have the explanation of the error. exception_string = plugin_manager.format_exceptions('napari_bad_plugin2') assert 'IOError' in exception_string assert "napari_get_reader" in exception_string
def test_iter_reader_plugins(plugin_manager): """Test safe iteration through reader plugins even with errors. `napari_bad_plugin2` is a plugin that loads fine but throws an error during file-reading. this tests that we can gracefully handle that. """ # the plugin loads fine, so there should be no exceptions yet. assert 'napari_bad_plugin2' not in PLUGIN_ERRORS # make sure 'napari_bad_plugin2' gets called first plugin_manager.hooks.napari_get_reader.bring_to_front( ['napari_bad_plugin2']) # but when we try to read an image path, it will raise an IOError. # we want to catch and store that IOError, and then move on to give other # plugins chance to return layer_data layer_data = read_data_with_plugins('image.ext', plugin_manager) # the good plugins (like "napari_test_plugin") should return layer_data assert layer_data # but the exception from `bad_plugin2` should now be stored. assert 'napari_bad_plugin2' in PLUGIN_ERRORS # we can print out a string that should have the explanation of the error. exception_string = format_exceptions('napari_bad_plugin2') assert 'IOError' in exception_string assert "napari_get_reader" in exception_string
def test_builtin_reader_plugin_url(builtins): layer_data, _ = io.read_data_with_plugins( ['https://samples.fiji.sc/FakeTracks.tif']) assert layer_data is not None assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple)
def test_reader_plugin_can_return_null_layer_sentinel(napari_plugin_manager, ): from napari_plugin_engine import napari_hook_implementation with pytest.raises(ValueError) as e: read_data_with_plugins('/') assert 'No plugin found capable of reading' in str(e) class sample_plugin: @napari_hook_implementation(tryfirst=True) def napari_get_reader(path): def _reader(path): return [(None, )] return _reader napari_plugin_manager.register(sample_plugin) layer_data, _ = read_data_with_plugins('') assert layer_data is not None assert len(layer_data) == 0
def test_builtin_reader_plugin(viewer_factory, builtin_plugin_manager): """Test the builtin reader plugin reads a temporary file.""" with NamedTemporaryFile(suffix='.tif', delete=False) as tmp: data = np.random.rand(20, 20) io.imsave(tmp.name, data) tmp.seek(0) layer_data = read_data_with_plugins(tmp.name, builtin_plugin_manager) assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert np.allclose(data, layer_data[0][0]) view, viewer = viewer_factory() viewer.add_path(tmp.name) assert np.allclose(viewer.layers[0].data, data)
def test_reader_plugin_can_return_null_layer_sentinel(napari_plugin_manager, monkeypatch): from napari_plugin_engine import napari_hook_implementation class sample_plugin: @napari_hook_implementation(tryfirst=True) def napari_get_reader(path): def _reader(path): return [(None, )] return _reader napari_plugin_manager.register(sample_plugin) monkeypatch.setattr(io, 'plugin_manager', napari_plugin_manager) layer_data, _ = io.read_data_with_plugins([''], stack=False) assert layer_data is not None assert len(layer_data) == 0
def test_builtin_reader_plugin_npy(): """Test the builtin reader plugin reads a temporary npy file.""" with NamedTemporaryFile(suffix='.npy', delete=False) as tmp: data = np.random.rand(20, 20) np.save(tmp.name, data) tmp.seek(0) layer_data, _ = io.read_data_with_plugins(tmp.name, 'builtins') assert layer_data is not None assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert np.allclose(data, layer_data[0][0]) viewer = ViewerModel() viewer.open(tmp.name, plugin='builtins') assert np.allclose(viewer.layers[0].data, data)
def test_builtin_reader_plugin_csv(viewer_factory, tmpdir): """Test the builtin reader plugin reads a temporary file.""" tmp = os.path.join(tmpdir, 'test.csv') column_names = ['index', 'axis-0', 'axis-1'] table = np.random.random((5, 3)) data = table[:, 1:] # Write csv file io.write_csv(tmp, table, column_names=column_names) layer_data = read_data_with_plugins(tmp) assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert layer_data[0][2] == 'points' assert np.allclose(data, layer_data[0][0]) view, viewer = viewer_factory() viewer.open(tmp, plugin='builtins') assert np.allclose(viewer.layers[0].data, data)
def test_builtin_reader_plugin(builtins: DynamicPlugin): """Test the builtin reader plugin reads a temporary file.""" with NamedTemporaryFile(suffix='.tif', delete=False) as tmp: data = np.random.rand(20, 20) utils.io.imsave(tmp.name, data) tmp.seek(0) layer_data, _ = io.read_data_with_plugins([tmp.name], builtins.name, stack=False) assert layer_data is not None assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert np.allclose(data, layer_data[0][0]) viewer = ViewerModel() viewer.open(tmp.name, plugin=builtins.name) assert np.allclose(viewer.layers[0].data, data)
def test_builtin_reader_plugin(viewer_factory): """Test the builtin reader plugin reads a temporary file.""" from napari.plugins import plugin_manager plugin_manager.hooks.napari_get_reader.bring_to_front(['builtins']) with NamedTemporaryFile(suffix='.tif', delete=False) as tmp: data = np.random.rand(20, 20) io.imsave(tmp.name, data) tmp.seek(0) layer_data = read_data_with_plugins(tmp.name) assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert np.allclose(data, layer_data[0][0]) view, viewer = viewer_factory() viewer.open_path(tmp.name) assert np.allclose(viewer.layers[0].data, data)
def test_builtin_reader_plugin_csv(tmpdir, builtins: DynamicPlugin): """Test the builtin reader plugin reads a temporary file.""" tmp = os.path.join(tmpdir, 'test.csv') column_names = ['index', 'axis-0', 'axis-1'] table = np.random.random((5, 3)) data = table[:, 1:] # Write csv file utils.io.write_csv(tmp, table, column_names=column_names) layer_data, _ = io.read_data_with_plugins([tmp], builtins.name, stack=False) assert layer_data is not None assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert layer_data[0][2] == 'points' assert np.allclose(data, layer_data[0][0]) viewer = ViewerModel() viewer.open(tmp, plugin=builtins.name) assert np.allclose(viewer.layers[0].data, data)
def test_nonsense_path_is_ok(plugin_manager): """Test that a path with no readers doesn't throw an exception.""" layer_data = read_data_with_plugins('image.NONsense', plugin_manager) assert not layer_data