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

Fix nifti fetch voi #611

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions e2e/core/test_space.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from typing import Tuple
import siibra


Expand All @@ -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, 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}"
11 changes: 8 additions & 3 deletions siibra/volumes/providers/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,24 @@ 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
vox_offset = result.header.get_data_offset()
zoom_xyz = np.array(result.header.get_zooms()) # voxel dimensions in mm
bb_vox = voi.transform(np.linalg.inv(result.affine))
x0, y0, z0 = np.floor((np.array(bb_vox.minpoint.coordinate) - vox_offset) / zoom_xyz).astype(int)
x1, y1, z1 = np.ceil((np.array(bb_vox.maxpoint.coordinate) - vox_offset) / 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.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
Expand Down
Loading