from __future__ import print_function
import mne
import numpy as np
Info
objects¶Note
for full documentation on the Info object, see The Info data structure.
Normally, mne.io.meas_info.Info
objects are created by the various
data import functions <ch_raw>`.
However, if you wish to create one from scratch, you can use the
mne.create_info()
function to initialize the minimally required
fields. Further fields can be assigned later as one would with a regular
dictionary.
The following creates the absolute minimum info structure:
# Create some dummy metadata
n_channels = 32
sampling_rate = 200
info = mne.create_info(32, sampling_rate)
print(info)
Script output:
<Info | 15 non-empty fields
bads : list | 0 items
ch_names : list | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
chs : list | 32 items (MISC: 32)
comps : list | 0 items
custom_ref_applied : bool | False
dev_head_t : 'mne.transforms.Transform | 3 items
events : list | 0 items
highpass : float | 0.0 Hz
hpi_meas : list | 0 items
hpi_results : list | 0 items
lowpass : float | 100.0 Hz
meas_date : numpy.ndarray | 1970-01-01 01:00:00
nchan : int | 32
projs : list | 0 items
sfreq : float | 200.0 Hz
acq_pars : NoneType
acq_stim : NoneType
buffer_size_sec : NoneType
ctf_head_t : NoneType
description : NoneType
dev_ctf_t : NoneType
dig : NoneType
experimenter : NoneType
file_id : NoneType
filename : NoneType
hpi_subsystem : NoneType
line_freq : NoneType
meas_id : NoneType
proj_id : NoneType
proj_name : NoneType
subject_info : NoneType
>
You can also supply more extensive metadata:
# Names for each channel
channel_names = ['MEG1', 'MEG2', 'Cz', 'Pz', 'EOG']
# The type (mag, grad, eeg, eog, misc, ...) of each channel
channel_types = ['grad', 'grad', 'eeg', 'eeg', 'eog']
# The sampling rate of the recording
sfreq = 1000 # in Hertz
# The EEG channels use the standard naming strategy.
# By supplying the 'montage' parameter, approximate locations
# will be added for them
montage = 'standard_1005'
# Initialize required fields
info = mne.create_info(channel_names, sfreq, channel_types, montage)
# Add some more information
info['description'] = 'My custom dataset'
info['bads'] = ['Pz'] # Names of bad channels
print(info)
Script output:
<Info | 16 non-empty fields
bads : list | Pz
ch_names : list | MEG1, MEG2, Cz, Pz, EOG
chs : list | 5 items (EOG: 1, EEG: 2, GRAD: 2)
comps : list | 0 items
custom_ref_applied : bool | False
description : str | 17 items
dev_head_t : 'mne.transforms.Transform | 3 items
events : list | 0 items
highpass : float | 0.0 Hz
hpi_meas : list | 0 items
hpi_results : list | 0 items
lowpass : float | 500.0 Hz
meas_date : numpy.ndarray | 1970-01-01 01:00:00
nchan : int | 5
projs : list | 0 items
sfreq : float | 1000.0 Hz
acq_pars : NoneType
acq_stim : NoneType
buffer_size_sec : NoneType
ctf_head_t : NoneType
dev_ctf_t : NoneType
dig : NoneType
experimenter : NoneType
file_id : NoneType
filename : NoneType
hpi_subsystem : NoneType
line_freq : NoneType
meas_id : NoneType
proj_id : NoneType
proj_name : NoneType
subject_info : NoneType
>
Note
When assigning new values to the fields of an
mne.io.meas_info.Info
object, it is important that the
fields are consistent:
Raw
objects¶To create a mne.io.Raw
object from scratch, you can use the
mne.RawArray
class, which implements raw data that is backed by a
numpy array. Its constructor simply takes the data matrix and
mne.io.meas_info.Info
object:
# Generate some random data
data = np.random.randn(5, 1000)
# Initialize an info structure
info = mne.create_info(
ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'],
ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'],
sfreq=100
)
custom_raw = mne.io.RawArray(data, info)
print(custom_raw)
Script output:
<RawArray | n_channels x n_times : 5 x 1000>
Epochs
objects¶To create an mne.Epochs
object from scratch, you can use the
mne.EpochsArray
class, which uses a numpy array directly without
wrapping a raw object. The array must be of shape(n_epochs, n_chans,
n_times)
# Generate some random data: 10 epochs, 5 channels, 2 seconds per epoch
sfreq = 100
data = np.random.randn(10, 5, sfreq*2)
# Initialize an info structure
info = mne.create_info(
ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'],
ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'],
sfreq=sfreq
)
It is necessary to supply an “events” array in order to create an Epochs object. This is of shape(n_events, 3) where the first column is the index of the event, the second column is the length of the event, and the third column is the event type.
# Create an event matrix: 10 events with a duration of 1 sample, alternating
# event codes
events = np.array([
[0, 1, 1],
[1, 1, 2],
[2, 1, 1],
[3, 1, 2],
[4, 1, 1],
[5, 1, 2],
[6, 1, 1],
[7, 1, 2],
[8, 1, 1],
[9, 1, 2],
])
More information about the event codes: subject was either smiling or frowning
event_id = dict(smiling=1, frowning=2)
Finally, we must specify the beginning of an epoch (the end will be inferred from the sampling frequency and n_samples)
# Trials were cut from -0.1 to 1.0 seconds
tmin = -0.1
Now we can create the mne.EpochsArray
object
custom_epochs = mne.EpochsArray(data, info, events, tmin, event_id)
print(custom_epochs)
# We can treat the epochs object as we would any other
_ = custom_epochs['smiling'].average().plot()
Script output:
<EpochsArray | n_events : 10 (all good), tmin : -0.1 (s), tmax : 1.89 (s), baseline : None,
'frowning': 5, 'smiling': 5>
Evoked
Objects¶If you already have data that is collapsed across trials, you may also directly create an evoked array. Its constructor accepts an array of shape(n_chans, n_times) in addition to some bookkeeping parameters.
# The averaged data
data_evoked = data.mean(0)
# The number of epochs that were averaged
nave = data.shape[0]
# A comment to describe to evoked (usually the condition name)
comment = "Smiley faces"
# Create the Evoked object
evoked_array = mne.EvokedArray(data_evoked, info, tmin,
comment=comment, nave=nave)
print(evoked_array)
_ = evoked_array.plot()
Script output:
<Evoked | comment : 'Smiley faces', kind : average, time : [-0.100000, 1.890000], n_epochs : 10, n_channels x n_times : 5 x 200>
Total running time of the script: (0 minutes 18.872 seconds)
Download Python source code: plot_creating_data_structures.py