Note
Go to the end 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=0.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 0x7fd1f00de6d0>, <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>, { 'clean_kwargs': {},
'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([[-0.114769, ..., -2.149296],
...,
[ 2.367151, ..., 0.779998]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.44475 , ..., 0.379275]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.013524, ..., 0.844135]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.217486, ..., -1.430348]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.106474, ..., -0.182434]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.747494, ..., -1.660679]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.080159, ..., -1.32614 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.253894, ..., -0.452682]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.240914, ..., 0.244136]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f00dddd0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1e32c8f50>, <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>, { 'clean_kwargs': {},
'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, random_state=None)
__________________________________________________________run_glm - 1.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.871458, ..., -0.990755]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.627194, ..., -1.290147]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.153013, ..., -1.320123]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.15748 , ..., 2.082416]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.997775, ..., -0.754066]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.109412, ..., 1.330079]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.030863, ..., -1.731439]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.559734, ..., -0.720924]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed781190>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1efefc890>, <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>, { 'clean_kwargs': {},
'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([[ 5.205584, ..., 26.587189],
...,
[-6.836576, ..., 10.676956]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.4s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.695564, ..., -0.455092]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.457214, ..., 1.537178]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.803925, ..., 0.570463]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.614932, ..., 0.232909]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.527175, ..., -1.062723]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.126756, ..., -2.274819]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.635166, ..., -0.395548]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.250365, ..., 0.364311]), <nibabel.nifti1.Nifti1Image object at 0x7fd1efcb2c50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1f2297d50>, <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>, { 'clean_kwargs': {},
'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([[-2.026206, ..., 5.974948],
...,
[ 2.616334, ..., 0.104535]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.503142, ..., -1.639351]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.271132, ..., -0.047089]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.318104, ..., 0.724813]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.073279, ..., -0.316956]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.380183, ..., -0.690685]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.631912, ..., -0.753286]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.155784, ..., -0.065658]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.186135, ..., -0.69267 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f225e2d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1f2930f50>, <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>, { 'clean_kwargs': {},
'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, random_state=None)
__________________________________________________________run_glm - 1.6s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.158342, ..., -0.068131]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.396497, ..., -0.424937]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.091867, ..., 0.463109]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.054041, ..., -0.122921]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.025223, ..., 0.562991]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.001653, ..., -0.968729]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.274665, ..., -0.667 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.564329, ..., 1.496068]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e3890>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1efe9fe50>, <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>, { 'clean_kwargs': {},
'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([[-27.150482, ..., -5.81308 ],
...,
[-30.204891, ..., 7.417917]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.987059, ..., 1.41717 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.24774 , ..., 0.674399]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.731234, ..., 1.341998]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.714869, ..., 1.182988]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.222674, ..., 0.480354]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.366899, ..., -0.091153]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.2708 , ..., -0.247146]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.251249, ..., -0.413063]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f26af510>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1e3426b10>, <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>, { 'clean_kwargs': {},
'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, random_state=None)
__________________________________________________________run_glm - 1.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.290412, ..., -0.609221]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.090297, ..., -0.822602]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.747918, ..., -0.108861]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.095788, ..., -1.376995]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.807425, ..., 1.826947]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.017351, ..., 0.622242]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.625042, ..., -0.231224]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.056424, ..., -1.672737]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f2294290>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1f292a2d0>, <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>, { 'clean_kwargs': {},
'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([[-15.915996, ..., 22.07737 ],
...,
[-16.981215, ..., 3.372383]], dtype=float32),
array([[ 0. , ..., 1. ],
...,
[-0.200737, ..., 1. ]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.008536, ..., -0.066075]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.168487, ..., -0.636238]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.145684, ..., 0.932773]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.580823, ..., -0.455655]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.685537, ..., 0.715791]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.245273, ..., -0.099707]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.79538 , ..., 1.913842]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([3.519925, ..., 0.629218]), <nibabel.nifti1.Nifti1Image object at 0x7fd1b20b3050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1f240aa10>, <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>, { 'clean_kwargs': {},
'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([[-0.292987, ..., 18.392956],
...,
[-3.935719, ..., 0.602484]], dtype=float32),
array([[ 0. , ..., 1. ],
...,
[-0.200737, ..., 1. ]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.425611, ..., 2.348025]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.31867 , ..., 0.408223]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.534932, ..., -0.150519]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.149007, ..., 0.640215]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.640699, ..., 1.50369 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.246384, ..., -1.346316]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.162243, ..., -0.519251]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.203695, ..., -1.335337]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f240b050>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1f227f290>, <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>, { 'clean_kwargs': {},
'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.223948, ..., -5.959582],
...,
[-7.677519, ..., 16.024363]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.7s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.034515, ..., 1.612397]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.62798 , ..., 1.160445]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.506632, ..., 0.459388]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.261016, ..., -0.747236]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.231796, ..., 1.098904]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.148582, ..., 0.999934]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.548262, ..., 0.09934 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.222824, ..., 0.318977]), <nibabel.nifti1.Nifti1Image object at 0x7fd1f27a1d90>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1efefced0>, <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>, { 'clean_kwargs': {},
'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([[-19.66533 , ..., -6.299562],
...,
[-24.647343, ..., 2.331865]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.5s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.175763, ..., -3.429485]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-3.146358, ..., -2.947626]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.806852, ..., -2.720554]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.008926, ..., 0.4544 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.279543, ..., 1.828183]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.463642, ..., 0.26599 ]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.058735, ..., -1.191442]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.390268, ..., -1.112207]), <nibabel.nifti1.Nifti1Image object at 0x7fd1ed34fe10>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7fd1f2932b10>, <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>, { 'clean_kwargs': {},
'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([[-1.095605, ..., 16.449202],
...,
[ 2.59974 , ..., -2.179998]], dtype=float32),
array([[0., ..., 1.],
...,
[0., ..., 1.]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.4s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.340751, ..., -1.056108]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.043261, ..., 1.144442]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.517954, ..., 0.611394]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-5.797134e-01, ..., 4.317655e-06]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.398581, ..., -0.488427]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.714396, ..., 0.869941]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.924894, ..., 0.723724]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.145297, ..., -0.821272]), <nibabel.nifti1.Nifti1Image object at 0x7fd1e33e0850>)
___________________________________________________________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 sklearn.model_selection import LeaveOneGroupOut
from nilearn.decoding import Decoder
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.0 / len(np.unique(conditions))
print(
f"Classification accuracy: {classification_accuracy:.4f} / "
f"Chance level: {chance_level}"
)
/home/remi/github/nilearn/env/lib/python3.11/site-packages/nilearn/image/resampling.py:493: UserWarning:
The provided image has no sform in its header. Please check the provided file. Results may not be as expected.
Classification accuracy: 0.7173 / Chance level: 0.125
Total running time of the script: (2 minutes 59.254 seconds)
Estimated memory usage: 917 MB