Skip to content

Commit 872490c

Browse files
fix(nifti): enable lazy loading for Nifti1ImageWrapper
- Change dataobj initialization from get_fdata() to .dataobj - Preserves nibabel's ArrayProxy for lazy loading - Prevents OOM errors on large 4D fMRI files - Defers I/O errors to usage time for better error handling Resolves #1
1 parent 004a5bf commit 872490c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/datasets/features/nifti.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Nifti1ImageWrapper(nib.nifti1.Nifti1Image):
2727

2828
def __init__(self, nifti_image: nib.nifti1.Nifti1Image):
2929
super().__init__(
30-
dataobj=nifti_image.get_fdata(),
30+
dataobj=nifti_image.dataobj,
3131
affine=nifti_image.affine,
3232
header=nifti_image.header,
3333
extra=nifti_image.extra,

tests/features/test_nifti.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,22 @@ def test_load_zipped_file_locally(shared_datadir):
128128

129129
ds = load_dataset("niftifolder", data_files=nifti_path)
130130
assert isinstance(ds["train"][0]["nifti"], nib.nifti1.Nifti1Image)
131+
132+
133+
@require_nibabel
134+
def test_nifti_lazy_loading(shared_datadir):
135+
import nibabel as nib
136+
import numpy as np
137+
138+
nifti_path = str(shared_datadir / "test_nifti.nii.gz")
139+
nifti = Nifti()
140+
encoded_example = nifti.encode_example(nifti_path)
141+
decoded_example = nifti.decode_example(encoded_example)
142+
143+
# Verify that the data object is an ArrayProxy (lazy) and not a numpy array (dense)
144+
assert nib.is_proxy(decoded_example.dataobj)
145+
assert not isinstance(decoded_example.dataobj, np.ndarray)
146+
147+
# Verify that we can still access the data
148+
data = decoded_example.get_fdata()
149+
assert data.shape == (80, 80, 10)

0 commit comments

Comments
 (0)