How to convert multi-resolution image pyramid data into a zarr file so that it can be loaded in Napari?

Hi,

first of all, thank you very much for the support and maintenance of this amazing open source project!.
I am new user, I just started to learn the basics of Napari. I am interested in using Napari for working
with big images. I found in the gallery an example called “pathology data” describing precisely this use case:

“We converted the multiresolution image pyramid data into a zarr file, which we could lazily read using dask, a python library for flexible parallel computation. We visualized the slide using a Pyramid layer. This layer allows us to dynamically swap in different resolution levels and image tiles depending on the zoom level so we can easily browse this large dataset without having to load it all into memory.”

I have been looking for the python code related with this example, but unfortunately I have not been able to find it.
I would like to ask some questions about it:

  • How should I convert the multi-resolution image pyramid data into a zarr file so that I can be used by Napari?, is there any specification describing the layout of such zarr file as well as the its associated metadata?. I tiny code snippet would be really appreciated :-).

  • Once created the zarr file, how can I load such file into Napari?. According to the description mentioned above, in order to accomplish this we can use a Pyramid layer, but this layer is not mentioned in the documentation (according to the docs, Napari supports six main different layer types, Image, Labels, Points, Vectors, Shapes, and Surface).

  • In case it is possible, could you please tell me where to find the python code related to the example mentioned above?

Thank you very much for all your help, and please excuse me for asking so many beginner questions!

1 Like

Hi @jmontoyam!

please excuse me for asking so many beginner questions!

Beginner questions are the best! It means a (hopefully) new happy user! =)

My overarching answer is to apologise for our pretty outdated docs. Your confusion:

According to the description mentioned above, in order to accomplish this we can use a Pyramid layer, but this layer is not mentioned in the documentation

is because we no longer have a Pyramid layer! In general, our docs need some love and some of the stuff in there might not work anymore. I think asking here is the right path if you can’t figure out something in the docs. =)

The good news is that this is because our image layer is now automatically a pyramid layer if you provide data that is a pyramid! So that’s one less step to worry about. =)

How should I convert the multi-resolution image pyramid data into a zarr file so that I can be used by Napari?, is there any specification describing the layout of such zarr file as well as the its associated metadata?. I tiny code snippet would be really appreciated :slight_smile:

The answer here is “it depends”. There is an ome-zarr multiscale spec described here:

Depending on your input image format, you can write out a zarr group that conforms to that metadata. The original file used in the documentation was created by @sofroniewn using this script:

then loaded using this script:

But, these days, if you also write a .zattrs conforming to the above spec using json, then pip install ome-zarr, you’ll be able to drag and drop that file (the folder containing the .zattrs and all the different scale datasets) directly into napari! Which is pretty swish, if you ask me. :stuck_out_tongue_winking_eye:

BUT, it gets even better. IF your input file is OpenSlide, @manzt made this plugin https://pypi.org/project/napari-lazy-openslide/ that automagically and on-the-fly converts from openslide to a zarr format that napari understands. So you might be able to pip install napari-lazy-openslide (but see the installation instructions linked above), and just drag and drop your tiff, and be done! :tada:

I hope the above gets you going with using napari with your data!

4 Likes

Thank you very much @jni for the detailed and friendly answer to all my questions! :-), I will follow all your suggestions and hopefully I will be able to start using Napari with my data soon, I am really looking forward to!.

1 Like

Hi @jni

I have followed your suggestions and everything worked fine!, thank you much!.
I created the zarr file in the same way as the example code you suggested me (sofroniewn/image-demos/blob/4ddcfc23980e37fbe5eda8150c14af8220369f24/helpers/make_2D_zarr_pathology.py), that is, without attaching any metadata to it, and Napari was able to load it just fine. Does this mean that Napari does not take into account the metadata of the zarr file for anything at all, am I right?..do you suggest me to create and attached the metadata described in (https://forum.image.sc/t/multiscale-arrays-v0-1/37930) just in case you guys decide to change the way Napari handle zarr file in the future?

Thank you very much for your help!

2 Likes

without attaching any metadata to it, and Napari was able to load it just fine.

Ha! Bonus! =) I think it will break if you have more than 10 levels due to lexicographical ordering, ie it will try to load levels 0, 1, 10, 2… and break when the levels are not in strictly decreasing size. But as long as the groups are sorted properly I think it’ll work forever/for a long time!

Actually I think the metadata spec is more likely to evolve than this one, so no need to add it yet — just wait until it breaks, and that day might never come! :joy:

1 Like