<jupyter_start><jupyter_text>.. _tut_epochs_objects:The :class:`Epochs ` data structure: epoched data=============================================================<jupyter_code>from __future__ import print_function

import mne
import os.path as op
import numpy as np
from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>:class:`Epochs ` objects are a way of representing continuousdata as a collection of time-locked trials, stored in an array of`shape(n_events, n_channels, n_times)`. They are useful for many statisticalmethods in neuroscience, and make it easy to quickly overview what occursduring a trial.:class:`Epochs ` objects can be created in three ways: 1. From a :class:`Raw ` object, along with event times 2. From an :class:`Epochs ` object that has been saved as a    `.fif` file 3. From scratch using :class:`EpochsArray `<jupyter_code># Load a dataset that contains events
raw = mne.io.RawFIF(
    op.join(mne.datasets.sample.data_path(), 'MEG', 'sample',
            'sample_audvis_raw.fif'))

# If your raw object has a stim channel, you can construct an event array
# easily
events = mne.find_events(raw, stim_channel='STI 014')

# Show the number of events (number of rows)
print('Number of events:', len(events))

# Show all unique event codes (3rd column)
print('Unique event codes:', np.unique(events[:, 2]))

# Specify event codes of interest with descriptive labels
event_id = dict(left=1, right=2)<jupyter_output><empty_output><jupyter_text>Now, we can create an :class:`mne.Epochs` object with the events we'veextracted. Note that epochs constructed in this manner will not have theirdata available until explicitly read into memory, which you can do with:func:`get_data `. Alternatively, you can use`preload=True`.Note that there are many options available when loading an:class:`mne.Epochs` object.  For more detailed information, see (**LINK TOEPOCHS LOADING TUTORIAL**)<jupyter_code># Expose the raw data as epochs, cut from -0.1 s to 1.0 s relative to the event
# onsets
epochs = mne.Epochs(raw, events, event_id, tmin=-0.1, tmax=1,
                    baseline=(None, 0), preload=True)
print(epochs)<jupyter_output><empty_output><jupyter_text>Epochs behave similarly to :class:`mne.io.Raw` objects. They have an:class:`info ` attribute that has all of the sameinformation, as well as a number of attributes unique to the events containedwithin the object.<jupyter_code>print(epochs.events[:3], epochs.event_id, sep='\n\n')<jupyter_output><empty_output><jupyter_text>You can select subsets of epochs by indexing the :class:`Epochs `object directly. Alternatively, if you have epoch names specified in`event_id` then you may index with strings instead.<jupyter_code>print(epochs[1:5])
print(epochs['right'])<jupyter_output><empty_output><jupyter_text>It is also possible to iterate through :class:`Epochs ` objectsin this way. Note that behavior is different if you iterate on `Epochs`directly rather than indexing:<jupyter_code># These will be epochs objects
for i in range(3):
    print(epochs[i])

# These will be arrays
for ep in epochs[:2]:
    print(ep)<jupyter_output><empty_output><jupyter_text>If you wish to look at the average across trial types, then you may do so,creating an `Evoked` object in the process.<jupyter_code>ev_left = epochs['left'].average()
ev_right = epochs['right'].average()

f, axs = plt.subplots(3, 2, figsize=(10, 5))
_ = f.suptitle('Left / Right', fontsize=20)
_ = ev_left.plot(axes=axs[:, 0], show=False)
_ = ev_right.plot(axes=axs[:, 1], show=False)
plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11995546896743668, 'fraction_numerical': 0.008906206512663512, 'mean_word_length': 5.858778625954199, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '298879', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1120, 'n_words': 389, 'path': '0.14/_downloads/plot_epochs_objects.ipynb', 'repo_name': 'drammock/mne-tools.github.io', 'size': '3593'}