.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/temporal-response-functions/trf_intro.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_temporal-response-functions_trf_intro.py: .. _exa-trf_intro: .. currentmodule:: eelbrain Introduction to Temporal Response Functions (TRFs) ================================================== A Temporal Response Function (TRF) is a linear stimulus-response model. The predicted response is a linear convolution of the stimulus with the TRF. A simple illustration of this is that every impulse in the stimulus is associated with a corresponding response, shaped like the TRF: .. GENERATED FROM PYTHON SOURCE LINES 15-36 .. code-block:: Python from eelbrain import * import numpy as np # Construct a 10 s long stimulus time = UTS(0, 0.01, 1000) x = NDVar(np.zeros(len(time)), time) # add a few impulses x[1] = 1 x[3] = 1 x[5] = 1 # Construct a TRF of length 500 ms trf_time = UTS(0, 0.01, 50) trf = gaussian(0.200, 0.050, trf_time) - gaussian(0.300, 0.050, trf_time) # The response is the convolution of the stimulus with the TRF y = convolve(trf, x) plot_args = dict(columns=1, axh=2, w=10, frame='t', legend=False, colors='r') p = plot.UTS([x, trf, y], ylabel=['Stimulus (x)', 'TRF', 'Response (y)'], **plot_args) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_001.png :alt: trf intro :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 37-41 Because the convolution is linear: - Scaled stimuli cause scaled responses - Overlapping responses add up .. GENERATED FROM PYTHON SOURCE LINES 41-53 .. code-block:: Python x[2] = 0.66 x[5.2] = 1 x[7] = 0.8 x[7.2] = 0.3 x[8.8] = 0.2 x[9] = 0.5 x[9.2] = 0.7 y = convolve(trf, x) p = plot.UTS([x, trf, y], ylabel=['Stimulus (x)', 'TRF', 'Response (y)'], **plot_args) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_002.png :alt: trf intro :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-56 When the stimulus contains only non-zero elements this works just the same, but the TRF shape may not be apparent in the response anymore: .. GENERATED FROM PYTHON SOURCE LINES 56-62 .. code-block:: Python x += np.random.normal(0, 0.1, x.shape) filter_data(x, 1, 40) y = convolve(trf, x) p = plot.UTS([x, trf, y], ylabel=['Stimulus (x)', 'TRF', 'Response (y)'], **plot_args) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_003.png :alt: trf intro :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-66 Given a stimulus and a response, there are different methods to reconstruct the TRF. Eelbrain comes with an implementation of :func:`boosting`, a coordinate descent algorithm with early stopping based on cross-validation: .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python fit = boosting(y, x, 0.000, 0.500, basis=0.050, partitions=2) plot_args = {**plot_args, 'columns': 3} p = plot.UTS([trf, fit.h, None], axtitle=["Model TRF", "Reconstructed TRF"], **plot_args) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_004.png :alt: Model TRF, Reconstructed TRF :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_trf_intro_004.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_examples_temporal-response-functions_trf_intro.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: trf_intro.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: trf_intro.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: trf_intro.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_