This repository contains a set of functions to pre-process and process electroencephalography (EEG) data.
With most recording devices, EEG data are structured as a big matrix of shape (time x electrodes). One electrode channel generaly corresponds to the trigger channel used to synchronise the participant response or the stimuli to the EEG signal. The raw EEG can be split in chunks of time according to this trigger channel. It is then possible to average EEG signal coming from same condition for instance.
These functions can be used to load data, do some kind of processing, plot etc.
This denoising method is an implementation of this matlab toolbox created by Alain de Cheveigné. More details about this method can be found in the following papers:
Plot all electrodes with an offset from t0 to t1. The stimulus channel is also ploted and red lines are used to show the events.
datainstance of pandas.core.DataFrameAdd offset to data.
offsetfloatValue of the offset.
Returns:
newDatainstance of pandas.core.DataFrameThe data with offset applied to each electrode.
Calculate and return the baseline (average of each data point) of a signal. The baseline will calculated from the first baselineDur seconds of this signal.
datainstance of pandas.core.DataFrameData used to calculate the baseline.
baselineDurfloatDuration of the baseline to use for the calulation of the average in seconds.
fsfloatSampling frequency of data in Hz.
Returns:
baselinefloatThe baseline value.
Design a filter with scipy functions avoiding unstable results (when using ab output and filtfilt(), lfilter()...). Cf. ()[]
datainstance of numpy.array | instance of pandas.core.DataFrameData to be filtered. Each column will be filtered if data is a dataframe.
cutoffarray-like of floatPass and stop frequencies in order:
- the first element is the stop limit in the lower bound
- the second element is the lower bound of the pass-band
- the third element is the upper bound of the pass-band
- the fourth element is the stop limit in the upper bound For instance, [0.9, 1, 45, 48] will create a band-pass filter between 1 Hz and 45 Hz.
gstopintThe minimum attenuation in the stopband (dB).
gpassintThe maximum loss in the passband (dB).
Returns:
filteredDatainstance of numpy.array | instance of pandas.core.DataFrameThe filtered data.
Check filtering and downsampling by ploting both datasets.
data1instance of pandas.core.DataFrameFirst dataframe.
data2instance of pandas.core.DataFrameSecond dataframe.
fs1floatSampling frequency of the first dataframe in Hz.
fs2floatSampling frequency of the second dataframe in Hz.
startfloatStart of data to plot in seconds.
endfloatEnd of data to plot in seconds.
electrodeNumintIndex of the column to plot.
Returns:
figinstance of matplotlib.figure.FigureThe figure containing both dataset plots.
Check filtering and downsampling by ploting both datasets.
data1instance of pandas.core.DataFrameFirst dataframe.
data2instance of pandas.core.DataFrameSecond dataframe.
fs1floatSampling frequency of the first dataframe in Hz.
fs2floatSampling frequency of the second dataframe in Hz.
startfloatStart of data to plot in seconds.
endfloatEnd of data to plot in seconds.
electrodeNumintIndex of the column to plot.
Returns:
figinstance of matplotlib.figure.FigureThe figure containing both dataset plots.
Compute the FFT of data and return also the axis in Hz for further plot.
dataarrayFirst dataframe.
fsfloatSampling frequency in Hz.
Returns:
fAxinstance of numpy.arrayAxis in Hz to plot the FFT.
fftDatainstance of numpy.arrayValue of the fft.
Calculate the relative energy at the frequency pickFreq from the the FFT of data. Compare the mean around the pick with the mean of a broader zone for each column.
dataarray-likeMatrix of the shape (time, electrode).
pickFreqfloatFrequency in Hz of the pick for which we want to calculate the relative energy.
showPlotbooleanA plot of the FFT can be shown.
fsfloatSampling frequency in Hz.
Returns:
pickRatiofloatRelative energy of the pick.
Create stim channel from events.
eventsinstance of pandas.core.DataFrameDataframe containing list of events obtained with mne.find_events(raw) .
Returns:
stiminstance of pandas.core.series.SeriesSeries containing the stimulus channel reconstructed from events.
Discriminate triggers when different kind of events are on the same channel. A time threshold is used to determine if two events are from the same trial.
eventsinstance of pandas.core.DataFrameDataframe containing the list of events obtained with mne.find_events(raw).
thresholdfloatTime threshold in milliseconds. Keeps an event if the time difference with the next one is superior than threshold.
Returns:
newDatainstance of pandas.series.SeriesList of trial number filling the requirements.
Resample data from oldFS to newFS using the scipy 'resample' function.
datainstance of pandas.core.DataFrameData to resample.
oldFSfloatThe sampling frequency of data.
newFSfloatThe new sampling frequency.
Returns:
newDatainstance of pandas.DataFrameThe downsampled dataset.
Modify the timestamps of events to match a new sampling frequency.
eventsinstance of pandas.core.DataFrameDataframe containing list of events obtained with mne.find_events(raw) .
oldFSfloatThe sampling frequency of the input events.
newFSfloatThe sampling frequency to the output events.
Returns:
newEventsinstance of pandas.DataFrameDataFrame containing the downsampled events.
Resample data from oldFS to newFS using the scipy 'resample' function.
datainstance of pandas.core.DataFrameData to resample.
oldFSfloatThe sampling frequency of data.
newFSfloatThe new sampling frequency.
Returns:
newDatainstance of pandas.DataFrameThe downsampled dataset.
Fetch behavior data from couchdb (SOA, SNR and trial duration).
dbAddressstrPath to the couch database.
dbNamestrName of the database on the couch instance.
sessionNumintBehavior data will be fetched from this sessionNum.
Returns:
lookupTableinstance of pandas.core.DataFrameA dataframe containing trial data.
Get the events corresponding to eventCode.
rawinstance of mne.io.edf.edf.RawEDFRawEDF object from the MNE library containing data from the .bdf files.
eventCodeintCode corresponding to a specific events. For instance, with a biosemi device, the triggers are coded 65284, 65288 and 65296 respectively on the first, second and third channel.
Returns:
startEventsinstance of pandas.core.DataFrameDataframe containing the list of timing corresponding to the event code in the first column. The second column contains the code before the event and the third the code of the selected event.
Returns a subset of table according to SOA, SNR and/or targetFreq. This is used to select trials with specific parameters.
tableinstance of pandas.core.DataFrameDataFrame containing trial number and their parameters (SOA, SNR...).
kwargsarray-like of int | NoneArray containing element from table to select. It can be
SOA,SNRortargetFreq.
Returns:
newDatainstance of pandas.series.SeriesList of trial number filling the requirements.
Load data from .bdf files. If an array of path is provided, files will be concatenated.
pathstr | array-like of strPath to the .bdf file(s) to load.
Returns:
rawinstance of mne.io.edf.edf.RawEDFRawEDF object from the MNE library containing data from the .bdf files.
Normalize data by subtracting the baseline to each data point. The data used to normalize has to be included at the beginning of data. For instance, to normalize a 10 seconds signal with a 0.1 second baseline, data has to be 10.1 seconds and the baseline used will be the first 0.1 second.
datainstance of pandas.core.DataFrameData to normalize.
baselineDurfloatDuration of the baseline to use for the normalization in seconds.
fsfloatSampling frequency of data in Hz.
Returns:
normalizedinstance of pandas.core.DataFrameThe normalized data.
Plot all electrodes with an offset from t0 to t1. The stimulus channel is also ploted and red lines are used to show the events.
datainstance of pandas.core.DataFrameData to plot (not epoched). Columns correspond to electrodes.
stiminstance of pandas.core.DataFrameOne column dataframe containing the event codes. Used to plot the stimulus timing along with EEG.
eventsinstance of pandas.core.DataFrameDataframe containing the list of events obtained with mne.find_events(raw).
offsetfloatOffset between each electrode line on the plot.
t0floatStart of data to plot.
t1floatEnd of data to plot.
fsfloatSampling frequency of data in Hz.
Returns:
figinstance of matplotlib.figure.FigureThe figure of the data subset in the time domain.
startOffset=0):
freqMin=None, freqMax=None, yMin=None, yMax=None, startOffset=0, noiseAve=None):
Plot the filter frequency response.
zpkarray-likeThe 3 parameters of the filter [z, p, k].
fsfloatSampling frequency in Hz.
Returns:
figinstance of matplotlib.figure.FigureThe figure of the filter response.
Transform each electrode of data according to the average of M1 and M2.
datainstance of pandas.core.DataFrameFirst column has to contain the timing of events in frames.
M1instance of pandas.core.series.SeriesValues of mastoid 1. This Series has to be the same length as data.
M2instance of pandas.core.series.SeriesValues of mastoid 2. This Series has to be the same length as data
Returns:
newDatainstance of pandas.core.DataFrameA dataframe referenced to matoids containing all electrode from which we subtract the average of M1 and M2.
It uses some methods of the MNE library and heavily depends on pandas and numpy.