Create an img_mask of a specific ROI out of a Templateflow atlas

Hi,
I loaded an atlas out of templateflow and I wish to extract a binary mask of a specific roi (e.g. mask of the amygdala).

Here is my code so far:

import nibabel as nb
from templateflow.api import get
atlas_file = get(‘MNI152NLin2009cAsym’,
resolution=2,
atlas=‘HOSPA’,
desc=‘th25’
)
atlas = nb.load(atlas_file)

I wish to know what are the commands for extracting a mask out of this atlas using nilearn or nibabel, I tried to use NiftiLabelsMasker but could not figure out how to use it properly and if it is the right approach.

Would appreciate any help, thanks!
Tamir

Hi, the integer values in the atlas image correspond to different regions.
To know which is the amygdala, you would need a list of labels or some metadata that should be distributed along with the image; the templateflow documentation should provide information about how to find it – you need a legend for this map.
Once you have it, if for example the amygdala corresponds to region 10, you can create a mask image with something like:

mask_img = nilearn.image.new_img_like(atlas, nilearn.image.get_data(atlas) == 10)
masker = nilearn.input_data.NiftiMasker(mask_img=mask_img).fit()

Thanks!
I was thinking maybe there is some built in func that works with atlases, but this works great as well :slight_smile:

Hey :slight_smile:
I have another question regarding the masking.
After creating and fitting the Masker object, I want to transform some functional images. The problem is that my functional image and the mask are in different dimensions. My functional image is shaped (65, 77, 60) with voxel size (3,3,3.3 mm), and the mask is in the shape of the atlas (97, 115, 97) with voxel size (2,2,2 mm). When applying the mask, it transforms the image to the atlas space, and I wish to have it in my functional image space. This is my code so far:

from nilearn.image import mean_img
from nilearn.masking import apply_mask

fmri_img = path + f'/fmriprep/sub-{subj}/func/sub-{subj}_task-Rew2_space- 
 MNI152NLin2009cAsym_desc-preproc_bold.nii.gz'
mean_image  = nilearn.image.mean_img(fmri_img)

atlas_file = get(
    'MNI152NLin2009cAsym',
    resolution=2,
    atlas='HOSPA',
    desc='th25',
)
atlas = nb.load(atlas_file)
amy = np.logical_or(nilearn.image.get_data(atlas) == 10, nilearn.image.get_data(atlas) == 20)
amy_img_from_mean = nilearn.image.new_img_like(mean_image,amy)
amy_img_from_atlas = nilearn.image.new_img_like(atlas,amy)
masker_from_mean = nilearn.input_data.NiftiMasker(mask_img=amy_img_from_mean).fit()
masker_from_atlas = nilearn.input_data.NiftiMasker(mask_img=amy_img_from_atlas).fit()
masked_mean_img_from_mean = masker_from_mean.transform_single_imgs(mean_image)
masked_mean_img_from_atlas = masker_from_atlas.transform_single_imgs(mean_image)
masked_mean_img_from_atlas_inverse = 
masker_from_atlas.inverse_transform(masked_mean_img_from_atlas)
masked_mean_img_from_mean_inverse = 
masker_from_mean.inverse_transform(masked_mean_img_from_mean)

The masked_mean_img_from_atlas_inverse works well, but in the atlas space, and the masked_mean_img_from_mean_inverse raise an error while trying to view it.

How can I apply the mask and inverse it back to the functional image space?

you could resample the mask to the data’s resolution before creating the masker, eg using nilearn.image.resample_to_img, or you could give the appropriate values to the masker’s constructor’s target_affine and target_shape parameters