Napari loading images from OMERO

Hi,

I just heard about your nice napari viewer and since we are working on python3 support for OMERO (Call Omero API from python3?) the timing is great.

Here’s some sample code to view a Z-stack multi-channel image from OMERO in napari.

import napari
from vispy.color import Colormap
import numpy
from omero.gateway import BlitzGateway
conn = BlitzGateway('username', 'password', port=4064, host="omero.server")
conn.connect()

IMAGE_ID = 4418
image = conn.getObject("Image", IMAGE_ID)

def get_z_stack(img, c=0, t=0):
    zct_list = [(z, c, t) for z in range(img.getSizeZ())]
    pixels = image.getPrimaryPixels()
    return numpy.array(list(pixels.getPlanes(zct_list)))

with napari.gui_qt():
    viewer = napari.Viewer()

    for c, channel in enumerate(image.getChannels()):
        print('loading channel %s' % c)
        data = get_z_stack(image, c=c)
        # use current rendering settings from OMERO
        color = channel.getColor().getRGB()
        color = [r/256 for r in color]
        cmap = Colormap([[0, 0, 0], color])
        # Z-scale for 3D viewing
        size_x = image.getPixelSizeX()
        size_z = image.getPixelSizeZ()
        z_scale = size_z / size_x
        viewer.add_image(data, blending='additive',
                         colormap=('from_omero', cmap),
                         scale=[1, z_scale, 1, 1],
                         name=channel.getLabel())

    print('closing conn...')
    conn.close()

Thanks,

Will.

7 Likes

Here’s the current screenshot:

5 Likes

This is awesome. Exactly what I have been looking for!
Might be a good solution for this as well
(5D volume viewing in Python)

1 Like

This is really cool!

EDIT: I updated the code sample above to include channel label, color and Z-scale metadata from OMERO.

1 Like

@will-moore - I wanted point you towards this recent “meta-issue” https://github.com/napari/napari/issues/881 around improving napari’s support for remote data + remote compute. You can see how we’re planning to lean into the dask ecosystem, though some of those improvements will help with any lazy loading approach. Curious what you make of those plans from your OMERO perspective or if there is more we could do to improve performance / ease of your use case.

Feel free to weigh in here or on that issue directly, thanks!! :slight_smile:

Thanks @sofroniewn for the pointer. I’ve been pretty pleased with how things are working currently for my use-case. If caching was ‘built-in’ then that might have saved me a couple of lines of code, but my very simple cache is working fine for now. And not loading planes while sliding the sliders is a good idea, although the current behaviour is not too bad.

I haven’t looked at lazy loading of tiles for big images. I don’t know if that’s supported in a similar way to planes?

For the remote compute options, I guess we’d think of using OMERO like:

# remote machine
omero import big_data
...
Imported Image 123
# local machine
omero napari view Image:123

But the import would happen after the big_data has been generated and wouldn’t change (in your example, would the big_data be manipulated and the local napari viewer update to show changes?

Thanks!

1 Like

Yes it should be, but if it doesn’t work as expected let us know.

I’ll have to explore this, it’s an important question. I think in the ideal world it you continue to get updated on the local side without you having to do too much. Maybe at least there would be a very simple refresh button on the local side that would then get the latest / whenever you did something like more to a new slice of the image that would be from the latest

Cache Invalidation - always tricky! :slight_smile:

2 Likes