def test_nD_volume_launch_order(qtbot): """Test adding nD volume when viewer launched with 3D.""" order = [1, 0, 2, 3] viewer = Viewer(ndisplay=3, order=order) view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) shape = (6, 10, 15, 20) data = np.random.random(shape) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert viewer.layers[0]._data_view.shape == tuple( shape[o] for o in order[-3:] ) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 4 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 1 assert viewer.dims.order == order # Close the viewer viewer.window.close()
def test_nD_image(qtbot): """Test adding nD image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.random((6, 10, 15)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 3 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 1 # Flip dims order displayed viewer.dims.order = [0, 2, 1] assert viewer.dims.order == [0, 2, 1] # Flip dims order including non-displayed viewer.dims.order = [1, 0, 2] assert viewer.dims.order == [1, 0, 2] # Switch to 3D rendering viewer.dims.ndisplay = 3 assert np.sum(view.dims._displayed_sliders) == 0 assert viewer.dims.ndisplay == 3 viewer.dims.ndisplay = 2 assert viewer.dims.ndisplay == 2 # Close the viewer viewer.window.close()
def test_screenshot(qtbot): "Test taking a screenshot" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) # Add image data = np.random.random((10, 15)) viewer.add_image(data) # Add labels data = np.random.randint(20, size=(10, 15)) viewer.add_labels(data) # Add points data = 20 * np.random.random((10, 2)) viewer.add_points(data) # Add vectors data = 20 * np.random.random((10, 2, 2)) viewer.add_vectors(data) # Add shapes data = 20 * np.random.random((10, 4, 2)) viewer.add_shapes(data) # Take screenshot screenshot = viewer.screenshot() assert screenshot.ndim == 3 # Close the viewer viewer.window.close()
def test_5D_image_3D_rendering(qtbot): """Test 3D rendering of a 5D image.""" np.random.seed(0) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) # add 4D image data data = np.random.random((2, 10, 12, 13, 14)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 5 assert viewer.dims.ndisplay == 2 assert viewer.layers[0]._data_view.ndim == 2 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 3 # switch to 3D rendering viewer.dims.ndisplay = 3 assert viewer.dims.ndisplay == 3 assert viewer.layers[0]._data_view.ndim == 3 assert np.sum(view.dims._displayed_sliders) == 2 # Close the viewer viewer.window.close()
def test_range_one_images_and_points(qtbot): """Test adding images with range one dimensions and points. Intially no sliders should be present as the images have range one dimensions. On adding the points the sliders should be displayed. """ np.random.seed(0) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) # add 5D image data with range one dimensions data = np.random.random((1, 1, 1, 100, 200)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 5 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 0 # now add 5D points data - check extra sliders have been created points = np.floor(5 * np.random.random((1000, 5))).astype(int) points[:, -2:] = 20 * points[:, -2:] viewer.add_points(points) assert np.all(viewer.layers[1].data == points) assert len(viewer.layers) == 2 assert viewer.dims.ndim == 5 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 3 # Close the viewer viewer.window.close()
def test_add_pyramid(qtbot): """Test adding image pyramid.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) shapes = [(40, 20), (20, 10), (10, 5)] np.random.seed(0) data = [np.random.random(s) for s in shapes] viewer.add_image(data, is_pyramid=True) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 2 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 0 # Switch to 3D rendering mode and back to 2D rendering mode viewer.dims.ndisplay = 3 assert viewer.dims.ndisplay == 3 viewer.dims.ndisplay = 2 assert viewer.dims.ndisplay == 2 # Close the viewer viewer.window.close()
def test_nD_volume_launch(qtbot): """Test adding nD volume when viewer launched with 3D.""" viewer = Viewer(ndisplay=3) view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) shape = (6, 10, 15, 20) data = np.random.random(shape) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert viewer.layers[0]._data_view.shape == shape[-3:] assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 4 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 1 # Switch to 3D rendering assert viewer.dims.ndisplay == 3 viewer.dims.ndisplay = 2 assert viewer.dims.ndisplay == 2 # Close the viewer viewer.window.close()
def test_add_volume(qtbot): """Test adding volume.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.random((10, 15, 20)) viewer.add_image(data) viewer.dims.ndisplay = 3 assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 3 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 0 # Switch to 3D rendering mode and back to 2D rendering mode viewer.dims.ndisplay = 3 assert viewer.dims.ndisplay == 3 viewer.dims.ndisplay = 2 assert viewer.dims.ndisplay == 2 # Close the viewer viewer.window.close()
def test_4D_5D_images(qtbot): """Test adding 4D followed by 5D image layers to the viewer. Intially only 2 sliders should be present, then a third slider should be created. """ np.random.seed(0) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) # add 4D image data data = np.random.random((2, 6, 30, 40)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 4 assert view.dims.nsliders == 2 assert np.sum(view.dims._displayed) == 2 # now add 5D image data - check an extra slider has been created data = np.random.random((4, 4, 5, 30, 40)) viewer.add_image(data) assert np.all(viewer.layers[1].data == data) assert len(viewer.layers) == 2 assert viewer.dims.ndim == 5 assert view.dims.nsliders == 3 assert np.sum(view.dims._displayed) == 3 # Close the viewer viewer.window.close()
def test_range_one_image(qtbot): """Test adding an image with a range one dimensions. There should be no slider shown for the axis corresponding to the range one dimension. """ np.random.seed(0) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) # add 5D image data with range one dimensions data = np.random.random((1, 1, 1, 100, 200)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 5 assert view.dims.nsliders == 3 assert np.sum(view.dims._displayed) == 0 # now add 5D points data - check extra sliders have been created points = np.floor(5 * np.random.random((1000, 5))).astype(int) points[:, -2:] = 20 * points[:, -2:] viewer.add_points(points) assert np.all(viewer.layers[1].data == points) assert len(viewer.layers) == 2 assert viewer.dims.ndim == 5 assert view.dims.nsliders == 3 assert np.sum(view.dims._displayed) == 3 # Close the viewer viewer.window.close()
def test_dask_2D(qtbot): """Test adding 2D dask image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) da.random.seed(0) data = da.random.random((10, 15)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) # Close the viewer viewer.window.close()
def test_zarr_dask_nD(qtbot): """Test adding nD zarr image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) 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) # Close the viewer viewer.window.close()
def test_xarray_nD(qtbot): """Test adding nD xarray image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) 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) # Close the viewer viewer.window.close()
def test_image_dytpes(qtbot, dtype): """Test different dtype images.""" np.random.seed(0) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) # 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) # Close the viewer viewer.window.close()
def test_zarr_nD(qtbot): """Test adding nD zarr image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) 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) # Close the viewer viewer.window.close()
def test_image_rendering(qtbot): """Test 3D image with different rendering.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) data = np.random.random((20, 20, 20)) layer = viewer.add_image(data) assert layer.rendering == 'mip' # Change rendering property layer.rendering = 'translucent' assert layer.rendering == 'translucent' # Change rendering property layer.rendering = 'attenuated_mip' assert layer.rendering == 'attenuated_mip' layer.attenuation = 0.2 assert layer.attenuation == 0.2 # Change rendering property layer.rendering = 'iso' assert layer.rendering == 'iso' layer.iso_threshold = 0.3 assert layer.iso_threshold == 0.3 # Change rendering property layer.rendering = 'additive' assert layer.rendering == 'additive' # Close the viewer viewer.window.close()
def __init__( self, image: np.ndarray, viewer: Viewer, nbins: int = 20, xlabel: str = '', ylabel: str = '', image_layer_name: str = 'image', name: str = 'histogram', ): self.image = image self.image_layer = viewer.add_image(image, name=image_layer_name, visible=False) # create the histogram self.nbins = nbins counts, values = self._calculate_histogram() self.histogram_widget = HistogramWidget(values, counts, xlabel=xlabel, ylabel=ylabel) self._threshold = 0 # update the SNR image and connect the event self.on_snr_changed() self.histogram_widget.threshold_changed_callbacks.append( self.on_snr_changed) # add the histogram dock widget to the viewer viewer.window.add_dock_widget(self.histogram_widget, name=name)
def test_add_image(qtbot): """Test adding image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.random((10, 15)) layer = viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 2 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 0 # Switch to 3D rendering mode and back to 2D rendering mode viewer.dims.ndisplay = 3 assert viewer.dims.ndisplay == 3 viewer.dims.ndisplay = 2 assert viewer.dims.ndisplay == 2 # Run all class keybindings for func in layer.class_keymap.values(): func(layer) # Close the viewer viewer.window.close()
def test_update(qtbot): import time data = np.random.random((512, 512)) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) layer = viewer.add_image(data) def layer_update(*, update_period, num_updates): # number of times to update for k in range(num_updates): time.sleep(update_period) dat = np.random.random((512, 512)) layer.data = dat assert layer.data.all() == dat.all() viewer.update(layer_update, update_period=0.01, num_updates=100) # if we do not sleep, main thread closes before update thread finishes and many qt components get cleaned time.sleep(3) # Close the viewer viewer.window.close()
def test_change_image_dims(qtbot): """Test changing the dims and shape of an image layer in place and checking the numbers of sliders and their ranges changes appropriately. """ np.random.seed(0) viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) # add 3D image data data = np.random.random((10, 30, 40)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 3 assert view.dims.nsliders == 1 assert np.sum(view.dims._displayed) == 1 # switch number of displayed dimensions viewer.layers[0].data = data[0] assert np.all(viewer.layers[0].data == data[0]) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 2 assert view.dims.nsliders == 0 assert np.sum(view.dims._displayed) == 0 # switch number of displayed dimensions viewer.layers[0].data = data[:6] assert np.all(viewer.layers[0].data == data[:6]) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 3 assert view.dims.nsliders == 1 assert np.sum(view.dims._displayed) == 1 # change the shape of the data viewer.layers[0].data = data[:3] assert np.all(viewer.layers[0].data == data[:3]) assert len(viewer.layers) == 1 assert viewer.dims.ndim == 3 assert view.dims.nsliders == 1 assert np.sum(view.dims._displayed) == 1 # Close the viewer viewer.window.close()
def test_nD_image(qtbot): """Test adding nD image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.random((6, 10, 15)) viewer.add_image(data) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 3 assert view.dims.nsliders == 1 assert np.sum(view.dims._displayed) == 1 # Close the viewer viewer.window.close()
def test_nD_pyramid(qtbot): """Test adding nD image pyramid.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) shapes = [(8, 40, 20), (4, 20, 10), (4, 10, 5)] np.random.seed(0) data = [np.random.random(s) for s in shapes] viewer.add_image(data, is_pyramid=True) assert np.all(viewer.layers[0].data == data) assert len(viewer.layers) == 1 assert view.layers.vbox_layout.count() == 2 * len(viewer.layers) + 2 assert viewer.dims.ndim == 3 assert view.dims.nsliders == viewer.dims.ndim assert np.sum(view.dims._displayed_sliders) == 1 # Flip dims order displayed viewer.dims.order = [0, 2, 1] assert viewer.dims.order == [0, 2, 1] # Flip dims order including non-displayed viewer.dims.order = [1, 0, 2] assert viewer.dims.order == [1, 0, 2] # Switch to 3D rendering viewer.dims.ndisplay = 3 assert np.sum(view.dims._displayed_sliders) == 0 assert viewer.dims.ndisplay == 3 viewer.dims.ndisplay = 2 assert viewer.dims.ndisplay == 2 # Close the viewer viewer.window.close()
def test_big_2D_image(qtbot): """Test big 2D image with axis exceeding max texture size.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) shape = (20_000, 10) data = np.random.random(shape) layer = viewer.add_image(data, is_pyramid=False) visual = view.layer_to_visual[layer] assert visual.node is not None if visual.MAX_TEXTURE_SIZE_2D is not None: ds = np.ceil(np.divide(shape, visual.MAX_TEXTURE_SIZE_2D)).astype(int) assert np.all(layer._scale_view == ds) # Close the viewer viewer.window.close()
def test_data_change_ndisplay_image(qtbot): """Test change data calls for image layer with ndisplay change.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.random((10, 15, 8)) layer = viewer.add_image(data) visual = view.layer_to_visual[layer] @patch.object(visual, '_on_data_change', wraps=visual._on_data_change) def test_ndisplay_change(mocked_method, ndisplay=3): viewer.dims.ndisplay = ndisplay mocked_method.assert_called_once() # Switch to 3D rendering mode and back to 2D rendering mode test_ndisplay_change(ndisplay=3) test_ndisplay_change(ndisplay=2) # Close the viewer viewer.window.close()
""" Displays a timeseries of a z-stack during mitosis """ from skimage.io import imread from napari import Viewer, gui_qt mitosis = imread('data/mitosis/mitosis.tif') with gui_qt(): # create an empty viewer viewer = Viewer() # add the first channel to a red channel red = viewer.add_image(mitosis[:, :, 0, :, :], name='red', contrast_limits=(1500.0, 15000.0), colormap='red', scale=[1, 10, 1, 1]) red.blending = 'additive' # add the first channel to a green channel green = viewer.add_image(mitosis[:, :, 1, :, :], name='green', contrast_limits=(1500.0, 15000.0), colormap='green', scale=[1, 10, 1, 1]) green.blending = 'additive'
To create nice dropdown menus with magicgui, it's best (but not required) to use Enums. Here we make an Enum class for all of the image math operations we want to allow. """ add = numpy.add subtract = numpy.subtract multiply = numpy.multiply divide = numpy.divide with gui_qt(): # create a viewer and add a couple image layers viewer = Viewer() viewer.add_image(numpy.random.rand(20, 20), name="Layer 1") viewer.add_image(numpy.random.rand(20, 20), name="Layer 2") # use the magic decorator! This takes a function, and generates a widget instance # using the function signature. Note that we aren't returning a napari Image layer, # but instead a numpy array which we want napari to interperate as Image data. @magicgui(call_button="execute") def image_arithmetic(layerA: Image, operation: Operation, layerB: Image) -> ImageData: """Add, subtracts, multiplies, or divides to image layers with equal shape.""" return operation.value(layerA.data, layerB.data) # add our new magicgui widget to the viewer viewer.window.add_dock_widget(image_arithmetic) # keep the dropdown menus in the gui in sync with the layer model
movie = imread('data/neurofinder/timeseries.tif') mean = imread('data/neurofinder/mean.tif') mask = imread('data/neurofinder/mask.tif') # localcorr = imread('data/neurofinder/localcorr.tif') # centers = np.loadtxt("data/neurofinder/centers.csv", delimiter=",") polygons = np.load('data/neurofinder/polygons_edit.npy', allow_pickle=True) with gui_qt(): # create an empty viewer viewer = Viewer() # add the timeseries movie_layer = viewer.add_image(movie, name='timeseries', contrast_limits=(0.0, 600.0), colormap='gray') mean_layer = viewer.add_image(mean, name='mean', contrast_limits=(0.0, 600.0), colormap='gray', visible=False) # # lc_layer = viewer.add_image(localcorr, name='localcorr', contrast_limits=(0.35, 1.0), colormap='gray', visible=False) # # centers_layer = viewer.add_points(centers, name='centers', edge_width=0, face_color='green', visible=False, opacity=0.5) # shapes_layer = viewer.add_shapes(polygons, shape_type='polygon', edge_width=0,
""" Dynamically load irregularly shapes images of ants and bees """ import numpy as np from dask_image.imread import imread from dask.cache import Cache from napari import Viewer, gui_qt cache = Cache(2e9) # Leverage two gigabytes of memory cache.register() base_name = 'data/kaggle-nuclei/fixes/stage1_train/*' images = imread(base_name + '/images/image_gray.tif') labels = imread(base_name + '/labels/label.tif') print(images.shape) with gui_qt(): # create an empty viewer viewer = Viewer() # add the images image_layer = viewer.add_image(images, name='nuceli', colormap='gray') labels_layer = viewer.add_labels(labels, name='labels', opacity=0.5)
""" Displays an MRI volume """ from napari import Viewer, gui_qt import numpy as np import zarr file_name = 'data/MRI/synthesized_FLASH25_pyr.zarr' mri = zarr.open(file_name, mode='r') mri = np.asarray(mri['3']) mri = np.clip(mri, 10, 10000) - 10 print(mri.shape) with gui_qt(): # create an empty viewer viewer = Viewer() # add the mri layer = viewer.add_image(mri, name='mri', colormap='gray')
from skimage.io import imread, imsave import numpy as np from napari import Viewer, gui_qt cells = imread('data-njs/allen_cell/cells.tif') labels = imread('data-njs/allen_cell/labels.tif') print(cells.shape) with gui_qt(): # create an empty viewer viewer = Viewer() ch1 = viewer.add_image(cells[:, 1, :, :], name='red spots', contrast_limits=(400.0, 520.0), colormap='red', blending='additive', scale=[3, 1, 1]) ch2 = viewer.add_image(cells[:, 2, :, :], name='cells', colormap='green', contrast_limits=(410.0, 700.0), blending='additive', scale=[3, 1, 1]) ch3 = viewer.add_image(cells[:, 3, :, :], name='yellow spots', contrast_limits=(420.0, 500.0), colormap='yellow', blending='additive',