Note
Go to the end to download the full example code. or to run this example in your browser via Binder
Single-subject data (two runs) in native space¶
The example shows the analysis of an SPM dataset, with two conditions: viewing a face image or a scrambled face image.
This example takes a lot of time because the input are lists of 3D images sampled in different positions (encoded by different affine functions).
See also
For more information see the dataset description.
Fetch and inspect the data¶
Fetch the SPM multimodal_faces data.
from nilearn.datasets import fetch_spm_multimodal_fmri
subject_data = fetch_spm_multimodal_fmri()
[fetch_spm_multimodal_fmri] Dataset created in
/home/runner/nilearn_data/spm_multimodal_fmri
[fetch_spm_multimodal_fmri] Missing 390 functional scans for session 1.
[fetch_spm_multimodal_fmri] Data absent, downloading...
[fetch_spm_multimodal_fmri] Downloading data from
https://www.fil.ion.ucl.ac.uk/spm/download/data/mmfaces/multimodal_fmri.zip ...
[fetch_spm_multimodal_fmri] Downloaded 1736704 of 134263085 bytes (1.3%%,
1.4min remaining)
[fetch_spm_multimodal_fmri] Downloaded 3325952 of 134263085 bytes (2.5%%,
1.4min remaining)
[fetch_spm_multimodal_fmri] Downloaded 4186112 of 134263085 bytes (3.1%%,
1.6min remaining)
[fetch_spm_multimodal_fmri] Downloaded 5013504 of 134263085 bytes (3.7%%,
1.8min remaining)
[fetch_spm_multimodal_fmri] Downloaded 5824512 of 134263085 bytes (4.3%%,
1.9min remaining)
[fetch_spm_multimodal_fmri] Downloaded 6717440 of 134263085 bytes (5.0%%,
2.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 7593984 of 134263085 bytes (5.7%%,
2.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 8560640 of 134263085 bytes (6.4%%,
2.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 9568256 of 134263085 bytes (7.1%%,
2.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 10674176 of 134263085 bytes (8.0%%,
2.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 11960320 of 134263085 bytes (8.9%%,
1.9min remaining)
[fetch_spm_multimodal_fmri] Downloaded 13475840 of 134263085 bytes (10.0%%,
1.8min remaining)
[fetch_spm_multimodal_fmri] Downloaded 14876672 of 134263085 bytes (11.1%%,
1.8min remaining)
[fetch_spm_multimodal_fmri] Downloaded 16449536 of 134263085 bytes (12.3%%,
1.7min remaining)
[fetch_spm_multimodal_fmri] Downloaded 18120704 of 134263085 bytes (13.5%%,
1.6min remaining)
[fetch_spm_multimodal_fmri] Downloaded 19398656 of 134263085 bytes (14.4%%,
1.6min remaining)
[fetch_spm_multimodal_fmri] Downloaded 20815872 of 134263085 bytes (15.5%%,
1.6min remaining)
[fetch_spm_multimodal_fmri] Downloaded 22298624 of 134263085 bytes (16.6%%,
1.5min remaining)
[fetch_spm_multimodal_fmri] Downloaded 23789568 of 134263085 bytes (17.7%%,
1.5min remaining)
[fetch_spm_multimodal_fmri] Downloaded 25280512 of 134263085 bytes (18.8%%,
1.5min remaining)
[fetch_spm_multimodal_fmri] Downloaded 26763264 of 134263085 bytes (19.9%%,
1.4min remaining)
[fetch_spm_multimodal_fmri] Downloaded 28344320 of 134263085 bytes (21.1%%,
1.4min remaining)
[fetch_spm_multimodal_fmri] Downloaded 30031872 of 134263085 bytes (22.4%%,
1.4min remaining)
[fetch_spm_multimodal_fmri] Downloaded 31965184 of 134263085 bytes (23.8%%,
1.3min remaining)
[fetch_spm_multimodal_fmri] Downloaded 34226176 of 134263085 bytes (25.5%%,
1.2min remaining)
[fetch_spm_multimodal_fmri] Downloaded 36610048 of 134263085 bytes (27.3%%,
1.2min remaining)
[fetch_spm_multimodal_fmri] Downloaded 38649856 of 134263085 bytes (28.8%%,
1.1min remaining)
[fetch_spm_multimodal_fmri] Downloaded 40468480 of 134263085 bytes (30.1%%,
1.1min remaining)
[fetch_spm_multimodal_fmri] Downloaded 42344448 of 134263085 bytes (31.5%%,
1.1min remaining)
[fetch_spm_multimodal_fmri] Downloaded 44359680 of 134263085 bytes (33.0%%,
1.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 46309376 of 134263085 bytes (34.5%%,
1.0min remaining)
[fetch_spm_multimodal_fmri] Downloaded 48209920 of 134263085 bytes (35.9%%,
58.3s remaining)
[fetch_spm_multimodal_fmri] Downloaded 50216960 of 134263085 bytes (37.4%%,
56.3s remaining)
[fetch_spm_multimodal_fmri] Downloaded 52248576 of 134263085 bytes (38.9%%,
54.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 54353920 of 134263085 bytes (40.5%%,
52.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 56745984 of 134263085 bytes (42.3%%,
50.1s remaining)
[fetch_spm_multimodal_fmri] Downloaded 59473920 of 134263085 bytes (44.3%%,
47.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 61947904 of 134263085 bytes (46.1%%,
45.1s remaining)
[fetch_spm_multimodal_fmri] Downloaded 63684608 of 134263085 bytes (47.4%%,
44.0s remaining)
[fetch_spm_multimodal_fmri] Downloaded 65560576 of 134263085 bytes (48.8%%,
42.6s remaining)
[fetch_spm_multimodal_fmri] Downloaded 67452928 of 134263085 bytes (50.2%%,
41.3s remaining)
[fetch_spm_multimodal_fmri] Downloaded 69378048 of 134263085 bytes (51.7%%,
39.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 71335936 of 134263085 bytes (53.1%%,
38.6s remaining)
[fetch_spm_multimodal_fmri] Downloaded 73252864 of 134263085 bytes (54.6%%,
37.2s remaining)
[fetch_spm_multimodal_fmri] Downloaded 75243520 of 134263085 bytes (56.0%%,
35.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 77398016 of 134263085 bytes (57.6%%,
34.3s remaining)
[fetch_spm_multimodal_fmri] Downloaded 79233024 of 134263085 bytes (59.0%%,
33.1s remaining)
[fetch_spm_multimodal_fmri] Downloaded 81174528 of 134263085 bytes (60.5%%,
31.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 83312640 of 134263085 bytes (62.1%%,
30.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 85532672 of 134263085 bytes (63.7%%,
28.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 87785472 of 134263085 bytes (65.4%%,
27.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 90095616 of 134263085 bytes (67.1%%,
25.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 92356608 of 134263085 bytes (68.8%%,
24.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 94470144 of 134263085 bytes (70.4%%,
23.1s remaining)
[fetch_spm_multimodal_fmri] Downloaded 96329728 of 134263085 bytes (71.7%%,
22.0s remaining)
[fetch_spm_multimodal_fmri] Downloaded 98263040 of 134263085 bytes (73.2%%,
20.8s remaining)
[fetch_spm_multimodal_fmri] Downloaded 99868672 of 134263085 bytes (74.4%%,
19.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 101646336 of 134263085 bytes (75.7%%,
18.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 103399424 of 134263085 bytes (77.0%%,
17.8s remaining)
[fetch_spm_multimodal_fmri] Downloaded 105209856 of 134263085 bytes (78.4%%,
16.8s remaining)
[fetch_spm_multimodal_fmri] Downloaded 107069440 of 134263085 bytes (79.7%%,
15.7s remaining)
[fetch_spm_multimodal_fmri] Downloaded 108847104 of 134263085 bytes (81.1%%,
14.7s remaining)
[fetch_spm_multimodal_fmri] Downloaded 110714880 of 134263085 bytes (82.5%%,
13.6s remaining)
[fetch_spm_multimodal_fmri] Downloaded 112771072 of 134263085 bytes (84.0%%,
12.4s remaining)
[fetch_spm_multimodal_fmri] Downloaded 114925568 of 134263085 bytes (85.6%%,
11.1s remaining)
[fetch_spm_multimodal_fmri] Downloaded 117448704 of 134263085 bytes (87.5%%,
9.6s remaining)
[fetch_spm_multimodal_fmri] Downloaded 120545280 of 134263085 bytes (89.8%%,
7.7s remaining)
[fetch_spm_multimodal_fmri] Downloaded 123879424 of 134263085 bytes (92.3%%,
5.8s remaining)
[fetch_spm_multimodal_fmri] Downloaded 126599168 of 134263085 bytes (94.3%%,
4.2s remaining)
[fetch_spm_multimodal_fmri] Downloaded 129081344 of 134263085 bytes (96.1%%,
2.8s remaining)
[fetch_spm_multimodal_fmri] Downloaded 131047424 of 134263085 bytes (97.6%%,
1.8s remaining)
[fetch_spm_multimodal_fmri] Downloaded 132669440 of 134263085 bytes (98.8%%,
0.9s remaining)
[fetch_spm_multimodal_fmri] Downloaded 134217728 of 134263085 bytes (100.0%%,
0.0s remaining)
[fetch_spm_multimodal_fmri] ...done. (75 seconds, 1 min)
[fetch_spm_multimodal_fmri] Extracting data from
/home/runner/nilearn_data/spm_multimodal_fmri/sub001/multimodal_fmri.zip...
[fetch_spm_multimodal_fmri] .. done.
[fetch_spm_multimodal_fmri] Downloading data from
https://www.fil.ion.ucl.ac.uk/spm/download/data/mmfaces/multimodal_smri.zip ...
[fetch_spm_multimodal_fmri] Downloaded 1581056 of 6852766 bytes (23.1%%, 3.6s
remaining)
[fetch_spm_multimodal_fmri] Downloaded 3563520 of 6852766 bytes (52.0%%, 1.9s
remaining)
[fetch_spm_multimodal_fmri] Downloaded 5406720 of 6852766 bytes (78.9%%, 0.8s
remaining)
[fetch_spm_multimodal_fmri] ...done. (5 seconds, 0 min)
[fetch_spm_multimodal_fmri] Extracting data from
/home/runner/nilearn_data/spm_multimodal_fmri/sub001/multimodal_smri.zip...
[fetch_spm_multimodal_fmri] .. done.
Let’s inspect one of the event files before using them.
import pandas as pd
events = [subject_data.events1, subject_data.events2]
events_dataframe = pd.read_csv(events[0], sep="\t")
events_dataframe["trial_type"].value_counts()
trial_type
scrambled 86
faces 64
Name: count, dtype: int64
We can confirm there are only 2 conditions in the dataset.
from nilearn.plotting import plot_event, show
plot_event(events)
show()

Resample the images:
this is achieved by the concat_imgs function of Nilearn.
import warnings
from nilearn.image import concat_imgs, mean_img, resample_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.
Fit the model¶
Fit the GLM for the 2 runs by specifying a FirstLevelModel and then fitting it.
# Sample at the beginning of each acquisition.
slice_time_ref = 0.0
# We use a discrete cosine transform to model signal drifts.
drift_model = "cosine"
# The cutoff for the drift model is 0.01 Hz.
high_pass = 0.01
# The hemodynamic response function
hrf_model = "spm + derivative"
from nilearn.glm.first_level import FirstLevelModel
print("Fitting a GLM")
fmri_glm = FirstLevelModel(
smoothing_fwhm=None,
t_r=subject_data.t_r,
hrf_model=hrf_model,
drift_model=drift_model,
high_pass=high_pass,
)
fmri_glm = fmri_glm.fit(fmri_img, events=events)
Fitting a GLM
View the results¶
Now we can compute contrast-related statistical maps (in z-scale), and plot them.
from nilearn.plotting import plot_stat_map
print("Computing contrasts")
Computing contrasts
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.
contrasts = ["faces - scrambled", "scrambled - faces"]
Let’s store common parameters for all plots.
We plot the contrasts values overlaid on the mean fMRI image and we will use the z-score values as transparency, with any voxel with | Z-score | > 3 being fully opaque and any voxel with 0 < | Z-score | < 1.96 being partly transparent.
plot_param = {
"vmin": 0,
"display_mode": "z",
"cut_coords": 3,
"black_bg": True,
"bg_img": mean_image,
"cmap": "inferno",
"transparency_range": [0, 3],
}
# Iterate on contrasts to compute and plot them.
for contrast_id in contrasts:
print(f"\tcontrast id: {contrast_id}")
results = fmri_glm.compute_contrast(contrast_id, output_type="all")
plot_stat_map(
results["stat"],
title=contrast_id,
transparency=results["z_score"],
**plot_param,
)
contrast id: faces - scrambled
/home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:133: RuntimeWarning: The same contrast will be used for all 2 runs. If the design matrices are not the same for all runs, (for example with different column names or column order across runs) you should pass contrast as an expression using the name of the conditions as they appear in the design matrices.
results = fmri_glm.compute_contrast(contrast_id, output_type="all")
contrast id: scrambled - faces
/home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:133: RuntimeWarning: The same contrast will be used for all 2 runs. If the design matrices are not the same for all runs, (for example with different column names or column order across runs) you should pass contrast as an expression using the name of the conditions as they appear in the design matrices.
results = fmri_glm.compute_contrast(contrast_id, output_type="all")
We also define the effects of interest contrast, a 2-dimensional contrasts spanning the two conditions.
import numpy as np
contrasts = np.eye(2)
results = fmri_glm.compute_contrast(contrasts, output_type="all")
plot_stat_map(
results["stat"],
title="effects of interest",
transparency=results["z_score"],
**plot_param,
)
show()

/home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:150: RuntimeWarning: The same contrast will be used for all 2 runs. If the design matrices are not the same for all runs, (for example with different column names or column order across runs) you should pass contrast as an expression using the name of the conditions as they appear in the design matrices.
results = fmri_glm.compute_contrast(contrasts, output_type="all")
/home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:150: UserWarning: F contrasts should have 20 columns, but it has only 2. The rest of the contrast was padded with zeros.
results = fmri_glm.compute_contrast(contrasts, output_type="all")
/home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:150: UserWarning: Running approximate fixed effects on F statistics.
results = fmri_glm.compute_contrast(contrasts, output_type="all")
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 24.630 seconds)
Estimated memory usage: 968 MB

