diff --git a/e2e/volumes/test_extracting_bbox_from_template.py b/e2e/volumes/test_extracting_bbox_from_template.py new file mode 100644 index 00000000..c45c9987 --- /dev/null +++ b/e2e/volumes/test_extracting_bbox_from_template.py @@ -0,0 +1,23 @@ +import pytest +import siibra +from nibabel import Nifti1Image + + +vois = [ + siibra.locations.BoundingBox( + (-54.90, -29.47, 12.66), (12.69, 35.12, 20.24), "bigbrain" + ), + siibra.locations.BoundingBox( + (-154.90, -29.47, 12.66), (112.69, 38.12, 80.24), "bigbrain" + ), + siibra.locations.BoundingBox( + (-54.90, -29.47, 12.66), (12.69, 38.12, 80.24), "bigbrain" + ), +] + + +@pytest.mark.parametrize("voi", vois) +def test_fetching_voi(voi): + assert isinstance( + voi.space.get_template().fetch(voi=voi, resolution_mm=0.52), Nifti1Image + ) diff --git a/siibra/volumes/providers/nifti.py b/siibra/volumes/providers/nifti.py index 5d3a4aaf..8c1dc8c4 100644 --- a/siibra/volumes/providers/nifti.py +++ b/siibra/volumes/providers/nifti.py @@ -188,7 +188,7 @@ def fetch( result = loader() if voi is not None: - bb_vox = voi.transform(np.linalg.inv(result.affine)) + bb_vox = voi.transform(np.linalg.inv(self.affine)) (x0, y0, z0), (x1, y1, z1) = bb_vox.minpoint, bb_vox.maxpoint shift = np.identity(4) shift[:3, -1] = bb_vox.minpoint diff --git a/siibra/volumes/volume.py b/siibra/volumes/volume.py index dc0bade2..83ae24b3 100644 --- a/siibra/volumes/volume.py +++ b/siibra/volumes/volume.py @@ -416,6 +416,20 @@ def fetch( f"volume are: {self.formats}" ) + # ensure the voi is inside the template + voi = kwargs.get("voi", None) + if voi is not None and voi.space is not None: + assert isinstance(voi, boundingbox.BoundingBox) + tmplt_bbox = voi.space.get_template().get_boundingbox(clip=False) + intersection_bbox = voi.intersection(tmplt_bbox) + if intersection_bbox is None: + raise RuntimeError(f"{voi=} provided lies out side the voxel space of the {voi.space.name} template.") + if intersection_bbox != voi: + logger.info( + f"Since provided voi lies outside the template ({voi.space}) it is clipped as: {intersection_bbox}" + ) + kwargs["voi"] = intersection_bbox + result = None # try each possible format for fmt in possible_formats: