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_nD_points(qtbot): """Test adding nD points.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = 20 * np.random.random((10, 3)) viewer.add_points(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_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_points(qtbot): """Test adding points.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = 20 * np.random.random((10, 2)) viewer.add_points(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 show_ax_dend(v: napari.Viewer, ax: pd.DataFrame, dend: pd.DataFrame, name: str): for (data, meta) in zip([ax, dend], metadata): v.add_points( data.loc[::10, "x":"z"], edge_width=0, name=f"{name}_{meta[1]}", face_color=f"{meta[0]}", size=data.loc[::10, 'alpha'], opacity=0.2, )
def show_pairs(viewer: napari.Viewer, data: pd.DataFrame): """Highlights interesting pairs from the axonal and dendritic trees.""" viewer.add_points( data.loc[:, 'ax_x':'ax_z'], edge_width=0, name='ax', face_color='green', size=data.loc[:, 'ax_alpha'], ) viewer.add_points( data.loc[:, 'dend_x':'dend_z'], edge_width=0, name='dend', face_color='orange', size=data.loc[:, 'dend_alpha'], )
def show_all_equal_size(v: napari.Viewer, data: pd.DataFrame, name: str): """Shows all points of a neuron in napari. Parameters ---------- v : napari.Viewer data : pd.DataFrame data name : str Neuron's name """ v.add_points( data.loc[:, "x":"z"], edge_width=0, name=f"{name}_all", face_color="magenta", )
def test_nD_points(qtbot): """Test adding nD points.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = 20 * np.random.random((10, 3)) viewer.add_points(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 show_collisions_with_napari(points: pd.DataFrame, viewer: napari.Viewer, neuron_name: str, size=None): """Plot the collisions of the neuron with the given napari viewer. This function receives the points to be plotted, the viewer instance to be used and the name of the current neuron (to be used as the layer's label) and shows them all together. The given dataframe is assumed to have a 'type' categorical index which contains information on the type of neurite this collision is located on top of. Parameters ---------- size : str, int, array (optional) Size of each point. If int - size in pixels. If None - defaults to 2. If str - should be a column name in `points`. If array - size of each point. """ if size is None or isinstance(size, (int, np.ndarray)): points["size"] = size elif isinstance(size, str): points = points.rename({size: "size"}, axis=1) colors = ["green", "orange", "red", "yellow", "purple"] points["color"] = "" for color, type_ in zip(colors, points.index.categories): points.loc[type_, "color"] = color viewer.add_points( points.loc[type_, "x":"z"], size=points.loc[type_, "size"], edge_width=0.5, edge_color='black', face_color=color, name=f"{neuron_name}_{type_}", )
def test_data_change_ndisplay_points(qtbot): """Test change data calls for points layer with ndisplay change.""" viewer = Viewer() view = viewer.window.qt_viewer qtbot.addWidget(view) np.random.seed(0) data = 20 * np.random.random((10, 3)) layer = viewer.add_points(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 FISH data, raw and deconvolved, with spots detected using starFISH """ from skimage.io import imread import numpy as np import pandas as pd from napari import Viewer, gui_qt raw_spots = pd.read_csv('data-njs/smFISH/allen_smfish_spots.csv') spots = raw_spots.loc[:, [['x', 'y']]].values spots[:, 1] = -spots[:, 1] print(len(spots)) with gui_qt(): # create an empty viewer viewer = Viewer() # add the spots layer = viewer.add_points(spots)
# create properties for each point properties = { 'class': np.array([1, 2, 3]), } # define the color cycle for the face_color annotation face_color_cycle = ['red', 'blue', 'green', 'cyan', 'magenta', 'yellow'] # create a points layer where the face_color is set by the good_point property # and the edge_color is set via a color map (grayscale) on the confidence property. points_layer = viewer.add_points( points, properties=properties, size=10, edge_width=1, edge_color='white', face_color='class', face_color_cycle=face_color_cycle, name='points' ) # bind a function to toggle the good_point annotation of the selected points current_class = 1 points_layer.help = str(current_class) @viewer.bind_key('1') def toggle_1(viewer): current_class = 1 points_layer.help = str(current_class) @viewer.bind_key('2')
with gui_qt(): # create an empty viewer viewer = Viewer() # add the raw images raw_layer = viewer.add_image(raw, name='images', colormap='gray', contrast_limits=(140.0, 1300.0)) decon_layer = viewer.add_image(deconvolved, name='deconvolved', colormap='gray', contrast_limits=(0.0, 0.2)) decon_layer.visible = False spots_layer = viewer.add_points(spots, face_color='red', edge_color='red', symbol='ring', size=8, n_dimensional=True, name='spots') spots_layer.opacity = 0.5 @viewer.bind_key('s') def swap(viewer): """Swaps dims """ viewer.dims.order = np.roll(viewer.dims.order, 1)
@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')