Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/datasets/features/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Nifti1ImageWrapper(nib.nifti1.Nifti1Image):

def __init__(self, nifti_image: nib.nifti1.Nifti1Image):
super().__init__(
dataobj=nifti_image.get_fdata(),
dataobj=nifti_image.dataobj,
affine=nifti_image.affine,
header=nifti_image.header,
extra=nifti_image.extra,
Expand Down
19 changes: 19 additions & 0 deletions tests/features/test_nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,22 @@ def test_load_zipped_file_locally(shared_datadir):

ds = load_dataset("niftifolder", data_files=nifti_path)
assert isinstance(ds["train"][0]["nifti"], nib.nifti1.Nifti1Image)


@require_nibabel
def test_nifti_lazy_loading(shared_datadir):
import nibabel as nib
import numpy as np

nifti_path = str(shared_datadir / "test_nifti.nii.gz")
nifti = Nifti()
encoded_example = nifti.encode_example(nifti_path)
decoded_example = nifti.decode_example(encoded_example)

# Verify that the data object is an ArrayProxy (lazy) and not a numpy array (dense)
assert nib.is_proxy(decoded_example.dataobj)
assert not isinstance(decoded_example.dataobj, np.ndarray)

# Verify that we can still access the data
data = decoded_example.get_fdata()
assert data.shape == (80, 80, 10)