NeuroVault cross-study ICA maps#

This example shows how to download statistical maps from NeuroVault, label them with NeuroSynth terms, and compute ICA components across all the maps.

See nilearn.datasets.fetch_neurovault documentation for more details.

Note

If you are using Nilearn with a version older than 0.9.0, then you should either upgrade your version or import maskers from the input_data module instead of the maskers module.

That is, you should manually replace in the following example all occurrences of:

from nilearn.maskers import NiftiMasker

with:

from nilearn.input_data import NiftiMasker
import warnings

import numpy as np
from scipy import stats
from sklearn.decomposition import FastICA

from nilearn import plotting
from nilearn.datasets import fetch_neurovault, load_mni152_brain_mask
from nilearn.image import smooth_img
from nilearn.maskers import NiftiMasker

Get image and term data#

# Download images
# Here by default we only download 80 images to save time,
# but for better results I recommend using at least 200.
print(
    "Fetching Neurovault images; "
    "if you haven't downloaded any Neurovault data before "
    "this will take several minutes."
)
nv_data = fetch_neurovault(max_images=30, fetch_neurosynth_words=True)

images = nv_data["images"]
term_weights = nv_data["word_frequencies"]
vocabulary = nv_data["vocabulary"]
if term_weights is None:
    term_weights = np.ones((len(images), 2))
    vocabulary = np.asarray(["Neurosynth is down", "Please try again later"])

# Clean and report term scores
term_weights[term_weights < 0] = 0
total_scores = np.mean(term_weights, axis=0)

print("\nTop 10 neurosynth terms from downloaded images:\n")

for term_idx in np.argsort(total_scores)[-10:][::-1]:
    print(vocabulary[term_idx])
Fetching Neurovault images; if you haven't downloaded any Neurovault data before this will take several minutes.
Reading local neurovault data.
Downloading file: https://neurosynth.org/api/decode/?neurovault=3042
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_98/neurosynth_words_for_image_3042.json
Already fetched 1 image
Downloading file: https://neurosynth.org/api/decode/?neurovault=2676
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_413/neurosynth_words_for_image_2676.json
Already fetched 2 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=2675
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_413/neurosynth_words_for_image_2675.json
Already fetched 3 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=32980
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1952/neurosynth_words_for_image_32980.json
Already fetched 4 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=2818
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_423/neurosynth_words_for_image_2818.json
Already fetched 5 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=2834
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_423/neurosynth_words_for_image_2834.json
Already fetched 6 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=3041
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_42/neurosynth_words_for_image_3041.json
Already fetched 7 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=151
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_42/neurosynth_words_for_image_151.json
Already fetched 8 images
Downloading file: https://neurosynth.org/api/decode/?neurovault=10426
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_658/neurosynth_words_for_image_10426.json
Already fetched 9 images
9 images found on local disk.
Reading server neurovault data.
Scroll collections: getting new batch: https://neurovault.org/api/collections/?limit=100&offset=0
Scroll collections: batch size: 100
Scroll images from collection 921: getting new batch: https://neurovault.org/api/collections/921/images/?limit=100&offset=0
Scroll images from collection 921: batch size: 1
On neurovault.org: no imagematched query in collection 921
Scroll images from collection 440: getting new batch: https://neurovault.org/api/collections/440/images/?limit=100&offset=0
Scroll images from collection 440: batch size: 2
On neurovault.org: no imagematched query in collection 440
Scroll images from collection 496: getting new batch: https://neurovault.org/api/collections/496/images/?limit=100&offset=0
Scroll images from collection 496: batch size: 1
On neurovault.org: no imagematched query in collection 496
Scroll images from collection 4008: getting new batch: https://neurovault.org/api/collections/4008/images/?limit=100&offset=0
Scroll images from collection 4008: batch size: 1
Downloading file: http://neurovault.org/media/images/4008/spmT_0001.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_4008/image_65426.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=65426
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_4008/neurosynth_words_for_image_65426.json
Already fetched 10 images
On neurovault.org: 1 imagematched query in collection 4008
Scroll images from collection 4: getting new batch: https://neurovault.org/api/collections/4/images/?limit=100&offset=0
Scroll images from collection 4: batch size: 1
On neurovault.org: no imagematched query in collection 4
Scroll images from collection 79: getting new batch: https://neurovault.org/api/collections/79/images/?limit=100&offset=0
Scroll images from collection 79: batch size: 5
Downloading file: http://neurovault.org/media/images/79/neurosynth_spectral_01.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_79/image_11020.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=11020
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_79/neurosynth_words_for_image_11020.json
Already fetched 11 images
Downloading file: http://neurovault.org/media/images/79/neurosynth_spectral_01_1.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_79/image_11021.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=11021
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_79/neurosynth_words_for_image_11021.json
Already fetched 12 images
On neurovault.org: 2 imagesmatched query in collection 79
Scroll images from collection 1257: getting new batch: https://neurovault.org/api/collections/1257/images/?limit=100&offset=0
Scroll images from collection 1257: batch size: 100
Downloading file: http://neurovault.org/media/images/1257/abstract_knowledge_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17327.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17327
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17327.json
Already fetched 13 images
Downloading file: http://neurovault.org/media/images/1257/acoustic_processing_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17328.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17328
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17328.json
Already fetched 14 images
Downloading file: http://neurovault.org/media/images/1257/action_perception_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17329.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17329
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17329.json
Already fetched 15 images
Downloading file: http://neurovault.org/media/images/1257/action_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17330.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17330
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17330.json
Already fetched 16 images
Downloading file: http://neurovault.org/media/images/1257/activation_level_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17331.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17331
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17331.json
Already fetched 17 images
Downloading file: http://neurovault.org/media/images/1257/activation_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17332.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17332
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17332.json
Already fetched 18 images
Downloading file: http://neurovault.org/media/images/1257/active_maintenance_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17333.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17333
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17333.json
Already fetched 19 images
Downloading file: http://neurovault.org/media/images/1257/active_retrieval_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17334.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17334
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17334.json
Already fetched 20 images
Downloading file: http://neurovault.org/media/images/1257/acuity_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17335.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17335
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17335.json
Already fetched 21 images
Downloading file: http://neurovault.org/media/images/1257/adaptation_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17336.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17336
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17336.json
Already fetched 22 images
Downloading file: http://neurovault.org/media/images/1257/adaptive_control_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17337.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17337
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17337.json
Already fetched 23 images
Downloading file: http://neurovault.org/media/images/1257/addiction_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17338.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17338
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17338.json
Already fetched 24 images
Downloading file: http://neurovault.org/media/images/1257/affect_perception_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17339.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17339
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17339.json
Already fetched 25 images
Downloading file: http://neurovault.org/media/images/1257/affect_recognition_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17340.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17340
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17340.json
Already fetched 26 images
Downloading file: http://neurovault.org/media/images/1257/agency_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17341.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17341
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17341.json
Already fetched 27 images
Downloading file: http://neurovault.org/media/images/1257/agreeableness_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17342.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17342
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17342.json
Already fetched 28 images
Downloading file: http://neurovault.org/media/images/1257/altruism_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17343.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17343
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17343.json
Already fetched 29 images
Downloading file: http://neurovault.org/media/images/1257/altruistic_motivation_regparam.nii.gz
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/image_17344.nii.gz
Downloading file: https://neurosynth.org/api/decode/?neurovault=17344
Download succeeded, downloaded to: /home/runner/work/nilearn/nilearn/nilearn_data/neurovault/collection_1257/neurosynth_words_for_image_17344.json
Already fetched 30 images
Computing word features.
Computing word features done; vocabulary size: 1307

Top 10 neurosynth terms from downloaded images:

superior temporal
auditory
task
planum temporale
temporale
planum
superior
anterior insula
parietal
posterior superior

Reshape and mask images#

print("\nReshaping and masking images.\n")

with warnings.catch_warnings():
    warnings.simplefilter("ignore", UserWarning)
    warnings.simplefilter("ignore", DeprecationWarning)

    mask_img = load_mni152_brain_mask(resolution=2)
    masker = NiftiMasker(
        mask_img=mask_img, memory="nilearn_cache", memory_level=1
    )
    masker = masker.fit()

    # Images may fail to be transformed, and are of different shapes,
    # so we need to transform one-by-one and keep track of failures.
    X = []
    is_usable = np.ones((len(images),), dtype=bool)

    for index, image_path in enumerate(images):
        # load image and remove nan and inf values.
        # applying smooth_img to an image with fwhm=None simply cleans up
        # non-finite values but otherwise doesn't modify the image.
        image = smooth_img(image_path, fwhm=None)
        try:
            X.append(masker.transform(image))
        except Exception as e:
            meta = nv_data["images_meta"][index]
            print(
                f"Failed to mask/reshape image: id: {meta.get('id')}; "
                f"name: '{meta.get('name')}'; "
                f"collection: {meta.get('collection_id')}; error: {e}"
            )
            is_usable[index] = False

# Now reshape list into 2D matrix, and remove failed images from terms
X = np.vstack(X)
term_weights = term_weights[is_usable, :]
Reshaping and masking images.

Run ICA and map components to terms#

print("Running ICA; may take time...")
# We use a very small number of components as we have downloaded only 80
# images. For better results, increase the number of images downloaded
# and the number of components
n_components = 8
fast_ica = FastICA(n_components=n_components, random_state=0)
ica_maps = fast_ica.fit_transform(X.T).T

term_weights_for_components = np.dot(fast_ica.components_, term_weights)
print("Done, plotting results.")
Running ICA; may take time...
Done, plotting results.

Generate figures#

with warnings.catch_warnings():
    warnings.simplefilter("ignore", DeprecationWarning)

    for index, (ic_map, ic_terms) in enumerate(
        zip(ica_maps, term_weights_for_components)
    ):
        if -ic_map.min() > ic_map.max():
            # Flip the map's sign for prettiness
            ic_map = -ic_map
            ic_terms = -ic_terms

        ic_threshold = stats.scoreatpercentile(np.abs(ic_map), 90)
        ic_img = masker.inverse_transform(ic_map)
        important_terms = vocabulary[np.argsort(ic_terms)[-3:]]
        title = f"IC{int(index)}  {', '.join(important_terms[::-1])}"

        plotting.plot_stat_map(
            ic_img, threshold=ic_threshold, colorbar=False, title=title
        )
  • plot ica neurovault
  • plot ica neurovault
  • plot ica neurovault
  • plot ica neurovault
  • plot ica neurovault
  • plot ica neurovault
  • plot ica neurovault
  • plot ica neurovault

As we can see, some of the components capture cognitive or neurological maps, while other capture noise in the database. More data, better filtering, and better cognitive labels would give better maps

# Done.
plotting.show()

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

Estimated memory usage: 237 MB

Gallery generated by Sphinx-Gallery