Extract atlas based values from a 3D niiimg object


I am trying to extract values from a 3D niimg object (a beta map) according to an atlas.
I succeeded in extracting values with spheres (NiftiSpheresMasker) but with NiftiLabelsMasker or NiftiMapsMasker, I get an error message :

Input data has incompatible dimensionality: Expected dimension is 4D and you provided a 3D image

I used a nilearn code example

from nilearn.input_data import NiftiLabelsMasker, NiftiMapsMasker
masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=False, memory='nilearn_cache', verbose=5)
time_series = masker.fit_transform(fmri_filenames) 

where frmi_filenames is a 3D-niimg object.

According to the documentation, it seems possible, so I did not understand the error.
Any suggestions ?

Thanks in advance.

from the error message it sounds like atlas_filename contains a 3d image (with integers indicating regions?) so NiftiLabelsMasker should work. If it doesn’t, could you share the atlas and beta map?

I tried two atlas from examples :

# dataset = datasets.fetch_atlas_destrieux_2009(lateralized=True)
# atlas_filename = dataset.maps
# labels = dataset.labels

dataset = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm')
atlas_filename = dataset.maps
labels = dataset.labels

# One beta map for test
fmri_filenames = 'DATA_2019/05_GT/STATS_level_1/beta_0001.nii'

Here is the link of this beta map


thanks. indeed it seems the maskers cannot handle a single 3d image, which is indeed a bit surprising and not the documented behaviour. I’ll open an issue but in the meanwhile you can use masker.fit_transform([fmri_filenames])[0]
(ie pass a list containing one filename instead of the filename directly)

Thanks a lot for your help and your time but the error message remains the same :confused:
I will try to concatenate several subjects (or several times the same) beta maps in order to get a fake 4D niimg object as implicitly required.
What is disturbing is that it works (or seems to work) with NiftiSpheresMasker for 3D niimg object.

For replication purpose, I just modified a bit the nilearn example provided

from nilearn import datasets, image

dataset = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm')
atlas_filename = dataset.maps
labels = dataset.labels

print('Atlas ROIs are located in nifti image (4D) at: %s' %
      atlas_filename)  # 4D data

# One subject of brain development fmri data
data = datasets.fetch_development_fmri(n_subjects=1)

######  code modification ###########
fmri_filenames = image.index_img(image.load_img(data.func[0]),0)
######  end of code modification ###### 

from nilearn.input_data import NiftiLabelsMasker
masker = NiftiLabelsMasker(labels_img=atlas_filename, standardize=True,
                           memory='nilearn_cache', verbose=5)

# Here we go from nifti files to the signal time series in a numpy
# array. Note how we give confounds to be regressed out during signal
# extraction
time_series = masker.fit_transform(fmri_filenames, confounds=data.confounds)

And using concat does not work with my beta maps but works with the example dataset reduced to one 3D niimg object.

fmri_img = image.concat_imgs([fmri_filenames,fmri_filenames], verbose=5)

sorry I’m not sure I understand; in both examples (with development dataset and with beta_0001.nii) replacing fmri_filenames with [fmri_filenames] worked for me.
you still get the same error when doing that?

You are right, it works now with [fmri_filenames] but It did not with the first suggestion hence the quiproquo.
Thanks a lot !!

you’re welcome! if you are interested the issue on the repo is here https://github.com/nilearn/nilearn/issues/2637

1 Like