@vitessce/spatial-utils 4.2.0 → 4.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,6 +29,31 @@ function isAxis(axisOrLabel) {
29
29
  return typeof axisOrLabel[0] !== 'string';
30
30
  }
31
31
 
32
+ /**
33
+ * Lowercase the axis names of every multiscale, since downstream code
34
+ * (viv selections, transforms, volume planning) expects t, c, z, y, x.
35
+ * Some writers use T, C, Z, Y, X (e.g. Zebrahub ZSNS003). Names that would
36
+ * collide once lowercased are left unchanged.
37
+ * @param {object} rootAttrs Flattened root attributes.
38
+ * @returns {object} Root attributes with lowercased axis names.
39
+ */
40
+ export function lowercaseAxisNames(rootAttrs) {
41
+ if (!Array.isArray(rootAttrs?.multiscales)) return rootAttrs;
42
+ const multiscales = rootAttrs.multiscales.map((multiscale) => {
43
+ const { axes } = multiscale;
44
+ if (!Array.isArray(axes)) return multiscale;
45
+ const names = axes.map(axis => (typeof axis === 'string' ? axis : axis.name));
46
+ const lowerNames = names.map(name => (typeof name === 'string' ? name.toLowerCase() : name));
47
+ if (new Set(lowerNames).size !== new Set(names).size) return multiscale;
48
+ return {
49
+ ...multiscale,
50
+ axes: axes.map((axis, i) => (typeof axis === 'string'
51
+ ? lowerNames[i] : { ...axis, name: lowerNames[i] })),
52
+ };
53
+ });
54
+ return { ...rootAttrs, multiscales };
55
+ }
56
+
32
57
  async function loadMultiscales(root) {
33
58
  let rootAttrs;
34
59
  try {
@@ -43,7 +68,7 @@ async function loadMultiscales(root) {
43
68
 
44
69
  // OME-NGFF v0.5 nests OME metadata under an `ome` key.
45
70
  // Hoist it to the root so downstream consumers see the flat v0.4 shape.
46
- rootAttrs = flattenOmeAttrs(rootAttrs);
71
+ rootAttrs = lowercaseAxisNames(flattenOmeAttrs(rootAttrs));
47
72
 
48
73
  let paths = ['0'];
49
74
  // Default axes used for v0.1 and v0.2.
@@ -0,0 +1,45 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { lowercaseAxisNames } from './load-ome-zarr.js';
3
+
4
+ describe('lowercaseAxisNames', () => {
5
+ it('lowercases axis objects and keeps their other properties', () => {
6
+ // As in Zebrahub ZSNS003.ome.zarr.
7
+ const rootAttrs = {
8
+ omero: {},
9
+ multiscales: [{
10
+ datasets: [{ path: '0' }],
11
+ axes: [
12
+ { name: 'T', type: 'time', unit: 'second' },
13
+ { name: 'C', type: 'channel' },
14
+ { name: 'Z', type: 'space', unit: 'micrometer' },
15
+ { name: 'Y', type: 'space', unit: 'micrometer' },
16
+ { name: 'X', type: 'space', unit: 'micrometer' },
17
+ ],
18
+ }],
19
+ };
20
+ const result = lowercaseAxisNames(rootAttrs);
21
+ expect(result.multiscales[0].axes).toEqual([
22
+ { name: 't', type: 'time', unit: 'second' },
23
+ { name: 'c', type: 'channel' },
24
+ { name: 'z', type: 'space', unit: 'micrometer' },
25
+ { name: 'y', type: 'space', unit: 'micrometer' },
26
+ { name: 'x', type: 'space', unit: 'micrometer' },
27
+ ]);
28
+ expect(result.multiscales[0].datasets).toBe(rootAttrs.multiscales[0].datasets);
29
+ expect(result.omero).toBe(rootAttrs.omero);
30
+ // The input is not mutated.
31
+ expect(rootAttrs.multiscales[0].axes[0].name).toBe('T');
32
+ });
33
+
34
+ it('lowercases v0.3 string axes', () => {
35
+ expect(lowercaseAxisNames({ multiscales: [{ axes: ['Z', 'Y', 'X'] }] }).multiscales[0].axes)
36
+ .toEqual(['z', 'y', 'x']);
37
+ });
38
+
39
+ it('leaves names that would collide, and metadata without axes, unchanged', () => {
40
+ const colliding = { multiscales: [{ axes: ['x', 'X'] }] };
41
+ expect(lowercaseAxisNames(colliding).multiscales[0].axes).toEqual(['x', 'X']);
42
+ const v02 = { multiscales: [{ datasets: [{ path: '0' }] }] };
43
+ expect(lowercaseAxisNames(v02)).toEqual(v02);
44
+ });
45
+ });
package/src/spatial.js CHANGED
@@ -650,6 +650,25 @@ export function normalizeCoordinateTransformations(coordinateTransformations, da
650
650
  return result;
651
651
  }
652
652
 
653
+ /**
654
+ * Scale of one OME-NGFF multiscales dataset (resolution level) per axis:
655
+ * the product of its scale transformations, including those within
656
+ * sequences (as proposed in https://github.com/ome/ngff/pull/138).
657
+ * Translations do not change the scale; other types are not supported.
658
+ * @param {object} dataset An element of multiscales[0].datasets.
659
+ * @returns {number[]|undefined} One factor per axis, in the order of the
660
+ * multiscales axes, or undefined when the dataset has no scale.
661
+ */
662
+ export function getDatasetScale(dataset) {
663
+ const scales = normalizeCoordinateTransformations(dataset?.coordinateTransformations, null)
664
+ .filter(transform => transform?.type === 'scale' && Array.isArray(transform.scale))
665
+ .map(transform => transform.scale);
666
+ if (scales.length === 0) {
667
+ return undefined;
668
+ }
669
+ return scales.reduce((product, scale) => product.map((factor, axis) => factor * scale[axis]));
670
+ }
671
+
653
672
  /**
654
673
  * Convert coordinate transformations to a 4x4 matrix for SpatialData
655
674
  * spatial elements. This function is intended to be used with SpatialData
@@ -4,6 +4,7 @@ import {
4
4
  normalizeCoordinateTransformations,
5
5
  getSwapAxesMatrix,
6
6
  coordinateTransformationsToMatrixForSpatialData,
7
+ getDatasetScale,
7
8
  } from './spatial.js';
8
9
 
9
10
  const defaultAxes = [
@@ -865,4 +866,38 @@ describe('Spatial.js', () => {
865
866
  ]);
866
867
  });
867
868
  });
869
+
870
+ describe('getDatasetScale', () => {
871
+ it('gives the scale of a dataset, ignoring translations', () => {
872
+ expect(getDatasetScale({
873
+ path: '1',
874
+ coordinateTransformations: [
875
+ { type: 'scale', scale: [1, 1, 0.4, 0.2, 0.2] },
876
+ { type: 'translation', translation: [0, 0, 0, 0.1, 0.1] },
877
+ ],
878
+ })).toEqual([1, 1, 0.4, 0.2, 0.2]);
879
+ });
880
+
881
+ it('multiplies scales, including those within sequences', () => {
882
+ expect(getDatasetScale({
883
+ coordinateTransformations: [{
884
+ type: 'sequence',
885
+ input: 'array',
886
+ output: 'physical',
887
+ transformations: [
888
+ { type: 'scale', scale: [2, 4, 4] },
889
+ { type: 'scale', scale: [1, 0.5, 0.25] },
890
+ ],
891
+ }],
892
+ })).toEqual([2, 2, 1]);
893
+ });
894
+
895
+ it('gives undefined without a scale', () => {
896
+ expect(getDatasetScale({ path: '0' })).toBeUndefined();
897
+ expect(getDatasetScale(undefined)).toBeUndefined();
898
+ expect(getDatasetScale({
899
+ coordinateTransformations: [{ type: 'identity' }],
900
+ })).toBeUndefined();
901
+ });
902
+ });
868
903
  });