def test_multiple_images(qtbot): """Test creating viewer with mutliple images.""" np.random.seed(0) data_a = np.random.random((10, 15)) data_b = np.random.random((20, 15)) data_c = np.random.random((10, 25)) viewer = view(data_a=data_a, data_b=data_b, data_c=data_c) qtbot.addWidget(viewer.window.qt_viewer) assert len(viewer.layers) == 3 assert np.all(viewer.layers['data_a'].data == data_a) assert np.all(viewer.layers['data_b'].data == data_b) assert np.all(viewer.layers['data_c'].data == data_c) # Close the viewer viewer.window.close()
def test_view_function(qtbot): """Test creating viewer with image data.""" np.random.seed(0) data = np.random.random((10, 15)) viewer = view(data) qtbot.addWidget(viewer.window.qt_viewer) assert viewer.title == 'napari' assert viewer.window.qt_viewer.viewer == viewer assert len(viewer.layers) == 1 assert np.all(viewer.layers[0].data == data) assert viewer.dims.ndim == 2 # Close the viewer viewer.window.close()
""" Display multiple image layers using the add_image API and then reorder them using the layers swap method and remove one """ from skimage import data from skimage.color import rgb2gray from napari import view, gui_qt with gui_qt(): # create the viewer with several image layers viewer = view( astronaut=rgb2gray(data.astronaut()), photographer=data.camera(), coins=data.coins(), moon=data.moon(), ) # remove the coins layer viewer.layers.remove('coins') # swap the order of astronaut and moon viewer.layers['astronaut', 'moon'] = viewer.layers['moon', 'astronaut']
Display a labels layer above of an image layer using the add_labels and add_image APIs """ from skimage import data from skimage.filters import threshold_otsu from skimage.segmentation import clear_border from skimage.measure import label from skimage.morphology import closing, square, remove_small_objects import napari with napari.gui_qt(): image = data.coins()[50:-50, 50:-50] # apply threshold thresh = threshold_otsu(image) bw = closing(image > thresh, square(4)) # remove artifacts connected to image border cleared = remove_small_objects(clear_border(bw), 20) # label image regions label_image = label(cleared) # initialise viewer with coins image viewer = napari.view(coins=image, multichannel=False) viewer.layers[0].colormap = 'gray' # add the labels label_layer = viewer.add_labels(label_image, name='segmentation')
""" Test adding an image with a range one dimensions. There should be no slider shown for the axis corresponding to the range one dimension. """ import numpy as np from skimage import data import napari with napari.gui_qt(): np.random.seed(0) # image = 2 * np.random.random((20, 20, 3)) - 1.0 image = 20 * np.random.random((20, 20, 3)) - 10 print(image.min(), image.max()) image = np.clip(image, 0, 1) viewer = napari.view(image)
""" Display one 4-D image layer using the add_image API """ import numpy as np from skimage import data import napari with napari.gui_qt(): blobs = data.binary_blobs(length=128, blob_size_fraction=0.05, n_dim=3, volume_fraction=0.1).astype(float) viewer = napari.view(blobs.astype(float)) # create one random rectangle per "plane" planes = np.tile(np.arange(128).reshape((128, 1, 1)), (1, 2, 1)) corners = np.random.uniform(0, 128, size=(128, 2, 2)) shapes = np.concatenate((planes, corners), axis=2) base_cols = ['red', 'green', 'blue', 'white', 'yellow', 'magenta', 'cyan'] colors = np.random.choice(base_cols, size=128) layer = viewer.add_shapes( np.array(shapes), shape_type='rectangle', face_color=colors, name='sliced', )
""" Displays an image and sets the theme to 'light'. """ from skimage import data import napari with napari.gui_qt(): # create the viewer with an image viewer = napari.view(astronaut=data.astronaut(), multichannel=True, title='napari') # set the theme to 'light' viewer.theme = 'light'
""" Display one image using the add_image API. """ from skimage import data from skimage.color import rgb2gray import napari with napari.gui_qt(): # create the viewer with an image viewer = napari.view(astronaut=rgb2gray(data.astronaut()), title='napari example')
""" Display one 4-D image layer using the add_image API """ import dask.array as da import zarr import napari with napari.gui_qt(): data = zarr.zeros((102_000, 200, 210), chunks=(100, 200, 210)) data[53_000:53_100, 100:110, 110:120] = 1 array = da.from_zarr(data) print(array.shape) # For big data, we should specify the contrast_limits range, or napari will try # to find the min and max of the full image. viewer = napari.view(array, contrast_limits=[0, 1], multichannel=False)
#This path is to the top level of the magellan dataset (i.e. the one that contains the Full resolution folder) data_path = '/path/to/data' #open the dataset magellan = MagellanDataset(data_path) #read tiles or tiles + metadata by channel, slice, time, and position indices #img is a numpy array and md is a python dictionary img, img_metadata = magellan.read_image(channel_index=0, z_index=30, pos_index=20, read_metadata=True) #Alternatively, all data can be opened at once in a single dask array. Using dask arrays enables all_data to be #held in a single memory-mapped array (i.e. the data are not loaded in RAM until they are used, enabing a convenient #way to work with data larger than the computer's memory. Dask arrays also enable visulization in Napari (see below), #and allow for code to be prototyped on a small computers and scaled up to clusters without having to rewrite code. #More information can be found at https://dask.org/ all_data = magellan.as_array( stitched=True ) #returns an array with 5 dimensions corresponding to time-channel-z-y-x # all_data = magellan.as_array(stitched=False) #this version has a leading axis for position #dask array can be used just like numpy array #take max intenisty z projection of z stack at time point 0 in channel 0 max_intensity = np.max(all_data[0, 0], axis=0) #visualize data using napari--this example will likely updated as the napari API changes and improves with napari.gui_qt(): napari.view(all_data, clim_range=[0, 255])
""" Display one 4-D image layer using the add_image API """ import dask.array as da import zarr import napari with napari.gui_qt(): data = zarr.zeros((102_000, 200, 210), chunks=(100, 200, 210)) data[53_000:53_100, 100:110, 110:120] = 1 array = da.from_zarr(data) print(array.shape) # For big data, we should specify the clim range, or napari will try # to find the min and max of the full image. viewer = napari.view(array, clim_range=[0, 1], multichannel=False)
""" Display one 5-D image layer using the add_image API """ import numpy as np from skimage import data import napari with napari.gui_qt(): viewer = napari.view(np.random.random((10, 20, 15, 30, 40)))
""" Display a labels layer above of an image layer using the add_labels and add_image APIs """ from skimage import data from skimage.color import rgb2gray from skimage.segmentation import slic import napari with napari.gui_qt(): astro = data.astronaut() # initialise viewer with astro image viewer = napari.view(astronaut=rgb2gray(astro), multichannel=False) # add the labels # we add 1 because SLIC returns labels from 0, which we consider background labels = slic(astro, multichannel=True, compactness=20) + 1 label_layer = viewer.add_labels(labels, name='segmentation') # Set the labels layer mode to picker with a string label_layer.mode = 'picker' print(f'The color of label 5 is {label_layer.get_color(5)}')
""" Display a labels layer above of an image layer using the add_labels and add_image APIs """ from skimage import data from scipy import ndimage as ndi import napari with napari.gui_qt(): blobs = data.binary_blobs(length=128, volume_fraction=0.1, n_dim=3) v = napari.view(blobs=blobs.astype(float)) v.layers[0].colormap = 'gray' labeled = ndi.label(blobs)[0] label_layer = v.add_labels(labeled, name='blob ID')
""" Display one points layer ontop of one image layer using the add_points and add_image APIs """ import numpy as np from skimage import data import napari print("click to add points; close the window when finished.") with napari.gui_qt(): viewer = napari.view(data.astronaut(), multichannel=True) points = viewer.add_points(np.zeros((0, 2))) points.mode = 'add' print("you clicked on:") print(points.data)