Merge Atlas for use in Nilearn

Hi all,
Was looking for some guidance, I am attempting to replicate the results from the following paper: https://www.ncbi.nlm.nih.gov/pubmed/29089883

In this they have used the Willard Atlas from the FIND Lab as found here: https://findlab.stanford.edu/functional_ROIs.html

The FIND lab has all of their segments separated out into individual .nii files for each brain region, and from all of the Nilearn examples I have been running the atlas is a single file. Is there a way to merge all of these nii files into a single atlas to feed into Nilearn?

Many thanks.
Chris

Hi @FL33TW00D!

You should be able to do this with the following code:

from nilearn import image

atlas = image.load_img('*.gz', wildcards=True)
atlas = image.math_img('np.sum(img, axis=-1)', img=atlas)

Note that this assumes you’re running the code from inside the directory with all the individual parcel files (and that they all have the '.gz' extension, as they do when you download them from the FIND Lab website!).

The first line (image.load_img) works by (1) finding all the individual parcel files via expansion of the wildcard '*.gz' to find every file with the '.gz' suffix, and (2) loading them into a single 4D image object, where the fourth dimension will be n = 357 (or however many parcels you are using).

The second line (image.math_img) take the sum of the image along this fourth dimension, condensing it into a single, 3D atlas file that you should be able to substitute into any of the nilearn examples! Since the parcels are all given unique integer IDs, the sum along the final axis will ensure that each parcel is represented independently in the final “combined” atlas.

Let me know if this makes sense or if you have any more questions!

Hope that helps,
Ross

2 Likes

Hi Ross,
Massive thanks for this makes my life much easier.
Some of the .nii.gz files in the Willard set are labeled, will the labels be preserved in the nifti header upon merging? Or should I extract them prior?

Thanks,
Chris

No, unfortunately the headers of the constituent files will not be preserved in the final image, so I would recommend you extract any relevant information from the individual parcel files and store it in an alternative format for joint use with the merged atlas.

Cheers,
Ross

1 Like

Hi Ross,
Thanks for this I have extracted the relevant labels,
Upon feeding the merged atlas into Nilearns NiftiMapsMasker it throws an error as the merged atlas is a 3D image and not a 4D Volume. Any ideas on how to remedy this? Apologies for the simple questions I do not come from a neuroscience background.

Thanks,
Chris

No problem! You can do this by modifying the second line to:

atlas = image.math_img('np.sum(img, axis=-1, keepdims=True)', img=atlas)

Also, it’s worth noting that I think NiftiLabelsMasker would be the more appropriate class to use here (NiftiMapsMasker is generally for continuously-valued maps that might be spatially overlapping, like ICA components).

Finally, you’ll need to make sure that the images you are transforming are also 4D!

2 Likes

Hi Ross,
Thanks so much works perfectly!