p300_detector.py
def preprocess(data_dict, ch_indices, fs, epoch_period=667):
    '''
    This function loads the data struct and extracts machine learning features
    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_scaled : 2d array: sample x feature
        DESCRIPTION. Scaled training data
    y : 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
    sample_all_electrodes = None
    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])
        
        #--------------extract 14 samples---------------------
        indices = np.arange(0, epochs.shape[2], int(epochs.shape[2]/14))
        sample = sample[:, indices]
        
        # combine electrode feature(s)
        if sample_all_electrodes is None:
            sample_all_electrodes = sample
        else:
            # sample_all_electrodes = sample_all_electrodes + sample
            sample_all_electrodes = np.hstack((sample_all_electrodes, sample))
    
    
    
    # ------------------ Filter -----------------------
    # create a 8-order bandpass Chebyshev Type I filter which cut-off 
    # frequencies are 0.1 and 10 Hz
    # filter_coefficients = signal.firwin(8, [0.5, 10], window='hann', 
    #                                     pass_zero=False, fs=fs)
    # # filter the eeg data
    # filtered_sample = signal.filtfilt(filter_coefficients, 1, sample)
    # # reform the dataset
    # dataset = np.hstack((filtered_sample, np.reshape(label, (-1, 1))))
    
    # testing...
    #------------
    #X = sample_all_electrodes/len(ch_indices)
    X = sample_all_electrodes
    y = label
    #------------
    
    # normalize X
    scaler = preprocessing.StandardScaler().fit(X)
    X_scaled = scaler.transform(X)
    
    return X_scaled, y

    This function loads the data struct and extracts machine learning features
    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_scaled : 2d array: sample x feature
        DESCRIPTION. Scaled training data
    y : 1d array
        DESCRIPTION. Labels

    
This function loads the data struct and extracts machine learning features
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_scaled : 2d array: sample x feature
DESCRIPTION. Scaled training data
y : 1d array
DESCRIPTION. Labels
This function loads the data struct and extracts machine learning features 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_scaled : 2d array : sample x feature DESCRIPTION . Scaled training data y : 1d array DESCRIPTION . Labels
def preprocess(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)
    sample_all_electrodes = None
    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])
        indices = np.arange(0, epochs.shape[2], int(epochs.shape[2]/14))
        sample = sample[:, indices]
        if sample_all_electrodes is None:
            sample_all_electrodes = sample
        else:
            sample_all_electrodes = np.hstack((sample_all_electrodes, sample))
    X = sample_all_electrodes
    y = label
    scaler = preprocessing.StandardScaler().fit(X)
    X_scaled = scaler.transform(X)
    return X_scaled, y
def preprocess ( 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 ) sample_all_electrodes = None 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 ] ) indices = np . arange ( 0 , epochs . shape [ 2 ] , int ( epochs . shape [ 2 ] / 14 ) ) sample = sample [ : , indices ] if sample_all_electrodes is None : sample_all_electrodes = sample else : sample_all_electrodes = np . hstack ( ( sample_all_electrodes , sample ) ) X = sample_all_electrodes y = label scaler = preprocessing . StandardScaler ( ) . fit ( X ) X_scaled = scaler . transform ( X ) return X_scaled , y
This function loads the data struct and extracts machine learning features
from raw dataset.
This function loads the data struct and extracts machine learning features from raw dataset .
'''
    This function loads the data struct and extracts machine learning features
    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_scaled : 2d array: sample x feature
        DESCRIPTION. Scaled training data
    y : 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 #--------------extract 14 samples--------------------- # combine electrode feature(s) # sample_all_electrodes = sample_all_electrodes + sample # ------------------ Filter ----------------------- # create a 8-order bandpass Chebyshev Type I filter which cut-off  # frequencies are 0.1 and 10 Hz # filter_coefficients = signal.firwin(8, [0.5, 10], window='hann',  #                                     pass_zero=False, fs=fs) # # filter the eeg data # filtered_sample = signal.filtfilt(filter_coefficients, 1, sample) # # reform the dataset # dataset = np.hstack((filtered_sample, np.reshape(label, (-1, 1)))) # testing... #------------ #X = sample_all_electrodes/len(ch_indices) #------------ # normalize X
data_dict
ch_indices
fs
epoch_period