Skip to content

Commit

Permalink
Make voi validity and cliping at Volume.fetch stage
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmetNSimsek committed Oct 15, 2024
1 parent 28f6efb commit 28e59b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
19 changes: 1 addition & 18 deletions siibra/volumes/providers/neuroglancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,24 +503,7 @@ def fetch(self, voi: _boundingbox.BoundingBox = None, **kwargs):
if voi is None:
bbox_ = _boundingbox.BoundingBox((0, 0, 0), self.size, space=None)
else:
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))
bbox_ = voi.transform(np.linalg.inv(self.affine))

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

if voi is not None:
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))
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

0 comments on commit 28e59b8

Please sign in to comment.