diff --git a/python/tests/local_volume_test.py b/python/tests/local_volume_test.py index 20d67b5b7e..1bd533a124 100644 --- a/python/tests/local_volume_test.py +++ b/python/tests/local_volume_test.py @@ -79,3 +79,37 @@ def test_tensorstore_features(): names=["x", "y", "d2"], units=units, scales=scales ) assert local_volume.dimensions.to_json() == dimensions.to_json() + + +def test_volume_info_preserves_offsets_with_padded_dimensions(webdriver): + viewer = webdriver.viewer + padded_dimensions = neuroglancer.CoordinateSpace( + names=["y", "x", "z"], units=["nm"] * 3, scales=[3, 5, 2] + ) + with viewer.txn() as state: + state.layout = "3d" + state.show_slices = False + state.layers["image"] = neuroglancer.ImageLayer( + source=neuroglancer.LayerDataSource( + url=neuroglancer.LocalVolume( + np.zeros((3, 5), dtype=np.uint8), + dimensions=neuroglancer.CoordinateSpace( + names=["y", "x"], units=["nm", "nm"], scales=[3, 5] + ), + voxel_offset=[2, 4], + ), + transform=neuroglancer.CoordinateSpaceTransform( + input_dimensions=padded_dimensions, + output_dimensions=padded_dimensions, + source_rank=2, + matrix=[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 7]], + ), + ), + volume_rendering_mode="off", + ) + webdriver.sync() + info = viewer.volume_info("image").result(timeout=15) + assert info.dimensions.rank == 3 + indices = [list(info.dimensions.names).index(axis) for axis in "xyz"] + np.testing.assert_array_equal(np.array(info.lower_bound)[indices], [4, 2, 7]) + np.testing.assert_array_equal(np.array(info.upper_bound)[indices], [9, 5, 8]) diff --git a/python/tests/zarr_test.py b/python/tests/zarr_test.py index b6e4fcc855..25eb1a6f52 100644 --- a/python/tests/zarr_test.py +++ b/python/tests/zarr_test.py @@ -375,17 +375,15 @@ def test_ome_zarr_0_6_translation(static_file_server, webdriver): webdriver.sync() model_space = _assert_renders(webdriver, "translation") - # Translation: [30, 20, 10] (z, y, x) in micrometers - # So the origin should be at [8, 7, 5] in (z, y, x) in world coordinates. - # However, in neuroglancer at the chunk/model level this is reversed in order (see getSources in zarr/frontend.ts for the permutation) + # The returned volume uses ZYX order, matching the physical coordinate axes. domain = model_space["volume"].domain - expected_origin = (10, 20, 30) + expected_origin = (30, 20, 10) actual_origin = domain.origin assert ( actual_origin == expected_origin ), f"Domain origin mismatch: expected {expected_origin}, got {actual_origin}" - translated_voxel = (TEST_VOXEL[0] + 10, TEST_VOXEL[1] + 20, TEST_VOXEL[2] + 30) + translated_voxel = (TEST_VOXEL[0] + 30, TEST_VOXEL[1] + 20, TEST_VOXEL[2] + 10) _verify_data_at_point(model_space["volume"], translated_voxel, EXPECTED_VALUE) @@ -410,8 +408,8 @@ def test_ome_zarr_0_6_map_axis(static_file_server, webdriver): model_space = _assert_renders(webdriver, "mapAxis") vol = model_space["volume"] - # The real map axis transform permutes axes (0, 1, 2) -> (1, 2, 0). The inverse of this is (2, 0, 1). - permuted_voxel = (TEST_VOXEL[2], TEST_VOXEL[0], TEST_VOXEL[1]) + # mapAxis [1,2,0] maps the input point to (y,x,z). + permuted_voxel = (TEST_VOXEL[1], TEST_VOXEL[2], TEST_VOXEL[0]) _verify_data_at_point(vol, permuted_voxel, EXPECTED_VALUE) @@ -429,10 +427,8 @@ def _check_sequence_result(model_space): # In voxel space with scale factored out: # - The translation/scale = [32/4, 21/3, 10/2] = [8, 7, 5] voxels # So the origin should be at [8, 7, 5] in (z, y, x) in world coordinates. - # However, in neuroglancer at the chunk/model level this is reversed in order (see getSources in zarr/frontend.ts for the permutation) - # so check for [5, 7, 8] as the domain domain = model_space["volume"].domain - expected_origin = (5, 7, 8) + expected_origin = (8, 7, 5) actual_origin = domain.origin assert ( actual_origin == expected_origin @@ -466,6 +462,30 @@ def test_ome_zarr_0_6_sequence(static_file_server, webdriver): _check_sequence_result(model_space) +@pytest.mark.parametrize( + "fixture", ["basic/sequenceScaleTranslation.zarr", "simple/affine.zarr"] +) +def test_volume_info_composes_ome_transforms(static_file_server, webdriver, fixture): + url = static_file_server(OME_ZARR_0_6_ROOT / fixture) + with webdriver.viewer.txn() as state: + state.layout = "3d" + state.show_slices = False + state.layers["image"] = neuroglancer.ImageLayer( + source=f"{url}|zarr3:", volume_rendering_mode="off" + ) + webdriver.sync() + info = webdriver.viewer.volume_info("image").result(timeout=15) + indices = [list(info.dimensions.names).index(axis) for axis in "xyz"] + # Metadata in ZYX: scale [4,3,2] um, translation [32,21,10] um, + # and shape [27,226,186]. Preserve the translation when reordering into XYZ. + np.testing.assert_allclose( + np.array(info.dimensions.scales)[indices], [2e-6, 3e-6, 4e-6], rtol=1e-7 + ) + assert all(info.dimensions.units[i] == "m" for i in indices) + np.testing.assert_array_equal(np.array(info.lower_bound)[indices], [5, 7, 8]) + np.testing.assert_array_equal(np.array(info.upper_bound)[indices], [191, 233, 35]) + + def test_ome_zarr_0_6_affine(static_file_server, webdriver): """affine: Affine matrix (JSON form) applied to single scale. Example dataset: simple/affine.zarr @@ -503,8 +523,8 @@ def test_ome_zarr_0_6_rotation(static_file_server, webdriver): model_space = _assert_renders(webdriver, "rotation") vol = model_space["volume"] - # The rotation transform permutes axes (0, 1, 2) -> (2, 0, 1). The inverse of this is (1, 2, 0). - rotated_voxel = (TEST_VOXEL[1], TEST_VOXEL[2], TEST_VOXEL[0]) + # Apply the rotation matrix to the input point: (z,y,x) -> (x,z,y). + rotated_voxel = (TEST_VOXEL[2], TEST_VOXEL[0], TEST_VOXEL[1]) _verify_data_at_point(vol, rotated_voxel, EXPECTED_VALUE) diff --git a/src/python_integration/volume.ts b/src/python_integration/volume.ts index 39d1d5a93a..6be84df679 100644 --- a/src/python_integration/volume.ts +++ b/src/python_integration/volume.ts @@ -26,6 +26,7 @@ import { ImageUserLayer } from "#src/layer/image/index.js"; import type { UserLayer } from "#src/layer/index.js"; import { SegmentationUserLayer } from "#src/layer/segmentation/index.js"; import type { RenderLayerTransform } from "#src/render_coordinate_transform.js"; +import { getChunkTransformParameters } from "#src/render_coordinate_transform.js"; import type { SliceViewSingleResolutionSource } from "#src/sliceview/frontend.js"; import type { UncompressedVolumeChunk } from "#src/sliceview/uncompressed_chunk_format.js"; import { getFillValueArray } from "#src/sliceview/uncompressed_chunk_format.js"; @@ -246,33 +247,11 @@ function getChunkToRenderLayerTransform( renderLayerScales: Float64Array, requestedScales: Float64Array, ) { - const { rank, unpaddedRank } = transform; - const chunkToLayerTransform = new Float32Array((rank + 1) ** 2); - matrix.multiply( - chunkToLayerTransform, - rank + 1, + const { rank } = transform; + const { chunkToLayerTransform } = getChunkTransformParameters( + transform, source.chunkToMultiscaleTransform, - unpaddedRank + 1, - transform.modelToRenderLayerTransform, - rank + 1, - unpaddedRank + 1, - unpaddedRank + 1, - unpaddedRank + 1, ); - if (unpaddedRank !== rank) { - matrix.copy( - chunkToLayerTransform.subarray( - rank + 1 * (unpaddedRank + 1) + unpaddedRank + 1, - ), - rank + 1, - transform.modelToRenderLayerTransform.subarray( - rank + 1 * (unpaddedRank + 1) + unpaddedRank + 1, - ), - rank + 1, - rank - unpaddedRank, - rank - unpaddedRank, - ); - } for (let row = 0; row < rank; ++row) { const factor = renderLayerScales[row] / requestedScales[row]; for (let col = 0; col <= rank; ++col) {