I’m struggling to clear the following error coming up when I’m using second_level_model.compute_contrast
Error:
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]
I’ve found that this was an issue with nilearn on GitHub repo but the issue seems to be solved in 2018 and closed. Does anyone know how to clear this? I use image.load_img to load the data.
SecondLevelModel expects as inputs, either a list of imgs, a list of FirstLevelModel, or a dataframe, as can be seen in its docstring:
and which is ensured here:
For some reason, passing a single image as second_level_input doesn’t raise an error in fit(), which seems like a bug to me (I’ll have to double check). So, when you call compute_contrast(), one of the first steps is to call _check_first_level_contratst here:
which then tries to take the first element of the list, which in your case is interpreted as slicing the image:
Here is the full code:
import pandas as pd
from nilearn.image import load_img
from nilearn.glm.second_level import SecondLevelModel
from nilearn.datasets import fetch_localizer_contrasts
n_samples = 16
data = fetch_localizer_contrasts(["left vs right button press"], n_samples,
get_tmaps=True)
# This will fail:
# cmap_filenames = load_img(data.cmaps)
# This will work:
cmap_filenames = data.cmaps
design_matrix = pd.DataFrame([1] * n_samples, columns=['intercept'])
second_level_model = SecondLevelModel().fit(cmap_filenames, design_matrix=design_matrix)
z_map = second_level_model.compute_contrast(output_type='z_score')
Let me know if this isn’t clear. In the meantime I’ll see whether we should raise an error when a single 4D image is passed, or if this indexing is a bug.