Custom made ROI data extraction

Hi,
I am working on HCP resting state data, trying to build brain correlation maps with certain parts of the brain. I have been using nilearn’s NiftiSphereMasker following this guide

and while it takes a long time, it would do the job. Now I was trying to make my procedure more structured, so instead of defining ROI coordinates and radius, I pre-built them, saved as Nifti images and loaded into similar script. Instead of NiftiSphereMasker, I tried using NiftiMasker, providing it with my mask

sgACC_masks = {}
for file_name in os.listdir(path):
if file_name.endswith(‘.nii.gz’):
base_name = os.path.splitext(file_name)[0]

    if base_name.endswith('.nii'):
        base_name = os.path.splitext(base_name)[0]
    
    file_path = os.path.join(path, file_name)
    
    nii_img = nib.load(file_path)
    
    sgACC_masks[base_name] = nii_img

fig_dir = os.path.join(image_dir,‘seedmaps’)
os.makedirs(fig_dir, exist_ok=True)

for name, nii_img in sgACC_masks.items():

seed_masker = NiftiMasker(
    mask_img=nii_img,
    standardize='zscore_sample',
    memory="nilearn_cache",
    memory_level=4
)
seed_time_series = seed_masker.fit_transform(image)

Image is the functional scan of a subject. nilearn does not raise an issue, however it takes a very long time to fit_transform(), eventually killing the script on the first mask.

Is this not the intended use for NiftiMasker? I tried different memory levels, but the end results is always a crash. ROI itself is of 10 mm radius. Alternatively I could roll back to NiftiSphereMasker…

Thanks!

Hi,
What does nii_img represent ? is it the specification of all your ROIs in one image ? If yes, and if you want to obtain a single time course for each ROI, you may prefer to use NiftiLabelsMasker.
If this takes a very long time, it may be because your images do not have the same reolution or field of view as nii_img.
You can avoid this step by pre-resampling nii_img to the filed of view of your fMRI data but using nilearn.image_resample_to_img.
HTH,
Bertrand

Hi Bertrand,
nii_img is a ROI image I stored in a dictionary and loop through. It is essentially the same ROI, but with different radii or x coordinate. This might be too memory intensive, but remains to be seen.
The ROIs should be already in functional data dimensions as I stored them.
Ultimately, I want to extract mean ROI timeseries and correlate with all brain voxels, so if I understood, NiftiLabelsMasker should be the function for that?

NiftiLabelsMasker worked.

seed_masker = NiftiLabelsMasker(
    labels_img=nii_img,
    standardize='zscore_sample',
)

Thank you.

Best,
Vytautas

Sounds great. Best,
Bertrand