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_nD_labels(qtbot): """Test adding nD labels image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.randint(20, size=(6, 10, 15)) viewer.add_labels(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_add_labels(qtbot): """Test adding labels image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.randint(20, size=(10, 15)) viewer.add_labels(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 # Close the viewer viewer.window.close()
def test_nD_labels(qtbot): """Test adding nD labels image.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.randint(20, size=(6, 10, 15)) viewer.add_labels(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 initialize_masks( self, viewer: Viewer, im_shape: tuple, cell_masks: list = [], initial_state: Union[np.ndarray, str] = 'good', accepted_layer_name: str = 'accepted_mask', rejected_layer_name: str = 'rejected_mask', ): self.masks = ContourManager(contours=cell_masks, im_shape=im_shape, initial_state=initial_state) # put the masks in their respective labels layers rejected_mask = self.masks.make_rejected_mask() self.rejected_labels = viewer.add_labels(rejected_mask, name=rejected_layer_name, visible=False) accepted_mask = self.masks.make_accepted_mask() self.accepted_labels = viewer.add_labels(accepted_mask, name=accepted_layer_name)
def test_labels_undo_redo(qtbot): """Test undoing/redoing on the labels layer.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) data = np.zeros((50, 50), dtype=np.uint8) data[:5, :5] = 1 data[5:10, 5:10] = 2 data[25:, 25:] = 3 labels = viewer.add_labels(data) l1 = labels.data.copy() # fill labels.fill((30, 30), 3, 42) l2 = labels.data.copy() assert not np.array_equal(l1, l2) # undo labels.undo() assert np.array_equal(l1, labels.data) # redo labels.redo() assert np.array_equal(l2, labels.data) # history limit labels._history_limit = 1 labels.fill((0, 0), 1, 3) l3 = labels.data.copy() assert not np.array_equal(l3, l2) labels.undo() assert np.array_equal(l2, labels.data) # cannot undo as limit exceded labels.undo() assert np.array_equal(l2, labels.data) # Close the viewer viewer.window.close()
def test_data_change_ndisplay_labels(qtbot): """Test change data calls for labels layer with ndisplay change.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = np.random.randint(20, size=(10, 15, 8)) layer = viewer.add_labels(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()
# 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, face_color='green', opacity=0.5, name='neurons', visible=False) # labels = label(mask) labels_layer = viewer.add_labels(labels, name='rois') # polygons_edit = shapes_layer.data # np.save('data/neurofinder/polygons_edit.npy', polygons_edit)
""" 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)
scale=[3, 1, 1]) ch3 = viewer.add_image(cells[:, 3, :, :], name='yellow spots', contrast_limits=(420.0, 500.0), colormap='yellow', blending='additive', scale=[3, 1, 1]) ch4 = viewer.add_image(cells[:, 4, :, :], name='DAPI', colormap='blue', contrast_limits=(500.0, 1300.0), blending='additive', scale=[3, 1, 1]) # add brightfield brightfield = viewer.add_image(-cells[:, 0, :, :], name='brightfield', colormap='gray', scale=[3, 1, 1]) #labels = np.zeros([cells.shape[i] for i in [0, 2, 3]]) labels_layer = viewer.add_labels(labels, name='annotations', scale=[3, 1, 1]) # labels = labels_layer.data # imsave('data/allen_cell/labels.tif', labels.astype('uint32'), plugin='tifffile', # photometric='minisblack')
""" Displays the allen brain reference atlas at 10 um resolution """ from skimage.io import imread from napari import Viewer, gui_qt brain = imread('data/allen_brain/average_template_25.tif') annotation = imread('data/allen_brain/annotation_25.tif') with gui_qt(): # create an empty viewer viewer = Viewer() # add the brain brain_layer = viewer.add_image(brain, name='brain') # add the annotations annotation_layer = viewer.add_labels(annotation, name='annotation')
"""Test converting an image to a pyramid. """ import numpy as np from napari import Viewer, gui_qt from skimage.transform import pyramid_gaussian from skimage import data from scipy import ndimage as ndi blobs = data.binary_blobs(length=9_000, volume_fraction=0.1, n_dim=2) labeled = ndi.label(blobs)[0] print(labeled.shape) with gui_qt(): viewer = Viewer() viewer.add_labels(labeled)
@viewer.bind_key('r') def rerun(viewer): blobs = viewer.layers['input'].data distance = viewer.layers['distance'].data local_maxi = viewer.layers['markers'].data print('Number of markers: ', len(local_maxi)) local_maxi_image = np.zeros(blobs.shape, dtype='bool') for cord in local_maxi: local_maxi_image[tuple(np.round(cord).astype(int))] = True markers = ndi.label(local_maxi_image)[0] labels = watershed(-distance, markers, mask=blobs) viewer.layers['output'].data = labels # add the raw image viewer.add_image(image, name='raw', colormap='gray') # add the input image viewer.add_image(blobs.astype('float'), name='input', colormap='gray') # add the distance image viewer.add_image(distance, name='distance', colormap='gray') # add the resulting labels image viewer.add_labels(labels, name='output') # add the ground truth as a labels image viewer.add_labels(gt, name='gt') # add the markers viewer.add_points(local_maxi, face_color='blue', size=3, name='markers')