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. In this decoding analysis, we will be doing a one-vs-all classification. 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
t_r = 2.5
[get_dataset_dir] Dataset found in /home/runner/nilearn_data/haxby2001
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"].to_numpy()
# Record these as an array of runs
runs = behavioral["chunks"].to_numpy()
unique_runs = behavioral["chunks"].unique()
# fMRI data: a unique file for each run
func_filename = haxby_dataset.func[0]
Build a proper event structure for each run¶
events = {}
# events will take the form of a dictionary of Dataframes, one per run
for run in unique_runs:
# get the condition label per run
conditions_run = conditions[runs == run]
# get the number of scans per run, then the corresponding
# vector of frame times
n_scans = len(conditions_run)
frame_times = t_r * np.arange(n_scans)
# each event last the full TR
duration = t_r * np.ones(n_scans)
# Define the events object
events_ = pd.DataFrame(
{
"onset": frame_times,
"trial_type": conditions_run,
"duration": duration,
}
)
# remove the rest condition and insert into the dictionary
events[run] = events_[events_.trial_type != "rest"]
Instantiate and run FirstLevelModel¶
We generate a list of z-maps together with their run and condition index
z_maps = []
conditions_label = []
run_label = []
# Instantiate the glm
from nilearn.glm.first_level import FirstLevelModel
glm = FirstLevelModel(
t_r=t_r,
mask_img=haxby_dataset.mask,
high_pass=0.008,
smoothing_fwhm=4,
memory="nilearn_cache",
)
Run the GLM on data from each run¶
events[run].trial_type.unique()
from nilearn.image import index_img
for run in unique_runs:
# grab the fmri data for that particular run
fmri_run = index_img(func_filename, runs == run)
# fit the GLM
glm.fit(fmri_run, events=events[run])
# set up contrasts: one per condition
conditions = events[run].trial_type.unique()
for condition_ in conditions:
z_maps.append(glm.compute_contrast(condition_))
conditions_label.append(condition_)
run_label.append(run)
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5a0e8ecd0>, <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>, { '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([[-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.3s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.575977, ..., 0.481522]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.00779 , ..., 0.924282]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.349128, ..., -1.50145 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.902378, ..., -0.291882]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.624816, ..., -1.682436]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.838935, ..., -1.528093]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.06136 , ..., -0.289828]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.983539, ..., 0.291568]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d48e50>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6d30>, <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.940878, ..., -0.740886]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.56852 , ..., -1.374435]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.141601, ..., -1.208286]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.922613, ..., 2.268412]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.904959, ..., -1.104956]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.242388, ..., 1.270843]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.197666, ..., -1.944003]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.6546 , ..., -0.644223]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abca1400>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ab242370>, <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.441012, ..., -0.384872]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.457669, ..., 1.351326]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.697867, ..., 0.601435]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.763713, ..., 0.209919]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.447911, ..., -1.13823 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.162822, ..., -2.114926]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.785086, ..., -0.260924]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.456513, ..., 0.23571 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5abcc6d30>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dda60>, <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.424686, ..., -1.190333]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.365571, ..., -0.28288 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.288251, ..., 0.627725]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.327741, ..., -0.321921]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.152188, ..., -0.366314]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.629164, ..., -0.735212]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.873351, ..., -0.14857 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.061121, ..., -0.426454]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6dd670>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ab6abf10>, <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.09849 , ..., -0.026579]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.329567, ..., -0.355602]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.134325, ..., 0.590796]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.998496, ..., -0.158513]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.0983 , ..., 0.684567]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.003411, ..., -1.001089]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.28481 , ..., -0.796836]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.349998, ..., 1.77109 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42f40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ac1a33d0>, <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>, { '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([[-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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.773093, ..., 1.506396]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.945765, ..., 0.964351]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.489424, ..., 1.36385 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.60046 , ..., 1.292458]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.115105, ..., 0.360045]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.341327, ..., -0.115557]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.0463 , ..., -0.011997]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.304337, ..., -0.465647]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ac0f6520>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5a0202ee0>, <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.091521, ..., -0.624162]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.375261, ..., -0.645241]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.852582, ..., 0.144768]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.861249, ..., -1.104223]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.147067, ..., 1.938447]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.043015, ..., 0.397187]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.518244, ..., -0.247356]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.292525e-03, ..., -1.546823e+00]), <nibabel.nifti1.Nifti1Image object at 0x7ff5a1b48850>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddbe0>, <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>, { '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([[-15.915996, ..., 22.07737 ],
...,
[-16.981215, ..., 3.372383]], dtype=float32),
array([[ 0. , ..., 1. ],
...,
[-0.352245, ..., 1. ]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.011367, ..., -0.044286]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 1.378178, ..., -0.435127]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.249686, ..., 1.006757]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.627259, ..., -0.703344]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.673 , ..., 0.634889]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.170346, ..., -0.003907]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.74249 , ..., 2.416095]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([3.813068, ..., 0.397476]), <nibabel.nifti1.Nifti1Image object at 0x7ff5ab6ddc40>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5abe61430>, <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>, { '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([[-0.292987, ..., 18.392956],
...,
[-3.935719, ..., 0.602484]], dtype=float32),
array([[ 0. , ..., 1. ],
...,
[-0.352245, ..., 1. ]]), noise_model='ar1', bins=100, n_jobs=1, random_state=None)
__________________________________________________________run_glm - 1.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.400515, ..., 2.602952]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.497881, ..., 0.140209]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.499281, ..., -0.49038 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.038187, ..., 0.57069 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.622503, ..., 1.679756]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.211798, ..., -1.574836]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.143717, ..., -0.364448]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.156681, ..., -1.209168]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d42370>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5c7bf9c10>, <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>, { '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.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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.00555 , ..., 1.955666]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.488905, ..., 1.063614]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.126069, ..., 0.347685]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.147577, ..., -0.722242]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.137613, ..., 1.219668]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.30493 , ..., 1.036247]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.64672 , ..., 0.256269]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.01524, ..., 0.32111]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c7b9f700>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5c5d3c2e0>, <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.497712, ..., -3.241636]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-2.884836, ..., -2.579124]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.361796, ..., -2.404684]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.15611 , ..., 0.717663]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.124367, ..., 1.85051 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.322726, ..., 0.187135]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.943745, ..., -1.223389]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.298717, ..., -1.02394 ]), <nibabel.nifti1.Nifti1Image object at 0x7ff5774affa0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.maskers.nifti_masker._filter_and_mask...
_filter_and_mask(<nibabel.nifti1.Nifti1Image object at 0x7ff5ac19cfd0>, <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>, { '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.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.028052, ..., -1.076144]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.07071 , ..., 1.117228]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([1.278396, ..., 0.666023]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-0.606351, ..., -0.186859]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([ 0.228201, ..., -0.299271]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([0.760997, ..., 0.931553]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.005349, ..., 0.835504]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________unmask - 0.1s, 0.0min
________________________________________________________________________________
[Memory] Calling nilearn.masking.unmask...
unmask(array([-1.093039, ..., -0.791928]), <nibabel.nifti1.Nifti1Image object at 0x7ff5c5d220d0>)
___________________________________________________________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, copy_header=True)
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 can access the report via a browser:
# report.open_in_browser()
# or we can save as an html file
# from pathlib import Path
# output_dir = Path.cwd() / "results" / "plot_haxby_glm_decoding"
# output_dir.mkdir(exist_ok=True, parents=True)
# report.save_as_html(output_dir / 'report.html')
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 runs which corresponds to a leave-one-run-out
We fit directly this pipeline on the Niimgs outputs of the GLM, with corresponding conditions labels and run 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=run_label)
# Return the corresponding mean prediction accuracy compared to chance
# for classifying one-vs-all items.
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/runner/work/nilearn/nilearn/.tox/doc/lib/python3.9/site-packages/nilearn/image/resampling.py:489: UserWarning:
The provided image has no sform in its header. Please check the provided file. Results may not be as expected.
Classification accuracy: 0.7589 / Chance level: 0.125
Total running time of the script: (2 minutes 11.967 seconds)
Estimated memory usage: 1095 MB