Skip to content

Commit 29f295e

Browse files
committed
Working OME-zarr block source
1 parent 3c5c671 commit 29f295e

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

django/applications/catmaid/static/js/tile-source.js

+42-37
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,8 @@
11171117
max.forEach((n, i) => maxNum[i] = Number(n));
11181118

11191119
let min = new Array(maxNum.length).fill(0);
1120-
return new CATMAID.BlockCoordBounds(min, maxNum);
1120+
let x = new CATMAID.BlockCoordBounds(min, maxNum)
1121+
return x;
11211122
}
11221123

11231124
blockSize(zoomLevel) {
@@ -1127,7 +1128,8 @@
11271128
1
11281129
];
11291130
let bs = this.datasetAttributes.get_block_size(zoomLevel);
1130-
return CATMAID.tools.permute(bs, this.sliceDims);
1131+
let x = CATMAID.tools.permute(bs, this.sliceDims);
1132+
return x;
11311133
}
11321134

11331135
voxelOffset(zoomLevel) {
@@ -1364,10 +1366,11 @@
13641366
if (block) {
13651367
let n = 1;
13661368
let stride = block.size.map(s => { let rn = n; n *= s; return rn; });
1369+
let dd = new nj.NdArray(nj.ndarray(block.data, block.size, stride))
1370+
.transpose(...this.sliceDims);
13671371
return {
13681372
etag: block.etag,
1369-
block: new nj.NdArray(nj.ndarray(block.data, block.size, stride))
1370-
.transpose(...this.sliceDims),
1373+
block: dd,
13711374
gridPosition: block.gridPosition,
13721375
};
13731376
} else {
@@ -1384,7 +1387,7 @@
13841387
/**
13851388
* Image block source type for OME Zarr datasets.
13861389
* See https://ngff.openmicroscopy.org/latest/
1387-
* See https://guido.io/zarr.js/
1390+
* See https://zarrita.dev/packages/zarrita.html
13881391
*
13891392
* Source type: 15
13901393
*/
@@ -1404,18 +1407,11 @@
14041407
}
14051408

14061409
if (!supportsDynamicImport() || typeof BigInt === 'undefined') {
1407-
// TODO: should fail gracefully here instead.
14081410
throw new CATMAID.Error(
14091411
'Your browser does not support features required for OME-Zarr mirrors');
14101412
}
14111413

14121414
this.datasetURL = this.baseURL;
1413-
// XXX: get dims from data, collecting x,y,z in axes field
1414-
this.sliceDims = [1, 2, 3];
1415-
this.reciprocalSliceDims = Array.from(Array(this.sliceDims.length).keys())
1416-
.sort((a, b) => this.sliceDims[a] - this.sliceDims[b]);
1417-
console.log('this.sliceDims',this.sliceDims)
1418-
console.log('this.reciprocalSliceDims',this.reciprocalSliceDims)
14191415

14201416
this.promiseReady = OMEZarrBlockSource.loadZarrita()
14211417
.then(() => this.loadRoot())
@@ -1466,6 +1462,18 @@
14661462
'Only OME-Zarr version 0.5 is supported');
14671463
}
14681464

1465+
// extract slice dims based on spatial axes names
1466+
const targetNames = ["x", "y", "z"];
1467+
this.sliceDims = this.rootData.attributes.ome.multiscales[0].axes.reduce((acc, item, index) => {
1468+
if (targetNames.includes(item.name)) {
1469+
acc.push(index);
1470+
}
1471+
return acc;
1472+
}, []);
1473+
1474+
this.reciprocalSliceDims = Array.from(Array(this.sliceDims.length).keys())
1475+
.sort((a, b) => this.sliceDims[a] - this.sliceDims[b]);
1476+
14691477
let datasets = this.rootData.attributes.ome.multiscales[0].datasets;
14701478
this.datasetAttributes = [];
14711479
this.stores = {};
@@ -1489,7 +1497,8 @@
14891497
allPathsPromise.push(request(index, url))
14901498
}
14911499

1492-
// get the results for each scale level and populate datasetAttributes and create stores
1500+
// get the resulting array specs for each scale level
1501+
// and populate datasetAttributes and create stores
14931502
return Promise.all(allPathsPromise).then(result => {
14941503
for (let index = 0; index < result.length; index++) {
14951504
let storePath = `${this.datasetURL}/${datasets[index].path}`
@@ -1507,17 +1516,22 @@
15071516
this.tileHeight,
15081517
1
15091518
];
1510-
// XXX: is chunk_shape correct as block_size
1511-
let bs = this.datasetAttributes[zoomLevel].ds.chunk_grid.configuration.chunk_shape;
1512-
// console.log('blockSize', zoomLevel, bs, 'permute', CATMAID.tools.permute(bs, this.sliceDims));
1519+
let bs = this.datasetAttributes[zoomLevel].ds.codecs[0].configuration.chunk_shape;
15131520
return CATMAID.tools.permute(bs, this.sliceDims);
15141521
}
15151522

15161523
blockCoordBounds(zoomLevel) {
15171524
if (!this.ready) return;
1518-
// XXX What should it return?
1519-
console.log('blockCoordBounds', zoomLevel)
1520-
return new CATMAID.BlockCoordBounds([0,0,0], [100,100,100]);
1525+
// volume shape divided by chunk_shape at zoom level
1526+
let dimension = this.datasetAttributes[zoomLevel].ds.shape;
1527+
let block_size = this.datasetAttributes[zoomLevel].ds.codecs[0].configuration.chunk_shape;
1528+
let max = dimension.map((d, i) => {
1529+
return d / block_size[i];
1530+
})
1531+
let maxNum = new Array(max.length);
1532+
max.forEach((n, i) => maxNum[i] = Number(n));
1533+
let min = [0,0,0]; // new Array(maxNum.length).fill(0);
1534+
return new CATMAID.BlockCoordBounds(min, maxNum);
15211535
}
15221536

15231537
dataType () {
@@ -1528,31 +1542,22 @@
15281542
}
15291543
}
15301544

1531-
15321545
readBlock(zoomLevel, ...sourceCoord) {
15331546
return this.promiseReady.then(() => {
1534-
let path = this.datasetPath(zoomLevel);
1535-
let dataAttrs = this.datasetAttributes[zoomLevel];
1536-
let blockCoord = CATMAID.tools.permute(sourceCoord, this.reciprocalSliceDims);
1537-
// console.log('readBlock', zoomLevel, sourceCoord, path, dataAttrs, 'blockCoord', blockCoord);
1538-
1539-
let arr = new nj.NdArray(nj.uint8(nj.random([32,32,32]).multiply(255).tolist()) );
1540-
// XXX: why is it not showing random data in the stack viewer?
1541-
console.log('arr', arr)
1542-
1543-
// XXX: load array data
1544-
const arr2 = Window.Zarrita.open.v3(this.stores[zoomLevel], { kind: "array" }).then(arr => {
1545-
console.log('opened arr', arr2);
1546-
//const view = Window.Zarrita.get(arr, [null, null, 0]).then(view => {console.log('view', arr); return view});
1547-
return arr
1547+
let blockCoord = CATMAID.tools.permute(sourceCoord, this.reciprocalSliceDims);
1548+
return Window.Zarrita.open.v3(this.stores[zoomLevel], { kind: "array" }).then(arr => {
1549+
const viewChunk2 = arr.getChunk(blockCoord).then(view => {
1550+
let d1 = new nj.NdArray(nj.ndarray(view.data, view.shape, view.stride))
1551+
let view2 = d1.transpose(...this.sliceDims);
1552+
return view2;
1553+
});
1554+
return {block: viewChunk2, etag: undefined};
15481555
});
1549-
1550-
return {block: arr.transpose(...this.sliceDims), etag: undefined};
15511556
});
15521557
}
15531558

15541559
datasetPath(zoomLevel) {
1555-
return `${this.datasetURL}/${this.datasetAttributes[zoomLevel].root.path.path}`;
1560+
return `${this.datasetURL}/${this.datasetAttributes[zoomLevel].root.path}`;
15561561
}
15571562

15581563
numScaleLevels() {

0 commit comments

Comments
 (0)