Note
Go to the end to download the full example code. or to run this example in your browser via Binder
Deriving spatial maps from group fMRI data using ICA and Dictionary Learning¶
Various approaches exist to derive spatial maps or networks from group fmr data. The methods extract distributed brain regions that exhibit similar BOLD fluctuations over time. Decomposition methods allow for generation of many independent maps simultaneously without the need to provide a priori information (e.g. seeds or priors.)
This example will apply two popular decomposition methods, ICA and Dictionary learning, to fMRI data measured while children and young adults watch movies. The resulting maps will be visualized using atlas plotting tools.
CanICA is an ICA method for group-level analysis of fMRI data. Compared to other strategies, it brings a well-controlled group model, as well as a thresholding algorithm controlling for specificity and sensitivity with an explicit model of the signal.
The reference paper is Varoquaux et al.[1].
Load brain development fMRI dataset¶
from nilearn import datasets
rest_dataset = datasets.fetch_development_fmri(n_subjects=30)
func_filenames = rest_dataset.func # list of 4D nifti files for each subject
# print basic information on the dataset
print(f"First functional nifti image (4D) is at: {rest_dataset.func[0]}")
[get_dataset_dir] Dataset found in /home/runner/nilearn_data/development_fmri
[get_dataset_dir] Dataset found in
/home/runner/nilearn_data/development_fmri/development_fmri
[get_dataset_dir] Dataset found in
/home/runner/nilearn_data/development_fmri/development_fmri
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e14712b400183b7097/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e32286e80018c3e42c/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e4a743a9001760814f/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e54712b400183b70a5/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e52286e80018c3e439/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e72286e80017c41b3d/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e9a743a90017608158/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3e82286e80018c3e443/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3ea4712b400183b70b7/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3eb2286e80019c3c194/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff37e2286e80016c3c2cb/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3832286e80019c3c10f/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3822286e80018c3e37b/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff382a743a90018606df8/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3814712b4001a3b5561/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3832286e80016c3c2d1/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3842286e80017c419e0/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3854712b4001a3b5568/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb4702f39926900171090ee/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e8b353c58001c9abe98/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3872286e80017c419ea/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3872286e80017c419e9/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3884712b400183b7023/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3884712b400193b5b5c/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff389a743a9001660a016/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff38c2286e80016c3c2da/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff38ca743a90018606dfe/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff38ca743a9001760809e/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb47056353c58001c9ac064/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e5af2be3c001801f799/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb4703bf2be3c001801fa49/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e92a3bc970019f0717f/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff38c4712b4001a3b5573/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff38da743a900176080a2/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb47016a3bc970017efe44f/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e43f2be3c0017056b8a/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb470413992690018133d8c/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e9a353c58001c9abeac/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff38f2286e80018c3e38d/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3914712b4001a3b5579/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb4702a353c58001b9cb5ae/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e9b39926900190fad5c/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff391a743a900176080a9/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3914712b400173b5329/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb47023353c58001c9ac02b/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46eaa39926900160f69af/ ...
[fetch_single_file] ...done. (4 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3912286e80018c3e393/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5c8ff3952286e80017c41a1b/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb47045a3bc970019f073a0/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e913992690018133b1c/ ...
[fetch_single_file] ...done. (2 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb47052f2be3c0017057069/ ...
[fetch_single_file] ...done. (1 seconds, 0 min)
[fetch_single_file] Downloading data from
https://osf.io/download/5cb46e5c353c5800199ac79f/ ...
[fetch_single_file] ...done. (3 seconds, 0 min)
First functional nifti image (4D) is at: /home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz
Apply CanICA on the data¶
We use “whole-brain-template” as a strategy to compute the mask, as this leads to slightly faster and more reproducible results. However, the images need to be in MNI template space.
from nilearn.decomposition import CanICA
canica = CanICA(
n_components=20,
memory="nilearn_cache",
memory_level=2,
verbose=10,
mask_strategy="whole-brain-template",
random_state=0,
standardize="zscore_sample",
n_jobs=2,
)
canica.fit(func_filenames)
# Retrieve the independent components in brain space. Directly
# accessible through attribute `components_img_`.
canica_components_img = canica.components_img_
# components_img is a Nifti Image object, and can be saved to a file with
# the following lines:
from pathlib import Path
output_dir = Path.cwd() / "results" / "plot_compare_decomposition"
output_dir.mkdir(exist_ok=True, parents=True)
print(f"Output will be saved to: {output_dir}")
canica_components_img.to_filename(output_dir / "canica_resting_state.nii.gz")
[CanICA.fit] Loading data from
[/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar124_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar125_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar126_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar127_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar128_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar001_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar002_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar003_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar004_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar005_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar006_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar007_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar008_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar009_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar010_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar011_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar012_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar013_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar014_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar015_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar016_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar017_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar018_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar019_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar020_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar021_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar022_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar023_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar024_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz].
[CanICA.fit] Computing mask
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/joblib/memory.py:632: UserWarning:
Cannot inspect object functools.partial(<function compute_multi_brain_mask at 0x7f519aee29d0>, mask_type='whole-brain'), ignore list will not work.
[CanICA.fit] Template whole-brain mask computation
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/joblib/memory.py:810: UserWarning:
Cannot inspect object functools.partial(<function compute_multi_brain_mask at 0x7f519aee29d0>, mask_type='whole-brain'), ignore list will not work.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/joblib/memory.py:632: UserWarning:
Cannot inspect object functools.partial(<function compute_multi_brain_mask at 0x7f519aee29d0>, mask_type='whole-brain'), ignore list will not work.
[CanICA.fit] Resampling mask
[CanICA.fit] Loading data
________________________________________________________________________________
[Memory] Calling sklearn.utils.extmath.randomized_svd...
randomized_svd(array([[0.003659, ..., 0.013254],
...,
[0.012477, ..., 0.002881]]), n_components=20, transpose=True, random_state=0, n_iter=3)
___________________________________________________randomized_svd - 0.7s, 0.0min
[Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.
[Parallel(n_jobs=2)]: Done 1 tasks | elapsed: 4.8s
[Parallel(n_jobs=2)]: Done 4 tasks | elapsed: 9.5s
[Parallel(n_jobs=2)]: Done 10 out of 10 | elapsed: 21.9s finished
Output will be saved to: /home/runner/work/nilearn/nilearn/examples/03_connectivity/results/plot_compare_decomposition
To visualize we plot the outline of all components on one figure
from nilearn.plotting import plot_prob_atlas
# Plot all ICA components together
plot_prob_atlas(canica_components_img, title="All ICA components")
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_axes.py:74: UserWarning:
No contour levels were found within the data range.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_axes.py:74: UserWarning:
linewidths is ignored by contourf
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/numpy/ma/core.py:2826: UserWarning:
Warning: converting a masked element to nan.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/matplotlib/contour.py:1494: UserWarning:
Warning: converting a masked element to nan.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/matplotlib/contour.py:1495: UserWarning:
Warning: converting a masked element to nan.
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7f518934d250>
Finally, we plot the map for each ICA component separately
from nilearn.image import iter_img
from nilearn.plotting import plot_stat_map, show
for i, cur_img in enumerate(iter_img(canica_components_img)):
plot_stat_map(
cur_img,
display_mode="z",
title=f"IC {int(i)}",
cut_coords=1,
colorbar=False,
)
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_slicers.py:1523: MatplotlibDeprecationWarning:
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
Compare CanICA to dictionary learning¶
Dictionary learning is a sparsity based decomposition method for extracting spatial maps. It extracts maps that are naturally sparse and usually cleaner than ICA. Here, we will compare networks built with CanICA to networks built with Dictionary learning.
For more detailse see Mensch et al.[2].
Create a dictionary learning estimator
from nilearn.decomposition import DictLearning
dict_learning = DictLearning(
n_components=20,
memory="nilearn_cache",
memory_level=2,
verbose=1,
random_state=0,
n_epochs=1,
mask_strategy="whole-brain-template",
standardize="zscore_sample",
n_jobs=2,
)
print("[Example] Fitting dictionary learning model")
dict_learning.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.
dictlearning_components_img = dict_learning.components_img_
dictlearning_components_img.to_filename(
output_dir / "dictionary_learning_resting_state.nii.gz"
)
[Example] Fitting dictionary learning model
[DictLearning.fit] Loading data from
[/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar123_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar124_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar125_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar126_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar127_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar128_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar001_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar002_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar003_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar004_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar005_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar006_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar007_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar008_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar009_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar010_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar011_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar012_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar013_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar014_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar015_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar016_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar017_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar018_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar019_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar020_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar021_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar022_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar023_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,
/home/runner/nilearn_data/development_fmri/development_fmri/sub-pixar024_task-p
ixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz].
[DictLearning.fit] Computing mask
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/joblib/memory.py:632: UserWarning:
Cannot inspect object functools.partial(<function compute_multi_brain_mask at 0x7f519aee29d0>, mask_type='whole-brain'), ignore list will not work.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/joblib/memory.py:810: UserWarning:
Cannot inspect object functools.partial(<function compute_multi_brain_mask at 0x7f519aee29d0>, mask_type='whole-brain'), ignore list will not work.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/joblib/memory.py:632: UserWarning:
Cannot inspect object functools.partial(<function compute_multi_brain_mask at 0x7f519aee29d0>, mask_type='whole-brain'), ignore list will not work.
[DictLearning.fit] Resampling mask
[DictLearning.fit] Loading data
[DictLearning.fit] Learning initial components
________________________________________________________________________________
[Memory] Calling sklearn.utils.extmath.randomized_svd...
randomized_svd(array([[-0.001315, ..., 0.004387],
...,
[ 0.011243, ..., 0.004194]]), n_components=20, transpose=True, random_state=0, n_iter=3)
___________________________________________________randomized_svd - 0.7s, 0.0min
[Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.
[Parallel(n_jobs=2)]: Done 1 out of 1 | elapsed: 0.3s finished
[DictLearning.fit] Computing initial loadings
________________________________________________________________________________
[Memory] Calling nilearn.decomposition.dict_learning._compute_loadings...
_compute_loadings(array([[ 0.006711, ..., 0.00316 ],
...,
[-0.002644, ..., -0.002689]]),
array([[-0.622651, ..., 5.322742],
...,
[ 0.777205, ..., 0.743122]]))
_________________________________________________compute_loadings - 0.2s, 0.0min
[DictLearning.fit] Learning dictionary
________________________________________________________________________________
[Memory] Calling sklearn.decomposition._dict_learning.dict_learning_online...
dict_learning_online(array([[-0.622651, ..., 0.777205],
...,
[ 5.322742, ..., 0.743122]]),
20, alpha=10, batch_size=20, method='cd', dict_init=array([[0.041336, ..., 0.477099],
...,
[0.579066, ..., 0.219633]]), verbose=0, random_state=0, return_code=True, shuffle=True, n_jobs=1, max_iter=1090)
_____________________________________________dict_learning_online - 2.9s, 0.0min
[Example] Saving results
Visualize the results
First plot all DictLearning components together
plot_prob_atlas(
dictlearning_components_img, title="All DictLearning components"
)
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_axes.py:74: UserWarning:
No contour levels were found within the data range.
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_axes.py:74: UserWarning:
linewidths is ignored by contourf
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7f51b1fd6760>
One plot of each component
for i, cur_img in enumerate(iter_img(dictlearning_components_img)):
plot_stat_map(
cur_img,
display_mode="z",
title=f"Comp {int(i)}",
cut_coords=1,
colorbar=False,
)
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/plotting/displays/_slicers.py:1523: MatplotlibDeprecationWarning:
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
Estimate explained variance per component and plot using matplotlib
The fitted object dict_learning can be used to calculate the score per component
scores = dict_learning.score(func_filenames, per_component=True)
# Plot the scores
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import FormatStrFormatter
plt.figure(figsize=(4, 4), constrained_layout=True)
positions = np.arange(len(scores))
plt.barh(positions, scores)
plt.ylabel("Component #", size=12)
plt.xlabel("Explained variance", size=12)
plt.yticks(np.arange(20))
plt.gca().xaxis.set_major_formatter(FormatStrFormatter("%.3f"))
show()
________________________________________________________________________________
[Memory] Calling nilearn.decomposition._base._explained_variance...
_explained_variance(array([[-6.227098e-01, ..., 5.322679e+00],
...,
[-2.975076e-17, ..., -4.364282e-16]]),
array([[0., ..., 0.],
...,
[0., ..., 0.]]), per_component=True)
/home/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/decomposition/_base.py:552: UserWarning:
Persisting input arguments took 1.30s 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.
______________________________________________explained_variance - 17.9s, 0.3min
Note
To see how to extract subject-level timeseries’ from regions created using Dictionary learning, see example Regions extraction using dictionary learning and functional connectomes. # noqa: E501
References¶
Total running time of the script: (4 minutes 42.761 seconds)
Estimated memory usage: 3150 MB