Interactive functions in Jupyter Lab#

In this notebook we demonstate interactive functions using stackview in combination with pyclesperanto_prototype. Interactive image stack viewing in jupyter notebooks based on ipycanvas and ipywidgets.

Note: This notebook is not fully rendered in the Jupyter book and on github. You need to run it locally to see how it works.

Usage#

First we test the installation by importing the two libraries and scikit-image for loading image data.

import stackview
import pyclesperanto_prototype as cle
from skimage.io import imread

Starting point is a 3D image dataset provided as numpy array.

image_stack = cle.imread('../../data/Lund_000500_resampled-cropped.tif')

image_stack.shape
(100, 256, 256)

cle-images are visualized in jupyer notebooks with a static view. In case the dataset is 3D, we see a maximum-intensity projection.

image_stack
cle._ image
shape(100, 256, 256)
dtypefloat32
size25.0 MB
min125.0
max680.0

Slicing#

We can explore the dataset interactively using stackview.slice.

stackview.slice(image_stack)

We can also hover with our mouse over the dataset and read out pixel intensities using stackview.picker.

stackview.picker(image_stack)

Curtain#

To compare the original image with a modified version, e.g. after background subtraction, we can use stackview.curtain.

background_subtracted = cle.top_hat_box(image_stack, radius_x=10, radius_y=10)

stackview.curtain(image_stack, background_subtracted)

Interaction#

You can also use sliceview.interact to explore parameters of functions interactively.

stackview.interact(cle.voronoi_otsu_labeling, background_subtracted)

This may also make sense with custom functions. When writing those it is important to use type-annotations.

def my_custom_segmentation(image, background_subtraction_radius: int = 10, spot_sigma:float = 1, outline_sigma:float = 1, show_labels: bool = True):
    background_subtraction_radius = abs(background_subtraction_radius)
    spot_sigma = abs(spot_sigma)
    outline_sigma = abs(outline_sigma)
    
    background_subtracted = cle.top_hat_box(image, radius_x=10, radius_y=10)

    label_image = cle.voronoi_otsu_labeling(background_subtracted, spot_sigma=spot_sigma, outline_sigma=outline_sigma)
    
    edge_image = cle.detect_label_edges(label_image)
    
    if show_labels:
        return label_image
    else:
        return edge_image * 255 + image 
stackview.interact(my_custom_segmentation, image_stack)

Exercise#

Load the blobs image and setup a custom segmentation workflow for segmenting the blobs. Use stackview.interact to tune the parameters for the segmentation (spoiler).

slice_image = cle.imread('../../data/blobs.tif')
slice_image
cle._ image
shape(254, 256)
dtypefloat32
size254.0 kB
min8.0
max248.0