@fideus-labs/ngff-zarr 0.0.1 → 0.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.
- package/README.md +10 -2
- package/esm/io/from_ngff_zarr.d.ts +4 -1
- package/esm/io/from_ngff_zarr.d.ts.map +1 -1
- package/esm/io/from_ngff_zarr.js +94 -27
- package/esm/io/hcs.d.ts +18 -0
- package/esm/io/hcs.d.ts.map +1 -0
- package/esm/io/hcs.js +51 -0
- package/esm/io/itk_image_to_ngff_image.d.ts +30 -0
- package/esm/io/itk_image_to_ngff_image.d.ts.map +1 -0
- package/esm/io/itk_image_to_ngff_image.js +127 -0
- package/esm/io/ngff_image_to_itk_image.d.ts +30 -0
- package/esm/io/ngff_image_to_itk_image.d.ts.map +1 -0
- package/esm/io/ngff_image_to_itk_image.js +218 -0
- package/esm/io/to_multiscales.d.ts +18 -0
- package/esm/io/to_multiscales.d.ts.map +1 -0
- package/esm/io/to_multiscales.js +62 -0
- package/esm/io/to_ngff_image.d.ts +17 -0
- package/esm/io/to_ngff_image.d.ts.map +1 -0
- package/esm/io/to_ngff_image.js +136 -0
- package/esm/io/to_ngff_zarr.d.ts +3 -2
- package/esm/io/to_ngff_zarr.d.ts.map +1 -1
- package/esm/io/to_ngff_zarr.js +289 -26
- package/esm/methods/itkwasm.d.ts +6 -0
- package/esm/methods/itkwasm.d.ts.map +1 -0
- package/esm/methods/itkwasm.js +958 -0
- package/esm/mod.d.ts +9 -2
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +10 -2
- package/esm/schemas/coordinate_systems.d.ts +251 -0
- package/esm/schemas/coordinate_systems.d.ts.map +1 -0
- package/esm/schemas/coordinate_systems.js +139 -0
- package/esm/schemas/index.d.ts +9 -0
- package/esm/schemas/index.d.ts.map +1 -0
- package/esm/schemas/index.js +38 -0
- package/esm/schemas/methods.d.ts.map +1 -1
- package/esm/schemas/methods.js +2 -0
- package/esm/schemas/multiscales.d.ts.map +1 -1
- package/esm/schemas/multiscales.js +2 -0
- package/esm/schemas/ngff_image.d.ts +9 -2
- package/esm/schemas/ngff_image.d.ts.map +1 -1
- package/esm/schemas/ngff_image.js +11 -2
- package/esm/schemas/ome_zarr.d.ts +617 -0
- package/esm/schemas/ome_zarr.d.ts.map +1 -0
- package/esm/schemas/ome_zarr.js +208 -0
- package/esm/schemas/rfc4.d.ts +334 -0
- package/esm/schemas/rfc4.d.ts.map +1 -0
- package/esm/schemas/rfc4.js +129 -0
- package/esm/schemas/units.d.ts +70 -5
- package/esm/schemas/units.d.ts.map +1 -1
- package/esm/schemas/units.js +4 -12
- package/esm/schemas/zarr_metadata.d.ts +10 -4
- package/esm/schemas/zarr_metadata.d.ts.map +1 -1
- package/esm/schemas/zarr_metadata.js +22 -1
- package/esm/types/array_interface.d.ts +7 -0
- package/esm/types/array_interface.d.ts.map +1 -0
- package/esm/types/array_interface.js +1 -0
- package/esm/types/hcs.d.ts +70 -0
- package/esm/types/hcs.d.ts.map +1 -0
- package/esm/types/hcs.js +204 -0
- package/esm/types/methods.d.ts.map +1 -1
- package/esm/types/methods.js +2 -0
- package/esm/types/multiscales.d.ts.map +1 -1
- package/esm/types/ngff_image.d.ts +6 -3
- package/esm/types/ngff_image.d.ts.map +1 -1
- package/esm/types/ngff_image.js +13 -1
- package/esm/types/rfc4.d.ts +94 -0
- package/esm/types/rfc4.d.ts.map +1 -0
- package/esm/types/rfc4.js +135 -0
- package/esm/types/units.d.ts +1 -1
- package/esm/types/units.d.ts.map +1 -1
- package/esm/types/zarr_metadata.d.ts +14 -5
- package/esm/types/zarr_metadata.d.ts.map +1 -1
- package/esm/utils/create_queue.d.ts +6 -0
- package/esm/utils/create_queue.d.ts.map +1 -0
- package/esm/utils/create_queue.js +11 -0
- package/esm/utils/factory.d.ts +1 -1
- package/esm/utils/factory.d.ts.map +1 -1
- package/esm/utils/factory.js +16 -7
- package/esm/utils/method_metadata.d.ts +10 -0
- package/esm/utils/method_metadata.d.ts.map +1 -0
- package/esm/utils/method_metadata.js +37 -0
- package/esm/utils/validation.d.ts.map +1 -1
- package/package.json +7 -1
- package/script/io/from_ngff_zarr.d.ts +4 -1
- package/script/io/from_ngff_zarr.d.ts.map +1 -1
- package/script/io/from_ngff_zarr.js +94 -27
- package/script/io/hcs.d.ts +18 -0
- package/script/io/hcs.d.ts.map +1 -0
- package/script/io/hcs.js +55 -0
- package/script/io/itk_image_to_ngff_image.d.ts +30 -0
- package/script/io/itk_image_to_ngff_image.d.ts.map +1 -0
- package/script/io/itk_image_to_ngff_image.js +153 -0
- package/script/io/ngff_image_to_itk_image.d.ts +30 -0
- package/script/io/ngff_image_to_itk_image.d.ts.map +1 -0
- package/script/io/ngff_image_to_itk_image.js +244 -0
- package/script/io/to_multiscales.d.ts +18 -0
- package/script/io/to_multiscales.d.ts.map +1 -0
- package/script/io/to_multiscales.js +67 -0
- package/script/io/to_ngff_image.d.ts +17 -0
- package/script/io/to_ngff_image.d.ts.map +1 -0
- package/script/io/to_ngff_image.js +162 -0
- package/script/io/to_ngff_zarr.d.ts +3 -2
- package/script/io/to_ngff_zarr.d.ts.map +1 -1
- package/script/io/to_ngff_zarr.js +289 -26
- package/script/methods/itkwasm.d.ts +6 -0
- package/script/methods/itkwasm.d.ts.map +1 -0
- package/script/methods/itkwasm.js +984 -0
- package/script/mod.d.ts +9 -2
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +12 -3
- package/script/schemas/coordinate_systems.d.ts +251 -0
- package/script/schemas/coordinate_systems.d.ts.map +1 -0
- package/script/schemas/coordinate_systems.js +142 -0
- package/script/schemas/index.d.ts +9 -0
- package/script/schemas/index.d.ts.map +1 -0
- package/script/schemas/index.js +101 -0
- package/script/schemas/methods.d.ts.map +1 -1
- package/script/schemas/methods.js +2 -0
- package/script/schemas/multiscales.d.ts.map +1 -1
- package/script/schemas/multiscales.js +2 -0
- package/script/schemas/ngff_image.d.ts +9 -2
- package/script/schemas/ngff_image.d.ts.map +1 -1
- package/script/schemas/ngff_image.js +11 -2
- package/script/schemas/ome_zarr.d.ts +617 -0
- package/script/schemas/ome_zarr.d.ts.map +1 -0
- package/script/schemas/ome_zarr.js +211 -0
- package/script/schemas/rfc4.d.ts +334 -0
- package/script/schemas/rfc4.d.ts.map +1 -0
- package/script/schemas/rfc4.js +132 -0
- package/script/schemas/units.d.ts +70 -5
- package/script/schemas/units.d.ts.map +1 -1
- package/script/schemas/units.js +4 -12
- package/script/schemas/zarr_metadata.d.ts +10 -4
- package/script/schemas/zarr_metadata.d.ts.map +1 -1
- package/script/schemas/zarr_metadata.js +23 -2
- package/script/types/array_interface.d.ts +7 -0
- package/script/types/array_interface.d.ts.map +1 -0
- package/script/types/array_interface.js +2 -0
- package/script/types/hcs.d.ts +70 -0
- package/script/types/hcs.d.ts.map +1 -0
- package/script/types/hcs.js +233 -0
- package/script/types/methods.d.ts.map +1 -1
- package/script/types/methods.js +2 -0
- package/script/types/multiscales.d.ts.map +1 -1
- package/script/types/ngff_image.d.ts +6 -3
- package/script/types/ngff_image.d.ts.map +1 -1
- package/script/types/ngff_image.js +13 -1
- package/script/types/rfc4.d.ts +94 -0
- package/script/types/rfc4.d.ts.map +1 -0
- package/script/types/rfc4.js +143 -0
- package/script/types/units.d.ts +1 -1
- package/script/types/units.d.ts.map +1 -1
- package/script/types/zarr_metadata.d.ts +14 -5
- package/script/types/zarr_metadata.d.ts.map +1 -1
- package/script/utils/create_queue.d.ts +6 -0
- package/script/utils/create_queue.d.ts.map +1 -0
- package/script/utils/create_queue.js +17 -0
- package/script/utils/factory.d.ts +1 -1
- package/script/utils/factory.d.ts.map +1 -1
- package/script/utils/factory.js +39 -7
- package/script/utils/method_metadata.d.ts +10 -0
- package/script/utils/method_metadata.d.ts.map +1 -0
- package/script/utils/method_metadata.js +40 -0
- package/script/utils/validation.d.ts.map +1 -1
- package/esm/schemas/lazy_array.d.ts +0 -8
- package/esm/schemas/lazy_array.d.ts.map +0 -1
- package/esm/schemas/lazy_array.js +0 -7
- package/esm/types/lazy_array.d.ts +0 -18
- package/esm/types/lazy_array.d.ts.map +0 -1
- package/esm/types/lazy_array.js +0 -27
- package/script/schemas/lazy_array.d.ts +0 -8
- package/script/schemas/lazy_array.d.ts.map +0 -1
- package/script/schemas/lazy_array.js +0 -10
- package/script/types/lazy_array.d.ts +0 -18
- package/script/types/lazy_array.d.ts.map +0 -1
- package/script/types/lazy_array.js +0 -31
package/esm/io/to_ngff_zarr.js
CHANGED
|
@@ -1,57 +1,320 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) Fideus Labs LLC
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import * as dntShim from "../_dnt.shims.js";
|
|
1
4
|
import * as zarr from "zarrita";
|
|
2
|
-
|
|
5
|
+
import { createQueue } from "../utils/create_queue.js";
|
|
6
|
+
export async function toNgffZarr(store, multiscales, options = {}) {
|
|
3
7
|
const _overwrite = options.overwrite ?? true;
|
|
4
8
|
const _version = options.version ?? "0.4";
|
|
5
9
|
try {
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
// Determine the appropriate store type based on the path
|
|
11
|
+
let _resolvedStore; // Use unknown for FileSystemStore since we import it dynamically
|
|
12
|
+
if (store instanceof Map) {
|
|
13
|
+
_resolvedStore = store;
|
|
14
|
+
}
|
|
15
|
+
else if (store instanceof zarr.FetchStore) {
|
|
16
|
+
throw new Error("FetchStore is read-only and cannot be used for writing. Use a local file path or MemoryStore instead.");
|
|
17
|
+
}
|
|
18
|
+
else if (store.startsWith("http://") || store.startsWith("https://")) {
|
|
19
|
+
throw new Error("HTTP/HTTPS URLs are read-only and cannot be used for writing. Use a local file path instead.");
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// For local paths, check if we're in a browser environment
|
|
23
|
+
if (typeof dntShim.dntGlobalThis !== "undefined") {
|
|
24
|
+
throw new Error("Local file paths are not supported in browser environments. Use a MemoryStore instead.");
|
|
25
|
+
}
|
|
26
|
+
// Use dynamic import for FileSystemStore in Node.js/Deno environments
|
|
27
|
+
try {
|
|
28
|
+
const { FileSystemStore } = await import("@zarrita/storage");
|
|
29
|
+
// Normalize the path for cross-platform compatibility
|
|
30
|
+
const normalizedPath = store.replace(/^\/([A-Za-z]:)/, "$1");
|
|
31
|
+
_resolvedStore = new FileSystemStore(normalizedPath);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw new Error(`Failed to load FileSystemStore: ${error}. Use MemoryStore for browser compatibility.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Create root location and group with zarrita v0.5.2 API
|
|
38
|
+
const root = zarr.root(_resolvedStore);
|
|
39
|
+
// Create the root group with OME-Zarr metadata
|
|
40
|
+
const rootGroup = await zarr.create(root, {
|
|
41
|
+
attributes: {
|
|
42
|
+
multiscales: [
|
|
43
|
+
{
|
|
44
|
+
version: _version,
|
|
45
|
+
name: multiscales.metadata.name,
|
|
46
|
+
axes: multiscales.metadata.axes,
|
|
47
|
+
datasets: multiscales.metadata.datasets,
|
|
48
|
+
...(multiscales.metadata.coordinateTransformations && {
|
|
49
|
+
coordinateTransformations: multiscales.metadata.coordinateTransformations,
|
|
50
|
+
}),
|
|
51
|
+
...(multiscales.metadata.type && {
|
|
52
|
+
type: multiscales.metadata.type,
|
|
53
|
+
}),
|
|
54
|
+
...(multiscales.metadata.metadata && {
|
|
55
|
+
metadata: multiscales.metadata.metadata,
|
|
56
|
+
}),
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
...(multiscales.metadata.omero && {
|
|
60
|
+
omero: multiscales.metadata.omero,
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
// Write each image in the multiscales
|
|
65
|
+
for (let i = 0; i < multiscales.images.length; i++) {
|
|
66
|
+
const image = multiscales.images[i];
|
|
67
|
+
const dataset = multiscales.metadata.datasets[i];
|
|
68
|
+
if (!dataset) {
|
|
69
|
+
throw new Error(`No dataset configuration found for image ${i}`);
|
|
70
|
+
}
|
|
71
|
+
await _writeImage(rootGroup, image, dataset.path);
|
|
72
|
+
}
|
|
14
73
|
}
|
|
15
74
|
catch (error) {
|
|
16
75
|
throw new Error(`Failed to write OME-Zarr: ${error instanceof Error ? error.message : String(error)}`);
|
|
17
76
|
}
|
|
18
77
|
}
|
|
78
|
+
function _convertDtypeToZarrType(dtype) {
|
|
79
|
+
// Map common numpy/LazyArray dtypes to zarrita data types
|
|
80
|
+
const dtypeMap = {
|
|
81
|
+
int8: "int8",
|
|
82
|
+
int16: "int16",
|
|
83
|
+
int32: "int32",
|
|
84
|
+
int64: "int64",
|
|
85
|
+
uint8: "uint8",
|
|
86
|
+
uint16: "uint16",
|
|
87
|
+
uint32: "uint32",
|
|
88
|
+
uint64: "uint64",
|
|
89
|
+
float32: "float32",
|
|
90
|
+
float64: "float64",
|
|
91
|
+
bool: "bool",
|
|
92
|
+
// Handle some alternative formats
|
|
93
|
+
i1: "int8",
|
|
94
|
+
i2: "int16",
|
|
95
|
+
i4: "int32",
|
|
96
|
+
i8: "int64",
|
|
97
|
+
u1: "uint8",
|
|
98
|
+
u2: "uint16",
|
|
99
|
+
u4: "uint32",
|
|
100
|
+
u8: "uint64",
|
|
101
|
+
f4: "float32",
|
|
102
|
+
f8: "float64",
|
|
103
|
+
};
|
|
104
|
+
if (dtype in dtypeMap) {
|
|
105
|
+
return dtypeMap[dtype];
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
throw new Error(`Unsupported data type: ${dtype}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function getTypedArrayConstructor(dtype) {
|
|
112
|
+
// Map zarrita data types to TypedArray constructors
|
|
113
|
+
const constructorMap = {
|
|
114
|
+
int8: Int8Array,
|
|
115
|
+
int16: Int16Array,
|
|
116
|
+
int32: Int32Array,
|
|
117
|
+
int64: BigInt64Array,
|
|
118
|
+
uint8: Uint8Array,
|
|
119
|
+
uint16: Uint16Array,
|
|
120
|
+
uint32: Uint32Array,
|
|
121
|
+
uint64: BigUint64Array,
|
|
122
|
+
float32: Float32Array,
|
|
123
|
+
float64: Float64Array,
|
|
124
|
+
bool: Uint8Array, // Use Uint8Array for boolean, where 0 represents false and 1 represents true
|
|
125
|
+
// Note: float16 and "v2:object" not supported by standard TypedArrays
|
|
126
|
+
};
|
|
127
|
+
const constructor = constructorMap[dtype];
|
|
128
|
+
if (constructor) {
|
|
129
|
+
return constructor;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
throw new Error(`Unsupported data type for typed array: ${dtype}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
19
135
|
async function _writeImage(group, image, arrayPath) {
|
|
20
136
|
try {
|
|
21
137
|
const chunks = getChunksFromImage(image);
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
const
|
|
28
|
-
// @ts-ignore - zarrita API types are complex, this is a temporary workaround
|
|
29
|
-
const _zarrArray = await zarr.create(resolvedPath, {
|
|
138
|
+
// Convert LazyArray dtype to zarrita DataType
|
|
139
|
+
const zarrDataType = _convertDtypeToZarrType(image.data.dtype);
|
|
140
|
+
// Create array location
|
|
141
|
+
const arrayLocation = group.resolve(arrayPath);
|
|
142
|
+
// Create the zarr array with proper configuration
|
|
143
|
+
const zarrArray = await zarr.create(arrayLocation, {
|
|
30
144
|
shape: image.data.shape,
|
|
31
|
-
data_type:
|
|
145
|
+
data_type: zarrDataType,
|
|
32
146
|
chunk_shape: chunks,
|
|
33
147
|
fill_value: 0,
|
|
34
148
|
});
|
|
35
|
-
|
|
36
|
-
// or updated using the store's set method for metadata files
|
|
149
|
+
await _writeArrayData(zarrArray, image);
|
|
37
150
|
}
|
|
38
151
|
catch (error) {
|
|
39
152
|
throw new Error(`Failed to write image array: ${error instanceof Error ? error.message : String(error)}`);
|
|
40
153
|
}
|
|
41
154
|
}
|
|
42
155
|
function getChunksFromImage(image) {
|
|
43
|
-
|
|
44
|
-
|
|
156
|
+
// zarr.Array.chunks is a number[] representing chunk shape
|
|
157
|
+
if (image.data.chunks && image.data.chunks.length > 0) {
|
|
158
|
+
return image.data.chunks;
|
|
45
159
|
}
|
|
46
160
|
return image.data.shape.map((dim) => Math.min(dim, 1024));
|
|
47
161
|
}
|
|
48
|
-
function _writeArrayData(
|
|
162
|
+
async function _writeArrayData(zarrArray, image) {
|
|
49
163
|
try {
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
164
|
+
// Get array shape for chunk calculation - we don't need the full data here
|
|
165
|
+
const shape = image.data.shape;
|
|
166
|
+
// Calculate chunk indices for parallel writing
|
|
167
|
+
const chunkIndices = calculateChunkIndices(shape, zarrArray.chunks);
|
|
168
|
+
// Create a queue for parallel chunk writing
|
|
169
|
+
const writeQueue = createQueue();
|
|
170
|
+
// Queue all chunks for writing
|
|
171
|
+
for (const chunkIndex of chunkIndices) {
|
|
172
|
+
writeQueue.add(async () => {
|
|
173
|
+
await writeChunkWithGet(zarrArray, image, chunkIndex);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// Wait for all chunks to be written
|
|
177
|
+
await writeQueue.onIdle();
|
|
53
178
|
}
|
|
54
179
|
catch (error) {
|
|
55
180
|
throw new Error(`Failed to write array data: ${error instanceof Error ? error.message : String(error)}`);
|
|
56
181
|
}
|
|
57
182
|
}
|
|
183
|
+
async function writeChunkWithGet(zarrArray, image, chunkIndex) {
|
|
184
|
+
// Calculate the chunk bounds
|
|
185
|
+
const shape = image.data.shape;
|
|
186
|
+
const chunkStart = chunkIndex.map((idx, dim) => idx * zarrArray.chunks[dim]);
|
|
187
|
+
const chunkEnd = chunkStart.map((start, dim) => Math.min(start + zarrArray.chunks[dim], shape[dim]));
|
|
188
|
+
// Calculate chunk shape
|
|
189
|
+
const chunkShape = chunkEnd.map((end, dim) => end - chunkStart[dim]);
|
|
190
|
+
// Create selection for this chunk from the source data
|
|
191
|
+
const sourceSelection = chunkStart.map((start, dim) => zarr.slice(start, chunkEnd[dim]));
|
|
192
|
+
// Get only the chunk data we need from the source
|
|
193
|
+
const { data: chunkSourceData } = await zarr.get(image.data, sourceSelection);
|
|
194
|
+
// Convert chunk data to target type
|
|
195
|
+
const targetTypedArrayConstructor = getTypedArrayConstructor(zarrArray.dtype);
|
|
196
|
+
const chunkTargetData = convertChunkToTargetType(chunkSourceData, zarrArray.dtype, targetTypedArrayConstructor);
|
|
197
|
+
// Validate chunk data size
|
|
198
|
+
const expectedSize = chunkShape.reduce((a, b) => a * b, 1);
|
|
199
|
+
const actualSize = chunkTargetData.byteLength /
|
|
200
|
+
(chunkTargetData
|
|
201
|
+
.BYTES_PER_ELEMENT || 1);
|
|
202
|
+
if (actualSize !== expectedSize) {
|
|
203
|
+
console.error(`[writeChunkWithGet] Chunk data size mismatch!`);
|
|
204
|
+
console.error(` Image shape:`, shape);
|
|
205
|
+
console.error(` Chunk index:`, chunkIndex);
|
|
206
|
+
console.error(` Chunk start:`, chunkStart);
|
|
207
|
+
console.error(` Chunk end:`, chunkEnd);
|
|
208
|
+
console.error(` Chunk shape:`, chunkShape);
|
|
209
|
+
console.error(` Expected size:`, expectedSize);
|
|
210
|
+
console.error(` Actual size:`, actualSize);
|
|
211
|
+
throw new Error(`Chunk data size mismatch: expected ${expectedSize} elements, got ${actualSize}`);
|
|
212
|
+
}
|
|
213
|
+
// Create the selection for writing to the target zarr array
|
|
214
|
+
const targetSelection = chunkStart.map((start, dim) => zarr.slice(start, chunkEnd[dim]));
|
|
215
|
+
// Write the chunk using zarrita's set function
|
|
216
|
+
await zarr.set(zarrArray, targetSelection, {
|
|
217
|
+
data: chunkTargetData,
|
|
218
|
+
shape: chunkShape,
|
|
219
|
+
stride: calculateChunkStride(chunkShape),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function convertChunkToTargetType(chunkData, targetDtype, targetTypedArrayConstructor) {
|
|
223
|
+
// Handle different source data types
|
|
224
|
+
if (chunkData instanceof BigInt64Array ||
|
|
225
|
+
chunkData instanceof BigUint64Array) {
|
|
226
|
+
// Handle BigInt arrays separately
|
|
227
|
+
if (chunkData.constructor === targetTypedArrayConstructor) {
|
|
228
|
+
return chunkData;
|
|
229
|
+
}
|
|
230
|
+
else if (targetDtype === "int64" || targetDtype === "uint64") {
|
|
231
|
+
// BigInt to BigInt conversion
|
|
232
|
+
const bigIntArray = new targetTypedArrayConstructor(chunkData.length);
|
|
233
|
+
for (let i = 0; i < chunkData.length; i++) {
|
|
234
|
+
bigIntArray[i] = chunkData[i];
|
|
235
|
+
}
|
|
236
|
+
return bigIntArray;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
// BigInt to regular number conversion
|
|
240
|
+
const numberArray = new targetTypedArrayConstructor(chunkData.length);
|
|
241
|
+
for (let i = 0; i < chunkData.length; i++) {
|
|
242
|
+
numberArray[i] = Number(chunkData[i]);
|
|
243
|
+
}
|
|
244
|
+
return numberArray;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if (chunkData instanceof Uint8Array ||
|
|
248
|
+
chunkData instanceof Int8Array ||
|
|
249
|
+
chunkData instanceof Uint16Array ||
|
|
250
|
+
chunkData instanceof Int16Array ||
|
|
251
|
+
chunkData instanceof Uint32Array ||
|
|
252
|
+
chunkData instanceof Int32Array ||
|
|
253
|
+
chunkData instanceof Float32Array ||
|
|
254
|
+
chunkData instanceof Float64Array) {
|
|
255
|
+
// Handle regular typed arrays
|
|
256
|
+
if (chunkData.constructor === targetTypedArrayConstructor) {
|
|
257
|
+
return chunkData;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
// Convert between typed arrays
|
|
261
|
+
if (targetDtype === "int64" || targetDtype === "uint64") {
|
|
262
|
+
// Regular number to BigInt conversion
|
|
263
|
+
const bigIntArray = new targetTypedArrayConstructor(chunkData.length);
|
|
264
|
+
for (let i = 0; i < chunkData.length; i++) {
|
|
265
|
+
bigIntArray[i] = BigInt(chunkData[i]);
|
|
266
|
+
}
|
|
267
|
+
return bigIntArray;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
// Standard numeric conversion - use typed conversion
|
|
271
|
+
const typedArrayMap = new Map([
|
|
272
|
+
[Uint8Array, (data) => new Uint8Array(Array.from(data))],
|
|
273
|
+
[Int8Array, (data) => new Int8Array(Array.from(data))],
|
|
274
|
+
[Uint16Array, (data) => new Uint16Array(Array.from(data))],
|
|
275
|
+
[Int16Array, (data) => new Int16Array(Array.from(data))],
|
|
276
|
+
[Uint32Array, (data) => new Uint32Array(Array.from(data))],
|
|
277
|
+
[Int32Array, (data) => new Int32Array(Array.from(data))],
|
|
278
|
+
[Float32Array, (data) => new Float32Array(Array.from(data))],
|
|
279
|
+
[Float64Array, (data) => new Float64Array(Array.from(data))],
|
|
280
|
+
]);
|
|
281
|
+
const createTypedArray = typedArrayMap.get(targetTypedArrayConstructor);
|
|
282
|
+
if (createTypedArray) {
|
|
283
|
+
return createTypedArray(chunkData);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
throw new Error(`Unsupported target constructor: ${targetTypedArrayConstructor.name}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
// Handle other types (fallback)
|
|
293
|
+
throw new Error(`Unsupported source data type: ${chunkData.constructor.name}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function calculateChunkIndices(shape, chunks) {
|
|
297
|
+
const indices = [];
|
|
298
|
+
function generateIndices(dimIndex, currentIndex) {
|
|
299
|
+
if (dimIndex === shape.length) {
|
|
300
|
+
indices.push([...currentIndex]);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const chunkSize = chunks[dimIndex];
|
|
304
|
+
const dimSize = shape[dimIndex];
|
|
305
|
+
for (let i = 0; i < Math.ceil(dimSize / chunkSize); i++) {
|
|
306
|
+
currentIndex[dimIndex] = i;
|
|
307
|
+
generateIndices(dimIndex + 1, currentIndex);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
generateIndices(0, new Array(shape.length));
|
|
311
|
+
return indices;
|
|
312
|
+
}
|
|
313
|
+
function calculateChunkStride(chunkShape) {
|
|
314
|
+
const stride = new Array(chunkShape.length);
|
|
315
|
+
stride[chunkShape.length - 1] = 1;
|
|
316
|
+
for (let i = chunkShape.length - 2; i >= 0; i--) {
|
|
317
|
+
stride[i] = stride[i + 1] * chunkShape[i + 1];
|
|
318
|
+
}
|
|
319
|
+
return stride;
|
|
320
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NgffImage } from "../types/ngff_image.js";
|
|
2
|
+
/**
|
|
3
|
+
* Main downsampling function for ITK-Wasm
|
|
4
|
+
*/
|
|
5
|
+
export declare function downsampleItkWasm(ngffImage: NgffImage, scaleFactors: (Record<string, number> | number)[], smoothing: "gaussian" | "bin_shrink" | "label_image"): Promise<NgffImage[]>;
|
|
6
|
+
//# sourceMappingURL=itkwasm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"itkwasm.d.ts","sourceRoot":"","sources":["../../src/methods/itkwasm.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAknCnD;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,SAAS,EAAE,SAAS,EACpB,YAAY,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,EAAE,EACjD,SAAS,EAAE,UAAU,GAAG,YAAY,GAAG,aAAa,GACnD,OAAO,CAAC,SAAS,EAAE,CAAC,CA8HtB"}
|