p300_detector.py
def preprocess1(data_dict, ch_indices, fs, epoch_period=667):
    '''
    This function loads the data struct and extracts machine learning features
    for EEGNet only from raw dataset. 

    Parameters
    ----------
    data_dict : dict
        DESCRIPTION. The EEG data dictionary
    ch_indices : int list/array
        DESCRIPTION. The list/array of the integer of desired channel
    fs : int
        DESCRIPTION. Sampling frequency in Hz
    epoch_period : int, optional
        DESCRIPTION. The epoch duration in ms. 
                     The default is 667.

    Returns
    -------
    X_ds : 2d array: sample x feature
        DESCRIPTION. Downsampled training data
    y_ds : 1d array
        DESCRIPTION. Labels

    '''
    # extract info
    flashing = data_dict['Flashing']
    types= data_dict['StimulusType']
    
    # get the event index
    event_time = np.where(np.diff([flashing])[0][0] > 0)[0] + 1
    # manually insert the first event at index 0
    event_time = np.insert(event_time, 0, 0)
    # extract labels for each sample/epoch
    label = types[:, event_time].flatten()
    # calculate the period length for each sample
    epoch_len = round(epoch_period * fs / 1000)
    
    # extract data for each electode
    X = []
    for ch in ch_indices:
        data = data_dict['Signal'][:, :, ch]
        # declare the 3d epoch array
        epochs = np.zeros((data.shape[0], len(event_time), epoch_len))
        # loop through the eeg data into epochs
        for epoch_index, epoch_start in enumerate(event_time):
            epoch_end = epoch_start + epoch_len
            epochs[:, epoch_index] = data[:, epoch_start:epoch_end]
        # reshape the epochs array to 2d
        sample = epochs.reshape((epochs.shape[0]*epochs.shape[1]), 
                                epochs.shape[2])
        # combine all data
        X.append(sample)
        
    # reshape X
    X = np.asarray(X)
    X = X.reshape(X.shape[1], X.shape[0], X.shape[2])
    
    
    # downsample size
    y= label
    downsample_size = 250
    # split target and nontarget samples
    target = X[np.where(y==1)[0]]
    nontarget = X[np.where(y==0)[0]]
    # generate indices
    target_ind = resample(np.arange(target.shape[0]), 
                          replace=False, n_samples=downsample_size)
    nontarget_ind = resample(np.arange(nontarget.shape[0]), 
                             replace=False, n_samples=downsample_size)
    # merge two classes
    X_ds = np.vstack((target[target_ind], nontarget[nontarget_ind]))
    y_ds = np.vstack((np.ones((downsample_size, 1)), 
                      np.zeros((downsample_size, 1))))
    
    return X_ds, y_ds

    This function loads the data struct and extracts machine learning features
    for EEGNet only from raw dataset. 

    Parameters
    ----------
    data_dict : dict
        DESCRIPTION. The EEG data dictionary
    ch_indices : int list/array
        DESCRIPTION. The list/array of the integer of desired channel
    fs : int
        DESCRIPTION. Sampling frequency in Hz
    epoch_period : int, optional
        DESCRIPTION. The epoch duration in ms. 
                     The default is 667.

    Returns
    -------
    X_ds : 2d array: sample x feature
        DESCRIPTION. Downsampled training data
    y_ds : 1d array
        DESCRIPTION. Labels

    
This function loads the data struct and extracts machine learning features
for EEGNet only from raw dataset.

Parameters

data_dict : dict
DESCRIPTION. The EEG data dictionary
ch_indices : int list/array
DESCRIPTION. The list/array of the integer of desired channel
fs : int
DESCRIPTION. Sampling frequency in Hz
epoch_period : int, optional
DESCRIPTION. The epoch duration in ms.
The default is 667.

Returns

X_ds : 2d array: sample x feature
DESCRIPTION. Downsampled training data
y_ds : 1d array
DESCRIPTION. Labels
This function loads the data struct and extracts machine learning features for EEGNet only from raw dataset . Parameters data_dict : dict DESCRIPTION . The EEG data dictionary ch_indices : int list / array DESCRIPTION . The list / array of the integer of desired channel fs : int DESCRIPTION . Sampling frequency in Hz epoch_period : int optional DESCRIPTION . The epoch duration in ms . The default is 667 . Returns X_ds : 2d array : sample x feature DESCRIPTION . Downsampled training data y_ds : 1d array DESCRIPTION . Labels
def preprocess1(data_dict, ch_indices, fs, epoch_period=667):
    flashing = data_dict['Flashing']
    types= data_dict['StimulusType']
    event_time = np.where(np.diff([flashing])[0][0] > 0)[0] + 1
    event_time = np.insert(event_time, 0, 0)
    label = types[:, event_time].flatten()
    epoch_len = round(epoch_period * fs / 1000)
    X = []
    for ch in ch_indices:
        data = data_dict['Signal'][:, :, ch]
        epochs = np.zeros((data.shape[0], len(event_time), epoch_len))
        for epoch_index, epoch_start in enumerate(event_time):
            epoch_end = epoch_start + epoch_len
            epochs[:, epoch_index] = data[:, epoch_start:epoch_end]
        sample = epochs.reshape((epochs.shape[0]*epochs.shape[1]), 
                                epochs.shape[2])
        X.append(sample)
    X = np.asarray(X)
    X = X.reshape(X.shape[1], X.shape[0], X.shape[2])
    y= label
    downsample_size = 250
    target = X[np.where(y==1)[0]]
    nontarget = X[np.where(y==0)[0]]
    target_ind = resample(np.arange(target.shape[0]), 
                          replace=False, n_samples=downsample_size)
    nontarget_ind = resample(np.arange(nontarget.shape[0]), 
                             replace=False, n_samples=downsample_size)
    X_ds = np.vstack((target[target_ind], nontarget[nontarget_ind]))
    y_ds = np.vstack((np.ones((downsample_size, 1)), 
                      np.zeros((downsample_size, 1))))
    return X_ds, y_ds
def preprocess1 ( data_dict , ch_indices , fs , epoch_period = 667 ) : flashing = data_dict [ 'Flashing' ] types = data_dict [ 'StimulusType' ] event_time = np . where ( np . diff ( [ flashing ] ) [ 0 ] [ 0 ] > 0 ) [ 0 ] + 1 event_time = np . insert ( event_time , 0 , 0 ) label = types [ : , event_time ] . flatten ( ) epoch_len = round ( epoch_period * fs / 1000 ) X = [ ] for ch in ch_indices : data = data_dict [ 'Signal' ] [ : , : , ch ] epochs = np . zeros ( ( data . shape [ 0 ] , len ( event_time ) , epoch_len ) ) for epoch_index , epoch_start in enumerate ( event_time ) : epoch_end = epoch_start + epoch_len epochs [ : , epoch_index ] = data [ : , epoch_start : epoch_end ] sample = epochs . reshape ( ( epochs . shape [ 0 ] * epochs . shape [ 1 ] ) , epochs . shape [ 2 ] ) X . append ( sample ) X = np . asarray ( X ) X = X . reshape ( X . shape [ 1 ] , X . shape [ 0 ] , X . shape [ 2 ] ) y = label downsample_size = 250 target = X [ np . where ( y == 1 ) [ 0 ] ] nontarget = X [ np . where ( y == 0 ) [ 0 ] ] target_ind = resample ( np . arange ( target . shape [ 0 ] ) , replace = False , n_samples = downsample_size ) nontarget_ind = resample ( np . arange ( nontarget . shape [ 0 ] ) , replace = False , n_samples = downsample_size ) X_ds = np . vstack ( ( target [ target_ind ] , nontarget [ nontarget_ind ] ) ) y_ds = np . vstack ( ( np . ones ( ( downsample_size , 1 ) ) , np . zeros ( ( downsample_size , 1 ) ) ) ) return X_ds , y_ds
This function loads the data struct and extracts machine learning features
for EEGNet only from raw dataset.
This function loads the data struct and extracts machine learning features for EEGNet only from raw dataset .
'''
    This function loads the data struct and extracts machine learning features
    for EEGNet only from raw dataset. 

    Parameters
    ----------
    data_dict : dict
        DESCRIPTION. The EEG data dictionary
    ch_indices : int list/array
        DESCRIPTION. The list/array of the integer of desired channel
    fs : int
        DESCRIPTION. Sampling frequency in Hz
    epoch_period : int, optional
        DESCRIPTION. The epoch duration in ms. 
                     The default is 667.

    Returns
    -------
    X_ds : 2d array: sample x feature
        DESCRIPTION. Downsampled training data
    y_ds : 1d array
        DESCRIPTION. Labels

    ''' # extract info # get the event index # manually insert the first event at index 0 # extract labels for each sample/epoch # calculate the period length for each sample # extract data for each electode # declare the 3d epoch array # loop through the eeg data into epochs # reshape the epochs array to 2d # combine all data # reshape X # downsample size # split target and nontarget samples # generate indices # merge two classes
data_dict
ch_indices
fs
epoch_period