Turn masker object from NiftiSpheresMasker into boolean mask

Hi there :slight_smile:

I’m using the NiftiSpheresMasker to create a spherical mask around a coordinate with a radius of 10mm. Next, I would like to turn this mask into a boolean mask, so that I get a numpy array of booleans for my sphere. I thought the best way of doing this is to first turn the mask into an image and then into a boolean or something of that sort, but haven’t been able to figure it out. Then I ran into this from nifti_spheres_masker that ought to help me, especially the first one:


However, when I tried to import it, I get the following error:

ImportError: cannot import name ‘_apply_mask_and_get_affinity’ from ‘nilearn.input_data.nifti_spheres_masker’ (C:\Users\maxb.WISMAIN\Anaconda3\lib\site-packages\nilearn\input_data\nifti_spheres_masker.py)

I’m wondering if someone has a solution for either of these problems (i.e. either a different way of getting a boolean mask of a sphere or an idea why I can’t import the above-mentioned function)?

Thanks in advance!



Hi @Max_Bringmann ,

I could recommend something like this :slightly_smiling_face:

from nilearn.masking import _unmask_3d
from nilearn.maskers import nifti_spheres_masker

_, A = nifti_spheres_masker._apply_mask_and_get_affinity(
    seeds=[(-42, -36, 16)], niimg=None, radius=10,
    allow_overlap=False, mask_img=gm_mask)
sphere_mask = _unmask_3d(

This will create a spherical ROI only within the grey matter as defined by your gm_mask.

Hope that’s helpful !


1 Like

Thanks, Elizabeth! Will try it out and get back to you :slight_smile:

Works great, thanks again for your help!

1 Like