.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_advanced/plot_surface_bids_analysis.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_07_advanced_plot_surface_bids_analysis.py: Surface-based dataset first and second level analysis of a dataset ================================================================== Full step-by-step example of fitting a :term:`GLM` (first and second level analysis) in a 10-subjects dataset and visualizing the results. More specifically: #. Download an :term:`fMRI` :term:`BIDS` dataset with two language conditions to contrast. #. Project the data to a standard mesh, fsaverage5, also known as the Freesurfer template :term:`mesh` downsampled to about 10k nodes per hemisphere. #. Run the first level model objects. #. Fit a second level model on the fitted first level models. Notice that in this case the preprocessed :term:`bold` images were already normalized to the same :term:`MNI` space. .. GENERATED FROM PYTHON SOURCE LINES 27-40 Fetch example :term:`BIDS` dataset ---------------------------------- We download a simplified :term:`BIDS` dataset made available for illustrative purposes. It contains only the necessary information to run a statistical analysis using Nilearn. The raw data subject folders only contain bold.json and events.tsv files, while the derivatives folder includes the preprocessed files preproc.nii and the confounds.tsv files. For more information see the :ref:`dataset description `. .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python from nilearn.datasets import fetch_language_localizer_demo_dataset data = fetch_language_localizer_demo_dataset() .. rst-class:: sphx-glr-script-out .. code-block:: none [fetch_language_localizer_demo_dataset] Dataset directory found: /home/runner/work/nilearn/nilearn/nilearn_data/fMRI-language-localizer-demo-dataset .. GENERATED FROM PYTHON SOURCE LINES 45-46 Here is the location of the dataset on disk. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python data.data_dir .. rst-class:: sphx-glr-script-out .. code-block:: none '/home/runner/work/nilearn/nilearn/nilearn_data/fMRI-language-localizer-demo-dataset' .. GENERATED FROM PYTHON SOURCE LINES 49-68 Subject level models -------------------- From the dataset directory we automatically obtain the FirstLevelModel objects with their subject_id filled from the :term:`BIDS` dataset. Along, we also obtain: - a list with the Nifti image associated with each run - a list of events read from events.tsv in the :term:`BIDS` dataset - a list of confounder motion regressors since in this case a confounds.tsv file is available in the :term:`BIDS` dataset. To get the first level models we only have to specify the dataset directory and the ``task_label`` as specified in the file names. .. GENERATED FROM PYTHON SOURCE LINES 68-79 .. code-block:: Python from nilearn.glm.first_level import first_level_from_bids models, run_imgs, events, confounds = first_level_from_bids( dataset_path=data.data_dir, task_label="languagelocalizer", space_label="", img_filters=[("desc", "preproc")], smoothing_fwhm=6, n_jobs=2, ) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/nilearn/nilearn/examples/07_advanced/plot_surface_bids_analysis.py:70: RuntimeWarning: 'StartTime' not found in file /home/runner/work/nilearn/nilearn/nilearn_data/fMRI-language-localizer-demo-dataset/derivatives/sub-01/func/sub-01_task-languagelocalizer_desc-preproc_bold.json. /home/runner/work/nilearn/nilearn/examples/07_advanced/plot_surface_bids_analysis.py:70: UserWarning: 'slice_time_ref' not provided and cannot be inferred from metadata. It will be assumed that the slice timing reference is 0.0 percent of the repetition time. If it is not the case it will need to be set manually in the generated list of models. .. GENERATED FROM PYTHON SOURCE LINES 80-101 Project :term:`fMRI` data to the surface, fit the GLM and compute contrasts The projection function simply takes the :term:`fMRI` data and the mesh. Note that those correspond spatially, as they are both in same space. .. warning:: Note that here we pass ALL the confounds when we fit the model. In this case we can do this because our regressors only include the motion realignment parameters. For most preprocessed BIDS dataset, you would have to carefully choose which confounds to include. When working with a typical BIDS derivative dataset generated by fmriprep, the :obj:`~nilearn.glm.first_level.first_level_from_bids` function allows you to indirectly pass arguments to :obj:`~nilearn.interfaces.fmriprep.load_confounds`, so you can selectively load specific subsets of confounds to implement certain denoising strategies. .. GENERATED FROM PYTHON SOURCE LINES 101-154 .. code-block:: Python from pathlib import Path from nilearn.datasets import load_fsaverage, load_fsaverage_data from nilearn.surface import SurfaceImage fsaverage5 = load_fsaverage() # let's get the fsaverage curvature data image # to use as background for the GLM report. curvature = load_fsaverage_data(mesh_type="inflated", data_type="curvature") threshold = 1.96 # Empty lists in which we are going to store activation values. z_scores = [] z_scores_left = [] z_scores_right = [] for i, (first_level_glm, fmri_img, confound, event) in enumerate( zip(models, run_imgs, confounds, events, strict=False) ): print(f"Running GLM on {Path(fmri_img[0]).relative_to(data.data_dir)}") image = SurfaceImage.from_volume( mesh=fsaverage5["pial"], volume_img=fmri_img[0], ) # Fit GLM. # Pass events and all confounds first_level_glm.fit( run_imgs=image, events=event[0], confounds=confound[0], ) # Compute contrast between 'language' and 'string' events z_scores.append( first_level_glm.compute_contrast( "language-string", stat_type="t", output_type="z_score" ) ) # Let's only generate a report for the first subject if i == 1: report_flm = first_level_glm.generate_report( contrasts="language-string", threshold=threshold, height_control=None, alpha=0.001, bg_img=curvature, title="surface based subject-level model", ) .. rst-class:: sphx-glr-script-out .. code-block:: none Running GLM on derivatives/sub-01/func/sub-01_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-02/func/sub-02_task-languagelocalizer_desc-preproc_bold.nii.gz /home/runner/work/nilearn/nilearn/examples/07_advanced/plot_surface_bids_analysis.py:145: RuntimeWarning: Meshes are not identical but have compatible number of vertices. Running GLM on derivatives/sub-03/func/sub-03_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-04/func/sub-04_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-05/func/sub-05_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-06/func/sub-06_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-07/func/sub-07_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-08/func/sub-08_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-09/func/sub-09_task-languagelocalizer_desc-preproc_bold.nii.gz Running GLM on derivatives/sub-10/func/sub-10_task-languagelocalizer_desc-preproc_bold.nii.gz .. GENERATED FROM PYTHON SOURCE LINES 155-159 View the GLM report of the first subject .. include:: ../../../examples/report_note.rst .. GENERATED FROM PYTHON SOURCE LINES 159-162 .. code-block:: Python report_flm .. raw:: html

surface based subject-level model Implement the General Linear Model for single run :term:`fMRI` data.

Description

Data were analyzed using Nilearn (version= 0.14.1; RRID:SCR_001362).

At the subject level, a mass univariate analysis was performed with a linear regression at each voxel of the brain, using generalized least squares with a global ar1 noise model to account for temporal auto-correlation and a cosine drift model (high pass filter=0.01 Hz).

Regressors were entered into run-specific design matrices and onsets were convolved with a glover canonical hemodynamic response function for the following conditions:

  • language
  • string

Input images were smoothed with gaussian kernel (full-width at half maximum=6 mm).

The following contrasts were computed using a fixed-effect approach across runs :

  • language-string

Model details

Value
Parameter
drift_model cosine
high_pass (Hertz) 0.01
hrf_model glover
noise_model ar1
signal_scaling 0
slice_time_ref 0.0
smoothing_fwhm (mm) 6
standardize False
subject_label 02
t_r (seconds) 1.5

Mask

Mask image

The mask includes 20484 voxels (100.0 %) of the image.

Statistical Maps

language-string

Stat map plot for the contrast: language-string
Cluster Table
Height control None
Threshold Z 1.96
Cluster ID Hemisphere Peak Stat Cluster Size (vertices)
1 left 10.44 249
2 left 6.76 81
3 left 2.43 7
4 left 9.64 665
5 left 3.66 41
6 left 3.26 15
7 left 4.77 108
8 left 4.67 107
9 left 3.10 10
10 left 3.21 20
11 left 2.50 4
12 left 3.58 56
13 left 4.59 63
14 left 2.49 4
15 left 5.80 29
16 left 5.32 23
17 left 7.32 73
18 left 3.75 26
19 left 2.79 17
20 left 3.42 20
21 left 2.29 2
22 left 2.49 4
23 left 2.77 8
24 left 4.41 30
25 left 2.21 3
26 left 2.42 10
27 left 3.40 19
28 left 2.79 17
29 left 3.90 26
30 left 3.82 30
31 left 3.31 24
32 left 2.93 12
33 left 2.18 4
34 left 2.83 9
35 left 2.32 3
36 left 2.56 6
37 left 2.18 2
38 left 2.13 4
39 left 2.44 2
40 left 2.35 4
41 left 2.15 4
42 left 2.66 13
43 left 2.63 4
44 left 2.72 4
45 left 2.12 2
46 left 2.55 7
47 left 2.05 2
48 left 2.46 8
49 left 2.71 17
50 left 2.01 1
51 left 2.03 1
52 left 2.62 4
53 left 2.01 1
54 left 2.14 2
55 left 1.99 1
56 right 8.55 195
57 right 4.10 36
58 right 4.23 66
59 right 5.91 192
60 right 3.15 53
61 right 7.85 272
62 right 3.06 31
63 right 4.34 37
64 right 2.41 9
65 right 7.97 132
66 right 3.55 29
67 right 3.05 28
68 right 4.28 23
69 right 3.01 16
70 right 4.39 30
71 right 2.68 12
72 right 5.18 36
73 right 5.08 31
74 right 2.34 6
75 right 2.67 9
76 right 2.97 13
77 right 3.36 9
78 right 2.64 5
79 right 2.49 15
80 right 2.54 5
81 right 2.82 19
82 right 2.52 4
83 right 2.52 14
84 right 2.06 1
85 right 2.62 7
86 right 3.15 10
87 right 2.69 3
88 right 2.73 8
89 right 2.49 6
90 right 2.34 3
91 right 3.73 12
92 right 2.16 3
93 right 1.98 1
94 right 1.99 2
95 right 2.16 2
96 right 2.13 2
97 right 2.27 3
98 right 2.15 2
99 right 2.05 1
100 right 2.11 1
101 right 2.27 1
102 right 2.20 1

About

  • Date preprocessed:


.. GENERATED FROM PYTHON SOURCE LINES 163-171 Group level model ----------------- Individual activation maps have been accumulated in the ``z_score``. We can now use them in a one-sample t-test at the group level model by passing them as input to :class:`~nilearn.glm.second_level.SecondLevelModel`. .. GENERATED FROM PYTHON SOURCE LINES 171-188 .. code-block:: Python import pandas as pd from nilearn.glm.second_level import SecondLevelModel second_level_glm = SecondLevelModel() design_matrix = pd.DataFrame([1] * len(z_scores), columns=["intercept"]) second_level_glm.fit(second_level_input=z_scores, design_matrix=design_matrix) report_slm = second_level_glm.generate_report( contrasts=["intercept"], threshold=threshold, height_control=None, alpha=0.001, bg_img=curvature, title="surface based group-level model", ) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/nilearn/nilearn/examples/07_advanced/plot_surface_bids_analysis.py:179: RuntimeWarning: Meshes are not identical but have compatible number of vertices. .. GENERATED FROM PYTHON SOURCE LINES 189-193 View the GLM report at the group level. .. include:: ../../../examples/report_note.rst .. GENERATED FROM PYTHON SOURCE LINES 193-194 .. code-block:: Python report_slm .. raw:: html

surface based group-level model Implement the :term:`General Linear Model` for multiple subject :term:`fMRI` data.

Description

Data were analyzed using Nilearn (version= 0.14.1; RRID:SCR_001362).

At the group level, a mass univariate analysis was performed with a linear regression at each voxel of the brain.

The following contrasts were computed :

  • intercept

Model details

Mask

Mask image

The mask includes 20484 voxels (100.0 %) of the image.

Statistical Maps

intercept

Stat map plot for the contrast: intercept
Cluster Table
Height control None
Threshold Z 1.96
Cluster ID Hemisphere Peak Stat Cluster Size (vertices)
1 left 4.50 378
2 left 3.12 14
3 left 3.10 44
4 left 3.63 18
5 left 3.64 90
6 left 3.52 43
7 left 3.10 16
8 left 3.09 20
9 left 2.38 4
10 left 2.71 8
11 left 2.39 3
12 left 2.97 25
13 left 2.15 1
14 left 2.07 2
15 left 2.31 4
16 left 2.30 3
17 left 1.98 1
18 left 1.97 1
19 left 1.99 1
20 left 2.07 1
21 left 2.04 2
22 right 3.83 114
23 right 4.04 131
24 right 2.15 3
25 right 2.52 5
26 right 2.34 10
27 right 2.29 5
28 right 2.67 7
29 right 2.43 12
30 right 2.45 10
31 right 2.71 7
32 right 2.42 3
33 right 2.32 5
34 right 2.10 2
35 right 2.33 2
36 right 2.55 4
37 right 2.05 1
38 right 2.25 2
39 right 2.17 1
40 right 1.96 1
41 right 2.18 2
42 right 2.22 1

About

  • Date preprocessed:


.. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 37.698 seconds) **Estimated memory usage:** 1137 MB .. _sphx_glr_download_auto_examples_07_advanced_plot_surface_bids_analysis.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.14.1?urlpath=lab/tree/notebooks/auto_examples/07_advanced/plot_surface_bids_analysis.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_surface_bids_analysis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_surface_bids_analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_surface_bids_analysis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_