Skip to content

Commit c4771c6

Browse files
authored
Merge pull request #10 from psobolewskiPhD/napari_ome_zarr
Update optional_napari.md to use the napari-ome-zarr plugin everywhere
2 parents 711350b + 37dbe3d commit c4771c6

File tree

1 file changed

+15
-48
lines changed

1 file changed

+15
-48
lines changed

optional_napari.md

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,29 @@ For example, you can use [napari](https://napari.org/) a Python viewer for n-dim
55

66
You can install napari along with a Qt backend using your preferred Python package manager, for example using conda:
77
```bash
8-
conda create -n napari-env -c conda-forge python=3.11 napari pyqt
8+
conda create -n napari-env -c conda-forge python=3.11 napari pyqt napari-ome-zarr
99
conda activate napari-env
1010
```
11+
In the command above we are also installing the [napari-ome-zarr](https://github.com/ome/napari-ome-zarr) plugin to facilitate accessing the miroscopy (OME) metadata from the zarr files.
1112

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"
13+
In this case, you can simply launch napari from the command line and open the remote zarr image using:
14+
```bash
15+
napari https://storage.googleapis.com/jax-public-ngff/example_v2/CMU-1_Crop.ome.zarr/0
2116
```
17+
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. You can also subsitute the path to your local copy of `CMU-1_Crop.ome.zarr`, making sure to append the `/0`.
2218

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-
```
19+
Once the viewer loads, you can then drag-and-drop the labels zarr folder (`CMU-1_Crop_labels_cellpose_cyto3.zarr`) into the napari viewer to add the labels layer. When prompted, select `napari-ome-zarr` plugin. Once the layer loads, you will need to right-click on the labels layer (named "C") in the layer list on the left side of the viewer and select "Convert to labels" to get the correct rendering of the labels.
4520

4621
After zooming in a bit, you should see something like this:
4722
![Screenshot of napari viewer with CMU-1 image and Cellpose labels loaded.](napari-screenshot.png)
4823

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-
6224
Or you can do it programmatically as follows:
6325
```python
6426
import napari
6527

6628
# path to the first (only) image multiscale/pyramid group
67-
img_path = "path/to/CMU-1_Crop.ome.zarr/0"
29+
# again, you can replace the URL with your local path
30+
img_path = "https://storage.googleapis.com/jax-public-ngff/example_v2/CMU-1_Crop.ome.zarr/0"
6831
labels_path = "path/to/CMU-1_Crop_labels_cellpose_cyto3.zarr"
6932

7033
# create a napari viewer
@@ -73,6 +36,10 @@ viewer = napari.Viewer()
7336
viewer.open(img_path, plugin="napari-ome-zarr")
7437
viewer.open(labels_path, plugin="napari-ome-zarr", layer_type="labels")
7538
```
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.
39+
Note: If you want to run this snippet as a script, append `napari.run()` at the end of the script to start the napari event loop.
7740

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`.
41+
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`, e.g.:
42+
43+
```bash
44+
NAPARI_ASYNC=1 napari https://storage.googleapis.com/jax-public-ngff/example_v2/CMU-1_Crop.ome.zarr/0
45+
```

0 commit comments

Comments
 (0)