Skip to content

Inspecting shape, dtype, etc. #263

Closed

Description

@jakirkham

It would be a really handy feature if one could inspect the shape and dtype of file without having to load it into memory. This would be really handy with things like Dask that need to know this information in advance, but don't necessarily need to load the data until a computation happens.

Edit: Maybe this could be (or is) possible through the Readers.

Activity

  1. almarklein commented on Jun 7, 2017

    @almarklein
    Member

    The Reader has been designed to allow reading metadata without loading the image data (if the plugin implements it correctly). Your idea is quite similar to this, but it would need some sort of API (user-facing and plugin-facing). Or we could make plugins publish this kind of information via the meta-data.

  2. jni commented on Jun 7, 2017

    @jni
    Contributor

    Indeed, a convenience function such as read_meta that returns a dict, potentially with some standard fields, would be really useful in many contexts.

    In the same vein as this issue, the TIFF metadata reader leaves much to be desired. In particular, if you look at the image I linked to in #226 [^1], opening it in Fiji yields a plethora of metadata (e.g. true volume dimensions), but virtually nothing in imageio, and only a bit better using TiffFile directly. @cgohlke might have more to say on the topic?

    [1] https://www.dropbox.com/s/dj3oarx7bqx4us2/E_z2_512_1um_CONTROL.tif?dl=1 (Updated)

  3. almarklein commented on Jun 7, 2017

    @almarklein
    Member

    potentially with some standard fields

    That sounds like a great idea. Let's come up with a set of fields (starting with shape, dtype, nchannels) that plugins are recommended to publish via their meta data.

  4. jni commented on Jun 7, 2017

    @jni
    Contributor

    The most critical after those is "spacing" for pixel/voxel spacing, along with "units" for the units of the rest of the metadata. @shoyer probably has another 50 fields to suggest. =D

    Incidentally, I think these standard fields should be returned in addition to whatever fields are in the format. Readers could then translate between the Reader's format and the standard format. But ideally, all the information in the file should be accessible from iio.

  5. almarklein commented on Jun 7, 2017

    @almarklein
    Member

    Incidentally, I think these standard fields should be returned in addition to whatever fields are in the format.

    I agree. We'd have to think about name clashes too. Perhaps by separating the two, e.g. by having a native field in the metadata, which is a dict with fields specific to the format.

  6. jni commented on Jun 7, 2017

    @jni
    Contributor

    having a native field in the metadata

    👍 !

  7. jakirkham commented on Jun 7, 2017

    @jakirkham
    Author

    starting with shape, dtype, nchannels

    Would recommend thinking about passing axes as well to specify axis ordering (especially if nchannels is considered). For instance if you look at the example, the axes are XYCZT. This is in the TIFF metadata or can also be found via the info in Fiji/ImageJ. Though letting this fall through in miscellaneous metadata might be ok too.

    It's not atypical to have all sorts of orderings of the axes and it can be a bit of a guessing game to figure out which is which. So any info passed along that might help is really great.

  8. jni commented on Jun 7, 2017

    @jni
    Contributor

    For instance if you look at the example, the axes are XYCZT.

    ... Which, in NumPy order, turns to TZCYX. This should be a fun project. =P

  9. cgohlke commented on Jun 7, 2017

    @cgohlke
    Contributor

    opening it in Fiji yields a plethora of metadata (e.g. true volume dimensions), but virtually nothing in imageio, and only a bit better using TiffFile directly

    I think it's all there but the info string needs to be converted to a dict:

    >>> from pprint import PrettyPrinter
    >>> from tifffile import TiffFile
    >>>
    >>> with TiffFile('E_z2_512_1um_CONTROL.tif') as tif:
    ...     PrettyPrinter().pprint(tif[0].imagej_tags)
    ...
    {'ImageJ': '1.50i',
     'channels': 3,
     'finterval': 1,
     'hyperstack': True,
     'images': 159,
     'info': ' BitsPerPixel = 12\n'
             ' DimensionOrder = XYCZT\n'
             ' IsInterleaved = false\n'
             ' IsRGB = false\n'
             ' LittleEndian = true\n'
             ' PixelType = uint16\n'
             ' Series 0 Name = Series 1\n'
             ' SizeC = 3\n'
             ' SizeT = 1\n'
             ' SizeX = 512\n'
             ' SizeY = 320\n'
             ' SizeZ = 53\n'
             'AS Level = 9999\n'
             'AbsPositionUnitName = mm\n'
             'AbsPositionValue = 0.0\n'
             'Adjust Outward = 0\n'
             'Adjust Retrun = 0\n'
             'AnalogPMTGain = 1.0\n'
             'Confocal = ON\n'
             'CountingPMTGain = 0.0\n'
             'CountingPMTOffset = 1.600000\n'
             'CountingPMTVoltage = 0\n'
             'Image 0 : AnalogPMTOffset = 0\n'
             'Image 0 : ExcitationOutPutLevel = 13\n'
             'Image 0 : PMTVoltage = 760\n'
             'Image 1 : AnalogPMTOffset = 4\n'
             'Image 1 : ExcitationOutPutLevel = 24\n'
             'Image 1 : PMTVoltage = 696\n'
    <snip>
     'loop': False,
     'luts': [array([  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    <snip>
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0], dtype=uint8)],
     'max': 4095.0,
     'min': 0.0,
     'mode': 'composite',
     'ranges': (0.0, 4095.0, 0.0, 4095.0, 0.0, 4095.0),
     'slices': 53,
     'unit': 'micron'}
  10. shoyer commented on Jun 7, 2017

    @shoyer

    As a point of reference, here are attribute we define for numpy-like array types used internally in xarray:
    https://github.com/pydata/xarray/blob/e39dae4c4ba83de09bea16ed5b3132ce4e88ba98/xarray/core/utils.py#L381-\L425

    dtype and shape are the most essential ones. An explicit __array__ method is also valuable for numpy compatibility. ndim and size are easy enough to add as well, defined based on shape.

  11. cgohlke commented on Jun 7, 2017

    @cgohlke
    Contributor

    It looks like the info string in the ImageJ metadata can be of many (undocumented) formats and can not be reliably be parsed in general. In the case of E_z2_512_1um_CONTROL.tif, the info string seems to contain metadata from an OIF file. I found other files where the info string contains a string representation of a Matlab struct. You might or might not be able to parse the info strings with oiffile.SettingsFile() or matlabstr2py().

  12. jni commented on Jun 7, 2017

    @jni
    Contributor

    I was afraid of this @cgohlke. Do you know whether ImageJ is using that info string or something else to pull out the pixel spacing?

  13. cgohlke commented on Jun 8, 2017

    @cgohlke
    Contributor

    I think ImageJ uses the x_resolution, y_resolution, resolution_unit, and image_description (the spacing and unit entries) TIFF tags of the first TIFF page to determine the pixel spacing. See blink1073/tifffile#15 (comment)

  14. stefanv commented on May 29, 2018

    @stefanv

    While it may be hard to parse general metadata, the basics asked for in the description (shape and dtype) may be feasible to implement already? EDIT: Looks like it is already available, and we just need an additional high-level API to expose it?

  15. almarklein commented on May 30, 2018

    @almarklein
    Member

    Looks like it is already available, and we just need an additional high-level API to expose it?

    That depend on the plugin. I think this is the case for the Pillow plugin, but not necessarily for any of the others ...

  16. jakirkham commented on May 30, 2018

    @jakirkham
    Author

    Think this should also be possible with tifffile as well.

  17. stefanv commented on May 30, 2018

    @stefanv

    Something like imread.header(image) could return whatever it can discover about the image as a dictionary with known fields. Whatever isn't there then becomes the consumer's responsibility to handle.

  18. almarklein commented on May 30, 2018

    @almarklein
    Member

    Actually, we already have Reader.get_meta_data (https://github.com/imageio/imageio/blob/master/imageio/core/format.py#L364), but its implementation/test coverage is not very complete. E.g.

    >>> imageio.get_reader('imageio:chelsea.png').get_meta_data()
    {'dpi': (72, 72)}

    I propose that we:

    • Formalize common fields that each plugin must try to make available, like shape and dtype.
    • Make plugins follow that, and add tests for it.
    • Add a convenience function to query this info in a somewhat easier way.

    (As for when/who: I probably don't have time to work on this before the summer myself.)

  19. almarklein commented on Sep 11, 2018

    @almarklein
    Member

    Closing this in favor of #382, where we propose/discuss a specific solution for this use-case.

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