AllenCellModeling / aicspylibczi

Python module utilizing libCZI for reading Zeiss CZI files.
https://allencellmodeling.github.io/aicspylibczi
GNU General Public License v3.0
36 stars 8 forks source link

Failure to get czi data with imperfect metadata #119

Open osthomas opened 3 months ago

osthomas commented 3 months ago

System and Software

Description

I have several images with apparently partially imperfect metadata:

image

I can open the image using Fiji and it displays correctly in all Z planes.

With aicsimageio, however, there is a mismatch between the dask and numpy representations of the image data. Computations on the array raise an error.

I also tried to read the image with bioio and bioio-czi and got the same error.

>>> aics = AICSImage(fpath)
>>> aics.shape

(1, 1, 76, 1024, 1024)
>>> aics_dask = aics.get_image_dask_data()
>>> aics_dask.shape
(1, 1, 76, 1024, 1024)
>>> aics_np = aics.get_image_data()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.12/site-packages/aicsimageio/aics_image.py", line 724, in get_image_data
    return self.data
           ^^^^^^^^^
  File ".../lib/python3.12/site-packages/aicsimageio/aics_image.py", line 542, in data
    return self.xarray_data.data
           ^^^^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/aicsimageio/aics_image.py", line 501, in xarray_data
    self.reader.xarray_data
  File ".../lib/python3.12/site-packages/aicsimageio/readers/reader.py", line 372, in xarray_data
    self._xarray_data = self._read_immediate()
                        ^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/aicsimageio/readers/czi_reader.py", line 732, in _read_immediate
    return xr.DataArray(
           ^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/xarray/core/dataarray.py", line 456, in __init__
    coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.12/site-packages/xarray/core/dataarray.py", line 195, in _infer_coords_and_dims
    _check_coords_dims(shape, new_coords, dims_tuple)
  File ".../lib/python3.12/site-packages/xarray/core/dataarray.py", line 129, in _check_coords_dims
    raise ValueError(
ValueError: conflicting sizes for dimension 'Z': length 69 on the data but length 76 on coordinate 'Z'

czifile also reads the image:

>>> import czifile
>>> czifile.CziFile(fpath).asarray().shape
(1, 1, 1, 1, 76, 1024, 1024, 1)

I have simple regular Z stacks, so in my application I can safely disregard the lost metadata. Maybe this is not so simple in more complicated setups? In any case, I think aicsimageio should either perform error recovery and read it anyway, or error out earlier.