BrainGlobe and napari

Whole brain microscopy analysis

Adam Tyson, Alessandro Felder & Laura Porta

Welcome

Schedule

Morning

  • Installation (AT & AF)
  • Introduction to BrainGlobe (AT)
  • Introduction to napari (AF)
  • Napari demo (AF)
  • BrainGlobe & napari (AF)
  • Whole brain atlas registration & segmentation (AT)

Schedule

Afternoon

  • 3D cell detection (AT)
  • Analysing silicon probe tracks (AT)
  • Analysing bulk fluorescence (AT)
  • Using BrainGlobe in your own software (AF)
  • Wrap up and Q&A (AT & AF)

Installation

Command line installation

Create a new conda environment and install napari

conda create --name brainglobe python=3.10 -y
conda activate brainglobe
conda install -c conda-forge napari pyqt

Double-check that running

napari

opens a new napari window.

More details

More details about using conda are available at brainglobe.info

Command line installation (alternative)

Create a new conda environment and install napari with pip

conda create --name brainglobe python=3.10 -y
conda activate brainglobe
pip install "napari[pyqt5]"

Double-check that running

napari

opens a new napari window.

More details

More details about using conda are available at brainglobe.info

Install brainglobe napari plugins

Silicon Mac users

Run this first:

conda install hdf5

Install all brainglobe tools with

pip install brainglobe

Initial checkup

Mentimeter

BrainGlobe

BrainGlobe Initiative

Established 2020 with three aims:

  1. Develop general-purpose tools to help others build interoperable software
  2. Develop specialist software for specific analysis and visualisation needs
  3. Reduce barriers of entry, and facilitate the building of an ecosystem of computational neuroanatomy tools.

BrainGlobe Atlas API

Initial observation - lots of similar communities working independently

  • Model species
  • Imaging modality
  • Anatomical focus
  • Developmental stage

BrainGlobe Atlas API

BrainGlobe Atlas API

Currently implemented atlases

BrainGlobe Atlas API

from pprint import pprint
from bg_atlasapi.bg_atlas import BrainGlobeAtlas
atlas = BrainGlobeAtlas("allen_mouse_25um")

# reference image
reference_image = atlas.reference
print(reference_image.shape)
# (528, 320, 456)

# hemispheres image (value 1 in left hemisphere, 2 in right)
hemispheres_image = atlas.hemispheres
print(hemispheres_image.shape)
# (528, 320, 456)

VISp = atlas.structures["VISp"]
pprint(VISp)
# {'acronym': 'VISp',
#  'id': 385,
#  'mesh': None,
#  'mesh_filename': PosixPath('/home/user/.brainglobe/allen_mouse_25um_v0.3/meshes/385.obj')
#  'name': 'Primary visual area',
#  'rgb_triplet': [8, 133, 140],
#  'structure_id_path': [997, 8, 567, 688, 695, 315, 669, 385]}

BrainGlobe Atlas API

Whole brain microscopy

Serial section two-photon tomography

Fluorescence micro-optical sectioning tomography

Light sheet fluorescence microscopy

Whole brain microscopy

Whole brain microscopy

brainreg

cellfinder

cellfinder

cellfinder

cellfinder

brainglobe-segmentation

brainglobe-segmentation

brainglobe-segmentation

brainrender

brainrender

BrainGlobe website

napari

What does napari look like?

The napari tech stack

Depends on many other open-source libraries

  • Python based, open-source
  • Graphical user interface uses qtpy
  • Rendering in vispy
  • Images are numpy(-like) arrays
  • Interactive scripting with IPython

Who has used napari before?

  • increasing in popularity
  • still a bit immature

Basic functionality

On it’s own, napari can:

  • open and display multi-dimensional images and overlays
  • manipulate these
  • install plugins

Why is it good?

It leverages Python well:

  • Open source
  • Easy access to (scientific) Python (in several ways)
  • Plugin installation (and development) easy
  • (Relatively) good documentation
  • Friendly and diverse community (including designers)
  • Funding for plugins in research available

Why is it not-so-good?

The immature ecosystem means:

  • things occasionally break (but lots of enthusiastic support available)
  • several plugins to do the same thing (please don’t reinvent the wheel - re-use other people’s wheels!)
  • installation trickier than e.g. Fiji/ImageJ

The possibilities are endless

napari demo

napari-demo

  • please open napari and follow allow with the live demo

Data exploration

After following along, your screen should look something like:

Data exploration

Mentimeter

Layer controls

After following along, your screen should look something like:

Layer controls

Mentimeter

The IPython console

  1. Navigate to Window > Console
  2. In the console, run (each line separately)
import napari
from pathlib import Path
from cellfinder_core.tools.IO import read_with_dask

viewer = napari.viewer.current_viewer()

# adapt "path/to/data" to your folder of tiffs
path_to_data = Path("path/to/data") 
viewer.open(path_to_data)

print(viewer.layers)

# which of these is quicker?
%timeit -r 3 -n 1 viewer.open(path_to_data)
%timeit -r 3 -n 1 viewer.add_image(read_with_dask(str(path_to_data)))

The IPython console

After following along, your screen should look something like:

The IPython console

Mentimeter

Plugin installation

  1. Navigate to Plugins > Install/Uninstall plugins...
  2. Search for brainrender-napari
  3. Click the Install button
  4. Close the pop-up notification
  5. Restart napari
  6. Open Plugins > Brainrender (brainrender-napari)
  7. Explore the Brainrender plugin

Plugin installation

After following along, your screen should look something like:

Plugin installation

Mentimeter

Summary

  • You can open, view, edit and save layers in napari
  • You can run Python commands with the napari console
  • You can install and open plugins

Registration & segmentation

Cell detection

Silicon probe tracing

Bulk fluorescence mapping

BrainGlobe & your software

BrainGlobe & your software

  • Not just standalone tools
  • Python APIs to help you build your own software
  • Easy compatibility with other packages
  • Released tools

Example script

  • Find all hippocampal regions (bg-atlasapi)
  • Display them in napari (brainrender-napari)

Example script (demo)

import napari
from bg_atlasapi import BrainGlobeAtlas
from brainrender_napari.napari_atlas_representation import NapariAtlasRepresentation

# setup a napari viewer and a brainglobe atlas
viewer = napari.viewer.Viewer()
viewer.dims.ndisplay = 3 # set to 3d mode
atlas = BrainGlobeAtlas("allen_mouse_100um")

# find all hippocampal regions
hip_id = 1080 # the id of the hippocampus is 1080
hip_regions = [
    region["acronym"] 
    for region in atlas.structures_list 
    if hip_id in region["structure_id_path"]
]

# make a representation of the brainglobe atlas in napari
napari_atlas = NapariAtlasRepresentation(atlas, viewer)

# add all hippocampal regions to the napari viewer
for hip_region in hip_regions:
    napari_atlas.add_structure_to_viewer(hip_region)

# add the whole brain mesh as a help for orientation
napari_atlas.add_structure_to_viewer("root")

# run this script
if __name__ == "__main__":
    napari.run()

Wrap up

Resources

You are welcome to contribute to BrainGlobe - get in touch anytime and we will support you!

Q+A

Mentimeter

Feedback

Please give us some feedback on this pilot course.

Thank you!