2.5.3. 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.

2.5.3.1. Fetch example Haxby dataset

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

from nilearn import datasets

# By default 2nd subject will be fetched
haxby_dataset = datasets.fetch_haxby()

# repetition has to be known
TR = 2.5

2.5.3.2. Load the behavioral data

import pandas as pd

# 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]

2.5.3.3. Build a proper event structure for each session

import numpy as np

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']

2.5.3.4. Instantiate and run FirstLevelModel

from nilearn.image import index_img
from nistats.first_level_model import FirstLevelModel

# we are going to generate a list of z-maps together with their session and condition index
z_maps = []
condition_idx = []
session_idx = []

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

Out:

/home/kshitij/workspace/nistats-org/nistats-repo/nistats-kchawla-pi/examples/05_complete_examples/plot_haxby_block_classification.py:89: DeprecationWarning: The parameter "mask" will be removed in next release of Nistats. Please use the parameter "mask_img" instead.
  memory='nilearn_cache')

2.5.3.5. Run the glm on data from each session

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_))
        condition_idx.append(condition_)
        session_idx.append(session)

Out:

________________________________________________________________________________
[Memory] Calling nilearn.image.resampling.resample_img...
resample_img(<nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>, target_affine=None, target_shape=None, copy=False, interpolation='nearest')
_____________________________________________________resample_img - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec44904e0>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.038572, ...,  0.855077]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.070285, ..., -1.222001]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.1724  , ...,  0.033508]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.96724 , ..., -1.474856]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.432136, ..., -1.197805]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.420626, ..., -0.443207]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.310409, ..., 0.196496]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec0e6cfd0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec3cf4390>, <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.705774, ..., -0.934083]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.476069, ..., -1.29404 ]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.210752, ..., -1.441079]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.341707, ...,  2.094528]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.097247, ..., -0.496306]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.118812, ...,  1.58503 ]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.801345, ..., -1.648133]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.621172, ..., -0.678871]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6240>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec1162d68>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>, { '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.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.733742, ..., -0.444561]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.502666, ..., 1.811852]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.690828, ...,  0.730153]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.403789, ...,  0.254288]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.472767, ..., -1.174186]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.07507 , ..., -2.221482]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.445359, ..., -0.363018]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.034083, ..., 0.362042]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41c9eb8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6c50>, <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>, { '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.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.217544, ..., -0.003551]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.337751, ..., 0.789979]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.161412, ..., -0.265537]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.538844, ..., -0.797649]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.589617, ..., -0.790328]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.306331, ..., -0.003053]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.20477 , ..., -0.804061]), <nibabel.nifti1.Nifti1Image object at 0x7f6f143425f8>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec42900b8>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>, { '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.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.161983, ..., -0.068078]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.467058, ..., -0.494102]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.008358, ..., 0.250096]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.127065, ..., -0.241985]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.066426, ..., 0.493324]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.049242, ..., -1.008997]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.336253, ..., -0.596381]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.707037, ..., 1.358865]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41ed438>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec44904e0>, <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.553131, ...,  0.392603]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.960821, ...,  1.15444 ]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.7086  , ...,  1.094486]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.26815 , ...,  0.542739]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.321163, ..., -0.019244]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.288938, ..., -0.355821]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.222105, ..., -0.420629]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13da6080>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec431aef0>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.374685, ..., -0.980248]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.654133, ..., -0.288692]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.244312, ..., -1.462072]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.665202, ...,  1.793466]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.036326, ..., 0.693956]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.672634, ..., -0.24818 ]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.099764, ..., -1.722951]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec1162f60>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6f13da6828>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>, { '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.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.98478 , ..., -0.770334]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.127461, ..., 0.929068]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.489347, ..., -0.230229]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.673052, ...,  0.6757  ]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.260282, ..., -0.346342]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.775541, ..., 1.603123]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([3.391727, ..., 0.653312]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec41e6be0>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec4290048>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.44548 , ..., 0.576334]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.483355, ..., -0.068969]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.166576, ..., 0.713549]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.612744, ..., 1.441012]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.254934, ..., -1.288018]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.134169, ..., -0.621367]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.234171, ..., -1.497943]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440ee80>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec3486f98>, <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.01344 , ...,  1.283233]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.79114 , ..., 1.031069]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.583575, ..., 0.488828]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.265741, ..., -0.623721]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.20672, ...,  1.09668]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.045178, ...,  0.822339]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.463833, ..., -0.151504]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.230311, ...,  0.198537]), <nibabel.nifti1.Nifti1Image object at 0x7f6f14342748>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec3483d30>, <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-3.083184, ..., -3.071235]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.724401, ..., -2.892927]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.044506, ..., 0.285176]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.257372, ..., 1.802962]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.485481, ...,  0.284696]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.032135, ..., -1.290339]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.417132, ..., -1.258094]), <nibabel.nifti1.Nifti1Image object at 0x7f6ec440e940>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.input_data.nifti_masker.filter_and_mask...
filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7f6ec4490c88>, <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>, { '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.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nistats.first_level_model.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 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.152042, ..., 1.145641]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.55236 , ..., 0.696758]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.684509, ...,  0.226524]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.423531, ..., -0.641954]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.726779, ..., 0.687642]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.809306, ...,  0.496974]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.116436, ..., -0.811166]), <nibabel.nifti1.Nifti1Image object at 0x7f6f13d94898>)
___________________________________________________________unmask - 0.0s, 0.0min

2.5.3.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 nistats.reporting import make_glm_report

mean_img_ = mean_img(func_filename)
report = make_glm_report(glm,
                         contrasts=conditions,
                         bg_img=mean_img_,
                         )

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

# report  # This report can be viewed in a notebook
# report.save_as_html('report.html')
# report.open_in_browser()

2.5.3.7. Transform the maps to an array of values

from nilearn.input_data import NiftiMasker

# no need to standardize or smooth the data
masker = NiftiMasker(mask_img=haxby_dataset.mask, memory='nilearn_cache',
                     memory_level=1)
X = masker.fit_transform(z_maps)

2.5.3.8. Build the decoder

Define the prediction function to be used. Here we use a Support Vector Classification, with a linear kernel

from sklearn.svm import SVC

svc = SVC(kernel='linear')

# Define the dimension reduction to be used.
# Here we use a classical univariate feature selection based on F-test,
# namely Anova. When doing full-brain analysis, it is better to use
# SelectPercentile, keeping 5% of voxels
# (because it is independent of the resolution of the data).
from sklearn.feature_selection import SelectPercentile, f_classif

feature_selection = SelectPercentile(f_classif, percentile=5)

# We have our classifier (SVC), our feature selection (SelectPercentile),and now,
# we can plug them together in a *pipeline** that performs the two operations
# successively:
from sklearn.pipeline import Pipeline

anova_svc = Pipeline([('anova', feature_selection), ('svc', svc)])

2.5.3.9. Obtain prediction scores via cross validation

from sklearn.model_selection import cross_val_score, LeaveOneGroupOut

# Define the cross-validation scheme used for validation.
# Here we use a LeaveOneGroupOut cross-validation on the session group
# which corresponds to a leave-one-session-out
cv = LeaveOneGroupOut()

# Compute the prediction accuracy for the different folds (i.e. session)
cv_scores = cross_val_score(anova_svc, X, condition_idx, cv=cv, groups=session_idx)

# Return the corresponding mean prediction accuracy
classification_accuracy = cv_scores.mean()
chance_level = 1. / len(np.unique(condition_idx))

# Print the results
print('Classification accuracy: {:.4f} / Chance level: {}'.format(
        classification_accuracy, chance_level))
# Classification accuracy:  0.375 / Chance level: 0.125

Out:

Classification accuracy: 0.3750 / Chance level: 0.125

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

Gallery generated by Sphinx-Gallery