Use specific ROI from HO-atlas as a mask

Hi,
I have my post-fmri prep data and I want to use Harvard-Oxford atlas to fetch the activity in 3 specific subcortical regions (label Ids 10,20, 26).
So it should be something like:

masker = NiftiMasker(mask_img = HO_atlas[10], smoothing_fwhm=6)
df = masker.fit_transform(nii_file)

How can I do it?
Thanks!

Hi @orko,

Hard to say with the limited amount of code you provided. Here’s an example from scratch:

from nilearn.datasets import fetch_atlas_harvard_oxford
from nilearn.maskers import NiftiLabelsMasker
from nilearn.image import index_img

nii_file = "/PATH/TO/IMAGE.nii.gz"
# FETCH ATLAS
ho = fetch_atlas_harvard_oxford('sub-maxprob-thr50-1mm') # Note this is deterministic 3D atlas, not probabilistic
ho_img = ho.maps
ho_labels = ho.labels
# GET ATLAS VALUES IN IMAGE
masker = NiftiLabelsMasker(labels_img=ho_img, labels=ho_labels)
ho_vals = masker.fit_transform(nii_file)

You can then index into ho_vals however you like. However, the subcortical atlas only has 21 regions, so it is not clear to me where your desired index of 26 is coming from.

Best,
Steven

Thanks!
Can you please explain the difference between deterministic/probabilistic atlas, and between atlas and parcellation?
Also, I looked for but couldn’t find a ref as to what considered ventral and dorsal striatum (assuming I want to contrast them), do you maybe have one?

Hi @orko,

Probabilistic - 4D image, composed of one 3D image per ROI. The values correspond to the probability that the given ROI is there (usually based off of some population overlap)

Deterministic - In this case, it is simply thresholding the probability maps at a given threshold (e.g., 50%) and then combining all of them into a single 3D image, where the values correspond to the label.

From sMRI Segmentation and Parcellation – Overview of Structural MRI (Pre)processing and Neuroimaging Analysis :

A parcellation can be defined as the splitting of an object into non-overlapping components. Since an atlas divide the brain into regions, atlasing can be considered a kind of parcellation. Reciprocally, any kind of brain parcellation can be considered an “atlas”. A difference is that an atlas is often expected to be defined on an entire brain, while a parcellation can be limited to a specific region (e.g. thalamus parcellation). A parcellation can also be applied for example to the cortical GM (the outer layer of the brain) or the subcortical GM for deep GM (GM structures below the cortex).

Perhaps Atlases/striatumstruc - FslWiki?

Best,
Steven

Thanks, I also saw Atlases/striatumstruc - FslWiki
but couldnt find it in nilearn or other pythonic tool

You can use any atlas in nilearn, provided it is formatted appropriately (3D image in standard space with values denoting ROI labels); it does not have to be built in to nilearn to be used in NiftiLabelsMasker.

Do you maybe have a simple code sample as to how to apply fit_transform of specific ROI (let say VST) from this atlas?

I do not, I have not worked with that atlas. But if you have that atlas you can load it with nilearn.image.load_img and use that as the labels_img, provided it is formatted as I described above.

Best,
Steven

In NiftiLabelsMasker,
labels_img is the 3D image (.nii)
and labels is simply list of the ROIs? (so probably [‘VST’,‘CD’,‘PU’] here?)

If those are the labels in order then yes. It expects as many labels as there are non-zero unique values in the image.

Re this code, when I run it I get a dataframe with the shape:
nTR X nRegions (21)

However, per region, I except to get a 2D data of:
nTR X nVoxels

How to get a:
nTR X nVoxels dataframe for a specific region?
(or a 3D data of nTR X nVoxels X nRegions)

Hi @orko,

This is the expected output of NiftiLabelsMasker.

Since different regions have different sizes, you would not be able get a consistent array of nTR X nVoxels X nRegions.

Below is code that will make a dictionary of values, where indexing the dictionary by the label returns an voxel X TR array:

from nilearn.datasets import fetch_atlas_harvard_oxford
from nilearn.maskers import NiftiLabelsMasker
from nilearn.image import load_img, index_img, resample_to_img

# LOAD BOLD
nii_file = "/PATH/TO/IMAGE.nii.gz"
nii_img = load_img(nii_file)
nii_data = nii_img.get_fdata()

# FETCH ATLAS
ho = fetch_atlas_harvard_oxford('sub-maxprob-thr50-1mm') # Note this is deterministic 3D atlas, not probabilistic
ho_img = ho.maps
ho_img_resampled = resample_to_img(ho_img, nii_img, interpolation='nearest')
ho_data = ho_img_resampled.get_fdata()
ho_labels = ho.labels

# MAKE DICTIONARY
data_dictionary = {}
for index,label in enumerate(ho_labels):
    print(index,label)
    if index == 0: # Skip background
        continue
    ho_region = (ho_data==index)
    nii_data_in_region = nii_data[ho_region,:]
    data_dictionary[label] = nii_data_in_region

Please confirm that you understand the code and its outputs.

Best,
Steven

Perfect thanks!

Just one last question please - is there an overlap between areas in that atlas?
Meaning, lets say I want to compare activity between two neighboring regions - can I be sure that there are no overlapping voxels?

Some of the low-probabilistic regions might extend to each other (I haven’t checked), but in the deterministic version each voxel is assigned a unique value.

Best,
Steven