Skip to content

Commit eaadeac

Browse files
authored
Merge pull request #8 from psobolewskiPhD/add-napari-example
Add example of viewing the zarrs in napari
2 parents 6c9df53 + 73ef54e commit eaadeac

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

_toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ parts:
1616
title: Part 1
1717
- file: notebooks/Solved_Zarr_and_Dask_for_large-scale_imaging-Part-2.ipynb
1818
title: Part 2
19+
- caption: Other resources
20+
chapters:
21+
- file: optional_napari.md

napari-screenshot.png

1.4 MB
Loading

optional_napari.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Optional: view data using napari
2+
3+
Instead of using matplotlib to visualize images, there are a number of alternatives for viewing zarr data.
4+
For example, you can use [napari](https://napari.org/) a Python viewer for n-dimensional images.
5+
6+
You can install napari along with a Qt backend using your preferred Python package manager, for example using conda:
7+
```bash
8+
conda create -n napari-env -c conda-forge python=3.11 napari pyqt
9+
conda activate napari-env
10+
```
11+
12+
You can then use the following code to load the CMU-1 zarr and labels zarr into napari:
13+
```python
14+
import napari
15+
import zarr
16+
import dask.array as da
17+
18+
# replace these paths with the path to your data
19+
img_path = "path/to/CMU-1_Crop.ome.zarr"
20+
labels_path = "path/to/CMU-1_Crop_labels_cellpose_cyto3.zarr"
21+
```
22+
23+
We will load the zarr data as a list of dask arrays, so napari understands that we have multiscale/pyramidal data.
24+
The list of data arrays must be in order from largest to smallest, following the downsampling of the pyramid.
25+
For convenience, we will squeeze out the singleton arrays (by default OME-Zarr store images as 5D arrays) and move the channel axis to the end of the array shape, such that we get RGB images.
26+
27+
```python
28+
img_zarr_group = zarr.open(img_path, mode="r")
29+
# use the first (and only) image multiscale/pyramid group
30+
dask_stack = [da.from_zarr(img_zarr_group[0][level]).squeeze() for level in img_zarr_group[0]]
31+
rgb_dask_stack = [da.moveaxis(arr, 0, -1) for arr in dask_stack]
32+
33+
labels_zarr_group = zarr.open(labels_path, mode="r")
34+
labels_dask_stack = [da.from_zarr(labels_zarr_group[level]) for level in labels_zarr_group]
35+
```
36+
37+
With the data prepared, we can now launch napari and add the image and labels to the viewer:
38+
```python
39+
# open a napari viewer
40+
viewer = napari.Viewer()
41+
# add the image
42+
viewer.add_image(rgb_dask_stack, name="CMU-1")
43+
viewer.add_labels(labels_dask_stack, name="Cellpose labels")
44+
```
45+
46+
After zooming in a bit, you should see something like this:
47+
![Screenshot of napari viewer with CMU-1 image and Cellpose labels loaded.](napari-screenshot.png)
48+
49+
Optionally, you can also install the [napari-ome-zarr](https://github.com/ome/napari-ome-zarr) plugin to facilitate accessing the miroscopy (OME) metadata from the zarr files:
50+
```bash
51+
conda create -n napari-env -c conda-forge python=3.11 napari pyqt napari-ome-zarr
52+
conda activate napari-env
53+
```
54+
55+
In this case, you can simply launch napari from the command line and open the zarr image using:
56+
```bash
57+
napari path/to/CMU-1_Crop.ome.zarr/0
58+
```
59+
Note the `/0` at the end of the path, indicating that napari should open the first (and only in this case) image multiscale/pyramid group
60+
With the viewer open, you can then drag-and-drop the labels zarr folder into the napari viewer to add the labels layer. When prompted, select `napari-ome-zarr` plugin. However, you will need to right-click on the layer with the labels data and select "Convert to labels" to get the correct rendering of the labels.
61+
62+
Or you can do it programmatically as follows:
63+
```python
64+
import napari
65+
66+
# path to the first (only) image multiscale/pyramid group
67+
img_path = "path/to/CMU-1_Crop.ome.zarr/0"
68+
labels_path = "path/to/CMU-1_Crop_labels_cellpose_cyto3.zarr"
69+
70+
# create a napari viewer
71+
viewer = napari.Viewer()
72+
73+
viewer.open(img_path, plugin="napari-ome-zarr")
74+
viewer.open(labels_path, plugin="napari-ome-zarr", layer_type="labels")
75+
```
76+
Note: If you want to run these snippets as scripts, append `napari.run()` at the end of the script to start the napari event loop.
77+
78+
Final tip: if you want to use napari with full size whole-slide images or remote zarr data, we recommend using the "Render Images Asynchronously" option in napari settings (under "Experimental"). You can also set this using the environment variable `NAPARI_ASYNC=1`.

0 commit comments

Comments
 (0)