Reading OME-Tiff and CZI via python scripting inside jupyter

Hi,

During our workshop last week at the MIAP in Freiburg we had a session about how to easily read and display OME-TIFF and CZI images using python and Jupyter Notebooks.

We mainly used the CZI file module and the apeer-ometiff-library and ipywidgets and the new napari viewer to display it.

The code can be found here:Read_And_Display_Images_using_Widgets_and_Napari

And for reading CZI with some “special” options we used Fiji Python scripts, which can be found here : Fiji Scripts

Feel free to use them and let us know if it works for you as well.

5 Likes

Hi,

I am trying to open CZI files and visualizing them inside Jupyter, and I thought your code might be useful. But apparently it is not available anymore, would you mind sharing it again?

Thank you!

It appears the folder was renamed in the repository, and @sebi06 unfortunately linked to the master branch instead of pasting a permalink. You can access the files here:

(@sebi06 while browsing GitHub, you can press Y to change the browser URL to a permalink that can be copied for cases like this one.)

2 Likes

Thx for pointing this out. I was completely unaware of that. Sorry for the inconvenience.

Hi @sebi06,

Thanks again for sharing this, it’s a great resource to get started on how to use czifile and apeer-ometiff-library.

I think it would be useful to also have the functions in imgfileutils.py available in a PyPI package easily installable via pip.

Also, would you happen to have some minimal code examples on:

  • how to query metadata about the pyramidal resolution levels in a CZI file?
  • how to open a specific subset (of channels, tiles, resolution level etc.) of a CZI file into a (numpy) array, without loading the image data of the entire dataset into memory?

Hi @imagejan,

I do have at least example code for some of the things you asked for. The best tool to read CZI in a “memory efficient” way in chunks is to use the python wrapper of our libCZI which is use inside the AICSImageIO package.

It allows to do things like this (just to illustrate what I mean … ):

import imgfileutils as imf
from aicsimageio import AICSImage, imread

# get the metadata from the czi file
filename = 'myimage.czi'
md = imf.get_metadata_czi(filename, dim2none=False)
readmethod = 'perscene'

# get the stack using the specified method
if readmethod == 'chunked':
    img = AICSImage(filename, chunk_by_dims=["S"])
    stack = img.get_image_data()

if readmethod == 'chunked_dask':
    img = AICSImage(filename, chunk_by_dims=["S"])
    stack = img.get_image_dask_data()

if readmethod == 'fullstack':
    img = AICSImage(filename)
    stack = img.get_image_data()

# loop over all dimensions and only read a single plane at a time
for s in range(md['SizeS']):
    for t in range(md['SizeT']):
        for z in range(md['SizeZ']):
            for ch in range(md['SizeZ']):

                if readmethod == 'chunked_dask':
                    image2d = stack[s, t, z, ch, :, :].compute()

                if readmethod == 'fullstack' or readmethod == 'chunked':
                    image2d = stack[s, t, z, ch, :, :]

                if readmethod == 'perscene':
                    image2d = img.get_image_data("YX", S=s, T=t, Z=z, C=ch)

                # do your processing for a single image2d here ...

So basically one can “chunk” along any supported dimensions.

As an example I created a notebook demonstrating how to read a CZI in chunks (every scene separately), extract the metadata, segment the nuclei and plot a heatmap: read_czi_analyze_display_heatmap.ipynb.

What I did not explore so far, if ready the different pyramidal levels already works with the python wrapper, but it is on my ToDo list. The wrapper itself was not created by us (not enough time), but by the community based upon our libCZI (this is why such things make total sense - especially also for companies).

And yes, I also thought about creaty a PyPi package out of my imgfileutils. But again this is a question of time (and trust in my own code … :-), since I not a trained developer. But we will discuss it since we already published PyPi packages like czmodel to import trained ML networks into ZEN.

2 Likes