Passing 4D Niimg-like object to scikit-learn pipeline leads to ValueError: Found input variables with inconsistent numbers of samples

nilearn
nifti
scikit-learn
#1

I have a list of filepaths to 3D Nifti Files. I use nilearn’s resample_img to resample the images to a target affine. resample_img returns the files as a 4D Image. I then want to pass this object and the labels to my scikit-learn pipeline, however I get the error:

ValueError: Found input variables with inconsistent numbers of samples

I know why this error happens: scikit-learn (or more precise check_consistent_length()) compares the length of the first dimension of X (so the length of the x-axis, which is of course the wrong axis) and y (number of samples) and will throw the error since they are of unequal length. Is there any way to let the pipeline accept my 4D object? I guess if I could hack this ‘safety function’ everything would still work. My first transformer contains NiftiMasker which will automatically transform the 4D img to the right format (n_samples * n_features), so after the 4D file is transformed, X and y do have the right shape.