Nilearn: Dealing with NaN when extracting time series from brain parcellations

Hello!
I am using nilearn to extract signals from a brain parcellation and compute a correlation matrix.
I have my fMRI data and I am using the Schaefer 400 atlas to define my ROIs.
The problem I have is that some of my subjects have some missing voxels in their fMRI data, therefore the time series for the ROIs with some missing voxels in my fMRI data are NaN.
It means that the mean of all voxel-time series within the ROI is NaN because some of the voxels NaN.
Then, of course, when I try to calculate the correlation matrix I have an error:

/Users/mot/.local/lib/python3.7/site-packages/nilearn/signal.py:71: RuntimeWarning: invalid value encountered in less
std[std < np.finfo(np.float).eps] = 1. # avoid numerical problems
Traceback (most recent call last): File “ztoRunLocal_RS.py”, line 28, in
correlation_matrix = correlation_measure.fit_transform([time_series])
File “/Users/mot/.local/lib/python3.7/site-packages/nilearn/connectome/connectivity_matrices.py”, line 545, in fit_transform return self.fit_transform(X, do_fit=True, do_transform=True)
File “/Users/mot/.local/lib/python3.7/site-packages/nilearn/connectome/connectivity_matrices.py”, line 490, in fit_transform).covariance for x in X]
File “/Users/mot/.local/lib/python3.7/site-packages/nilearn/connectome/connectivity_matrices.py”, line 490, in ).covariance
for x in X]
File “/Users/mot/opt/anaconda3/lib/python3.7/site packages/sklearn/covariance/_shrunk_covariance.py”, line 422, in fit X = check_array(X)
File “/Users/mot/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py”, line 578, in check_array allow_nan=force_all_finite == ‘allow-nan’)
File “/Users/mot/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py”, line 60, in _assert_all_finite msg_dtype if msg_dtype is not None else X.dtype)

ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’).

I explored the time series for all the voxels inside the ROIs with NaN time series to be sure that all the voxels are not NaN, and there are only few voxels missing (e.g. 10 over 112 in one ROI).

My question is: For these regions with some missing voxels, there is an option using nilearn to avoid the missing values and to calculate the mean time series within the ROI with the remaining voxels? Therefore I can have the time series for all ROIs and calculate the correlation matrix with 400 regions for all the participants.
As I am not expert in python, I explored the options in nilearn but I did not find any appropriate option.
Any suggestion? Thanks a lot!

I am using the following code:
atlas_filename = ‘/Users/mot/Desktop/PhD/data_mdl_RS/Schaefer400.nii’
fmri_filename = ‘/Users/mot/Desktop/PhD/data_mdl_RS/wdn_ts_OC_S21.nii’

masker = NiftiLabelsMasker (labels_img = atlas_filename)
time_series = masker.fit_transform (fmri_filename)

correlation_measure = ConnectivityMeasure (kind=‘correlation’)
correlation_matrix = correlation_measure.fit_transform([time_series])

Marcela

1 Like

You can clean the images (basically imputing NaNs by 0) using nilearn.image.clean_img (https://nilearn.github.io/modules/generated/nilearn.image.clean_img.html#nilearn.image.clean_img)
prior to masking.
HTH,
Bertrand

2 Likes

Thank you very much! I will try that!

Hello!
Thanks a lot for your help @bthirion !
I used the clean_img function and it worked, I could continue my analyses to have my correlation matrices.
But, I wonder if replacing NaN by zeros to calculate the mean time series within a ROI
affects the mean and later, the correlation matrix?
I tried to create a mask to be applied before extracting the time series within the ROIs (to avoid replacing NaN by zeros and to only considers the voxels that exist in the brain of my participants), but I did not success, I had the NaN error before performing the calculation of correlation:

atlas_filename = ‘/Users/mot/Desktop/PhD/data_mdl_RS/Schaefer400.nii’
fmri_filename = ‘/Users/mot/Desktop/PhD/data_mdl_RS/wdn_ts_OC_S21.nii’

nifti_masker = NiftiMasker(mask_strategy=‘epi’)
nifti_masker.fit(fmri_filename)
mask_img = nifti_masker.mask_img_

masker = NiftiLabelsMasker (labels_img = atlas_filename, mask_img=mask_img)
time_series = masker.fit_transform (fmri_filename)

correlation_measure = ConnectivityMeasure (kind=‘correlation’)
correlation_matrix = correlation_measure.fit_transform([time_series])

ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’).

Do you have any suggestion or do I have some important error in my script?
O replacing the NaN by zeros should not affect subsequent analyses?

I would appreciate any help! Thanks again

What likely happens is that one of the regions gets zeo signal, which makes its correlation with anything else undefined —hence the NaNs.
I would remove that regions from the correlation analysis ?
Best,
Bertrand

1 Like