.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/temporal-response-functions/epoch_impulse.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_epoch_impulse.py: .. _exa-impulse: Impulse predictors for epochs ============================= :func:`epoch_impulse_predictor` generates predictor variables for reverse correlation in trial-based experiments with discrete events. The function generates one impulse per trial, and these impulsescan be of varaiable magnitude and have variable latency. The example uses simulated data meant to vaguely resemble data from an N400 experiment, but not intended as a physiologically realistic simulation. .. contents:: Sections :local: :backlinks: top .. GENERATED FROM PYTHON SOURCE LINES 20-26 .. code-block:: Python # sphinx_gallery_thumbnail_number = 2 from eelbrain import * ds = datasets.simulate_erp(snr=1) print(ds.summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Key Type Values ------------------------------------------------------------------------- eeg NDVar 140 time, 65 sensor; -7.47251e-06 - 7.20853e-06 cloze Var 0.00563694 - 0.997675 predictability Factor high:40, low:40 n_chars Var 3:10, 4:20, 5:22, 6:16, 7:12 ------------------------------------------------------------------------- Dataset: 80 cases .. GENERATED FROM PYTHON SOURCE LINES 27-30 Discrete events --------------- Computing a TRF for an impulse at trial onset is very similar to averaging: .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python any_trial = epoch_impulse_predictor('eeg', 1, data=ds) fit = boosting('eeg', any_trial, -0.100, 0.600, basis=0.050, data=ds, partitions=2, delta=0.01) average = ds['eeg'].mean('case') trf = fit.h.sub(time=(average.time.tmin, average.time.tstop)) p = plot.TopoButterfly([fit.h_scaled, average], xlim=(-0.100, 0.600), axtitle=['Impulse response', 'Average'], t=0.400) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_001.png :alt: epoch impulse :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-43 Categorial variables -------------------- Impulse predictors can be used like dummy codes in a regression model. Use one impulse to code for occurrence of any word (``any_word``), and a second impulse to code for unpredictable words only (``cloze``): .. GENERATED FROM PYTHON SOURCE LINES 43-52 .. code-block:: Python any_word = epoch_impulse_predictor('eeg', 1, data=ds, name='any_word') # effect code for cloze (1 for low cloze, -1 for high cloze) cloze_code = Var.from_dict(ds['predictability'], {'high': 0, 'low': 1}) low_cloze = epoch_impulse_predictor('eeg', cloze_code, data=ds, name='low_cloze') # plot the predictors for each trial p = plot.UTS([any_word, low_cloze], '.case', stem=True) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_002.png :alt: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79 :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-56 Estimate response functions for these two predictors. Based on the coding, ``any_word`` reflects the response to predictable words, and ``low_cloze`` reflects how unpredictable words differ from predictable words: .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python fit = boosting('eeg', [any_word, low_cloze], 0, 0.5, basis=0.050, model='predictability', data=ds, partitions=2, delta=0.01) p = plot.TopoButterfly(fit.h, xlim=(-0.100, 0.600), t=0.400) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_003.png :alt: epoch impulse :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-64 Continuous variables -------------------- Impulse predictors can similarly accommodate continuous variables: .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. code-block:: Python # effect code for cloze (1 for low cloze, -1 for high cloze) n_chars = epoch_impulse_predictor('eeg', 'n_chars', data=ds, name='n_chars') surprisal = epoch_impulse_predictor('eeg', '-numpy.log2(cloze)', data=ds, name='surprisal') # plot the predictors for each trial p = plot.UTS([n_chars, surprisal, any_word], '.case', stem=True) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_004.png :alt: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79 :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-77 Estimate response functions. Based on the coding, ``any_word`` reflects the hypothetical response to a words with 0 characters and 0 surprisal, whereas the two other predictors reflect the change in response by ``n_chars`` and surprisal: .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python fit = boosting('eeg', [any_word, n_chars, surprisal], 0, 0.5, basis=0.050, model='predictability', data=ds, partitions=2, delta=0.01) p = plot.TopoButterfly(fit.h, xlim=(-0.100, 0.600), t=0.400) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_005.png :alt: epoch impulse :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_epoch_impulse_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 27.091 seconds) .. _sphx_glr_download_auto_examples_temporal-response-functions_epoch_impulse.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: epoch_impulse.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: epoch_impulse.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: epoch_impulse.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_