Unfold Nifti-Images using NiftiMasker without applying Mask

I want to transform my list of 3D Niimg-like objects to a 2D array (n_samples x n_features). I know that NiftiMasker can do this but apparently it will always mask the data (and therefore reduce the dimensionality) even if I don’t provide it with a mask-object:

if not given, a mask is computed in the fit step. Optional parameters (mask_args and mask_strategy) can be set to fine tune the mask extraction)

Is there any way I can avoid this masking process? If no, I would probably have to write my own “Unfolder” but in this case I would like to stick to the order/logic of the unfolding process used by NiftiMasker . Moreover and more generally asking what is the reason that NitiMasker always masks the data? I know that it will fall back to the ‘background’-mask strategy if no mask is provided. Maybe I am just missing something:

mask_strategy: {‘background’, ‘epi’ or ‘template’}, optional
The strategy used to compute the mask: use ‘background’ if your images present a clear homogeneous background, ‘epi’ if they are raw EPI images, or you could use ‘template’ which will extract the gray matter part of your data by resampling the MNI152 brain mask for your data’s field of view. Depending on this value, the mask will be computed from masking.compute_background_mask, masking.compute_epi_mask or masking.compute_gray_matter_mask. Default is ‘background’.

you could provide it with a mask image that only contains 1 and no 0 ; in this case all voxels will be inside the “mask” and kept

@jeromedockes Thanks for the reply. I have another question, maybe you have the answer to that as well. If I provide NiftiMasker with an image, does that mean, that there’s still a masking process going on before the actual masking process with my provided mask (triggered by one of the arguments for mask_strategy)? Or will the arguments for mask_stratey completely ignored then and only my provided mask will be used?

if you provide a mask it will use exactly the mask you have provided and mask_strategy is ignored. you still need to call NiftiMasker.fit but the argument doesn’t matter: you can do masker.fit([]) .

also note that if you want no masking and you don’t need the other steps provided by the masker, such as resampling to the mask’s resolution, smoothing, standardizing, detrending etc you can simply use Nifti1Image.get_data().ravel()

1 Like

Thank you @jeromedockes!