Single-subject data (two sessions) in native space#

The example shows the analysis of an SPM dataset studying face perception. The analysis is performed in native space. Realignment parameters are provided with the input images, but those have not been resampled to a common space.

The experimental paradigm is simple, with two conditions; viewing a face image or a scrambled face image, supposedly with the same low-level statistical properties, to find face-specific responses.

For details on the data, please see: Henson, R.N., Goshen-Gottstein, Y., Ganel, T., Otten, L.J., Quayle, A., Rugg, M.D. Electrophysiological and haemodynamic correlates of face perception, recognition and priming. Cereb Cortex. 2003 Jul;13(7):793-805. http://www.dx.doi.org/10.1093/cercor/13.7.793

This example takes a lot of time because the input are lists of 3D images sampled in different positions (encoded by different affine functions).

print(__doc__)

Fetch the SPM multimodal_faces data.

Dataset created in /home/alexis/nilearn_data/spm_multimodal_fmri

Missing 390 functional scans for session 1.
Data absent, downloading...
Downloading data from http://www.fil.ion.ucl.ac.uk/spm/download/data/mmfaces/multimodal_fmri.zip ...

Downloaded 1277952 of 134263085 bytes (1.0%,  1.8min remaining)
Downloaded 3620864 of 134263085 bytes (2.7%,  1.2min remaining)
Downloaded 7331840 of 134263085 bytes (5.5%,   52.3s remaining)
Downloaded 12173312 of 134263085 bytes (9.1%,   40.3s remaining)
Downloaded 18833408 of 134263085 bytes (14.0%,   30.8s remaining)
Downloaded 20725760 of 134263085 bytes (15.4%,   33.0s remaining)
Downloaded 23216128 of 134263085 bytes (17.3%,   33.6s remaining)
Downloaded 27025408 of 134263085 bytes (20.1%,   31.9s remaining)
Downloaded 31768576 of 134263085 bytes (23.7%,   29.1s remaining)
Downloaded 38248448 of 134263085 bytes (28.5%,   25.2s remaining)
Downloaded 40509440 of 134263085 bytes (30.2%,   25.6s remaining)
Downloaded 42901504 of 134263085 bytes (32.0%,   25.7s remaining)
Downloaded 46686208 of 134263085 bytes (34.8%,   24.5s remaining)
Downloaded 51838976 of 134263085 bytes (38.6%,   22.4s remaining)
Downloaded 55517184 of 134263085 bytes (41.3%,   21.4s remaining)
Downloaded 57090048 of 134263085 bytes (42.5%,   21.7s remaining)
Downloaded 59236352 of 134263085 bytes (44.1%,   21.6s remaining)
Downloaded 62799872 of 134263085 bytes (46.8%,   20.6s remaining)
Downloaded 67747840 of 134263085 bytes (50.5%,   18.7s remaining)
Downloaded 73916416 of 134263085 bytes (55.1%,   16.4s remaining)
Downloaded 75792384 of 134263085 bytes (56.5%,   16.3s remaining)
Downloaded 78716928 of 134263085 bytes (58.6%,   15.6s remaining)
Downloaded 83107840 of 134263085 bytes (61.9%,   14.2s remaining)
Downloaded 88858624 of 134263085 bytes (66.2%,   12.3s remaining)
Downloaded 95682560 of 134263085 bytes (71.3%,   10.1s remaining)
Downloaded 98811904 of 134263085 bytes (73.6%,    9.4s remaining)
Downloaded 101949440 of 134263085 bytes (75.9%,    8.6s remaining)
Downloaded 106315776 of 134263085 bytes (79.2%,    7.4s remaining)
Downloaded 111992832 of 134263085 bytes (83.4%,    5.8s remaining)
Downloaded 119447552 of 134263085 bytes (89.0%,    3.7s remaining)
Downloaded 122396672 of 134263085 bytes (91.2%,    3.0s remaining)
Downloaded 125468672 of 134263085 bytes (93.4%,    2.3s remaining)
Downloaded 129933312 of 134263085 bytes (96.8%,    1.1s remaining) ...done. (34 seconds, 0 min)
Extracting data from /home/alexis/nilearn_data/spm_multimodal_fmri/sub001/multimodal_fmri.zip..... done.
Downloading data from http://www.fil.ion.ucl.ac.uk/spm/download/data/mmfaces/multimodal_smri.zip ...
 ...done. (1 seconds, 0 min)
Extracting data from /home/alexis/nilearn_data/spm_multimodal_fmri/sub001/multimodal_smri.zip..... done.

Specfiy timing and design matrix parameters.

tr = 2.  # repetition time, in seconds
slice_time_ref = 0.  # Sample at the beginning of each acquisition.
drift_model = 'Cosine'  # We use a discrete cosine transform to model signal drifts.
high_pass = .01  # The cutoff for the drift model is 0.01 Hz.
hrf_model = 'spm + derivative'  # The hemodynamic response function is the SPM canonical one.

Resample the images.

This is achieved by the concat_imgs function of Nilearn.

import warnings
from nilearn.image import concat_imgs, resample_img, mean_img

# Avoid getting too many warnings due to resampling
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fmri_img = [concat_imgs(subject_data.func1, auto_resample=True),
                concat_imgs(subject_data.func2, auto_resample=True)]
affine, shape = fmri_img[0].affine, fmri_img[0].shape
print('Resampling the second image (this takes time)...')
fmri_img[1] = resample_img(fmri_img[1], affine, shape[:3])
Resampling the second image (this takes time)...

Let’s create mean image for display purposes.

Make the design matrices.

import numpy as np
import pandas as pd
from nilearn.glm.first_level import make_first_level_design_matrix
design_matrices = []

Loop over the two sessions.

for idx, img in enumerate(fmri_img, start=1):
    # Build experimental paradigm
    n_scans = img.shape[-1]
    events = pd.read_table(subject_data['events{}'.format(idx)])
    # Define the sampling times for the design matrix
    frame_times = np.arange(n_scans) * tr
    # Build design matrix with the reviously defined parameters
    design_matrix = make_first_level_design_matrix(
            frame_times,
            events,
            hrf_model=hrf_model,
            drift_model=drift_model,
            high_pass=high_pass,
            )

    # put the design matrices in a list
    design_matrices.append(design_matrix)

We can specify basic contrasts (to get beta maps). We start by specifying canonical contrast that isolate design matrix columns.

contrast_matrix = np.eye(design_matrix.shape[1])
basic_contrasts = dict([(column, contrast_matrix[i])
                        for i, column in enumerate(design_matrix.columns)])

We actually want more interesting contrasts. The simplest contrast just makes the difference between the two main conditions. We define the two opposite versions to run one-tailed t-tests. We also define the effects of interest contrast, a 2-dimensional contrasts spanning the two conditions.

contrasts = {
    'faces-scrambled': basic_contrasts['faces'] - basic_contrasts['scrambled'],
    'scrambled-faces': -basic_contrasts['faces'] + basic_contrasts['scrambled'],
    'effects_of_interest': np.vstack((basic_contrasts['faces'],
                                      basic_contrasts['scrambled']))
    }

Fit the GLM for the 2 sessions by specifying a FirstLevelModel and then fitting it.

from nilearn.glm.first_level import FirstLevelModel
print('Fitting a GLM')
fmri_glm = FirstLevelModel()
fmri_glm = fmri_glm.fit(fmri_img, design_matrices=design_matrices)
Fitting a GLM

Now we can compute contrast-related statistical maps (in z-scale), and plot them.

print('Computing contrasts')
from nilearn import plotting

# Iterate on contrasts
for contrast_id, contrast_val in contrasts.items():
    print("\tcontrast id: %s" % contrast_id)
    # compute the contrasts
    z_map = fmri_glm.compute_contrast(
        contrast_val, output_type='z_score')
    # plot the contrasts as soon as they're generated
    # the display is overlaid on the mean fMRI image
    # a threshold of 3.0 is used, more sophisticated choices are possible
    plotting.plot_stat_map(
        z_map, bg_img=mean_image, threshold=3.0, display_mode='z',
        cut_coords=3, black_bg=True, title=contrast_id)
    plotting.show()
  • plot spm multimodal faces
  • plot spm multimodal faces
  • plot spm multimodal faces
Computing contrasts
        contrast id: faces-scrambled
/home/alexis/miniconda3/envs/nilearn/lib/python3.10/site-packages/nilearn/glm/first_level/first_level.py:657: UserWarning:

One contrast given, assuming it for all 2 runs

        contrast id: scrambled-faces
/home/alexis/miniconda3/envs/nilearn/lib/python3.10/site-packages/nilearn/glm/first_level/first_level.py:657: UserWarning:

One contrast given, assuming it for all 2 runs

        contrast id: effects_of_interest
/home/alexis/miniconda3/envs/nilearn/lib/python3.10/site-packages/nilearn/glm/first_level/first_level.py:657: UserWarning:

One contrast given, assuming it for all 2 runs

/home/alexis/miniconda3/envs/nilearn/lib/python3.10/site-packages/nilearn/glm/contrasts.py:352: UserWarning:

Running approximate fixed effects on F statistics.

Based on the resulting maps we observe that the analysis results in wide activity for the ‘effects of interest’ contrast, showing the implications of large portions of the visual cortex in the conditions. By contrast, the differential effect between “faces” and “scrambled” involves sparser, more anterior and lateral regions. It also displays some responses in the frontal lobe.

Total running time of the script: ( 2 minutes 9.126 seconds)

Estimated memory usage: 984 MB

Gallery generated by Sphinx-Gallery