Nilearn signal.clean signal length error

Summary of what happened:

Hi, when I use the signal.clean function, I get the following error :
ValueError: Confound signal has an incorrect lengthSignal length: 204; confound length: 280

My input_file is 4D fMRI data of 280 volumes across time - when I compute signal size to check :

signal_data = nib.load(input_file).get_fdata()
signal_length = signal_data.shape[-1]
print("Signal length:", signal_length)

Output is : Signal length: 280

And my confound file is a txt file listing 42 confounds for each volume. To make sure this was in a correct format, I converted it into a dataframe of size 280 rows × 42 columns

Command used :

signal.clean(input_file, runs=None, detrend=False, standardize=False, sample_mask=mask_img, confounds=df, standardize_confounds=True, high_pass=high_pass, t_r=2)

Version: version = ‘0.9.2’

Environment : on Jupyter-lab

Thanks for your help !
Raphaëlle

Hi @raphaschl,

I think the problem here is that you specify a sample_mask argument. Keep in mind this isn’t an image mask, like a brain mask. It is a vector that is telling you which samples you want to consider out of your time series. It appears you entered an image for that argument which is probably having unintended consequences. Try removing that argument and try again.

Best,
Steven

Hi Steven,

Thanks for your quick answer !
I hadn’t understood the mask parameter correctly indeed. Just tried it again without the sample_mask, but still get the exact same error :

signal.clean(input_file, runs=None, detrend=False, standardize=False, confounds=df, standardize_confounds=True, high_pass=0.008, t_r=2)
ValueError: Confound signal has an incorrect lengthSignal length: 204; confound length: 280

Cheers,
Raphaëlle

Hi,
Quick update : I realized the error came from my input being a path to my 4D fMRI file instead of a 2D numpy.ndarray. I hence changed my input format :

input_file='myinput.nii.gz'
output_file = input_file.replace('.nii', '_cleaned.nii')
confound_file='noiseconfounds.txt'
df=pd.read_csv(confound_file, sep=' ', header=None)
# Import and reshape the 4D fMRI data by flattening the first three dimensions and transposing resulting array
fmri_img = nib.load(input_file)
fmri_data = fmri_img.get_fdata()
flattened_data = fmri_data.reshape(-1, fmri_data.shape[-1]).T 

cleaned_data = signal.clean(flattened_data, runs=None, detrend=False, standardize=False, confounds=df, standardize_confounds=True, high_pass=0.008, t_r=2) 

# Reshape the cleaned data back to its original shape and save as 4D nifti file
cleaned_data_reshaped = cleaned_data.T.reshape(fmri_data.shape)
cleaned_img = nib.Nifti1Image(cleaned_data_reshaped, affine=fmri_img.affine)
nib.save(cleaned_img, output_file)

This worked fine. Sorry for the confusion, and thanks again for your input !

Cheers,
Raphaëlle

2 Likes