.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ERP_reconstruction.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_examples_ERP_reconstruction.py: ERP Construction. ===================== Example on how to interpolate missing channels. .. GENERATED FROM PYTHON SOURCE LINES 6-11 .. code-block:: Python import os import matplotlib.pyplot as plt import mne .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python import numpy as np from eegrasp import EEGrasp current_dir = os.getcwd() os.chdir(os.path.dirname(current_dir)) data_dir = './datasets' #os.makedirs("./datasets", exist_ok=True) #os.environ['MNE_EEGBCI_PATH'] = './datasets/' .. GENERATED FROM PYTHON SOURCE LINES 23-45 .. code-block:: Python subjects = np.arange(1, 10) runs = [4, 8, 12] # Download eegbci dataset through MNE # Comment the following line if already downloaded raw_fnames = [ mne.datasets.eegbci.load_data(s, runs, path=data_dir, update_path=True) for s in subjects ] raw_fnames = np.reshape(raw_fnames, -1) raws = [mne.io.read_raw_edf(f, preload=True) for f in raw_fnames] raw = mne.concatenate_raws(raws) mne.datasets.eegbci.standardize(raw) raw.annotations.rename(dict(T1='left', T2='right')) montage = mne.channels.make_standard_montage('standard_1005') raw.set_montage(montage) eeg_pos = np.array( [pos for _, pos in raw.get_montage().get_positions()['ch_pos'].items()]) ch_names = montage.ch_names .. rst-class:: sphx-glr-script-out .. code-block:: none Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S002/S002R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S002/S002R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S002/S002R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S003/S003R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S003/S003R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S003/S003R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S004/S004R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S004/S004R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S004/S004R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S005/S005R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S005/S005R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S005/S005R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S006/S006R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S006/S006R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S006/S006R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S007/S007R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S007/S007R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S007/S007R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19999 = 0.000 ... 124.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S008/S008R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S008/S008R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S008/S008R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S009/S009R04.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S009/S009R08.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... Extracting EDF parameters from /home/docs/checkouts/readthedocs.org/user_builds/eegrasp/checkouts/latest/examples/datasets/MNE-eegbci-data/files/eegmmidb/1.0.0/S009/S009R12.edf... EDF file detected Setting channel info structure... Creating raw.info structure... Reading 0 ... 19679 = 0.000 ... 122.994 secs... .. GENERATED FROM PYTHON SOURCE LINES 46-53 .. code-block:: Python L_FREQ = 1 # Hz H_FREQ = 30 # Hz raw.filter(L_FREQ, H_FREQ, fir_design='firwin', skip_by_annotation='edge') raw, ref_data = mne.set_eeg_reference(raw) events, events_id = mne.events_from_annotations(raw) .. rst-class:: sphx-glr-script-out .. code-block:: none Filtering raw data in 27 contiguous segments Setting up band-pass filter from 1 - 30 Hz FIR filter parameters --------------------- Designing a one-pass, zero-phase, non-causal bandpass filter: - Windowed time-domain design (firwin) method - Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation - Lower passband edge: 1.00 - Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz) - Upper passband edge: 30.00 Hz - Upper transition bandwidth: 7.50 Hz (-6 dB cutoff frequency: 33.75 Hz) - Filter length: 529 samples (3.306 s) [Parallel(n_jobs=1)]: Done 17 tasks | elapsed: 0.0s [Parallel(n_jobs=1)]: Done 64 out of 64 | elapsed: 0.0s finished EEG channel type selected for re-referencing Applying average reference. Applying a custom ('EEG',) reference. Used Annotations descriptions: [np.str_('T0'), np.str_('left'), np.str_('right')] .. GENERATED FROM PYTHON SOURCE LINES 54-55 Exclude bad channels .. GENERATED FROM PYTHON SOURCE LINES 55-61 .. code-block:: Python TMIN, TMAX = -1.0, 3.0 picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = mne.Epochs(raw, events, events_id, picks=picks, tmin=TMIN, tmax=TMAX, baseline=(-1, 0), detrend=1) .. rst-class:: sphx-glr-script-out .. code-block:: none Not setting metadata 810 matching events found Applying baseline correction (mode: mean) 0 projection items activated .. GENERATED FROM PYTHON SOURCE LINES 62-72 .. code-block:: Python left = epochs['left'] erp_left = left.average() right = epochs['right'] erp_right = right.average() # Use only data on the Left condition to find # the best distance (epsilon) value data = erp_left.get_data() .. GENERATED FROM PYTHON SOURCE LINES 73-84 .. code-block:: Python # 1. Define index of the missing channel MISSING_IDX = 5 # 2. Initialize instance of EEGrasp eegsp = EEGrasp(data, eeg_pos, ch_names) # 3. Compute the electrode distance matrix dist_mat = eegsp.compute_distance(normalize=True) # 4. Find the best parameter for the channel results = eegsp.fit_sigma(missing_idx=MISSING_IDX, distances=dist_mat, epsilon=0.5, min_sigma=0.01, max_sigma=0.5, step=0.05) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/10 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ERP_reconstruction.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ERP_reconstruction.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_