.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_recover_events.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_recover_events.py: ================================================== Manually Recover Events Not Found by the Algorithm ================================================== In this example, we use ``pd-parser`` to find photodiode events that have corrupted pre-event baselines, photodiode plateaus or post-event baselines but not corrupted onsets or offsets. Note that it might be a good idea not to recover these events as there might be noise in the data around this time. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: default # Authors: Alex Rockhill # # License: BSD (3-clause) .. GENERATED FROM PYTHON SOURCE LINES 17-22 Simulate data and use it to make a raw object We'll make an `mne.io.Raw` object so that we can save out some random data with a photodiode event channel in it in fif format (a commonly used electrophysiology data format). .. GENERATED FROM PYTHON SOURCE LINES 22-82 .. code-block:: default import os.path as op import numpy as np import mock import mne from mne.utils import _TempDir import pd_parser from pd_parser.parse_pd import _load_data import matplotlib.pyplot as plt out_dir = _TempDir() # simulate photodiode data np.random.seed(29) n_events = 300 # let's make our photodiode events on random uniform from 0.25 to 0.75 seconds n_secs_on = np.random.random(n_events) * 0.5 + 0.25 raw, beh, events, _ = \ pd_parser.simulate_pd_data(n_events=n_events, n_secs_on=n_secs_on, prop_corrupted=0.0) sfreq = np.round(raw.info['sfreq']).astype(int) # corrupt some events corrupted_indices = [8, 144, 234] amount = raw._data.max() fig, axes = plt.subplots(1, len(corrupted_indices), figsize=(8, 4)) fig.suptitle('Corrupted Events') axes[0].set_ylabel('voltage') for j, i in enumerate(events[corrupted_indices, 0]): if j == 0: raw._data[0, i - sfreq // 5: i - sfreq // 10] = -amount elif j == 1: raw._data[0, i + sfreq // 4: i + sfreq // 3] = -amount else: raw._data[0, i + 3 * sfreq // 4: i + 5 * sfreq // 6] = amount axes[j].plot(np.linspace(-1, 2, 3 * sfreq), raw._data[0, i - sfreq: i + sfreq * 2]) axes[j].set_xlabel('time (s)') # make figure nicer fig.tight_layout() # make fake electrophysiology data info = mne.create_info(['ch1', 'ch2', 'ch3'], raw.info['sfreq'], ['seeg'] * 3) raw2 = mne.io.RawArray(np.random.random((3, raw.times.size)) * 1e-6, info) raw.add_channels([raw2]) raw.info['line_freq'] = 60 # for bids # add some offsets to the behavior so it's a bit more realistic offsets = np.random.randn(n_events) * 0.001 beh['time'] = np.array(beh['time']) + offsets # save to disk as required by ``pd-parser``, raw needs to have a filename fname = op.join(out_dir, 'sub-1_task-mytask_raw.fif') raw.save(fname) .. image-sg:: /auto_examples/images/sphx_glr_plot_recover_events_001.png :alt: Corrupted Events :srcset: /auto_examples/images/sphx_glr_plot_recover_events_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Creating RawArray with float64 data, n_channels=1, n_times=2039600 Range : 0 ... 2039599 = 0.000 ... 2039.599 secs Ready. Creating RawArray with float64 data, n_channels=3, n_times=2039600 Range : 0 ... 2039599 = 0.000 ... 2039.599 secs Ready. Writing /tmp/tmp_mne_tempdir__n31r5wk/sub-1_task-mytask_raw.fif Closing /tmp/tmp_mne_tempdir__n31r5wk/sub-1_task-mytask_raw.fif [done] .. GENERATED FROM PYTHON SOURCE LINES 83-91 Find the photodiode events relative to the behavioral timing of interest This function will use the default parameters to find and align the photodiode events, recovering the events that we just corrupted. Note that the mock function mocks user input so when you run the example, you want to delete that line and unindent the next line, and then provide your own input depending on whether you want to keep the events or not. .. GENERATED FROM PYTHON SOURCE LINES 91-96 .. code-block:: default with mock.patch('builtins.input', return_value='y'): pd_parser.parse_pd(fname, pd_event_name='Stim On', beh=beh, max_len=1.5, pd_ch_names=['pd'], beh_key='time', recover=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_recover_events_002.png :alt: Alignment (First 10), Event Differences :srcset: /auto_examples/images/sphx_glr_plot_recover_events_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_recover_events_003.png :alt: Corrupted Event 8 :srcset: /auto_examples/images/sphx_glr_plot_recover_events_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_recover_events_004.png :alt: Corrupted Event 144 :srcset: /auto_examples/images/sphx_glr_plot_recover_events_004.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_recover_events_005.png :alt: Corrupted Event 234 :srcset: /auto_examples/images/sphx_glr_plot_recover_events_005.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_recover_events_006.png :alt: Excluded Events, 8 recovered (not excluded), 144 recovered (not excluded), 234 recovered (not excluded) :srcset: /auto_examples/images/sphx_glr_plot_recover_events_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Reading in /tmp/tmp_mne_tempdir__n31r5wk/sub-1_task-mytask_raw.fif Opening raw data file /tmp/tmp_mne_tempdir__n31r5wk/sub-1_task-mytask_raw.fif... Range : 0 ... 2039599 = 0.000 ... 2039.599 secs Ready. Reading 0 ... 2039599 = 0.000 ... 2039.599 secs... Finding photodiode events 0%| | 0/606 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_recover_events.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_