Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ome-tiff files are being read with wrong preference #79

Open
TimMonko opened this issue Dec 17, 2024 · 3 comments
Open

ome-tiff files are being read with wrong preference #79

TimMonko opened this issue Dec 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@TimMonko
Copy link
Contributor

TimMonko commented Dec 17, 2024

Describe the Bug

Given an image saved with bioio.OmeTiffWriter.save(), BioImage is still defaulting to bioio-tifffile.Reader instead of bioio-ome-tiff.Reader on bioio version 1.1.0.

Expected Behavior

After #55, bioio-ome-tiff is supposed to take precedence over bioio-tifffile when doing BioImage(file_path). From what I understand, the file does not need to end with .ome.tiff but could just end in .tiff and bioio-ome-tiff should take preference (this is for an image with a .tiff ending).

Reproduction

bioio.plugin_feasibility_report(file_path)

{'bioio-ome-tiff': PluginSupport(supported=True, error=None),
 'bioio-imageio': PluginSupport(supported=True, error=None),
 'bioio-tifffile': PluginSupport(supported=True, error=None),
 'bioio-ome-zarr': PluginSupport(supported=False, error=None),
 'bioio-czi': PluginSupport(supported=False, error=None),
 'bioio-lif': PluginSupport(supported=False, error=None),
 'bioio-nd2': PluginSupport(supported=False, error=None),
 'ArrayLike': PluginSupport(supported=False, error=None)}

Reader defaults to bioio-tifffile:

BioImage.determine_plugin(file_path)

PluginEntry(entrypoint=EntryPoint(name='bioio-tifffile', value='bioio_tifffile', group='bioio.readers'), metadata=<class 'bioio_tifffile.reader_metadata.ReaderMetadata'>, timestamp=1734118509.7816575)

But, bioio_ome_tiff shows that it was saved properly as an OME-TIFF via OmeTiffWriter

img = BioImage(image_files[0], reader=bioio_ome_tiff.Reader)
img.ome_metadata

OME(
images=[<1 field_type>],
creator='bioio 1.1.0',
)

bioio.plugins.dump_plugins()

bioio-czi
  Author  : Not Specified
  Version : 1.0.1
  License : MIT License
  Date    : 2024-12-13 13:35:12.960038
  Supported Extensions : .czi
bioio-imageio
  Author  : Not Specified
  Version : 1.0.0
  License : MIT License
  Date    : 2024-12-13 13:35:05.444297
  Supported Extensions : 264, 265, 3fr, 3g2, A64, IMT, MCIDAS, PCX, SPIDER, XVTHUMB, a64, adp, amr, amv, apng, arw, asf, avc, avi, avs, avs2, bay, bif, bmp, cdg, cgi, cif, ct, dcr, dib, dip, dng, dnxhd, dv, dvd, erf, exr, fff, gif, icb, if, iiq, ism, jif, jfif, jng, jp2, jpg, mov, mp4, mpo, msp, pdf, pngppm, ps, zif
bioio-ome-zarr
  Author  : Not Specified
  Version : 1.0.1
  License : MIT License
  Date    : 2024-12-13 13:35:06.050469
  Supported Extensions : .zarr
bioio-ome-tiff
  Author  : Not Specified
  Version : 1.0.1
  License : MIT License
  Date    : 2024-12-13 13:35:06.013810
  Supported Extensions : .ome.tiff, .tiff, ome.tif, .tif
bioio-tifffile
  Author  : Not Specified
  Version : 1.0.0
  License : MIT License
  Date    : 2024-12-13 13:35:09.781657
  Supported Extensions : tif, tiff
bioio-lif
  Author  : Not Specified
  Version : 1.0.0
  License : MIT License
  Date    : 2024-12-13 13:35:12.998235
  Supported Extensions : .lif
bioio-nd2
  Author  : Not Specified
  Version : 1.0.0
  License : MIT License
  Date    : 2024-12-13 13:35:05.899621
  Supported Extensions : .nd2

Environment

  • OS Version: Windows 11
  • bioio Version: 1.1.0
@TimMonko TimMonko added the bug Something isn't working label Dec 17, 2024
@TimMonko
Copy link
Contributor Author

Ohhh, maybe I figured out how it works...
If I rename/save the file ending with ome.tiff instead of .tiff, then bioio-ome-tiff takes precendence. I think my expectation would be that any ome-tiff compatible image would default to bioio-ome-tiff. I think I understand the pattern in #55 now (had to sleep on it).

I'm thinking of two possibilities to resolve this

  1. either a more expensive call to check if there is ome-compatable metadata in the image, and then using the ome-tiff reader or
  2. let users set a preference to a certain reader, and then fall back to other readers if it's not possible.

The use case of this is something like napari-bioio (when it exists) or other libraries which want to just dump all the bioio plugins into the library dependenciesfor broader compatability reasons, instead of installing only the plugins relevant to a specific project.

@SeanLeRoy
Copy link
Contributor

We currently only rely on file extension and plugin installation time. You can always install the reader if this becomes a blocker. Hopefully in the future we can move towards something a bit smarter, but none of the alternatives we have encountered have been flexible & performant enough.

@TimMonko Do you have any thoughts on improvements?

@TimMonko
Copy link
Contributor Author

TimMonko commented Jan 14, 2025

Here is what I ended up doing. Not really the most elegant solution, but you could have some kind of setting like bioio.preferred_reader = 'ome-tiff-reader', that intercepts on init of the reader selection (if its compatible). But this is something a user would have to set, instead of the default behavior (which I think makes sense and is ok). This means if someone develops some format competing reader that they prefer (for some reason) they could also specify in settings as necessary, but still keep the other readers for fallback

https://github.com/TimMonko/napari-ndev/blob/f091133e54bdf82d98d7520cdffe9194ccaadc16/src/napari_ndev/nimage.py#L21-L76

class nImage(BioImage):
    def __init__(
        self,
        image: ImageLike,
        reader: Reader | None = None
    ) -> None:
        """
        Initialize an nImage with an image, and optionally a reader.


        If a reader is not provided, a reader will be determined by bioio.
        However, if the image is supported by bioio-ome-tiff, the reader
        will be set to bioio_ome_tiff.Reader to override the softer decision
        made by bioio.BioImage.determine_plugin().


        Note: The issue here is that bioio.BioImage.determine_plugin() will
        sort by install time and choose the first plugin that supports the
        image. This is not always the desired behavior, because bioio-tifffile
        can take precedence over bioio-ome-tiff, even if the image was saved
        as an OME-TIFF via bioio.writers.OmeTiffWriter (which is the case
        for napari-ndev).
        """
        if reader is None:
            from bioio import plugin_feasibility_report as pfr
            fr = pfr(image)
            if 'bioio-ome-tiff' in fr and fr['bioio-ome-tiff'].supported:
                import bioio_ome_tiff
                reader = bioio_ome_tiff.Reader

        super().__init__(image, reader)
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants