.. 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_hrf.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` 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_hrf.py: Example of MRI response functions ================================= Within this example we are going to plot the hemodynamic response function (:term:`HRF`) model in :term:`SPM` together with the :term:`HRF` shape proposed by G.Glover, as well as their time and dispersion derivatives. We also illustrate how users can input a custom response function, which can for instance be useful when dealing with non human primate data acquired using a contrast agent. In our case, we input a custom response function for MION, a common `agent `_ used to enhance contrast on MRI images of monkeys. The :term:`HRF` is the filter which couples neural responses to the metabolic-related changes in the MRI signal. :term:`HRF` models are simply phenomenological. In current analysis frameworks, the choice of :term:`HRF` model is essentially left to the user. Fortunately, using the :term:`SPM` or Glover model does not make a huge difference. Adding derivatives should be considered whenever timing information has some degree of uncertainty, and is actually useful to detect timing issues. This example requires matplotlib and scipy. .. GENERATED FROM PYTHON SOURCE LINES 31-36 Define stimulus parameters and response models ---------------------------------------------- To get an impulse response, we simulate a single event occurring at time t=0, with duration 1s. .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. code-block:: default import numpy as np time_length = 30.0 frame_times = np.linspace(0, time_length, 61) onset, amplitude, duration = 0.0, 1.0, 1.0 exp_condition = np.array((onset, duration, amplitude)).reshape(3, 1) .. GENERATED FROM PYTHON SOURCE LINES 45-46 Make a time array of this condition for display: .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: default stim = np.zeros_like(frame_times) stim[(frame_times > onset) * (frame_times <= onset + duration)] = amplitude .. GENERATED FROM PYTHON SOURCE LINES 50-52 Define custom response functions for MION. Custom response functions should at least take tr and oversampling as arguments: .. GENERATED FROM PYTHON SOURCE LINES 52-111 .. code-block:: default from scipy.stats import gamma def mion_response_function(tr, oversampling=16, onset=0.0): """Implementation of the MION response function model. Parameters ---------- tr: float scan repeat time, in seconds oversampling: int, optional temporal oversampling factor onset: float, optional hrf onset time, in seconds Returns ------- response_function: array of shape(length / tr * oversampling, dtype=float) response_function sampling on the oversampled time grid """ dt = tr / oversampling time_stamps = np.linspace( 0, time_length, np.rint(float(time_length) / dt).astype(int) ) time_stamps -= onset # parameters of the gamma function delay = 1.55 dispersion = 5.5 response_function = gamma.pdf(time_stamps, delay, loc=0, scale=dispersion) response_function /= response_function.sum() response_function *= -1 return response_function def mion_time_derivative(tr, oversampling=16.0): """Implementation of the MION time derivative response function model. Parameters ---------- tr: float scan repeat time, in seconds oversampling: int, optional temporal oversampling factor, optional Returns ------- drf: array of shape(time_length / tr * oversampling, dtype=float) derived_response_function sampling on the provided grid """ do = 0.1 drf = ( mion_response_function(tr, oversampling) - mion_response_function(tr, oversampling, do) ) / do return drf .. GENERATED FROM PYTHON SOURCE LINES 112-113 Define response function models to be displayed: .. GENERATED FROM PYTHON SOURCE LINES 113-124 .. code-block:: default rf_models = [ ("spm + derivative + dispersion", "SPM HRF", None), ("glover + derivative + dispersion", "Glover HRF", None), ( [mion_response_function, mion_time_derivative], "Mion RF + derivative", ["main", "main_derivative"], ), ] .. GENERATED FROM PYTHON SOURCE LINES 125-127 Sample and plot response functions ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 127-164 .. code-block:: default import matplotlib.pyplot as plt from nilearn.glm.first_level import compute_regressor oversampling = 16 fig = plt.figure(figsize=(9, 4)) for i, (rf_model, model_title, labels) in enumerate(rf_models): # compute signal of interest by convolution signal, _labels = compute_regressor( exp_condition, rf_model, frame_times, con_id="main", oversampling=oversampling, ) # plot signal plt.subplot(1, len(rf_models), i + 1) plt.fill(frame_times, stim, "k", alpha=0.5, label="stimulus") for j in range(signal.shape[1]): plt.plot( frame_times, signal.T[j], label=( labels[j] if labels is not None else (_labels[j] if _labels is not None else None) ), ) plt.xlabel("time (s)") plt.legend(loc=1) plt.title(model_title) # adjust plot plt.subplots_adjust(bottom=0.12) plt.show() .. image-sg:: /auto_examples/04_glm_first_level/images/sphx_glr_plot_hrf_001.png :alt: SPM HRF, Glover HRF, Mion RF + derivative :srcset: /auto_examples/04_glm_first_level/images/sphx_glr_plot_hrf_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.277 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_04_glm_first_level_plot_hrf.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn.github.io/main?filepath=examples/auto_examples/04_glm_first_level/plot_hrf.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_hrf.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_hrf.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_