Skip to content

Incorrect image dimensions after reading in 3D+c TIFF #226

Closed

Description

@jni

I'm trying to read the following file:

https://www.dropbox.com/s/0ynbscx4cmd5k91/E_z2_512_1um_CONTROL.tif?dl=1

skimage (ie tifffile) reads this correctly, but imageio doesn't — even though I thought it also used tifffile as a backend for tiffs?

In [1]: import imageio
In [2]: from skimage import io
In [3]: im_iio = imageio.imread('E_z2_512_1um_CONTROL.tif')
In [4]: im_iio.shape
Out[4]: (159, 320, 512)

In [5]: im_ski = io.imread('E_z2_512_1um_CONTROL.tif')
In [6]: im_ski.shape
Out[6]: (53, 320, 512, 3)

What imageio is doing is reading the file in the array order present in the file (plane, channel, row, column), and squashing pln and ch dimensions together, while skimage is behaving as expected and producing two separate dimensions for pln and ch and (maybe a bit more than expected) moving ch to the last dimension, which is the dimension order expected by most scipy packages.

Any ideas about what is going wrong here?

Activity

  1. cgohlke commented on Feb 23, 2017

    @cgohlke
    Contributor

    Re skimage "moving ch to the last dimension" see scikit-image/scikit-image#1496 and scikit-image/scikit-image#1498

  2. cgohlke commented on Feb 23, 2017

    @cgohlke
    Contributor

    The file E_z2_512_1um_CONTROL.tif is a ImageJ hyperstack. Newer tifffile.py versions by default try to read these files in a fast mode and return all the data in the file as one contiguous array when requesting the data from first TIFF page. Other TIFF pages are discarded. This allows for memory mapping the data in the file, it significantly speeds up parsing hyperstack files with thousands TIFF pages, and it correctly reads corrupted TIFF files >4 GB.

    imageio.imread('E_z2_512_1um_CONTROL.tif') returns the data from the first TIFF page. Disable the fast ImageJ hyperstack mode in imageio, e.g. imageio.imread('E_z2_512_1um_CONTROL.tif', fastij=False) to get the expected result.

    skimage.io.imread('E_z2_512_1um_CONTROL.tif') returns the data of the first series of images in the file. These data are shaped according to the metadata in the file. To read the data from the first TIFF page, use skimage.io.imread('E_z2_512_1um_CONTROL.tif', img_num=0) instead. The released version of scikit-image contains an old version of tifffile without fast ImageJ hyperstack handling.

    Besides this, iterating over or indexing individual TIFF pages in ImageJ hyperstacks, OME-TIFF, LSM or STK files containing high dimensional data are most often not what one wants.

  3. almarklein commented on Feb 23, 2017

    @almarklein
    Member

    Do I understand correctly that imageio is working as expected? Or is there anything we can do to improve things?

    cc @blink1073

  4. blink1073 commented on Feb 23, 2017

    @blink1073

    @jni, do you have tifffile installed separately as a package pip list | grep tifffile? If not, can you please install it and try with imageio again? Either way, imageio should vendor the most recent tifffile.py.

  5. jni commented on Feb 25, 2017

    @jni
    ContributorAuthor

    @cgohlke thanks for the description! fastij doesn't quite solve things, though:

    In [2]: import imageio as iio
    
    In [3]: im = iio.imread('E_z2_512_1um_CONTROL.tif', fastij=False)
    
    In [4]: im.shape
    Out[4]: (320, 512)
    
    In [5]: imf = iio.imread('E_z2_512_1um_CONTROL.tif', fastij=True)
    
    In [6]: imf.shape
    Out[6]: (159, 320, 512)

    Indeed, the images return in array order (which is z, ch, y, x), but why are the z and ch dimensions fused together? If it returned (53, 3, 320, 512), I would certainly not have raised an issue.

    @blink1073 no, it appears I am using the vendored versions both for skimage and imageio. But pip installing tifffile made no difference to the outcome above.

  6. jni commented on Feb 25, 2017

    @jni
    ContributorAuthor

    Incidentally, skimage master still does "the right thing".

  7. blink1073 commented on Feb 25, 2017

    @blink1073

    @jni, that is bizarre considering skimage master and the latest tifffile.py use the same file. Can you verify you have tifffile 0.11.1 and commit 2fa77fca6c1f5d7ce73cddec31f07a01cfc84b1c in skimage?

  8. jni commented on Feb 26, 2017

    @jni
    ContributorAuthor

    @blink1073 the vendored skimage version is written as "2017.01.12" in skimage/external/tifffile/tifffile.py. How do I check which version is being used as a plugin (vendored or installed)? Installed is 0.11.1. At any rate, it seems from @cgohlke's linked PR that skimage does some magic on top of tifffile's returned array, so it's not that surprising that its output remains magical?

  9. blink1073 commented on Feb 26, 2017

    @blink1073

    Ah, yep.

  10. blink1073 commented on Feb 26, 2017

    @blink1073

    It will use the site-package vs the vendored one by design, I had forgotten about the extra processing.

  11. blink1073 commented on Feb 26, 2017

    @blink1073

    This is the extent of what we do in skimage:

        if 'img_num' in kwargs:
            kwargs['key'] = kwargs.pop('img_num')
        with open(fname, 'rb') as f:
            tif = TiffFile(f)
            return tif.asarray(**kwargs)

    versus imageio

    self._tf = _tifffile.TiffFile(self.request.get_file(), **kwargs)
    ...
    # Get data
    if index < 0 or index >= len(self._tf):
        raise IndexError(
             'Index out of range while reading from tiff file')
     im = self._tf[index].asarray()
  12. jni commented on Feb 27, 2017

    @jni
    ContributorAuthor

    @blink1073 We definitely do more to it. Here's the swapaxes magic.

    But, the difference appears to be in imageio using the 0 index for the TiffFile object, which returns a TiffPage object:

    > /Users/nuneziglesiasj/anaconda/envs/36/lib/python3.6/site-packages/imageio/plugins/tifffile.py(189)_get_data()
    -> im = self._tf[index].asarray()
    (Pdb) p index
    0
    (Pdb) p self._tf
    <tifffile.tifffile.TiffFile object at 0x11e3621d0>
    (Pdb) p self._tf[0]
    <tifffile.tifffile.TiffPage object at 0x11e3622b0>
    (Pdb) n
    > /Users/nuneziglesiasj/anaconda/envs/36/lib/python3.6/site-packages/imageio/plugins/tifffile.py(190)_get_data()
    -> meta = self._meta or self._get_meta_data(index)
    (Pdb) p type(im)
    <class 'numpy.ndarray'>
    (Pdb) p im.shape
    (159, 320, 512)
    (Pdb) p self._tf.asarray().shape
    (53, 3, 320, 512)

    I don't know whether this is the right way to deal with this. @cgohlke @almarklein do you know why imageio uses 0, and why the tiffpage ravels the first two dimensions?

  13. cgohlke commented on Feb 27, 2017

    @cgohlke
    Contributor

    why the tiffpage ravels the first two dimensions

    This unexpected shape for the data in the first TiffPage is an side-effect of the fast read mode for ImageJ hyperstacks. Use fastij=False to get the expected data of shape (320, 512). Use the TiffFile.asarray or TiffFile.series interfaces to get data from multiple TiffPages shaped according to the ImageJ metadata in the file.

  14. jni commented on Mar 2, 2017

    @jni
    ContributorAuthor

    @cgohlke @almarklein so the question is, should imageio.imread use TiffFile.asarray by default, instead of TiffPage? Based on my experience the answer is yes. =P

  15. almarklein commented on Mar 2, 2017

    @almarklein
    Member

    My experience with tiff files is limited, and I don't sufficiently understand this problem; leaving this decision to @blink1073 :)

  16. blink1073 commented on Mar 2, 2017

    @blink1073

    I'd say adopt the skimage convention.

  17. jni commented on Mar 6, 2017

    @jni
    ContributorAuthor

    @almarklein regarding the TiffFile plugin, do you know why a bare imread() ends up with an index of 0 instead of None?

  18. almarklein commented on Mar 6, 2017

    @almarklein
    Member

    @jni yes, imageio assumes that an image file contains 1 of more images, making index 0 the default. Formats that support just one image will typically raise an error if the index is nonzero. Using imread() on a multi-page image file will yield the first image.

    We could consider defaulting to None, if that helps in this case. But we'd have to adjust the other formats accordingly.

    Also worth noting that a Format can provide more methods to extend the API of the reader/writer. Though that only applies to the use of reader = imageio.get_reader(...), not to the simpler imread and mimread().

  19. added a commit that references this issue on May 21, 2017
    abe135e
  20. almarklein commented on May 21, 2017

    @almarklein
    Member

    Attempt at a fix: #255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions