Note
Click here to download the full example code or to run this example in your browser via Binder
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:
Download the Haxby dataset.
Extract the information to generate a glm representing the blocks of stimuli.
Analyze the decoding performance using a classifier.
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
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]
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']
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')
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)
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff907f9f6a0>, <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 1.1s, 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 - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.256013, ..., 0.308334]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.038572, ..., 0.855077]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.070285, ..., -1.222001]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.1724 , ..., 0.033508]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.96724 , ..., -1.474856]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.432136, ..., -1.197805]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.420626, ..., -0.443207]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.310409, ..., 0.196496]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9df30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8facd6860>, <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.8s, 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 - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.705774, ..., -0.934083]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.476069, ..., -1.29404 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.210752, ..., -1.441079]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.341707, ..., 2.094528]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.097247, ..., -0.496306]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.118812, ..., 1.58503 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.801345, ..., -1.648133]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.621172, ..., -0.678871]), <nibabel.nifti1.Nifti1Image object at 0x7ff8facd43a0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8fade7040>, <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=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.205584, ..., 26.587189],
...,
[-6.836576, ..., 10.676956]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.733742, ..., -0.435687]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.502666, ..., 1.789333]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.690828, ..., 0.731519]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.403789, ..., 0.257657]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.472767, ..., -1.160892]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.07507 , ..., -2.203006]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.445359, ..., -0.36161 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.034083, ..., 0.35299 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fade5c30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fd00>, <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=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 - 1.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.596132, ..., -1.910225]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.217544, ..., -0.003551]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.337751, ..., 0.789979]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.161412, ..., -0.265537]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.538844, ..., -0.797649]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.589617, ..., -0.790328]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.306331, ..., -0.003053]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.20477 , ..., -0.804061]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e320>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8ed541480>, <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.8s, 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 - 1.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.161983, ..., -0.068078]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.467058, ..., -0.494102]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.008358, ..., 0.250096]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.127065, ..., -0.241985]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.066426, ..., 0.493324]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.049242, ..., -1.008997]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.336253, ..., -0.596381]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.707037, ..., 1.358865]), <nibabel.nifti1.Nifti1Image object at 0x7ff8ed543d00>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e680>, <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.8s, 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 - 1.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.197916, ..., 1.581644]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.576467, ..., 0.392603]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.969252, ..., 1.15444 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.716041, ..., 1.094486]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.280537, ..., 0.542739]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.331329, ..., -0.019244]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.298501, ..., -0.355821]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.232015, ..., -0.420629]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8ebc0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8facd50f0>, <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 0.8s, 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 - 1.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.490124, ..., -0.665442]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.374685, ..., -0.980248]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.654133, ..., -0.288692]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.244312, ..., -1.462072]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.665202, ..., 1.793466]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.036326, ..., 0.693956]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.672634, ..., -0.24818 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.099764, ..., -1.722951]), <nibabel.nifti1.Nifti1Image object at 0x7ff907f9eef0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff908161810>, <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=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([[-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 - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.033872, ..., -0.317176]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.98478 , ..., -0.770334]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.127461, ..., 0.929068]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.489347, ..., -0.230229]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.673052, ..., 0.6757 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.260282, ..., -0.346342]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.775541, ..., 1.603123]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([3.391727, ..., 0.653312]), <nibabel.nifti1.Nifti1Image object at 0x7ff908160400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8fad8da20>, <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=None, copy=True, dtype=None)
__________________________________________________filter_and_mask - 1.2s, 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 - 1.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.620911, ..., 2.309993]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.44548 , ..., 0.576334]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.483355, ..., -0.068969]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.166576, ..., 0.713549]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.612744, ..., 1.441012]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.254934, ..., -1.288018]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.134169, ..., -0.621367]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.234171, ..., -1.497943]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8fbb0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff907f9f760>, <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=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.223948, ..., -5.959582],
...,
[-7.677519, ..., 16.024363]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1)
__________________________________________________________run_glm - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.01344 , ..., 1.283233]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.79114 , ..., 1.031069]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.583575, ..., 0.488828]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.265741, ..., -0.623721]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.20672, ..., 1.09668]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.045178, ..., 0.822339]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.463833, ..., -0.151504]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.230311, ..., 0.198537]), <nibabel.nifti1.Nifti1Image object at 0x7ff8f2dd6860>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff8facd60b0>, <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=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 - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.080165, ..., -3.497239]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-3.083184, ..., -3.071235]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.724401, ..., -2.892927]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.044506, ..., 0.285176]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.257372, ..., 1.802962]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.485481, ..., 0.284696]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.032135, ..., -1.290339]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.417132, ..., -1.258094]), <nibabel.nifti1.Nifti1Image object at 0x7ff8fad8e740>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff908162cb0>, <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>, { 'detrend': False,
'dtype': None,
'high_pass': None,
'high_variance_confounds': False,
'low_pass': None,
'reports': True,
'runs': None,
'smoothing_fwhm': 4,
'standardize': False,
'standardize_confounds': True,
't_r': 2.5,
'target_affine': None,
'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=0, confounds=None, sample_mask=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 - 1.2s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.545625, ..., -1.041515]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.152042, ..., 1.145641]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.55236 , ..., 0.696758]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.684509, ..., 0.226524]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.423531, ..., -0.641954]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.726779, ..., 0.687642]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.809306, ..., 0.496974]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.116436, ..., -0.811166]), <nibabel.nifti1.Nifti1Image object at 0x7ff9081634f0>)
___________________________________________________________unmask - 0.1s, 0.0min
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()
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))
Classification accuracy: 0.6890 / Chance level: 0.125
Total running time of the script: ( 2 minutes 43.126 seconds)
Estimated memory usage: 958 MB