@vitessce/image-utils 3.2.0

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.
@@ -0,0 +1,399 @@
1
+ import {
2
+ coordinateTransformationsToMatrix,
3
+ getNgffAxes,
4
+ getNgffAxesForTiff,
5
+ physicalSizeToMatrix,
6
+ hexToRgb,
7
+ getSourceFromLoader,
8
+ canLoadResolution,
9
+ getStatsForResolution,
10
+ getBoundingCube,
11
+ isInterleaved as isInterleavedUtil,
12
+ } from '@vitessce/spatial-utils';
13
+ import { VIEWER_PALETTE } from '@vitessce/utils';
14
+ import type { LoadOmeTiffReturnValue } from './ome-tiff-types.js';
15
+ import type { LoadOmeZarrReturnValue } from './ome-zarr-types.js';
16
+
17
+
18
+ type VivLoaderType<S extends string[]> = LoadOmeTiffReturnValue<S> | LoadOmeZarrReturnValue<S>;
19
+ type VivLoaderDataType<S extends string[]> = VivLoaderType<S>['data'];
20
+ type ImageOptions = {
21
+ coordinateTransformations?: object[]; // TODO: stricter type
22
+ offsetsUrl?: string;
23
+ };
24
+
25
+ type ChannelObject = {
26
+ name: string;
27
+ // Defaults that originate from image file contents
28
+ // (takes precedence over automatic defaults below)
29
+ defaultColor?: number[];
30
+ defaultWindow?: [number, number];
31
+ // Defaults for automatic initialization
32
+ // (if defaults above are null or not provided).
33
+ // TODO: should autoDefaultColor be exposed as a separate value?
34
+ // or just set as the value of defaultColor when applicable?
35
+ autoDefaultColor?: number[];
36
+ };
37
+
38
+ type ResolutionObject = {
39
+ height: number;
40
+ width: number;
41
+ depthDownsampled: number;
42
+ totalBytes: number;
43
+ canLoad: boolean;
44
+ };
45
+
46
+ type BoundingCube = [
47
+ [number, number],
48
+ [number, number],
49
+ [number, number]
50
+ ];
51
+
52
+ /**
53
+ * A wrapper around the Viv loader, to provide a common interface for
54
+ * all image file types.
55
+ */
56
+ export default class ImageWrapper<S extends string[]> {
57
+ vivLoader: VivLoaderType<S>;
58
+
59
+ options: ImageOptions;
60
+
61
+ constructor(vivLoader: VivLoaderType<S>, options: ImageOptions) {
62
+ this.options = options || {};
63
+ this.vivLoader = vivLoader;
64
+ }
65
+
66
+ getType(): 'ome-tiff' | 'ome-zarr' {
67
+ if ('Pixels' in this.vivLoader.metadata) {
68
+ return 'ome-tiff';
69
+ }
70
+ if ('omero' in this.vivLoader.metadata) {
71
+ return 'ome-zarr';
72
+ }
73
+ throw new Error('Unknown image type.');
74
+ }
75
+
76
+ hasPhysicalSize(): boolean {
77
+ if ('Pixels' in this.vivLoader.metadata) {
78
+ // This is the OME-TIFF case.
79
+ const {
80
+ Pixels: {
81
+ PhysicalSizeX,
82
+ PhysicalSizeXUnit,
83
+ PhysicalSizeY,
84
+ PhysicalSizeYUnit,
85
+ },
86
+ } = this.vivLoader.metadata;
87
+ return Boolean(
88
+ PhysicalSizeX
89
+ && PhysicalSizeXUnit
90
+ && PhysicalSizeY
91
+ && PhysicalSizeYUnit,
92
+ );
93
+ }
94
+ // This is the OME-Zarr case.
95
+ // OME-Zarr is required to have coordinateTransformations.
96
+ return true;
97
+ }
98
+
99
+ getData(): VivLoaderDataType<S> {
100
+ return this.vivLoader.data;
101
+ }
102
+
103
+ getModelMatrix(): number[] {
104
+ // The user can always provide an additional transform matrix
105
+ // via the file definition options property.
106
+ const { coordinateTransformations: coordinateTransformationsFromOptions } = this.options;
107
+ // We combine any user-provided transform matrix with the one
108
+ // from the image file.
109
+ if ('multiscales' in this.vivLoader.metadata) {
110
+ // OME-Zarr case.
111
+ const {
112
+ multiscales: [
113
+ {
114
+ coordinateTransformations,
115
+ axes,
116
+ },
117
+ ],
118
+ } = this.vivLoader.metadata;
119
+ // Axes in v0.4 format.
120
+ const ngffAxes = getNgffAxes(axes);
121
+ const transformMatrixFromOptions = coordinateTransformationsToMatrix(
122
+ coordinateTransformationsFromOptions, ngffAxes,
123
+ );
124
+ const transformMatrixFromFile = coordinateTransformationsToMatrix(
125
+ coordinateTransformations, ngffAxes,
126
+ );
127
+ const transformMatrix = transformMatrixFromFile.multiplyLeft(transformMatrixFromOptions);
128
+ return transformMatrix;
129
+ }
130
+ if ('Pixels' in this.vivLoader.metadata) {
131
+ // OME-TIFF case.
132
+ const {
133
+ Pixels: {
134
+ PhysicalSizeX,
135
+ PhysicalSizeXUnit,
136
+ PhysicalSizeY,
137
+ PhysicalSizeYUnit,
138
+ PhysicalSizeZ,
139
+ PhysicalSizeZUnit,
140
+ DimensionOrder,
141
+ },
142
+ } = this.vivLoader.metadata;
143
+
144
+ const ngffAxes = getNgffAxesForTiff(DimensionOrder);
145
+ const transformMatrixFromOptions = coordinateTransformationsToMatrix(
146
+ coordinateTransformationsFromOptions, ngffAxes,
147
+ );
148
+ // For the OME-TIFF case, we convert the size and unit information
149
+ // to a transformation matrix.
150
+ const transformMatrixFromFile = physicalSizeToMatrix(
151
+ PhysicalSizeX, PhysicalSizeY, PhysicalSizeZ,
152
+ PhysicalSizeXUnit, PhysicalSizeYUnit, PhysicalSizeZUnit,
153
+ );
154
+ const transformMatrix = transformMatrixFromFile.multiplyLeft(transformMatrixFromOptions);
155
+ return transformMatrix;
156
+ }
157
+ throw new Error('Unknown image type.');
158
+ }
159
+
160
+ getDefaultTargetT(): number {
161
+ if ('omero' in this.vivLoader.metadata) {
162
+ // OME-Zarr case.
163
+ const {
164
+ omero: {
165
+ rdefs: {
166
+ defaultT,
167
+ },
168
+ },
169
+ } = this.vivLoader.metadata;
170
+ return defaultT || 0;
171
+ }
172
+ return 0;
173
+ }
174
+
175
+ getDefaultTargetZ(): number {
176
+ if ('omero' in this.vivLoader.metadata) {
177
+ // OME-Zarr case.
178
+ const {
179
+ omero: {
180
+ rdefs: {
181
+ defaultZ,
182
+ },
183
+ },
184
+ } = this.vivLoader.metadata;
185
+ return defaultZ || 0;
186
+ }
187
+ return 0;
188
+ }
189
+
190
+ getName(): string {
191
+ let result;
192
+ if ('Pixels' in this.vivLoader.metadata) {
193
+ // This is the OME-TIFF case.
194
+ const {
195
+ Name,
196
+ } = this.vivLoader.metadata;
197
+ result = Name;
198
+ }
199
+ if ('omero' in this.vivLoader.metadata) {
200
+ // This is the OME-Zarr case.
201
+ const {
202
+ omero: {
203
+ name,
204
+ },
205
+ } = this.vivLoader.metadata;
206
+ result = name;
207
+ }
208
+ if (!result) {
209
+ // Fallback to a default name.
210
+ result = 'Image';
211
+ }
212
+ return result;
213
+ }
214
+
215
+ getNumChannels(): number {
216
+ if ('Pixels' in this.vivLoader.metadata) {
217
+ const {
218
+ Pixels: {
219
+ Channels,
220
+ },
221
+ } = this.vivLoader.metadata;
222
+ return Channels.length;
223
+ }
224
+ if ('omero' in this.vivLoader.metadata) {
225
+ const {
226
+ omero: {
227
+ channels,
228
+ },
229
+ } = this.vivLoader.metadata;
230
+ return channels.length;
231
+ }
232
+ return 0;
233
+ }
234
+
235
+ getChannelNames(): string[] {
236
+ if ('Pixels' in this.vivLoader.metadata) {
237
+ const {
238
+ Pixels: {
239
+ Channels,
240
+ },
241
+ } = this.vivLoader.metadata;
242
+ return Channels.map((channel, i) => channel.Name || `Channel ${i}`);
243
+ }
244
+ if ('omero' in this.vivLoader.metadata) {
245
+ const {
246
+ omero: {
247
+ channels,
248
+ },
249
+ } = this.vivLoader.metadata;
250
+ return channels.map((channel, i) => channel.label || `Channel ${i}`);
251
+ }
252
+ return [];
253
+ }
254
+
255
+ getChannelObjects(): ChannelObject[] {
256
+ if ('omero' in this.vivLoader.metadata) {
257
+ // This is the OME-Zarr case.
258
+ const {
259
+ omero: {
260
+ channels,
261
+ },
262
+ } = this.vivLoader.metadata;
263
+ return channels.map((channel, i) => ({
264
+ name: channel.label || `Channel ${i}`,
265
+ defaultColor: channel.color
266
+ ? hexToRgb(channel.color)
267
+ : undefined,
268
+ defaultWindow: channel.window
269
+ ? [channel.window.start, channel.window.end]
270
+ : undefined,
271
+ autoDefaultColor: VIEWER_PALETTE[i % VIEWER_PALETTE.length],
272
+ }));
273
+ }
274
+ if ('Pixels' in this.vivLoader.metadata) {
275
+ const {
276
+ Pixels: {
277
+ Channels,
278
+ },
279
+ } = this.vivLoader.metadata;
280
+ return Channels.map((channel, i) => ({
281
+ name: channel.Name || `Channel ${i}`,
282
+ defaultColor: channel.Color
283
+ ? channel.Color
284
+ : undefined,
285
+ defaultWindow: undefined, // TODO: does OME-TIFF support this?
286
+ autoDefaultColor: VIEWER_PALETTE[i % VIEWER_PALETTE.length],
287
+ }));
288
+ }
289
+ return [];
290
+ }
291
+
292
+ getDtype(): string | undefined {
293
+ const loader = this.vivLoader;
294
+ const source = getSourceFromLoader(loader) as any;
295
+ if ('dtype' in source) {
296
+ return source.dtype as string;
297
+ }
298
+ return undefined;
299
+ }
300
+
301
+ hasZStack(): boolean {
302
+ const loader = this.vivLoader;
303
+ const { labels, shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
304
+ const hasZStack = shape[labels.indexOf('z')] > 1;
305
+ return hasZStack;
306
+ }
307
+
308
+ hasTStack(): boolean {
309
+ const loader = this.vivLoader;
310
+ const { labels, shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
311
+ const hasTStack = shape[labels.indexOf('t')] > 1;
312
+ return hasTStack;
313
+ }
314
+
315
+ getNumZ(): number {
316
+ const loader = this.vivLoader;
317
+ const { labels, shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
318
+ return shape[labels.indexOf('z')];
319
+ }
320
+
321
+ getNumT(): number {
322
+ const loader = this.vivLoader;
323
+ const { labels, shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
324
+ return shape[labels.indexOf('t')];
325
+ }
326
+
327
+ isMultiResolution(): boolean {
328
+ const loader = this.vivLoader;
329
+ const hasViewableResolutions = Boolean(
330
+ Array.from({
331
+ length: loader.data.length,
332
+ }).filter((_, resolution) => canLoadResolution(loader.data, resolution)).length,
333
+ );
334
+ return hasViewableResolutions;
335
+ }
336
+
337
+ getMultiResolutionStats(): ResolutionObject[] {
338
+ const loader = this.vivLoader;
339
+ return Array.from({ length: loader.data.length })
340
+ .fill(0)
341
+ // eslint-disable-next-line no-unused-vars
342
+ .map((_, resolution) => {
343
+ const {
344
+ height,
345
+ width,
346
+ depthDownsampled,
347
+ totalBytes,
348
+ } = getStatsForResolution(loader.data, resolution);
349
+ return {
350
+ canLoad: canLoadResolution(loader.data, resolution),
351
+ height,
352
+ width,
353
+ depthDownsampled,
354
+ totalBytes,
355
+ };
356
+ });
357
+ }
358
+
359
+ /**
360
+ * Compute an index of an array element returned by getMultiResolutionStats()
361
+ * which corresponds to a "good" automatic target resolution to select.
362
+ * In the future, we could make this more sophisticated, for example
363
+ * to take into account the network speed.
364
+ */
365
+ getAutoTargetResolution(): number|null {
366
+ const multiResStats = this.getMultiResolutionStats();
367
+ if (multiResStats.length === 0) {
368
+ return null;
369
+ }
370
+ let nextTargetResolution = -1;
371
+ let totalBytes = Infinity;
372
+ do {
373
+ nextTargetResolution += 1;
374
+ // eslint-disable-next-line prefer-destructuring
375
+ totalBytes = multiResStats[nextTargetResolution].totalBytes;
376
+ } while (totalBytes > 5e7 && nextTargetResolution < multiResStats.length - 1);
377
+ return nextTargetResolution;
378
+ }
379
+
380
+ getBoundingCube(): BoundingCube {
381
+ const loader = this.vivLoader;
382
+ const { labels, shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
383
+
384
+ const physicalSizeScalingMatrix = this.getModelMatrix();
385
+ const xSlice: [number, number] = [0, physicalSizeScalingMatrix[0] * shape[labels.indexOf('x')]];
386
+ const ySlice: [number, number] = [0, physicalSizeScalingMatrix[5] * shape[labels.indexOf('y')]];
387
+ const zSlice: [number, number] = [
388
+ 0,
389
+ physicalSizeScalingMatrix[10] * shape[labels.indexOf('z')],
390
+ ];
391
+ return [xSlice, ySlice, zSlice];
392
+ }
393
+
394
+ isInterleaved(): boolean {
395
+ const loader = this.vivLoader;
396
+ const { shape } = Array.isArray(loader.data) ? loader.data[0] : loader.data;
397
+ return isInterleavedUtil(shape);
398
+ }
399
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as ImageWrapper } from './ImageWrapper.js';
@@ -0,0 +1,117 @@
1
+ import type { TiffPixelSource } from '@hms-dbmi/viv';
2
+
3
+ type DimensionOrder = 'XYZCT'
4
+ | 'XYZTC'
5
+ | 'XYCTZ'
6
+ | 'XYCZT'
7
+ | 'XYTCZ'
8
+ | 'XYTZC';
9
+
10
+ type PixelType =
11
+ | 'int8'
12
+ | 'int16'
13
+ | 'int32'
14
+ | 'uint8'
15
+ | 'uint16'
16
+ | 'uint32'
17
+ | 'float'
18
+ | 'bit'
19
+ | 'double'
20
+ | 'complex'
21
+ | 'double-complex';
22
+
23
+ type UnitsLength =
24
+ | 'Ym'
25
+ | 'Zm'
26
+ | 'Em'
27
+ | 'Pm'
28
+ | 'Tm'
29
+ | 'Gm'
30
+ | 'Mm'
31
+ | 'km'
32
+ | 'hm'
33
+ | 'dam'
34
+ | 'm'
35
+ | 'dm'
36
+ | 'cm'
37
+ | 'mm'
38
+ | 'µm'
39
+ | 'nm'
40
+ | 'pm'
41
+ | 'fm'
42
+ | 'am'
43
+ | 'zm'
44
+ | 'ym'
45
+ | 'Å'
46
+ | 'thou'
47
+ | 'li'
48
+ | 'in'
49
+ | 'ft'
50
+ | 'yd'
51
+ | 'mi'
52
+ | 'ua'
53
+ | 'ly'
54
+ | 'pc'
55
+ | 'pt'
56
+ | 'pixel'
57
+ | 'reference frame';
58
+
59
+ type Instrument = {
60
+ Objective: {
61
+ ID: string;
62
+ Model: string;
63
+ NominalMagnification: unknown;
64
+ };
65
+ };
66
+
67
+ type Channel = {
68
+ ID: string;
69
+ SamplesPerPixel: number;
70
+ Name?: string;
71
+ Color?: number[]; // [R, G, B, A]
72
+ };
73
+
74
+ type Pixels = {
75
+ Channels: Channel[];
76
+ TiffData: {
77
+ IFD: string;
78
+ PlaneCount: number;
79
+ };
80
+ PhysicalSizeX: number;
81
+ PhysicalSizeXUnit: UnitsLength;
82
+ PhysicalSizeY: number;
83
+ PhysicalSizeYUnit: UnitsLength;
84
+ PhysicalSizeZ: number;
85
+ PhysicalSizeZUnit: UnitsLength;
86
+ SizeT: number;
87
+ SizeC: number;
88
+ SizeZ: number;
89
+ SizeY: number;
90
+ SizeX: number;
91
+ SignificantBits: number;
92
+ BigEndian: boolean;
93
+ Interleaved: boolean;
94
+ ID: string;
95
+ DimensionOrder: DimensionOrder;
96
+ Type: PixelType;
97
+ }
98
+
99
+ type Image = {
100
+ ID: string;
101
+ Name: string;
102
+ AquisitionDate?: string;
103
+ Description?: string;
104
+ Pixels: Pixels;
105
+ InstrumentRef: { ID: string };
106
+ ObjectiveSettings: { ID: string };
107
+ }
108
+
109
+ type OME = {
110
+ Instrument: Instrument;
111
+ Image: Image | Image[];
112
+ }
113
+
114
+ export type LoadOmeTiffReturnValue<S extends string[]> = {
115
+ data: TiffPixelSource<S>[];
116
+ metadata: Image;
117
+ };
@@ -0,0 +1,47 @@
1
+ import type { ZarrPixelSource } from '@hms-dbmi/viv';
2
+
3
+ type Channel = {
4
+ channelsVisible: boolean;
5
+ color: string;
6
+ label: string;
7
+ window: {
8
+ min?: number;
9
+ max?: number;
10
+ start: number;
11
+ end: number;
12
+ };
13
+ }
14
+
15
+ type Omero = {
16
+ channels: Channel[];
17
+ rdefs: {
18
+ defaultT?: number;
19
+ defaultZ?: number;
20
+ model: string;
21
+ };
22
+ name?: string;
23
+ }
24
+
25
+ // See https://ngff.openmicroscopy.org/latest/#axes-md
26
+ type Axis = {
27
+ name: string;
28
+ type?: string;
29
+ unit?: string;
30
+ }
31
+
32
+ type Multiscale = {
33
+ datasets: { path: string }[];
34
+ axes?: string[] | Axis[];
35
+ version?: string;
36
+ coordinateTransformations?: object[]; // TODO: stricter type
37
+ }
38
+
39
+ type RootAttrs = {
40
+ omero: Omero;
41
+ multiscales: Multiscale[];
42
+ }
43
+
44
+ export type LoadOmeZarrReturnValue<S extends string[]> = {
45
+ data: ZarrPixelSource<S>[];
46
+ metadata: RootAttrs;
47
+ };