diff --git a/e2e/core/test_space.py b/e2e/core/test_space.py index e06b91455..206a0b74a 100644 --- a/e2e/core/test_space.py +++ b/e2e/core/test_space.py @@ -1,4 +1,5 @@ import pytest +from typing import Tuple import siibra @@ -14,3 +15,28 @@ def test_space(space: siibra.core.space.Space): @pytest.mark.parametrize("space", should_have_desc) def test_has_desc(space: siibra.core.space.Space): assert space.description + + +voi_params = [ + ("mni152", "nii", (57, 56, 57), {}), + ("mni152", "neuroglancer/precomputed", (27, 27, 27), {}), + ("mni152", "neuroglancer/precomputed", (55, 55, 55), {"resolution_mm": 1.0}), + ("bigbrain", "neuroglancer/precomputed", (40, 43, 40), {}), +] + + +@pytest.mark.parametrize("space_spec, format, shape, kwargs", voi_params) +def test_voi_fetching( + space_spec: str, format: str, shape: Tuple[int, int, int], kwargs +): + space = siibra.spaces.get(space_spec) + voi = siibra.locations.BoundingBox( + [-12.033761221226529, -45.77021322122653, -0.28205922122652827], + [43.50666522122653, 9.770213221226529, 55.25836722122653], + space, + ) + template = space.get_template() + img = template.fetch(format=format, voi=voi, **kwargs) + assert ( + img.shape == shape + ), f"Expected fetched voi to have the shape {shape} but has {img.shape}" diff --git a/siibra/volumes/providers/nifti.py b/siibra/volumes/providers/nifti.py index 9a1abf7c8..ea5b5cf2e 100644 --- a/siibra/volumes/providers/nifti.py +++ b/siibra/volumes/providers/nifti.py @@ -188,19 +188,23 @@ def fetch( result = loader() if voi is not None: - bb_vox = voi.transform(np.linalg.inv(self.affine)) - (x0, y0, z0), (x1, y1, z1) = bb_vox.minpoint, bb_vox.maxpoint + zoom_xyz = np.array(result.header.get_zooms()) # voxel dimensions in xyzt_units + bb_vox = voi.transform(np.linalg.inv(result.affine)) + x0, y0, z0 = np.floor(np.array(bb_vox.minpoint.coordinate) / zoom_xyz).astype(int) + x1, y1, z1 = np.ceil(np.array(bb_vox.maxpoint.coordinate) / zoom_xyz).astype(int) shift = np.identity(4) shift[:3, -1] = bb_vox.minpoint result = nib.Nifti1Image( dataobj=result.dataobj[x0:x1, y0:y1, z0:z1], affine=np.dot(result.affine, shift), + dtype=result.header.get_data_dtype(), ) if label is not None: result = nib.Nifti1Image( (result.get_fdata() == label).astype('uint8'), - result.affine + result.affine, + dtype='uint8' ) return result