Nibabel load bottlenecking

I’ve been profiling some code, where a processing bottleneck occurs (method 'decompress' of 'zlib.Decompress' objects}). I believe this is from my nibabel.load command for loading large resting state nifti files (1.2G; shape=(88, 88, 60, 1130)). Loading one file can take 8-11 seconds, and I’m attempting to load multiple files per subject, across many subjects in a dataset. To save time and memory, I’d like to simply load a single volume, but haven’t been able to do so. My code is as follows:

import nibabel as nib
img_vol = nib.load('rsfMRI.nii.gz').dataobj[..., -1]

I was wondering if there’s a way to speed this loading process in nibabel, or if this is to be expected given the large file sizes?

Just realized that the issue is due to selecting the last volume. Faster solution is to take the first volume (or an earlier on in general).

img_vol = nib.load('rsfMRI.nii.gz').dataobj[..., 0]

If you have indexed-gzip installed, you will dramatically speed up slicing the image. It does this automatically with no configuration needed.

1 Like