Skip to content

Commit

Permalink
clip voi to template's to if voi lies partially outside of the templa…
Browse files Browse the repository at this point in the history
…te (#605)
  • Loading branch information
AhmetNSimsek committed Oct 14, 2024
1 parent 6c09878 commit 28f6efb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
19 changes: 18 additions & 1 deletion siibra/volumes/providers/neuroglancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,24 @@ def fetch(self, voi: _boundingbox.BoundingBox = None, **kwargs):
if voi is None:
bbox_ = _boundingbox.BoundingBox((0, 0, 0), self.size, space=None)
else:
bbox_ = voi.transform(np.linalg.inv(self.affine))
if voi.space is not None:
# ensure the voi is inside the template
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:
bbox_ = voi.transform(np.linalg.inv(self.affine))
else:
logger.warning(
(
f"{voi=} provided was cliped to be in the voxel space of the {voi.space.name} template.\n",
f"Clipped boundingbox={intersection_bbox}"
)
)
bbox_ = intersection_bbox.transform(np.linalg.inv(self.affine))
else:
bbox_ = voi.transform(np.linalg.inv(self.affine))

for dim in range(3):
if bbox_.shape[dim] < 1:
Expand Down
19 changes: 18 additions & 1 deletion siibra/volumes/providers/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,24 @@ def fetch(
result = loader()

if voi is not None:
bb_vox = voi.transform(np.linalg.inv(result.affine))
if voi.space is not None:
# ensure the voi is inside the template
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:
bb_vox = voi.transform(np.linalg.inv(result.affine))
else:
logger.warning(
(
f"{voi=} provided was cliped to be in the voxel space of the {voi.space.name} template.\n",
f"Clipped boundingbox={intersection_bbox}"
)
)
bb_vox = intersection_bbox.transform(np.linalg.inv(result.affine))
else:
bb_vox = voi.transform(np.linalg.inv(result.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

0 comments on commit 28f6efb

Please sign in to comment.