What can napari be used for?

Introduction to napari

Peter Sobolewski (he/him)

Systems Analyst, Imaging Applications

Research IT

COI Disclaimer: Peter is a napari core developer

Plan

  • Introduction to napari (these slides)
  • napari application: the GUI
    • napari console: integrated Python REPL
  • exploratory image analysis with napari and Jupyter lab
    • Image layers: visualization
    • Points, Shapes, Labels layers: annotation
    • Example workflow
  • extending napari

What is napari?

A Python library for n-dimensional image visualization, annotation, and analysis.

  • view & explore 2D, 3D, and higher-dimensional arrays
  • overlay derived data such as points, polygons, labels, and more
  • seamlessly weave exploration, computation, and annotation together in imaging data analysis
  • leverage scientific Python ecosystem

A bit of background

napari strengths

  • graphical UI to view & explore 2D, 3D, and higher-dimensional array-like data
    • easy to integrate into existing Python workflows
  • intuitive stacking/overlay layer model for channels, modalities, annotations, etc.
  • robust GUI data annotation tools
    • annotation data immediately available as Python objects
  • highly extensible GUI
  • robust plugin ecosystem: https://napari-hub.org

napari limitations

  • it’s just a viewer & annotation tool
    • no built-in analysis tools (but you have Python!)
  • not yet feature complete
    • no parity between different layers/features (e.g. multiscale handling)
    • there can be bugs 🪲
  • can run into GPU/memory limitations for some large-data workflows
  • Python packaging and dependencies

napari is an open-source, community developed project—anyone can contribute!


Even me—or you!

Where to get help

Report bugs

Exploring the napari viewer GUI

napari viewer

  • native application you can launch from the terminal/command prompt using napari command or via the application programming interface (API), e.g. viewer = napari.Viewer()
  • this viewer consists of menus, a canvas used for display, and widgets with controls for interacting with what is displayed on the canvas

napari viewer

napari viewer: Preferences

Screenshot of napari UI with the Preferences window open.

Access via File menu (napari on macOS)

  • Extensive, editable keyboard shortcuts!
  • Stored per Python env, reset using napari --reset

napari layers

  • the napari visualization data model uses layers
  • each layer (an image or annotation) is composited as a overlay on the previous one, with different opacity and blending options
  • the LayerList widget allows you to toggle visibility and stacking order
  • the LayerControls widget allows you to change visualization settings of a selected layer

napari layers

Dimensions in napari

  • by default, 2 dimensions are displayed: the last two (right-most) axes of an array
  • non-displayed dimensions will be represented using a slider
  • 3D display can be toggled using viewer button or API
  • axes can be rotated or transposed using viewer buttons or API

Dimensions in napari

The napari API

Headless napari is just Python!

– Juan Nunez-Iglesias (@jni)

Interact with napari using the built-in Python console

Screenshot of napari UI with the Python console open (docked at bottom) and showing some code.

Only available when launched from a non-interactive environment

Run napari from scripts or notebooks

expand for full code
from skimage import data
from scipy import ndimage as ndi

import napari

blobs = data.binary_blobs(length=128, volume_fraction=0.1, n_dim=3)
labeled = ndi.label(blobs)[0]
viewer, image_layer = napari.imshow(blobs, name='blobs')
viewer.add_labels(labeled, name='blob ID')
viewer.dims.ndisplay = 3
viewer.camera.zoom = 3
viewer.camera.angles = (3, 38, 53)

if __name__ == '__main__':
    napari.run()

napari API

  • the API and the GUI are bidirectional:
    • GUI changes are reflected in object property changes
    • API changes are reflected in the GUI

This is even true for annotations, like Point positions or Shape vectors

  • if you’re not using an interactive environment, you can access it using the built-in console
  • napari was designed to be extensible

Python scripts can handle keyboard and mouse events!

@viewer.bind_key('a')
def accept_image(viewer):
    msg = 'this is a good image'
    viewer.status = msg
    print(msg)

Python scripts can contribute widgets!

Screenshot of napari UI with a docked widget on the right for applying scikit-image filters

Python scripts can contribute widgets!

Beyond that, can develop a napari plugin

napari plugins are Python packages that can:

  • add file-type readers & writers
  • customize functionality (events)
  • extend the GUI (widgets, menus)
  • provide demo/sample data

Plugin developer guide: https://napari.org/dev/plugins/index.html

Plugins can be installed from within napari

  • plugins can be publically discoverable and have global reach via PyPI and napari-hub.org

Screenshot of napari UI showing the plugin install/uninstall UI