Lab Streaming Layer and PsychoPy
How every BCI lab synchronizes a recording with the stimuli that caused it. Streams, timestamps, markers, and a stimulus program with millisecond timing, in an afternoon.
On this pageOne afternoon
An event-related potentialEvent-related potential (ERP) The small, stereotyped voltage waveform the brain produces in response to an event, invisible in a single trial and revealed by averaging many. Related: P300P300 A positive bump about 300 milliseconds after a rare or meaningful stimulus, the basis of the classic BCI speller., N170N170 A negative deflection about 170 milliseconds after seeing a face, over the back of the head on the right., Error-related negativity (ERN)Error-related negativity (ERN) A sharp negative wave about 50 to 100 milliseconds after you make a mistake, before you consciously notice., Trial averagingTrial averaging Adding many time-locked epochs together so the consistent response grows and the random noise shrinks by the square root of the number of trials.. Glossary entry is defined by an event. To average brain responses to a flash, you have to know, to the millisecond, when each flash happened, in the same clock as the EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry samples. Lab Streaming LayerLab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. Related: PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses., BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages.. Glossary entry is the system that does this: every device and every program publishes a stream with timestamps from a shared clock, and a recorder captures them all. PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. Related: Lab Streaming Layer (LSL)Lab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it.. Glossary entry is the program that shows the flashes and publishes the markers. Together they are the infrastructure of every experiment in Phase 3.
Streams and outlets
An LSLLab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. Related: PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry outlet is a named source of data: “MyEEG,” eight channels, 250 Hz, float. Your acquisition code creates one and pushes each sample with a timestamp from local_clock(). Any program on the network can find it and pull samples. A marker outlet is the same thing with one string channel and irregular timing: it pushes “target” or “standard” or “flash_10Hz” at the moment the event happens.
from pylsl import StreamInfo, StreamOutlet, local_clock
info = StreamInfo('MyEEG', 'EEG', 8, 250, 'float32', 'myeeg001')
outlet = StreamOutlet(info)
# in the acquisition loop:
outlet.push_sample(list_of_8_floats) # timestamp taken automatically
If your board already speaks the OpenBCIOpenBCI The open-source EEG hardware company whose Cyton (ADS1299) and Ganglion boards are the default hobby and research-lite amplifiers. Related: ADS1299ADS1299 Texas Instruments' 8-channel, 24-bit biopotential front-end chip used in OpenBCI and many research amplifiers. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry protocol, the OpenBCIOpenBCI The open-source EEG hardware company whose Cyton (ADS1299) and Ganglion boards are the default hobby and research-lite amplifiers. Related: ADS1299ADS1299 Texas Instruments' 8-channel, 24-bit biopotential front-end chip used in OpenBCI and many research amplifiers. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry GUI or BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. Related: OpenBCIOpenBCI The open-source EEG hardware company whose Cyton (ADS1299) and Ganglion boards are the default hobby and research-lite amplifiers., Lab Streaming Layer (LSL)Lab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it.. Glossary entry can publish it to LSLLab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. Related: PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry for you.
Markers from PsychoPy
PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. Related: Lab Streaming Layer (LSL)Lab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. . Glossary entry shows stimuli with frame-accurate timing and lets you run Python at the moment of each frame. At the flip that shows the stimulus, push a marker.
from psychopy import visual, core
from pylsl import StreamInfo, StreamOutlet
win = visual.Window(fullscr=True, color='black')
markers = StreamOutlet(StreamInfo('Markers', 'Markers', 1, 0, 'string', 'mk001'))
square = visual.Rect(win, width=0.3, height=0.3, fillColor='white')
for trial in range(50):
core.wait(1.0 + 0.5 * random.random()) # jittered inter-trial interval
square.draw()
win.callOnFlip(markers.push_sample, ['flash']) # marker pushed exactly at the frame that shows it
win.flip()
core.wait(0.1); win.flip()
callOnFlip is the important line: the marker’s timestamp is taken when the screen actually changes, not when the code asked it to.
Recording
LabRecorder (a small program from the LSLLab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. Related: PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry project) lists every stream on the network; tick the ones you want and press record. It writes an XDF file with every stream and its timestamps, corrected for clock offsets between machines. MNEMNE-Python The standard open-source Python library for EEG and MEG analysis: filtering, epoching, ICA, topographies, source localization. Related: Independent component analysis (ICA)Independent component analysis (ICA) A method that unmixes multichannel data into statistically independent sources, so blinks and heartbeat can be identified and removed. . Glossary entry reads XDF through the pyxdf package. Now you have EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry and markers in one file and one clock, and epoching around markers is one line.
Timing reality
Screens refresh at 60 Hz, so a stimulus appears at some point within a 16.7 ms window after the flip; PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. Related: Lab Streaming Layer (LSL)Lab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. . Glossary entry times the flip, not the pixels. Monitors add their own processing delay, tens of milliseconds on some televisions. LSLLab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. Related: PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry’s clock synchronization is good to about a millisecond on one machine and a few across a network. For P300P300 A positive bump about 300 milliseconds after a rare or meaningful stimulus, the basis of the classic BCI speller. Related: Event-related potential (ERP)Event-related potential (ERP) The small, stereotyped voltage waveform the brain produces in response to an event, invisible in a single trial and revealed by averaging many. , Oddball paradigmOddball paradigm An experiment in which a rare target is mixed in among frequent standards; the rare one produces a P300. . Glossary entry and SSVEPSteady-state visual evoked potential (SSVEP) When you stare at something flickering at a fixed rate, visual cortex oscillates at that rate, and the frequency can be read straight out of the EEG spectrum. Related: Canonical correlation analysis (CCA)Canonical correlation analysis (CCA) Finding the combination of channels that correlates best with reference sinusoids at each candidate frequency; the standard SSVEP decoder. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry this is fine. For anything that needs sub-millisecond precision, a photodiode on the screen wired to a spare amplifier channel records the true onset; do this once to measure your setup’s delay and then correct for it.
- Install
pylsl,psychopy, and LabRecorder. - Publish your board’s data as an LSLLab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. Related: PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. , BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. . Glossary entry stream (or BrainFlowBrainFlow A library that talks to OpenBCI and most consumer EEG headsets with one API, in Python and other languages. Related: OpenBCIOpenBCI The open-source EEG hardware company whose Cyton (ADS1299) and Ganglion boards are the default hobby and research-lite amplifiers. , Lab Streaming Layer (LSL)Lab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. . Glossary entry’s). Confirm it appears in LabRecorder.
- Run the PsychoPyPsychoPy Free Python software for presenting stimuli with precise timing and logging responses. Related: Lab Streaming Layer (LSL)Lab Streaming Layer (LSL) A protocol that timestamps and synchronizes streams from many devices and stimulus programs on a network; every BCI lab uses it. . Glossary entry script above. Confirm the Markers stream appears.
- Record two minutes of both. Open the XDF in Python, epochEpoch A fixed-length slice of the recording locked to an event, such as one second starting 200 milliseconds before each stimulus. Related: Trial averagingTrial averaging Adding many time-locked epochs together so the consistent response grows and the random noise shrinks by the square root of the number of trials. , Baseline correctionBaseline correction Subtracting the mean of the pre-stimulus interval from each epoch so trials line up. . Glossary entry the EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry around each flash, and average. Even without a proper experiment you should see a visual evoked response in occipital channels: a bump 100 ms after the flash.
- Tape a photodiode to the screen, feed it to a spare channel, and measure the delay between the marker and the light. Write it down; it is a property of your setup.