hms-dbmi / viv

Library for multiscale visualization of high-resolution multiplexed bioimaging data on the web. Directly renders Zarr and OME-TIFF.
http://avivator.gehlenborglab.org
MIT License
273 stars 43 forks source link

Avivator cannot display images with default TiffData attributes in XML metadata #782

Closed CameronFRWatson closed 5 months ago

CameronFRWatson commented 5 months ago

Describe the bug Avivator fails when attempting to view images with metadata written by ome-types (v0.5.0) possibly due to IFD and PlaneCount attributes not being considered optional in Viv's OME XML validation.

As far as I understand, IFD and PlaneCount are optional attributes of the TiffData element according to the most recent version of the OME XML Schema (http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd):

<xsd:attribute name="IFD" type="NonNegativeInt" default="0" use="optional">

<xsd:attribute name="PlaneCount" use="optional" type="NonNegativeInt">

In Viv's OME XML validation (packages/loaders/src/omexml.ts), these attributes appear to not be considered optional:

const TiffDataSchema = z
  .object({ UUID: UuidSchema.optional() })
  .extend({
    attr: z.object({
      IFD: z.coerce.number(),
      PlaneCount: z.coerce.number(),
      FirstT: z.coerce.number().optional(),
      FirstC: z.coerce.number().optional(),
      FirstZ: z.coerce.number().optional()
    })
  })
  .transform(flattenAttributes);

This causes images with metadata written by ome_types to fail because if the value is the default (IFD = 0, for example), the attribute is omitted from the XML. The following errors are thrown in the UI when reading such an image with Avivator:

Something has gone wrong loading your image. Please refer to the docs for information about supported file formats.

and in the console:

ZodError: [
  {
    "code": "invalid_type",
    "expected": "number",
    "received": "nan",
    "path": [
      "Image",
      0,
      "Pixels",
      "TiffData",
      0,
      "attr",
      "IFD"
    ],
    "message": "Expected number, received nan"
  }
]

To Reproduce Steps to reproduce the behavior:

I've generated two versions of the same image which are available on our Galaxy instance here:

https://cancer.usegalaxy.org/u/watsocam/h/avivator-ifd-default-issue-example

You can download the images (400MB each, or view them using the display at Avivator button which appears when you click on the dataset)

One of the images was created using bioformats and has <TiffData IFD="0" PlaneCount="12" />

The second image has metadata generated by ome_types, which omits attributes if they are the default value: <TiffData PlaneCount="12" />

The second image is not renderable in Avivator and throws the error outlined above, despite the IFD attribute being optional for TiffData elements.

Please let me know if there's anything else I can provide to help explain the issue, or if you have any problems with the examples I provided.

Thank you!

Cameron

Environment:

ilan-gold commented 5 months ago

@CameronFRWatson I have a branch for this but just as a heads up, the ome_types file has all [255, 255, 255] as default colors and these will get rendered

CameronFRWatson commented 5 months ago

@ilan-gold thank you for the quick response, and for the heads up! I'll update our script to set non-default colors for the channels

manzt commented 5 months ago

Thanks very much for the detailed issue! We should make our handling of IFD and PlaneCount optional as well. The schema strictness was introduced to help us internally. If I understand correctly, should the default IFD be 0 and is there a default for PlaneCount?

See #783

CameronFRWatson commented 5 months ago

@manzt looking at the OME schema (ome.xsd), yes, IFD has a default of 0. I think that the PlaneCount default is 1, although it's odd that isn't specified in the XML tag, and instead just in the xsd:documentation tag:

      <xsd:attribute name="IFD" type="NonNegativeInt" default="0" use="optional">
        <xsd:annotation>
          <xsd:documentation>
            Gives the IFD(s) for which this element is applicable. Indexed from 0.
            Default is 0 (the first IFD). [units:none]
          </xsd:documentation>
        </xsd:annotation>
      </xsd:attribute>

      <xsd:attribute name="PlaneCount" use="optional" type="NonNegativeInt">
        <xsd:annotation>
          <xsd:documentation>
            Gives the number of IFDs affected. Dimension order of IFDs is given by the enclosing
            Pixels element's DimensionOrder attribute. Default is the number of IFDs in the TIFF
            file, unless an IFD is specified, in which case the default is 1. [units:none]
          </xsd:documentation>
        </xsd:annotation>
manzt commented 5 months ago

Thanks so much for helping track down this info! Let's just go with default PlaneCount=1 as well.

CameronFRWatson commented 5 months ago

Thank you @manzt and @ilan-gold for your speedy help!!