Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clip voi to template's to if voi lies partially outside of the template #606

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions e2e/volumes/test_extracting_bbox_from_template.py
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion siibra/volumes/providers/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions siibra/volumes/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading