Group Sparse inverse covariance for multi-subject connectome

This example shows how to estimate a connectome on a group of subjects using the group sparse inverse covariance estimate.

This example is a toy example as running it on more subjects will require a longer run time.

import numpy as np

from nilearn.plotting import plot_matrix


def plot_matrices(cov, prec, title, labels):
    """Plot covariance and precision matrices, for a given processing."""
    prec = prec.copy()  # avoid side effects

    # Put zeros on the diagonal, for graph clarity.
    size = prec.shape[0]
    prec[list(range(size)), list(range(size))] = 0
    span = max(abs(prec.min()), abs(prec.max()))

    # Display covariance matrix
    plot_matrix(
        cov,
        vmin=-1,
        vmax=1,
        title=f"{title} / covariance",
        labels=labels,
    )
    # Display precision matrix
    plot_matrix(
        prec,
        vmin=-span,
        vmax=span,
        title=f"{title} / precision",
        labels=labels,
    )

Fetching datasets

from nilearn.datasets import fetch_atlas_msdl, fetch_development_fmri

n_subjects = 4  # subjects to consider for group-sparse covariance (max: 40)


msdl_atlas_dataset = fetch_atlas_msdl()
rest_dataset = fetch_development_fmri(n_subjects=n_subjects)

# print basic information on the dataset
print(
    f"First subject functional nifti image (4D) is at: {rest_dataset.func[0]}"
)
[fetch_atlas_msdl] Dataset directory found: /home/runner/work/nilearn/nilearn/nilearn_data/msdl_atlas
[fetch_development_fmri] Dataset directory found: /home/runner/work/nilearn/nilearn/nilearn_data/development_fmri
First subject functional nifti image (4D) is at: /home/runner/work/nilearn/nilearn/nilearn_data/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz

Extracting region signals

from nilearn.maskers import MultiNiftiMapsMasker

masker = MultiNiftiMapsMasker(
    msdl_atlas_dataset.maps,
    resampling_target="maps",
    detrend=True,
    high_variance_confounds=True,
    low_pass=None,
    high_pass=0.01,
    t_r=rest_dataset.t_r,
    standardize="zscore_sample",
    standardize_confounds=True,
    memory="nilearn_cache",
    memory_level=1,
    verbose=1,
)

func_filenames = rest_dataset.func
confound_filenames = rest_dataset.confounds

subject_time_series = masker.fit_transform(
    func_filenames, confounds=confound_filenames
)
[MultiNiftiMapsMasker.fit_transform] Loading data from [
 sub-pixar123_task-...,
         ...
 sub-pixar003_task-...,
]
[MultiNiftiMapsMasker.fit_transform] Loading regions from .../msdl_rois.nii
[MultiNiftiMapsMasker.fit_transform] Finished fit
________________________________________________________________________________
[Memory] Calling nilearn.image.image.high_variance_confounds...
high_variance_confounds('/home/runner/work/nilearn/nilearn/nilearn_data/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz')
__________________________________________high_variance_confounds - 0.4s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.image.image.high_variance_confounds...
high_variance_confounds('/home/runner/work/nilearn/nilearn/nilearn_data/development_fmri/sub-pixar001_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz')
__________________________________________high_variance_confounds - 0.4s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.image.image.high_variance_confounds...
high_variance_confounds('/home/runner/work/nilearn/nilearn/nilearn_data/development_fmri/sub-pixar002_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz')
__________________________________________high_variance_confounds - 0.4s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.image.image.high_variance_confounds...
high_variance_confounds('/home/runner/work/nilearn/nilearn/nilearn_data/development_fmri/sub-pixar003_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz')
__________________________________________high_variance_confounds - 0.4s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_maps_masker.NiftiMapsMasker.transform_single_imgs...
transform_single_imgs(imgs=<nibabel.nifti1.Nifti1Image object at 0x7fcf3faf5ed0>, confounds=array([[-0.000233, ..., -0.048779],
       ...,
       [-0.026896, ...,  0.155444]]), sample_mask=None)
/home/runner/work/nilearn/nilearn/examples/03_connectivity/plot_multi_subject_connectome.py:86: UserWarning:

Resampling images at transform time...
To avoid this warning, make sure to resample the images you want to transform to the shape of the maps or set resampling_target to 'data'.

________________________________________________________________________________
[Memory] Calling nilearn.maskers.base_masker.filter_and_extract...
filter_and_extract(<nibabel.nifti1.Nifti1Image object at 0x7fcf3faf5ed0>, <nilearn.maskers.nifti_maps_masker._ExtractionFunctor object at 0x7fcf36f93ed0>, { 'allow_overlap': True,
  'clean_args': None,
  'clean_kwargs': {},
  'cmap': 'CMRmap_r',
  'detrend': True,
  'dtype': None,
  'high_pass': 0.01,
  'high_variance_confounds': True,
  'low_pass': None,
  'maps_img': '/home/runner/work/nilearn/nilearn/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii',
  'mask_img': None,
  'reports': True,
  'smoothing_fwhm': None,
  'standardize': 'zscore_sample',
  'standardize_confounds': True,
  't_r': 2,
  'target_affine': array([[   4.,    0.,    0.,  -78.],
       [   0.,    4.,    0., -111.],
       [   0.,    0.,    4.,  -51.],
       [   0.,    0.,    0.,    1.]]),
  'target_shape': (40, 48, 35)}, confounds=array([[-0.000233, ..., -0.048779],
       ...,
       [-0.026896, ...,  0.155444]]), sample_mask=None, memory=Memory(location=nilearn_cache/joblib), memory_level=1, verbose=1, sklearn_output_config=None)
[MultiNiftiMapsMasker.fit_transform] Loading data from <nibabel.nifti1.Nifti1Image object at 0x7fcf3faf5ed0>
[MultiNiftiMapsMasker.fit_transform] Resampling images
[MultiNiftiMapsMasker.fit_transform] Extracting region signals
[MultiNiftiMapsMasker.fit_transform] Cleaning extracted signals
_______________________________________________filter_and_extract - 4.1s, 0.1min
____________________________________________transform_single_imgs - 4.2s, 0.1min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_maps_masker.NiftiMapsMasker.transform_single_imgs...
transform_single_imgs(imgs=<nibabel.nifti1.Nifti1Image object at 0x7fcf15024150>, confounds=array([[ 0.013422, ..., -0.057023],
       ...,
       [ 0.087146, ...,  0.102714]]), sample_mask=None)
/home/runner/work/nilearn/nilearn/examples/03_connectivity/plot_multi_subject_connectome.py:86: UserWarning:

Resampling images at transform time...
To avoid this warning, make sure to resample the images you want to transform to the shape of the maps or set resampling_target to 'data'.

________________________________________________________________________________
[Memory] Calling nilearn.maskers.base_masker.filter_and_extract...
filter_and_extract(<nibabel.nifti1.Nifti1Image object at 0x7fcf15024150>, <nilearn.maskers.nifti_maps_masker._ExtractionFunctor object at 0x7fcf2c59c0d0>, { 'allow_overlap': True,
  'clean_args': None,
  'clean_kwargs': {},
  'cmap': 'CMRmap_r',
  'detrend': True,
  'dtype': None,
  'high_pass': 0.01,
  'high_variance_confounds': True,
  'low_pass': None,
  'maps_img': '/home/runner/work/nilearn/nilearn/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii',
  'mask_img': None,
  'reports': True,
  'smoothing_fwhm': None,
  'standardize': 'zscore_sample',
  'standardize_confounds': True,
  't_r': 2,
  'target_affine': array([[   4.,    0.,    0.,  -78.],
       [   0.,    4.,    0., -111.],
       [   0.,    0.,    4.,  -51.],
       [   0.,    0.,    0.,    1.]]),
  'target_shape': (40, 48, 35)}, confounds=array([[ 0.013422, ..., -0.057023],
       ...,
       [ 0.087146, ...,  0.102714]]), sample_mask=None, memory=Memory(location=nilearn_cache/joblib), memory_level=1, verbose=1, sklearn_output_config=None)
[MultiNiftiMapsMasker.fit_transform] Loading data from <nibabel.nifti1.Nifti1Image object at 0x7fcf15024150>
[MultiNiftiMapsMasker.fit_transform] Resampling images
[MultiNiftiMapsMasker.fit_transform] Extracting region signals
[MultiNiftiMapsMasker.fit_transform] Cleaning extracted signals
_______________________________________________filter_and_extract - 4.1s, 0.1min
____________________________________________transform_single_imgs - 4.2s, 0.1min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_maps_masker.NiftiMapsMasker.transform_single_imgs...
transform_single_imgs(imgs=<nibabel.nifti1.Nifti1Image object at 0x7fcf15026a10>, confounds=array([[ 0.      , ..., -0.087084],
       ...,
       [ 0.00349 , ..., -0.02587 ]]), sample_mask=None)
/home/runner/work/nilearn/nilearn/examples/03_connectivity/plot_multi_subject_connectome.py:86: UserWarning:

Resampling images at transform time...
To avoid this warning, make sure to resample the images you want to transform to the shape of the maps or set resampling_target to 'data'.

________________________________________________________________________________
[Memory] Calling nilearn.maskers.base_masker.filter_and_extract...
filter_and_extract(<nibabel.nifti1.Nifti1Image object at 0x7fcf15026a10>, <nilearn.maskers.nifti_maps_masker._ExtractionFunctor object at 0x7fcf65c75650>, { 'allow_overlap': True,
  'clean_args': None,
  'clean_kwargs': {},
  'cmap': 'CMRmap_r',
  'detrend': True,
  'dtype': None,
  'high_pass': 0.01,
  'high_variance_confounds': True,
  'low_pass': None,
  'maps_img': '/home/runner/work/nilearn/nilearn/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii',
  'mask_img': None,
  'reports': True,
  'smoothing_fwhm': None,
  'standardize': 'zscore_sample',
  'standardize_confounds': True,
  't_r': 2,
  'target_affine': array([[   4.,    0.,    0.,  -78.],
       [   0.,    4.,    0., -111.],
       [   0.,    0.,    4.,  -51.],
       [   0.,    0.,    0.,    1.]]),
  'target_shape': (40, 48, 35)}, confounds=array([[ 0.      , ..., -0.087084],
       ...,
       [ 0.00349 , ..., -0.02587 ]]), sample_mask=None, memory=Memory(location=nilearn_cache/joblib), memory_level=1, verbose=1, sklearn_output_config=None)
[MultiNiftiMapsMasker.fit_transform] Loading data from <nibabel.nifti1.Nifti1Image object at 0x7fcf15026a10>
[MultiNiftiMapsMasker.fit_transform] Resampling images
[MultiNiftiMapsMasker.fit_transform] Extracting region signals
[MultiNiftiMapsMasker.fit_transform] Cleaning extracted signals
_______________________________________________filter_and_extract - 4.1s, 0.1min
____________________________________________transform_single_imgs - 4.2s, 0.1min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_maps_masker.NiftiMapsMasker.transform_single_imgs...
transform_single_imgs(imgs=<nibabel.nifti1.Nifti1Image object at 0x7fcf3faf5f50>, confounds=array([[ 6.680960e-06, ..., -6.231628e-02],
       ...,
       [ 2.379980e-02, ..., -2.286822e-02]]), sample_mask=None)
/home/runner/work/nilearn/nilearn/examples/03_connectivity/plot_multi_subject_connectome.py:86: UserWarning:

Resampling images at transform time...
To avoid this warning, make sure to resample the images you want to transform to the shape of the maps or set resampling_target to 'data'.

________________________________________________________________________________
[Memory] Calling nilearn.maskers.base_masker.filter_and_extract...
filter_and_extract(<nibabel.nifti1.Nifti1Image object at 0x7fcf3faf5f50>, <nilearn.maskers.nifti_maps_masker._ExtractionFunctor object at 0x7fcf65c75e50>, { 'allow_overlap': True,
  'clean_args': None,
  'clean_kwargs': {},
  'cmap': 'CMRmap_r',
  'detrend': True,
  'dtype': None,
  'high_pass': 0.01,
  'high_variance_confounds': True,
  'low_pass': None,
  'maps_img': '/home/runner/work/nilearn/nilearn/nilearn_data/msdl_atlas/MSDL_rois/msdl_rois.nii',
  'mask_img': None,
  'reports': True,
  'smoothing_fwhm': None,
  'standardize': 'zscore_sample',
  'standardize_confounds': True,
  't_r': 2,
  'target_affine': array([[   4.,    0.,    0.,  -78.],
       [   0.,    4.,    0., -111.],
       [   0.,    0.,    4.,  -51.],
       [   0.,    0.,    0.,    1.]]),
  'target_shape': (40, 48, 35)}, confounds=array([[ 6.680960e-06, ..., -6.231628e-02],
       ...,
       [ 2.379980e-02, ..., -2.286822e-02]]), sample_mask=None, memory=Memory(location=nilearn_cache/joblib), memory_level=1, verbose=1, sklearn_output_config=None)
[MultiNiftiMapsMasker.fit_transform] Loading data from <nibabel.nifti1.Nifti1Image object at 0x7fcf3faf5f50>
[MultiNiftiMapsMasker.fit_transform] Resampling images
[MultiNiftiMapsMasker.fit_transform] Extracting region signals
[MultiNiftiMapsMasker.fit_transform] Cleaning extracted signals
_______________________________________________filter_and_extract - 4.1s, 0.1min
____________________________________________transform_single_imgs - 4.2s, 0.1min

Computing group-sparse precision matrices

from nilearn.connectome import GroupSparseCovarianceCV

gsc = GroupSparseCovarianceCV(verbose=1)
gsc.fit(subject_time_series)
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    7.3s finished
[GroupSparseCovarianceCV.fit] [GroupSparseCovarianceCV] Done refinement  0 out of 4
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:   12.3s finished
[GroupSparseCovarianceCV.fit] [GroupSparseCovarianceCV] Done refinement  1 out of 4
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:   14.5s finished
[GroupSparseCovarianceCV.fit] [GroupSparseCovarianceCV] Done refinement  2 out of 4
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:   11.6s finished
[GroupSparseCovarianceCV.fit] [GroupSparseCovarianceCV] Done refinement  3 out of 4
[GroupSparseCovarianceCV.fit] Final optimization
GroupSparseCovarianceCV(verbose=1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


from sklearn.covariance import GraphicalLassoCV

gl = GraphicalLassoCV(verbose=True)
gl.fit(np.concatenate(subject_time_series))
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    0.4s finished
[GraphicalLassoCV] Done refinement  1 out of 4:   0s
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    0.5s finished
[GraphicalLassoCV] Done refinement  2 out of 4:   0s
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    0.6s finished
[GraphicalLassoCV] Done refinement  3 out of 4:   1s
[Parallel(n_jobs=1)]: Done   5 out of   5 | elapsed:    0.5s finished
[GraphicalLassoCV] Done refinement  4 out of 4:   2s
GraphicalLassoCV(verbose=True)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Displaying results

plot multi subject connectome
<nilearn.plotting.displays._projectors.LZRProjector object at 0x7fcf65998810>
plot_connectome(
    -gl.precision_,
    atlas_region_coords,
    edge_threshold="90%",
    title="Sparse inverse covariance (GraphicalLasso)",
    display_mode="lzr",
    edge_vmax=0.5,
    edge_vmin=-0.5,
)
plot_connectome(
    -gsc.precisions_[..., 0],
    atlas_region_coords,
    edge_threshold="90%",
    title="GroupSparseCovariance",
    display_mode="lzr",
    edge_vmax=0.5,
    edge_vmin=-0.5,
)

show()
  • plot multi subject connectome
  • plot multi subject connectome
plot_matrices(gl.covariance_, gl.precision_, "GraphicalLasso", labels)
plot_matrices(
    gsc.covariances_[..., 0],
    gsc.precisions_[..., 0],
    "GroupSparseCovariance",
    labels,
)

show()
  • GraphicalLasso / covariance
  • GraphicalLasso / precision
  • GroupSparseCovariance / covariance
  • GroupSparseCovariance / precision

Total running time of the script: (1 minutes 15.802 seconds)

Estimated memory usage: 1443 MB

Gallery generated by Sphinx-Gallery