Statistical map viewer

Generate a brainsprite viewer for an activation map with an anatomical background, by populating an html template.

First fetch the MNI high-resolution template and a functional statistical map.

from nilearn import datasets

anat = datasets.MNI152_FILE_PATH

# one motor contrast map from NeuroVault
motor_images = datasets.fetch_neurovault_motor_task()
stat_img = motor_images.images[0]
/home/runner/work/brainsprite/brainsprite/examples/plot_stat_map.py:15: DeprecationWarning: The 'fetch_neurovault_motor_task' function will be removed in version>0.13.1 as it returns the same data as 'load_sample_motor_activation_image'.
Please use 'load_sample_motor_activation_image' instead.'
  motor_images = datasets.fetch_neurovault_motor_task()
[fetch_neurovault_motor_task] Dataset created in /home/runner/nilearn_data/neurovault

We are going to use the same template and instruction as in the plot_anat tutorial. The defaults are set for a functional map, so there is not much to do. We still tweak a couple parameters to get a clean map:

  • apply a threshold to get rid of small activation (threshold),

  • reduce the opacity of the overlay to see the underlying anatomy (opacity)

  • Put a title inside the figure (title)

  • manually specify the cut coordinates (cut_coords)

from brainsprite import viewer_substitute

bsprite = viewer_substitute(
    threshold=3,
    opacity=0.5,
    title="plot_stat_map",
    cut_coords=[36, -27, 66],
    radiological=True,
)
bsprite.fit(stat_img, bg_img=anat)
/home/runner/work/brainsprite/brainsprite/.tox/doc/lib/python3.9/site-packages/brainsprite/brainsprite.py:143: FutureWarning: From release 0.13.0 onwards, this function will, by default, copy the header of the input image to the output. Currently, the header is reset to the default Nifti1Header. To suppress this warning and use the new behavior, set `copy_header=True`.
  stat_map_img = resample_to_img(
/home/runner/work/brainsprite/brainsprite/.tox/doc/lib/python3.9/site-packages/brainsprite/brainsprite.py:149: FutureWarning: From release 0.13.0 onwards, this function will, by default, copy the header of the input image to the output. Currently, the header is reset to the default Nifti1Header. To suppress this warning and use the new behavior, set `copy_header=True`.
  mask_img = resample_to_img(

We can now open the template with tempita, and fill it with the required information. The parameters indicate which tempita names we used in the template for the javascript, html and library code, respectively.

from pathlib import Path

import tempita

examples_dir = Path.cwd()
if examples_dir.name != "examples":
    examples_dir = examples_dir / "examples"
file_template = examples_dir / "viewer_template.html"
template = tempita.Template.from_filename(file_template, encoding="utf-8")

viewer = bsprite.transform(template, javascript="js", html="html", library="bsprite")

# In a Jupyter notebook, if ``view`` is the output of a cell, it will
# be displayed below the cell
viewer


The following instruction can be used to save the viewer in a stand-alone, html document:

viewer.save_as_html(examples_dir / "plot_stat_map.html")

There are a lot more control one can use to modify the appearance of the brain viewer. Check the Python API for more information.

Total running time of the script: (0 minutes 4.310 seconds)

Gallery generated by Sphinx-Gallery