Reputation: 371
I have big TIFF-arrays that I want to save with xarray and view in napari. However, napari seems unable to read the zarr-format produced by xarray. Is there a way that I can specify the arguments of xarray.DataArray.to_zarr to make it readable by napari?
I know that dask.array.to_zarr is an option but I would prefer to use xarray.
Minimum reproducible example:
import os
import numpy as np
import xarray as xa
arr = np.random.randint(0, 2**16-1, size=(100, 400, 400))
coords = {'z': np.arange(0, 100), 'y': np.arange(0, 400), 'x': np.arange(0, 400)}
da = xa.DataArray(arr, dims=['z', 'y', 'x'], coords=coords)
dir_save = os.getcwd()
path_save = os.path.join(dir_save, 'test.zarr')
da.to_zarr(path_save)
The traceback of the error that I get when I drag-and-drop it into napari is longer than the allowed length of posts, but maybe this part is a clue?
File C:\ProgramData\anaconda3\envs\napari_env\lib\site-packages\napari\layers\image_image_utils.py:94, in guess_multiscale(data=[dask.array<from-zarr, shape=(100, 400, 400), dty...hunksize=(25, 100, 100), chunktype=numpy.ndarray>, dask.array<from-zarr, shape=(400,), dtype=int64, chunksize=(400,), chunktype=numpy.ndarray>, dask.array<from-zarr, shape=(400,), dtype=int64, chunksize=(400,), chunktype=numpy.ndarray>, dask.array<from-zarr, shape=(100,), dtype=int64, chunksize=(100,), chunktype=numpy.ndarray>]) 93 if not consistent: ---> 94 raise ValueError( trans = <napari.utils.translations.TranslationBundle object at 0x000002492857E5E0> sizes = [16000000, 400, 400, 100] 95 trans._( 96 'Input data should be an array-like object, or a sequence of arrays of decreasing size. Got arrays in incorrect order, sizes: {sizes}', 97 deferred=True, 98 sizes=sizes, 99 ) 100 ) 102 return True, MultiScaleData(data)
ValueError: Input data should be an array-like object, or a sequence of arrays of decreasing size. Got arrays in incorrect order, sizes: [16000000, 400, 400, 100]
Package versions:
Upvotes: 1
Views: 22
Reputation: 9664
There are a couple of ways of doing this. First method is to launch a new instance of napari
form Python, you could load your data then launch napari
in a short script:
import napari
import xarray as xr
ds = xr.open_zarr('test.zarr/')
# This is the name of the dataarray from your MRE.
viewer, image_layer = napari.imshow(ds.__xarray_dataarray_variable__)
Second method, after you have already opened napari
, you can do much the same from the console (>_
button):
import xarray as xr
ds = xr.open_zarr('test.zarr/')
viewer.add_image(ds.__xarray_dataarray_variable__)
Finally, a third option might be to save as netCDF4 then use a plugin to load this, if you want to avoid scripting for some reason: enter link description here (I haven't tried this).
Upvotes: 0