8.4.7. Dictionary Learning and ICA for doing group analysis of resting-state fMRI

This example applies dictionary learning and ICA to resting-state data, visualizing resulting components using atlas plotting tools.

Dictionary learning is a sparsity based decomposition method for extracting spatial maps. It extracts maps that are naturally sparse and usually cleaner than ICA

Note

The use of the attribute components_img_ from decomposition estimators is implemented from version 0.4.1. For older versions, unmask the deprecated attribute components_ to get the components image using attribute masker_ embedded in estimator. See the section Inverse transform: unmasking data.

8.4.7.1. Load ADHD rest dataset

from nilearn import datasets

adhd_dataset = datasets.fetch_adhd(n_subjects=30)
func_filenames = adhd_dataset.func  # list of 4D nifti files for each subject

# print basic information on the dataset
print('First functional nifti image (4D) is at: %s' %
      adhd_dataset.func[0])  # 4D data

Out:

First functional nifti image (4D) is at: /home/kamalakar/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz

8.4.7.2. Create two decomposition estimators

from nilearn.decomposition import DictLearning, CanICA

n_components = 40

8.4.7.3. Dictionary learning

dict_learning = DictLearning(n_components=n_components,
                             memory="nilearn_cache", memory_level=2,
                             verbose=1,
                             random_state=0,
                             n_epochs=1)

8.4.7.4. CanICA

canica = CanICA(n_components=n_components,
                memory="nilearn_cache", memory_level=2,
                threshold=3.,
                n_init=1,
                verbose=1)

8.4.7.5. Fit both estimators

estimators = [dict_learning, canica]
names = {dict_learning: 'DictionaryLearning', canica: 'CanICA'}
components_imgs = []

for estimator in estimators:
    print('[Example] Learning maps using %s model' % names[estimator])
    estimator.fit(func_filenames)
    print('[Example] Saving results')
    # Grab extracted components umasked back to Nifti image.
    # Note: For older versions, less than 0.4.1. components_img_
    # is not implemented. See Note section above for details.
    components_img = estimator.components_img_
    components_img.to_filename('%s_resting_state.nii.gz' %
                               names[estimator])
    components_imgs.append(components_img)

Out:

[Example] Learning maps using DictionaryLearning model
[MultiNiftiMasker.fit] Loading data from [/home/kamalakar/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz, /home/kamalakar/nilearn_data/adhd/data/0010064/0010064_rest_tshift_RPI_voreg_mni.nii.gz, /home/kamalakar/nilea
[MultiNiftiMasker.fit] Computing mask
[MultiNiftiMasker.transform] Resampling mask
[DictLearning] Loading data
[DictLearning] Learning initial components
________________________________________________________________________________
[Memory] Calling sklearn.decomposition.fastica_.fastica...
fastica(array([[ 0.002635, ..., -0.000921],
       ...,
       [ 0.003435, ..., -0.001996]]), fun='cube', random_state=209652396, whiten=True)
_________________________________________________________fastica - 15.6s, 0.3min
[DictLearning] Computing initial loadings
________________________________________________________________________________
[Memory] Calling nilearn.decomposition.dict_learning._compute_loadings...
_compute_loadings(array([[ 0.005129, ...,  0.00071 ],
       ...,
       [-0.00015 , ...,  0.000482]], dtype=float32),
array([[ 2.036888, ...,  2.190327],
       ...,
       [ 0.777675, ..., -0.215321]], dtype=float32))
_________________________________________________compute_loadings - 0.9s, 0.0min
[DictLearning] Learning dictionary
________________________________________________________________________________
[Memory] Calling sklearn.decomposition.dict_learning.dict_learning_online...
dict_learning_online(array([[ 2.036888, ...,  0.777675],
       ...,
       [ 2.190327, ..., -0.215321]], dtype=float32),
40, n_iter=3213, n_jobs=1, shuffle=True, verbose=0, return_code=True, batch_size=20, dict_init=array([[-0.221039, ..., -0.253061],
       ...,
       [ 0.237223, ..., -0.051644]], dtype=float32), random_state=0, alpha=10, method='cd')
____________________________________________dict_learning_online - 38.7s, 0.6min
[Example] Saving results
[Example] Learning maps using CanICA model
[MultiNiftiMasker.fit] Loading data from [/home/kamalakar/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz, /home/kamalakar/nilearn_data/adhd/data/0010064/0010064_rest_tshift_RPI_voreg_mni.nii.gz, /home/kamalakar/nilea
[MultiNiftiMasker.fit] Computing mask
[MultiNiftiMasker.transform] Resampling mask
[CanICA] Loading data
________________________________________________________________________________
[Memory] Calling sklearn.utils.extmath.randomized_svd...
randomized_svd(array([[ 0.002997, ..., -0.005745],
       ...,
       [ 0.002863, ...,  0.00074 ]], dtype=float32), n_iter=3, random_state=None, transpose=True, n_components=40)
___________________________________________________randomized_svd - 3.5s, 0.1min
________________________________________________________________________________
[Memory] Calling sklearn.decomposition.fastica_.fastica...
fastica(array([[ 0.002969, ..., -0.002977],
       ...,
       [ 0.00354 , ..., -0.002544]]), fun='cube', random_state=907826566, whiten=True)
__________________________________________________________fastica - 6.5s, 0.1min
[Example] Saving results

8.4.7.6. Visualize the results

from nilearn.plotting import (plot_prob_atlas, find_xyz_cut_coords, show,
                              plot_stat_map)
from nilearn.image import index_img

# Selecting specific maps to display: maps were manually chosen to be similar
indices = {dict_learning: 1, canica: 31}
# We select relevant cut coordinates for displaying
cut_component = index_img(components_imgs[0], indices[dict_learning])
cut_coords = find_xyz_cut_coords(cut_component)
for estimator, components in zip(estimators, components_imgs):
    # 4D plotting
    plot_prob_atlas(components, view_type="filled_contours",
                    title="%s" % names[estimator],
                    cut_coords=cut_coords, colorbar=False)
    # 3D plotting
    plot_stat_map(index_img(components, indices[estimator]),
                  title="%s" % names[estimator],
                  cut_coords=cut_coords, colorbar=False)
show()
  • ../../_images/sphx_glr_plot_compare_resting_state_decomposition_001.png
  • ../../_images/sphx_glr_plot_compare_resting_state_decomposition_002.png
  • ../../_images/sphx_glr_plot_compare_resting_state_decomposition_003.png
  • ../../_images/sphx_glr_plot_compare_resting_state_decomposition_004.png

Total running time of the script: ( 2 minutes 25.230 seconds)

Generated by Sphinx-Gallery