Mask large nifti using NiftiMasker and avoid loading the whole image

Hi,

I am using nilearn to mask a large nifti using the following command:

mask_nii = nib.load(mask)
self.mask_img = new_img_like(mask_nii, get_data(mask_nii)==ROI_id)
self.masker = NiftiMasker(mask_img=self.mask_img)
residual_img = nib.load(errts_file)

self.residual = self.masker.fit_transform(residual_img)

It looks like when masking nilearn loads the entire image into memory, which can cause memory issue when the residual image is big. I tracked the memory usage, it looks like it’s only at the step of

self.residual = self.masker.fit_transform(residual_img) 

nilearn loads the residual image into memory.

Is there anyway to avoid loading the whole image into memory?

Thanks.
mx