9.3.9. Decoding of a dataset after GLM fit for signal extraction

Full step-by-step example of fitting a GLM to perform a decoding experiment. We use the data from one subject of the Haxby dataset.

More specifically:

  1. Download the Haxby dataset.
  2. Extract the information to generate a glm representing the blocks of stimuli.
  3. Analyze the decoding performance using a classifier.

To run this example, you must launch IPython via ipython --matplotlib in a terminal, or use the Jupyter notebook.

9.3.9.1. Fetch example Haxby dataset

We download the Haxby dataset This is a study of visual object category representation

# By default 2nd subject will be fetched
import numpy as np
import pandas as pd
from nilearn import datasets
haxby_dataset = datasets.fetch_haxby()

# repetition has to be known
TR = 2.5

9.3.9.2. Load the behavioral data

# Load target information as string and give a numerical identifier to each
behavioral = pd.read_csv(haxby_dataset.session_target[0], sep=' ')
conditions = behavioral['labels'].values

# Record these as an array of sessions
sessions = behavioral['chunks'].values
unique_sessions = behavioral['chunks'].unique()

# fMRI data: a unique file for each session
func_filename = haxby_dataset.func[0]

9.3.9.3. Build a proper event structure for each session

events = {}
# events will take  the form of a dictionary of Dataframes, one per session
for session in unique_sessions:
    # get the condition label per session
    conditions_session = conditions[sessions == session]
    # get the number of scans per session, then the corresponding
    # vector of frame times
    n_scans = len(conditions_session)
    frame_times = TR * np.arange(n_scans)
    # each event last the full TR
    duration = TR * np.ones(n_scans)
    # Define the events object
    events_ = pd.DataFrame(
        {'onset': frame_times, 'trial_type': conditions_session, 'duration': duration})
    # remove the rest condition and insert into the dictionary
    events[session] = events_[events_.trial_type != 'rest']

9.3.9.4. Instantiate and run FirstLevelModel

We generate a list of z-maps together with their session and condition index

z_maps = []
conditions_label = []
session_label = []

# Instantiate the glm
from nilearn.glm.first_level import FirstLevelModel
glm = FirstLevelModel(t_r=TR,
                      mask_img=haxby_dataset.mask,
                      high_pass=.008,
                      smoothing_fwhm=4,
                      memory='nilearn_cache')

9.3.9.5. Run the glm on data from each session

events[session].trial_type.unique()
from nilearn.image import index_img
for session in unique_sessions:
    # grab the fmri data for that particular session
    fmri_session = index_img(func_filename, sessions == session)

    # fit the glm
    glm.fit(fmri_session, events=events[session])

    # set up contrasts: one per condition
    conditions = events[session].trial_type.unique()
    for condition_ in conditions:
        z_maps.append(glm.compute_contrast(condition_))
        conditions_label.append(condition_)
        session_label.append(session)

Out:

________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-0.114769, ..., -2.149296],
       ...,
       [ 2.367151, ...,  0.779998]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.256013, ...,  0.308334]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.038572, ...,  0.855077]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.070285, ..., -1.222001]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.1724  , ...,  0.033508]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.96724 , ..., -1.474856]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.432136, ..., -1.197805]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.420626, ..., -0.443207]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.310409, ..., 0.196496]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb7400d0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfc2174c0>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[ 12.660587, ..., -13.536042],
       ...,
       [ -3.254408, ..., -33.842804]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.705774, ..., -0.934083]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.476069, ..., -1.29404 ]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.210752, ..., -1.441079]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.341707, ...,  2.094528]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.097247, ..., -0.496306]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.118812, ...,  1.58503 ]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.801345, ..., -1.648133]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.621172, ..., -0.678871]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb740190>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bf7e41c40>, <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[ 5.205584, ..., 26.587189],
       ...,
       [-6.836576, ..., 10.676956]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.733742, ..., -0.444561]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.502666, ..., 1.811852]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.690828, ...,  0.730153]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.403789, ...,  0.254288]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.472767, ..., -1.174186]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.07507 , ..., -2.221482]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.445359, ..., -0.363018]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.034083, ..., 0.362042]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7e5e4f0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfb50fdf0>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-2.026206, ...,  5.974948],
       ...,
       [ 2.616334, ...,  0.104535]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.596132, ..., -1.910225]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.217544, ..., -0.003551]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.337751, ..., 0.789979]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.161412, ..., -0.265537]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.538844, ..., -0.797649]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.589617, ..., -0.790328]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.306331, ..., -0.003053]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.20477 , ..., -0.804061]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5df0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfbf5e970>, <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[ 53.033577, ..., -55.45955 ],
       ...,
       [-51.57195 , ..., -55.994713]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.161983, ..., -0.068078]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.467058, ..., -0.494102]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.008358, ..., 0.250096]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.127065, ..., -0.241985]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.066426, ..., 0.493324]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.049242, ..., -1.008997]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.336253, ..., -0.596381]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.707037, ..., 1.358865]), <nibabel.nifti1.Nifti1Image object at 0x7f8bdff5cf40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e5d60>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-27.150482, ...,  -5.81308 ],
       ...,
       [-30.204891, ...,   7.417917]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.18641 , ...,  1.581644]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.553131, ...,  0.392603]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.960821, ...,  1.15444 ]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.7086  , ...,  1.094486]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.26815 , ...,  0.542739]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.321163, ..., -0.019244]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.288938, ..., -0.355821]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.222105, ..., -0.420629]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfbcfd100>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfc2177c0>, <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 1.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[129.51173 , ..., -15.279282],
       ...,
       [-18.911755, ...,  21.839058]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.490124, ..., -0.665442]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.374685, ..., -0.980248]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.654133, ..., -0.288692]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.244312, ..., -1.462072]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.665202, ...,  1.793466]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.036326, ..., 0.693956]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.672634, ..., -0.24818 ]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.099764, ..., -1.722951]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f1e8b0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfb42d8b0>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-15.915996, ...,  22.07737 ],
       ...,
       [-16.981215, ...,   3.372383]], dtype=float32),
array([[ 0.      , ...,  1.      ],
       ...,
       [-0.259023, ...,  1.      ]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.033872, ..., -0.317176]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.98478 , ..., -0.770334]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.127461, ..., 0.929068]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.489347, ..., -0.230229]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.673052, ...,  0.6757  ]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.260282, ..., -0.346342]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.775541, ..., 1.603123]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([3.391727, ..., 0.653312]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfb50ff40>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfc553430>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-0.292987, ..., 18.392956],
       ...,
       [-3.935719, ...,  0.602484]], dtype=float32),
array([[ 0.      , ...,  1.      ],
       ...,
       [-0.259023, ...,  1.      ]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.620911, ...,  2.309993]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.44548 , ..., 0.576334]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.483355, ..., -0.068969]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.166576, ..., 0.713549]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.612744, ..., 1.441012]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.254934, ..., -1.288018]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.134169, ..., -0.621367]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.234171, ..., -1.497943]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf430>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfc6e3e50>, <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 1.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-5.223948, ..., -5.959582],
       ...,
       [-7.677519, ..., 16.024363]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.01344 , ...,  1.283233]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.79114 , ..., 1.031069]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.583575, ..., 0.488828]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.265741, ..., -0.623721]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.20672, ...,  1.09668]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.045178, ...,  0.822339]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.463833, ..., -0.151504]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.230311, ...,  0.198537]), <nibabel.nifti1.Nifti1Image object at 0x7f8bf7f7b280>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bfc5bf550>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-19.66533 , ...,  -6.299562],
       ...,
       [-24.647343, ...,   2.331865]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.080165, ..., -3.497239]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-3.083184, ..., -3.071235]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.724401, ..., -2.892927]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.044506, ..., 0.285176]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.257372, ..., 1.802962]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.485481, ...,  0.284696]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.032135, ..., -1.290339]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.417132, ..., -1.258094]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc5534c0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f8bf7ea56a0>, <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>, { 'detrend': False,
  'dtype': None,
  'high_pass': None,
  'low_pass': None,
  'reports': True,
  'sample_mask': None,
  'sessions': None,
  'smoothing_fwhm': 4,
  'standardize': False,
  't_r': 2.5,
  'target_affine': None,
  'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.9s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.glm.first_level.first_level.run_glm...
run_glm(array([[-1.095605, ..., 16.449202],
       ...,
       [ 2.59974 , ..., -2.179998]], dtype=float32),
array([[0., ..., 1.],
       ...,
       [0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 0.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.545625, ..., -1.041515]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.152042, ..., 1.145641]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.55236 , ..., 0.696758]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.684509, ...,  0.226524]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.423531, ..., -0.641954]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.726779, ..., 0.687642]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.809306, ...,  0.496974]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.116436, ..., -0.811166]), <nibabel.nifti1.Nifti1Image object at 0x7f8bfc6b00a0>)
___________________________________________________________unmask - 0.0s, 0.0min

9.3.9.6. Generating a report

Since we have already computed the FirstLevelModel and have the contrast, we can quickly create a summary report.

from nilearn.image import mean_img
from nilearn.reporting import make_glm_report
mean_img_ = mean_img(func_filename)
report = make_glm_report(glm,
                         contrasts=conditions,
                         bg_img=mean_img_,
                         )

report  # This report can be viewed in a notebook


In a jupyter notebook, the report will be automatically inserted, as above. We have several other ways to access the report:

# report.save_as_html('report.html')
# report.open_in_browser()

9.3.9.7. Build the decoding pipeline

To define the decoding pipeline we use Decoder object, we choose :

  • a prediction model, here a Support Vector Classifier, with a linear kernel
  • the mask to use, here a ventral temporal ROI in the visual cortex
  • although it usually helps to decode better, z-maps time series don’t need to be rescaled to a 0 mean, variance of 1 so we use standardize=False.
  • we use univariate feature selection to reduce the dimension of the problem keeping only 5% of voxels which are most informative.
  • a cross-validation scheme, here we use LeaveOneGroupOut cross-validation on the sessions which corresponds to a leave-one-session-out

We fit directly this pipeline on the Niimgs outputs of the GLM, with corresponding conditions labels and session labels (for the cross validation).

from nilearn.decoding import Decoder
from sklearn.model_selection import LeaveOneGroupOut
decoder = Decoder(estimator='svc', mask=haxby_dataset.mask, standardize=False,
                  screening_percentile=5, cv=LeaveOneGroupOut())
decoder.fit(z_maps, conditions_label, groups=session_label)

# Return the corresponding mean prediction accuracy compared to chance

classification_accuracy = np.mean(list(decoder.cv_scores_.values()))
chance_level = 1. / len(np.unique(conditions))
print('Classification accuracy: {:.4f} / Chance level: {}'.format(
    classification_accuracy, chance_level))

Out:

Classification accuracy: 0.6905 / Chance level: 0.125

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

Gallery generated by Sphinx-Gallery