MNE-Python Workflow
MNE-Python is where much EEG analysis happens, and the two meet offline:
a Result carries what MNE needs, and an EDF+ file written by g.Pype is
read by MNE directly. MNE is not a g.Pype dependency: pip install
mne first. Every snippet on this page was run before it was published.
A Result into a Raw
Season 4, Episode 9 builds an
mne.io.RawArray from a Result with a short function of its own,
to_mne, in examples/s4e9_into_mne.py; it is a function in the
example, not a method of Result. What it does:
the data transposed to MNE’s
(channel, time)and scaled to volts by each channel’s unit,result.to_si();a channel type per role,
signalaseegandtriggerasstim, the trigger codes unscaled;the markers as
mne.Annotations, relative to the start of the data, which MNE converts to events withevents_from_annotations; the stim channel carries them as well, sofind_eventsworks too;a refusal where the recording records no units, rather than a guess at microvolts.
A montage by electrode name
g.Pype’s Montage holds electrode names; MNE holds positions. Where the names are standard ones, MNE looks the positions up:
eeg = raw.copy().pick("eeg")
eeg.set_montage("standard_1020", match_case=False)
An EDF file into a Raw
Write with EDFWriter and read with MNE, no bridge needed:
raw = mne.io.read_raw_edf("session.edf", preload=True)
Labels, the rate and the markers, as annotations, come with the file.
Two things to know: EDF is 16-bit, about 0.3 µV per step at the
default ±10000 µV range, and a file may end with up to one record of
padding, since EDF cannot express a sample count that is not a whole
number of records. g.Pype records the true count in an annotation,
gpype:samples=668, so crop to it: raw.crop(tmax=(n - 1) /
raw.info["sfreq"]). File Formats has the layout.
Epochs into an EpochsArray
An Epochs node in a batch run, or a Trigger into a Collector in a
realtime one, delivers a (time, channel, trial) block. MNE wants
(trial, channel, time) in volts:
block = result.data # (time, channel, trial)
info = mne.create_info(list(result.labels), result.rate, ch_types="eeg")
epochs = mne.EpochsArray(np.transpose(block, (2, 1, 0)) * 1e-6, info,
tmin=-0.1) # the Trigger's time_pre
tmin is the start of the window relative to the event: the Epochs
node’s own tmin, or the negative of a Trigger’s time_pre. An
.h5 or .mat recording whose channels were never named gives
result.labels of None; name them at the source, or supply names
here.
Season 4, Episode 10
cuts such a block.
Live, through LSL
An LSLSender publishes a pipeline’s stream, and MNE’s mne_lsl
package reads a live LSL stream into a StreamLSL object with a
rolling buffer. That is the live route; it runs in another process and
is MNE’s tool, not g.Pype’s. LSL Integration
says what the stream carries.
The other way round
To take data from MNE into g.Pype, write a file g.Pype reads:
raw.export("session.edf") for EDF+, or an HDF5 file in the layout
File Formats describes, then an EDFReader or
HDF5Reader in batch mode. A live stream from MNE through mne_lsl’s
PlayerLSL arrives at an LslReceiver like any other.