From 2198e5088a592843e18f0f1e536b7b2e215739f4 Mon Sep 17 00:00:00 2001 From: Trevor Manz Date: Tue, 14 Nov 2023 14:20:27 -0500 Subject: [PATCH] fix(loaders): Allow optional TiffData in OME-XML (#714) --- CHANGELOG.md | 1 + packages/loaders/src/omexml.ts | 2 +- sites/avivator/src/utils.js | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6491f76..4971f7e58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Changed - Fix Avivator demo for OME-Zarr with only spatial axes +- Allow OME-XML to omit `TiffData` tags ## 0.14.1 diff --git a/packages/loaders/src/omexml.ts b/packages/loaders/src/omexml.ts index f3edf62d5..00c034af0 100644 --- a/packages/loaders/src/omexml.ts +++ b/packages/loaders/src/omexml.ts @@ -114,7 +114,7 @@ const TiffDataSchema = z const PixelsSchema = z .object({ Channel: z.preprocess(ensureArray, ChannelSchema.array()), - TiffData: z.preprocess(ensureArray, TiffDataSchema.array()) + TiffData: z.preprocess(ensureArray, TiffDataSchema.array()).optional() }) .extend({ attr: z.object({ diff --git a/sites/avivator/src/utils.js b/sites/avivator/src/utils.js index cec9b5d13..a41e04cb1 100644 --- a/sites/avivator/src/utils.js +++ b/sites/avivator/src/utils.js @@ -135,6 +135,14 @@ async function getTotalImageCount(src, rootMeta, data) { return numImagesPerResolution * levels; } +/** + * @param {unknown} e + * @returns {e is Error & { issues: unknown }} + */ +function isZodError(e) { + return e instanceof Error && 'issues' in e; +} + /** * Given an image source, creates a PixelSource[] and returns XML-meta * @@ -211,7 +219,13 @@ export async function createLoader( let source; try { source = await loadBioformatsZarr(urlOrFile); - } catch { + } catch (e) { + if (isZodError(e)) { + // If the error is a ZodError, it means there was an OME-XML file + // but it was invalid. We shouldn't try to load the file as a OME-Zarr. + throw e; + } + // try ome-zarr const res = await loadOmeZarr(urlOrFile, { type: 'multiscales' }); // extract metadata into OME-XML-like form