Using a probabilistic mask with nilearn's NiftiMasker function


I’m trying to use nilearn’s NiftiMasker function to extract gray matter.

I’ve run into variable degrees of compatibility with a probabilistic mask. Masking to generate reports seems fine. But when I try to save the masked data, I get an error stating that the mask isn’t binary:

       nii_masker = NiftiMasker(mask_img=graymatter_mask, memory="nilearn_cache",
                         memory_level=2, reports=1, t_r=TR)
        # this is fine
        report = nii_masker.generate_report() # no problems with reports, masking appears reasonable

        epi_masked_2d = nii_masker.transform(epi) # here, nilearn complains that the mask isn't binary

Does anyone know of any workarounds, short of just binarizing the probabilistic mask?

As always, thanks for your help.


You probably want ot binarize the brain mask: My understanding is that you want to extract time courses from all voxels that have a certain level of grey matter density ?
binary_mask = nilearn.image.math_img('i1 > threshold', i1=my_probabilistic_mask)
where threshold is picked by you, as a number between 0 and 1 I guess.