Nilearn error: cannot slice image objects

I am using nilearn for decoding my data. I’ve been following this tutorial: ROI-based decoding analysis in Haxby et al. dataset - Nilearn

However, this line gives me an error:

mask_filename = func_img_run1[mask_name]

I am using my own data (single subject) instead of Haxby dataset. I am attaching the error message below:

TypeError Traceback (most recent call last)
7 print(“working on %s” % mask_name)
----> 9 mask_filename = func_img_run1[mask_name]
10 masker = NiftiMasker(mask_img=mask_filename, standardize = True)
11 mask_scores[mask_name] = {}

~/opt/anaconda3/lib/python3.8/site-packages/nibabel/ in getitem(self, idx)
583 own risk.
584 “”"
→ 585 raise TypeError(
586 "Cannot slice image objects; consider using img.slicer[slice] "
587 "to generate a sliced image (see documentation for caveats) or "

TypeError: Cannot slice image objects; consider using img.slicer[slice] to generate a sliced image (see documentation for caveats) or slicing image array data with img.dataobj[slice] or img.get_fdata()[slice]

func_img_run1.shape returns (64, 64, 40, 75). Any help or insight into this issue would be greatly appreciated. Thanks.

haxby_dataset in the example is an sklearn.utils.Bunch object- basically a dictionary storing different elements of the overall dataset. Your func_img_run1 is probably a Nifti1Image object instead.

If you want to use the Haxby mask directly, you need to use the nilearn.dataset.fetch_haxby call from the example and grab the mask from the resulting Bunch. Otherwise, if you have your own mask you want to use, you just need to provide that the path to that mask (e.g., mask_filename = "/path/to/my/mask.nii.gz") instead of func_img_run1[mask_name].