.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_glm_first_level/plot_spm_multimodal_faces.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_glm_first_level_plot_spm_multimodal_faces.py: Single-subject data (two runs) in native space ============================================== The example shows the analysis of an :term:`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). .. seealso:: For more information see the :ref:`dataset description `. .. GENERATED FROM PYTHON SOURCE LINES 18-21 Fetch and inspect the data -------------------------- Fetch the :term:`SPM` multimodal_faces data. .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python from nilearn.datasets import fetch_spm_multimodal_fmri subject_data = fetch_spm_multimodal_fmri() .. rst-class:: sphx-glr-script-out .. code-block:: none [wrapper] Dataset created in /home/runner/nilearn_data/spm_multimodal_fmri [wrapper] Missing 390 functional scans for session 1. [wrapper] Data absent, downloading... [wrapper] Downloading data from https://www.fil.ion.ucl.ac.uk/spm/download/data/mmfaces/multimodal_fmri.zip ... [wrapper] Downloaded 3555328 of 134263085 bytes (2.6%%, 39.4s remaining) [wrapper] Downloaded 7684096 of 134263085 bytes (5.7%%, 34.6s remaining) [wrapper] Downloaded 11608064 of 134263085 bytes (8.6%%, 32.8s remaining) [wrapper] Downloaded 15982592 of 134263085 bytes (11.9%%, 30.6s remaining) [wrapper] Downloaded 20144128 of 134263085 bytes (15.0%%, 29.1s remaining) [wrapper] Downloaded 24641536 of 134263085 bytes (18.4%%, 27.4s remaining) [wrapper] Downloaded 28844032 of 134263085 bytes (21.5%%, 26.2s remaining) [wrapper] Downloaded 33333248 of 134263085 bytes (24.8%%, 24.9s remaining) [wrapper] Downloaded 37609472 of 134263085 bytes (28.0%%, 23.7s remaining) [wrapper] Downloaded 42278912 of 134263085 bytes (31.5%%, 22.2s remaining) [wrapper] Downloaded 46874624 of 134263085 bytes (34.9%%, 20.9s remaining) [wrapper] Downloaded 51929088 of 134263085 bytes (38.7%%, 19.4s remaining) [wrapper] Downloaded 57810944 of 134263085 bytes (43.1%%, 17.5s remaining) [wrapper] Downloaded 64503808 of 134263085 bytes (48.0%%, 15.4s remaining) [wrapper] Downloaded 69885952 of 134263085 bytes (52.1%%, 14.1s remaining) [wrapper] Downloaded 74317824 of 134263085 bytes (55.4%%, 13.1s remaining) [wrapper] Downloaded 78774272 of 134263085 bytes (58.7%%, 12.2s remaining) [wrapper] Downloaded 83509248 of 134263085 bytes (62.2%%, 11.1s remaining) [wrapper] Downloaded 88276992 of 134263085 bytes (65.7%%, 10.1s remaining) [wrapper] Downloaded 93077504 of 134263085 bytes (69.3%%, 9.0s remaining) [wrapper] Downloaded 97853440 of 134263085 bytes (72.9%%, 7.9s remaining) [wrapper] Downloaded 100663296 of 134263085 bytes (75.0%%, 7.5s remaining) [wrapper] Downloaded 103383040 of 134263085 bytes (77.0%%, 7.0s remaining) [wrapper] Downloaded 106078208 of 134263085 bytes (79.0%%, 6.5s remaining) [wrapper] Downloaded 108978176 of 134263085 bytes (81.2%%, 5.9s remaining) [wrapper] Downloaded 111747072 of 134263085 bytes (83.2%%, 5.3s remaining) [wrapper] Downloaded 114679808 of 134263085 bytes (85.4%%, 4.7s remaining) [wrapper] Downloaded 117514240 of 134263085 bytes (87.5%%, 4.0s remaining) [wrapper] Downloaded 120594432 of 134263085 bytes (89.8%%, 3.3s remaining) [wrapper] Downloaded 123682816 of 134263085 bytes (92.1%%, 2.6s remaining) [wrapper] Downloaded 127221760 of 134263085 bytes (94.8%%, 1.7s remaining) [wrapper] Downloaded 131096576 of 134263085 bytes (97.6%%, 0.8s remaining) [wrapper] ...done. (34 seconds, 0 min) [wrapper] Extracting data from /home/runner/nilearn_data/spm_multimodal_fmri/sub001/multimodal_fmri.zip... [wrapper] .. done. [wrapper] Downloading data from https://www.fil.ion.ucl.ac.uk/spm/download/data/mmfaces/multimodal_smri.zip ... [wrapper] Downloaded 3817472 of 6852766 bytes (55.7%%, 0.8s remaining) [wrapper] ...done. (2 seconds, 0 min) [wrapper] Extracting data from /home/runner/nilearn_data/spm_multimodal_fmri/sub001/multimodal_smri.zip... [wrapper] .. done. .. GENERATED FROM PYTHON SOURCE LINES 26-27 Let's inspect one of the event files before using them. .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: Python 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() .. rst-class:: sphx-glr-script-out .. code-block:: none trial_type scrambled 86 faces 64 Name: count, dtype: int64 .. GENERATED FROM PYTHON SOURCE LINES 35-37 We can confirm there are only 2 conditions in the dataset. .. GENERATED FROM PYTHON SOURCE LINES 37-43 .. code-block:: Python from nilearn.plotting import plot_event, show plot_event(events) show() .. image-sg:: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_001.png :alt: plot spm multimodal faces :srcset: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-46 Resample the images: this is achieved by the ``concat_imgs`` function of Nilearn. .. GENERATED FROM PYTHON SOURCE LINES 46-63 .. code-block:: Python 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], copy_header=True, force_resample=True ) .. rst-class:: sphx-glr-script-out .. code-block:: none Resampling the second image (this takes time)... .. GENERATED FROM PYTHON SOURCE LINES 64-65 Let's create mean image for display purposes. .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python mean_image = mean_img(fmri_img, copy_header=True) .. GENERATED FROM PYTHON SOURCE LINES 68-72 Fit the model ------------- Fit the :term:`GLM` for the 2 runs by specifying a FirstLevelModel and then fitting it. .. GENERATED FROM PYTHON SOURCE LINES 72-96 .. code-block:: Python # 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) .. rst-class:: sphx-glr-script-out .. code-block:: none Fitting a GLM .. GENERATED FROM PYTHON SOURCE LINES 97-101 View the results ---------------- Now we can compute contrast-related statistical maps (in z-scale), and plot them. .. GENERATED FROM PYTHON SOURCE LINES 101-105 .. code-block:: Python from nilearn.plotting import plot_stat_map print("Computing contrasts") .. rst-class:: sphx-glr-script-out .. code-block:: none Computing contrasts .. GENERATED FROM PYTHON SOURCE LINES 106-111 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. .. GENERATED FROM PYTHON SOURCE LINES 111-114 .. code-block:: Python contrasts = ["faces - scrambled", "scrambled - faces"] .. GENERATED FROM PYTHON SOURCE LINES 115-121 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. .. GENERATED FROM PYTHON SOURCE LINES 121-144 .. code-block:: Python 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, ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_002.png :alt: plot spm multimodal faces :srcset: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_003.png :alt: plot spm multimodal faces :srcset: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none contrast id: faces - scrambled /home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:135: 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:135: 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") .. GENERATED FROM PYTHON SOURCE LINES 145-148 We also define the effects of interest contrast, a 2-dimensional contrasts spanning the two conditions. .. GENERATED FROM PYTHON SOURCE LINES 148-163 .. code-block:: Python 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() .. image-sg:: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_004.png :alt: plot spm multimodal faces :srcset: /auto_examples/04_glm_first_level/images/sphx_glr_plot_spm_multimodal_faces_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/nilearn/nilearn/examples/04_glm_first_level/plot_spm_multimodal_faces.py:152: 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:152: 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:152: UserWarning: Running approximate fixed effects on F statistics. results = fmri_glm.compute_contrast(contrasts, output_type="all") .. GENERATED FROM PYTHON SOURCE LINES 164-173 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. .. rst-class:: sphx-glr-timing **Total running time of the script:** (2 minutes 22.572 seconds) **Estimated memory usage:** 973 MB .. _sphx_glr_download_auto_examples_04_glm_first_level_plot_spm_multimodal_faces.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/0.12.1?urlpath=lab/tree/notebooks/auto_examples/04_glm_first_level/plot_spm_multimodal_faces.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_spm_multimodal_faces.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_spm_multimodal_faces.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_spm_multimodal_faces.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_