Napari: should I reorder the dimensions of an array created via ij.py.from_java?

Hi, I am trying to open a multichannel image in Napari, after turning it into a numpy array via pyimagej. I have a CZI file of a confocal stacks with six channels, no time dimensions. After running ij.io().open and ij.py.from_java I get dims like ('z', 'Channel', 'y', 'x') and data.shape like (33, 6, 225, 512).
Now, I want to run viewer.add_image(image, channel_axis=1) to separate each channel in a new image layer.
Instead, I get ValueError: dimensions ('z', 'Channel', 'y', 'x') must have the same length as the number of data dimensions, ndim=3.
How should I proceed? Do I need to swap the dimensions of my image so channel is the first one? I remember Numpy array and ImageJ don’t have the same order of dimensions.

import napari
import imagej
ij = imagej.init('sc.fiji:fiji:2.1.1')
viewer = napari.Viewer()
dataset = ij.io().open("Image 27.czi")
image = ij.py.from_java(dataset)
viewer.add_image(image, channel_axis=1) # buggy line
ij.getContext().dispose()
napari.run()

Ex: Add axis convention handling to conversion functions · Issue #17 · imagej/pyimagej · GitHub

Can you add the full error message with line numbers etc - I’m trying to track this down, but don’t quite know where it is coming from

Can you also let us know what object image is in the above, is it a numpy array? If not is it something you can call np.asarray on?

If you do

image = np.random.random((33, 6, 225, 512))
viewer.add_image(image, channel_axis=1)

does this work for you?

Also if you just do

image = ij.py.from_java(dataset)
viewer.add_image(image)

does this work?

We should be able to get this one sorted out relatively easily I think just need a little more info, thanks!!

  • Just running image = ij.py.from_java(dataset) / viewer.add_image(image) works, I started from that. I get the viewer with grayscales across my channels
  • image = np.random.random((33, 6, 225, 512)) that also works, I get a pretty point cloud
  • the image object returned by ij.py.from_java comes up as a xarray.DataArray. I tried swapping the dimensions via xarray.DataArray.tranpose, but that does nothing for me.

Full error paste: ---------------------------------------------------------------------------Val - Pastebin.com

Thanks for your help!

EDIT: solved for me by using viewer.add_image(image.values, channel_axis=1) to convert to a numpy array my xarray.DataArray. The picture shows up as desired.
I can create a github issue in Napari if you want.

1 Like

ok, thanks i saw you made you cannot split an xarray.DataArray image into channels via add_image / channel_axis · Issue #2814 · napari/napari · GitHub, that’s great - we should extend support to xarray’s here too

Use slicing rather than np.take in add_image by jni · Pull Request #2780 · napari/napari · GitHub closed it actually, it will go out in 4.9

oh nice, that should go out early next week!!