Resampling#

This example demonstrates how to resample a recording.

Note

The lemon datasets used in this tutorial is composed of EEGLAB files. To use the MNE reader mne.io.read_raw_eeglab(), the pymatreader optional dependency is required. Use the following installation method appropriate for your environment:

  • pip install pymatreader

  • conda install -c conda-forge pymatreader

Note that an environment created via the MNE installers includes pymatreader by default.

import mne
from mne.io import read_raw_eeglab

from pycrostates.datasets import lemon


raw_fname = lemon.data_path(subject_id='010004', condition='EC')
raw = read_raw_eeglab(raw_fname, preload=True)
raw.crop(0, 30)

raw.pick('eeg')
raw.set_eeg_reference('average')
/home/docs/checkouts/readthedocs.org/user_builds/pycrostates/checkouts/0.6.1/tutorials/preprocessing/10_resampling.py:32: RuntimeWarning: Limited 1 annotation(s) that were expanding outside the data range.
  raw = read_raw_eeglab(raw_fname, preload=True)
/home/docs/checkouts/readthedocs.org/user_builds/pycrostates/checkouts/0.6.1/tutorials/preprocessing/10_resampling.py:32: RuntimeWarning: The data contains 'boundary' events, indicating data discontinuities. Be cautious of filtering and epoching around these events.
  raw = read_raw_eeglab(raw_fname, preload=True)
General
Measurement date Unknown
Experimenter Unknown
Participant Unknown
Channels
Digitized points 64 points
Good channels 61 EEG
Bad channels None
EOG channels Not available
ECG channels Not available
Data
Sampling frequency 250.00 Hz
Highpass 0.00 Hz
Lowpass 125.00 Hz
Filenames sub-010004_EC.fdt
Duration 00:00:30 (HH:MM:SS)


The resample() function draws n_resamples of n_samples from a recording where n_samples defines the number of samples contained in each epoch and n_resamples defines the number of epochs to draw.

from pycrostates.preprocessing import resample
resamples = resample(raw, n_resamples=3, n_samples=150, random_state=40)
resamples
[< ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >]

The coverage parameter can be used to automatically compute one of the two previous parameters based on the amount of original data to cover. For example, by setting n_resamples and coverage:

resamples = resample(raw, n_resamples=3, coverage=0.5, random_state=40)
resamples
[< ChData | 1250 samples >, < ChData | 1250 samples >, < ChData | 1250 samples >]

or by setting n_samples and coverage:

resamples = resample(raw, n_samples=1000, coverage=0.5, random_state=40)
resamples
[< ChData | 1000 samples >, < ChData | 1000 samples >, < ChData | 1000 samples >]

Finally, this function can be used to resample Epochs.

epochs = mne.make_fixed_length_epochs(raw, duration=2, preload=True)
resamples = resample(epochs, n_samples=150, coverage=0.5, random_state=40)
resamples
[< ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >, < ChData | 150 samples >]

Total running time of the script: (0 minutes 1.139 seconds)

Estimated memory usage: 239 MB

Gallery generated by Sphinx-Gallery