-
Notifications
You must be signed in to change notification settings - Fork 299
/
ome.ts
310 lines (286 loc) · 8.78 KB
/
ome.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* @license
* Copyright 2022 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { CoordinateSpace } from "#src/coordinate_transform.js";
import { makeCoordinateSpace } from "#src/coordinate_transform.js";
import {
parseArray,
parseFixedLengthArray,
verifyFiniteFloat,
verifyFinitePositiveFloat,
verifyObject,
verifyObjectProperty,
verifyOptionalObjectProperty,
verifyString,
} from "#src/util/json.js";
import * as matrix from "#src/util/matrix.js";
import { allSiPrefixes } from "#src/util/si_units.js";
export interface OmeMultiscaleScale {
url: string;
transform: Float64Array;
}
export interface OmeMultiscaleMetadata {
scales: OmeMultiscaleScale[];
coordinateSpace: CoordinateSpace;
}
const SUPPORTED_OME_MULTISCALE_VERSIONS = new Set(["0.4", "0.5-dev", "0.5"]);
const OME_UNITS = new Map<string, { unit: string; scale: number }>([
["angstrom", { unit: "m", scale: 1e-10 }],
["foot", { unit: "m", scale: 0.3048 }],
["inch", { unit: "m", scale: 0.0254 }],
["mile", { unit: "m", scale: 1609.34 }],
// eslint-disable-next-line no-loss-of-precision
["parsec", { unit: "m", scale: 3.0856775814913673e16 }],
["yard", { unit: "m", scale: 0.9144 }],
["minute", { unit: "s", scale: 60 }],
["hour", { unit: "s", scale: 60 * 60 }],
["day", { unit: "s", scale: 60 * 60 * 24 }],
]);
for (const unit of ["meter", "second"]) {
for (const siPrefix of allSiPrefixes) {
const { longPrefix } = siPrefix;
if (longPrefix === undefined) continue;
OME_UNITS.set(`${longPrefix}${unit}`, {
unit: unit[0],
scale: 10 ** siPrefix.exponent,
});
}
}
interface Axis {
name: string;
unit: string;
scale: number;
type: string | undefined;
}
function parseOmeAxis(axis: unknown): Axis {
verifyObject(axis);
const name = verifyObjectProperty(axis, "name", verifyString);
const type = verifyOptionalObjectProperty(axis, "type", verifyString);
const parsedUnit = verifyOptionalObjectProperty(
axis,
"unit",
(unit) => {
const x = OME_UNITS.get(unit);
if (x === undefined) {
throw new Error(`Unsupported unit: ${JSON.stringify(unit)}`);
}
return x;
},
{ unit: "", scale: 1 },
);
return { name, unit: parsedUnit.unit, scale: parsedUnit.scale, type };
}
function parseOmeAxes(axes: unknown): CoordinateSpace {
const parsedAxes = parseArray(axes, parseOmeAxis);
return makeCoordinateSpace({
names: parsedAxes.map((axis) => {
const { name, type } = axis;
if (type === "channel") {
return `${name}'`;
}
return name;
}),
scales: Float64Array.from(parsedAxes, (axis) => axis.scale),
units: parsedAxes.map((axis) => axis.unit),
});
}
function parseScaleTransform(rank: number, obj: unknown) {
const scales = verifyObjectProperty(obj, "scale", (values) =>
parseFixedLengthArray(
new Float64Array(rank),
values,
verifyFinitePositiveFloat,
),
);
return matrix.createHomogeneousScaleMatrix(Float64Array, scales);
}
function parseIdentityTransform(rank: number, obj: unknown) {
obj;
return matrix.createIdentity(Float64Array, rank + 1);
}
function parseTranslationTransform(rank: number, obj: unknown) {
const translation = verifyObjectProperty(obj, "translation", (values) =>
parseFixedLengthArray(new Float64Array(rank), values, verifyFiniteFloat),
);
return matrix.createHomogeneousTranslationMatrix(Float64Array, translation);
}
const coordinateTransformParsers = new Map([
["scale", parseScaleTransform],
["identity", parseIdentityTransform],
["translation", parseTranslationTransform],
]);
function parseOmeCoordinateTransform(
rank: number,
transformJson: unknown,
): Float64Array<ArrayBuffer> {
verifyObject(transformJson);
const transformType = verifyObjectProperty(
transformJson,
"type",
verifyString,
);
const parser = coordinateTransformParsers.get(transformType);
if (parser === undefined) {
throw new Error(
`Unsupported coordinate transform type: ${JSON.stringify(transformType)}`,
);
}
return parser(rank, transformJson);
}
function parseOmeCoordinateTransforms(
rank: number,
transforms: unknown,
): Float64Array {
let transform = matrix.createIdentity(Float64Array, rank + 1);
if (transforms === undefined) return transform;
parseArray(transforms, (transformJson) => {
const newTransform = parseOmeCoordinateTransform(rank, transformJson);
transform = matrix.multiply(
new Float64Array(transform.length) as Float64Array<ArrayBuffer>,
rank + 1,
newTransform,
rank + 1,
transform,
rank + 1,
rank + 1,
rank + 1,
rank + 1,
);
});
return transform;
}
function parseMultiscaleScale(
rank: number,
url: string,
obj: unknown,
): OmeMultiscaleScale {
const path = verifyObjectProperty(obj, "path", verifyString);
const transform = verifyObjectProperty(
obj,
"coordinateTransformations",
(x) => parseOmeCoordinateTransforms(rank, x),
);
const scaleUrl = `${url}/${path}`;
return { url: scaleUrl, transform };
}
function parseOmeMultiscale(
url: string,
multiscale: unknown,
): OmeMultiscaleMetadata {
const coordinateSpace = verifyObjectProperty(
multiscale,
"axes",
parseOmeAxes,
);
const rank = coordinateSpace.rank;
const transform = verifyObjectProperty(
multiscale,
"coordinateTransformations",
(x) => parseOmeCoordinateTransforms(rank, x),
);
const scales = verifyObjectProperty(multiscale, "datasets", (obj) =>
parseArray(obj, (x) => {
const scale = parseMultiscaleScale(rank, url, x);
scale.transform = matrix.multiply(
new Float64Array((rank + 1) ** 2) as Float64Array<ArrayBuffer>,
rank + 1,
transform,
rank + 1,
scale.transform,
rank + 1,
rank + 1,
rank + 1,
rank + 1,
);
return scale;
}),
);
if (scales.length === 0) {
throw new Error("At least one scale must be specified");
}
const baseTransform = scales[0].transform;
// Extract the scale factor from `baseTransform`.
//
// TODO(jbms): If coordinate transformations other than `scale` and `translation` are supported,
// this will need to be modified.
const baseScales = new Float64Array(rank);
for (let i = 0; i < rank; ++i) {
const scale = (baseScales[i] = baseTransform[i * (rank + 1) + i]);
coordinateSpace.scales[i] *= scale;
}
for (const scale of scales) {
const t = scale.transform;
// In OME's coordinate space, the origin of a voxel is its center, while in Neuroglancer it is
// the "lower" (in coordinates) corner. Translate by the physical size of half a voxel in the
// current scale.
for (let i = 0; i < rank; ++i) {
let offset = 0;
for (let j = 0; j < rank; ++j) {
offset += t[j * (rank + 1) + i] * 0.5;
}
t[rank * (rank + 1) + i] -= offset;
}
// Make the scale relative to the base scale.
for (let i = 0; i < rank; ++i) {
for (let j = 0; j <= rank; ++j) {
t[j * (rank + 1) + i] /= baseScales[i];
}
}
}
return { coordinateSpace, scales };
}
export function parseOmeMetadata(
url: string,
attrs: any,
zarrVersion: number,
): OmeMultiscaleMetadata | undefined {
const ome = attrs.ome;
const multiscales = ome == undefined ? attrs.multiscales : ome.multiscales; // >0.4
if (!Array.isArray(multiscales)) return undefined;
const errors: string[] = [];
for (const multiscale of multiscales) {
if (
typeof multiscale !== "object" ||
multiscale == null ||
Array.isArray(multiscale)
) {
// Not valid OME multiscale spec.
return undefined;
}
const version = ome == undefined ? multiscale.version : ome.version; // >0.4
if (version === undefined) return undefined;
if (!SUPPORTED_OME_MULTISCALE_VERSIONS.has(version)) {
errors.push(
`OME multiscale metadata version ${JSON.stringify(
version,
)} is not supported`,
);
continue;
}
if (version === "0.5" && zarrVersion !== 3) {
errors.push(
`OME multiscale metadata version ${JSON.stringify(
version,
)} is not supported for zarr v${zarrVersion}`,
);
continue;
}
return parseOmeMultiscale(url, multiscale);
}
if (errors.length !== 0) {
throw new Error(errors[0]);
}
return undefined;
}