.. 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_design_matrix.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_design_matrix.py: Examples of design matrices =========================== Three examples of design matrices specification and computation for first-level :term:`fMRI` data analysis (event-related design, block design, :term:`FIR` design). This examples requires matplotlib. .. GENERATED FROM PYTHON SOURCE LINES 14-17 Define parameters ----------------- At first, we define parameters related to the images acquisition. .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: Python import numpy as np from nilearn.plotting import plot_design_matrix tr = 1.0 # repetition time is 1 second n_scans = 128 # the acquisition comprises 128 scans frame_times = np.arange(n_scans) * tr # here are the corresponding frame times .. GENERATED FROM PYTHON SOURCE LINES 26-27 Then we define parameters related to the experimental design. .. GENERATED FROM PYTHON SOURCE LINES 27-39 .. code-block:: Python # these are the types of the different trials conditions = ["c0", "c0", "c0", "c1", "c1", "c1", "c3", "c3", "c3"] duration = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] # these are the corresponding onset times onsets = [30.0, 70.0, 100.0, 10.0, 30.0, 90.0, 30.0, 40.0, 60.0] # Next, we simulate 6 motion parameters jointly observed with fMRI acquisitions motion = np.cumsum(np.random.randn(n_scans, 6), 0) # The 6 parameters correspond to three translations and three # rotations describing rigid body motion add_reg_names = ["tx", "ty", "tz", "rx", "ry", "rz"] .. GENERATED FROM PYTHON SOURCE LINES 40-44 Create design matrices ---------------------- The same parameters allow us to obtain a variety of design matrices. We first create an events object. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python import pandas as pd events = pd.DataFrame( {"trial_type": conditions, "onset": onsets, "duration": duration} ) .. GENERATED FROM PYTHON SOURCE LINES 51-53 We sample the events into a design matrix, also including additional regressors. .. GENERATED FROM PYTHON SOURCE LINES 53-66 .. code-block:: Python from nilearn.glm.first_level import make_first_level_design_matrix hrf_model = "glover" X1 = make_first_level_design_matrix( frame_times, events, drift_model="polynomial", drift_order=3, add_regs=motion, add_reg_names=add_reg_names, hrf_model=hrf_model, ) .. GENERATED FROM PYTHON SOURCE LINES 67-70 Now we compute a block design matrix. We add duration to create the blocks. For this we first define an event structure that includes the duration parameter. .. GENERATED FROM PYTHON SOURCE LINES 70-76 .. code-block:: Python duration = 7.0 * np.ones(len(conditions)) events = pd.DataFrame( {"trial_type": conditions, "onset": onsets, "duration": duration} ) .. GENERATED FROM PYTHON SOURCE LINES 77-78 Then we sample the design matrix. .. GENERATED FROM PYTHON SOURCE LINES 78-87 .. code-block:: Python X2 = make_first_level_design_matrix( frame_times, events, drift_model="polynomial", drift_order=3, hrf_model=hrf_model, ) .. GENERATED FROM PYTHON SOURCE LINES 88-89 Finally we compute a :term:`FIR` model .. GENERATED FROM PYTHON SOURCE LINES 89-103 .. code-block:: Python events = pd.DataFrame( {"trial_type": conditions, "onset": onsets, "duration": duration} ) hrf_model = "FIR" X3 = make_first_level_design_matrix( frame_times, events, hrf_model="fir", drift_model="polynomial", drift_order=3, fir_delays=np.arange(1, 6), ) .. GENERATED FROM PYTHON SOURCE LINES 104-105 Here are the three designs side by side. .. GENERATED FROM PYTHON SOURCE LINES 105-115 .. code-block:: Python import matplotlib.pyplot as plt fig, (ax1, ax2, ax3) = plt.subplots(figsize=(10, 6), nrows=1, ncols=3) plot_design_matrix(X1, ax=ax1) ax1.set_title("Event-related design matrix", fontsize=12) plot_design_matrix(X2, ax=ax2) ax2.set_title("Block design matrix", fontsize=12) plot_design_matrix(X3, ax=ax3) ax3.set_title("FIR design matrix", fontsize=12) fig.show() .. image-sg:: /auto_examples/04_glm_first_level/images/sphx_glr_plot_design_matrix_001.png :alt: Event-related design matrix, Block design matrix, FIR design matrix :srcset: /auto_examples/04_glm_first_level/images/sphx_glr_plot_design_matrix_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.194 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_04_glm_first_level_plot_design_matrix.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.10.4?urlpath=lab/tree/notebooks/auto_examples/04_glm_first_level/plot_design_matrix.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_design_matrix.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_design_matrix.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_