Resting-state de-noising with clean_img, signal.clean and NiftiMasker

Hi everyone,

I am new to nilearn (and to python in general). I have been trying out different Nilearn functions to de-noise 7T resting state data, that was preprocessed using fmriprep. I tried nilearn.image.clean_img, nilearn.signal.clean and nilearn.maskers.NiftiMasker but each time the resultant de-noised image looks weird.

I copied and pasted a brief example of the code I used for each function below, along with an example output image. I would really appreciate it if someone can tell me what I am doing wrong / what might be going wrong and how I can fix it.

Thank you in advance.


from nilearn import image as nimg
from nilearn.maskers import NiftiMasker
import numpy as np
import pandas as pd

mask_img = nimg.load_img(mask_file)

func_img = nimg.load_img(func_file)

confounds= pd.read_csv("confounds.tsv", delimiter ='\t')

#(confounds include motion, WM, CSF, and derivatives for all from fmriprep, RETROICOR regressors derived using a toolbox)


clean_img= nimg.clean_img(func_img, confounds=confounds, detrend=True, standardize=False, low_pass=0.2, high_pass=0.01, t_r=0.8, mask_img=mask_img)





func_data = np.reshape(func_data, (Number_of_Trs, -1)) #matching signal.shape[0] and confound.shape[0]

signal_cleaned=nilearn.signal.clean(func_data, runs=None, detrend=True, standardize=False, sample_mask=None, confounds=confounds, standardize_confounds=False, filter='butterworth', low_pass=0.2, high_pass=0.01, t_r=0.8, ensure_finite=False)

clean_data = np.reshape(signal_cleaned, func_img.shape)

clean_img= nb.Nifti1Image(clean_data, func_img.get_affine(),


The output from this one looks more like a normal functional image, but the image has stripes on the it and it seems have a background added to it now (the input image here was skull-stripped with no background).


#based on the whole brain time series extraction given in the example

brain_masker=NiftiMasker(mask_img=mask_img, runs=None, smoothing_fwhm=3.2, standardize=False, standardize_confounds=False, detrend=False, high_variance_confounds=False, low_pass=0.2, high_pass=0.01, t_r=0.8, memory_level=1, memory='nilearn cache', verbose=0, reports=True)

brain_time_series=brain_masker.fit_transform(func_img, confounds)



Output image here looks a bit more like above, but better (still nothing like a functional image).

Regarding NiftiMasker

#When I initiated the fit_transform, I got this warning;

*Generation of a mask has been requested (y != None) while a mask has been provided at masker creation. Given mask will be used. warnings.warn('[] Generation of a mask has been'*

#And this one below after it ran

*local/lib/python3.9/site-packages/nilearn/maskers/ UserWarning: Persisting input arguments took 31.77s to run.* If this happens often in your code, it can cause performance problems (results will be correct in all cases). 
*The reason for this is probably some large input arguments for a wrapped function (e.g. large strings). THIS IS A JOBLIB ISSUE. If you can, kindly provide the joblib's team with an example so that they can fix the problem. data = self._cache(*
1 Like

FWIW I’ve gotten terrible results when using standardize and detrend in nilearn, so maybe try without those. Did you use the mask generated by fMRIprep?