def BandPower(extractor,signal,band,filtering,reference,samples_segment,noverlap_segment,signal_len):
    'Band Power according Absolute, Relative & Reference values'
    
    signal_power = np.zeros(0)
    # (1) Signal Filtering
    # --- highpass filter
    filtered_tempo = filtfilt(band[1][0], band[1][1], signal)
    # --- lowpass filter
    filtered_tempo = filtfilt(band[2][0], band[2][1], filtered_tempo)
    # (2) Signal Power
    power = filtered_tempo**2
    # (3) Reference Signal Calculation for Relative Values
    if extractor == 'relative':
        ref_tempo = filtfilt(filtering[10][1][0], filtering[10][1][1], signal)
        ref_tempo = filtfilt(filtering[10][2][0], filtering[10][2][1], ref_tempo)
        ref_power = ref_tempo**2
    # (4) ERD/ERS Power 
    if extractor == 'ERD/ERS': power = power - reference
    # (5) Signal Segmentation & Powering
    # --- time window initialization 
    start, end = 0, samples_segment 
    while end <= signal_len:                                                           
        #  A- absolute power or ERDS
        power_point = np.average(power[start:end])
        #  B- relative power
        if extractor == 'relative':
            ref_point = np.average(ref_power[start:end])
            power_point = power_point/ref_point            
        #  C- segment power into signal power
        #row = np.append(row, np.log10(power_point))
        signal_power = np.append(signal_power, power_point)
        #  D- time window update
        start += noverlap_segment
        end   += noverlap_segment
    return signal_power
def ERDSPower_REF(eeg_ref, bandpass_filtering):
    'Reference Calculation for ERD/ERS Power'
    
    # ***** Offline Processing *****
    if type(eeg_ref) == list:
        # (a) Dimension of the eeg_ref matrix
        Reference, dimX, dimY, = [], [], []
        for idx in range(len(eeg_ref)):
            dimX.append(np.size(eeg_ref[idx], axis = 0))
            dimY.append(np.size(eeg_ref[idx], axis = 1))
        # (b) Feature Extraction
        row = np.zeros(0)    
        for idx in range(len(eeg_ref)):                
            for tr in range(dimY[idx]):                        
                for ch in range(dimX[idx]):
                    for band in bandpass_filtering:                            
                        if band[0] == 'on':
                            # -- highpass filtering
                            tempo = filtfilt(band[1][0], band[1][1], eeg_ref[idx][ch, tr, :])
                            # -- lowpass filtering + band power 
                            tempo = filtfilt(band[2][0], band[2][1], tempo)
                            row = np.append(row, np.mean(tempo**2))          
                # -- matrix creation according to the number of features
                if tr == 0: 
                    Reference.append(np.zeros((1, len(row))))
                    Reference[idx][0, :] = row.copy()
                else:
                    row = np.reshape(row, (1, len(row)))
                    Reference[idx] = np.append(Reference[idx], row.copy(), axis = 0)                    
                # -- row reset                  
                row = np.zeros(0)  
        return Reference
    # ***** Online Processing *****
    else:
        # (a) Dimension of the eeg_ref matrix
        dimX = np.size(eeg_ref, axis = 0)
        # (b) Feature Extraction
        row = np.zeros(0)                             
        for ch in range(dimX):
            for band in bandpass_filtering:                            
                if band[0] == 'on':
                    # -- highpass filtering
                    tempo = filtfilt(band[1][0], band[1][1], eeg_ref[ch, :])
                    # -- lowpass filtering + band power 
                    tempo = filtfilt(band[2][0], band[2][1], tempo)
                    row = np.append(row, np.mean(tempo**2))          
        return row
def ERDS_PreProcessing(eeg_data, eeg_samples, eeg_trials, bandpass_filtering, channels):
    'ERD/ERS Maps Preprocessing'
    
    # (a) Local Variables Declaration    
    # --  number of bands
    numbands = 0
    for item in bandpass_filtering:
        if item[0] == 'on': numbands += 1
    # --  returning matrix
    Power_ERDS = []
    # (b) Time Course of ERD/ERS  
    for idx in range(len(eeg_data)):  
        # -- number of samples 
        Samples = eeg_samples[idx]
        Power_ERDS.append(np.zeros((len(channels),numbands,len(Samples))))
        # -- number of trials
        Trials = eeg_trials[idx]
        # -- processing for one specific band
        index_band = -1
        for Band in bandpass_filtering:
            if Band[0]  == 'on': 
                # -- number of current selected bands
                index_band += 1
                for index_ch in range(len(channels)):               
                    # -- current bandpass filtering of all trial for the selected channel
                    filtered_trials = np.zeros((len(Trials),len(Samples)))                                                        
                    for index_tr in Trials: 
                        # signal extraction
                        signal = eeg_data[idx][channels[index_ch], index_tr, Samples]
                        # highpass filtering
                        signal = filtfilt(Band[1][0], Band[1][1], signal)
                        # lowpass filtering
                        filtered_trials[index_tr,:] = filtfilt(Band[2][0], Band[2][1], signal) 
                    # -- average calculation over all band-pass filtered trials
                    mean_trial = np.mean(filtered_trials, axis = 0) 
                    mean_trial = np.reshape(mean_trial, (1,len(mean_trial)))
                    mean_trial = np.repeat(mean_trial, len(Trials), axis = 0)
                    # -- substraction of the mean of the data for each sample to avoid that
                    #    the evoked potentials may mask induced activities
                    filtered_trials = filtered_trials - mean_trial
                    # -- squaring of the amplitude samples to obtain power samples
                    filtered_trials = filtered_trials**2
                    # -- averaging of power samples for all trials
                    filtered_trials = np.median(filtered_trials, axis = 0)   
                    # -- data storage
                    Power_ERDS[idx][index_ch, index_band, :] = filtered_trials.copy()        
    return Power_ERDS
예제 #4
0
def BandPower(extractor, signal, band, filtering, reference, samples_segment,
              noverlap_segment, signal_len):
    'Band Power according Absolute, Relative & Reference values'

    signal_power = np.zeros(0)
    # (1) Signal Filtering
    # --- highpass filter
    filtered_tempo = filtfilt(band[1][0], band[1][1], signal)
    # --- lowpass filter
    filtered_tempo = filtfilt(band[2][0], band[2][1], filtered_tempo)
    # (2) Signal Power
    power = filtered_tempo**2
    # (3) Reference Signal Calculation for Relative Values
    if extractor == 'relative':
        ref_tempo = filtfilt(filtering[10][1][0], filtering[10][1][1], signal)
        ref_tempo = filtfilt(filtering[10][2][0], filtering[10][2][1],
                             ref_tempo)
        ref_power = ref_tempo**2
    # (4) ERD/ERS Power
    if extractor == 'ERD/ERS': power = power - reference
    # (5) Signal Segmentation & Powering
    # --- time window initialization
    start, end = 0, samples_segment
    while end <= signal_len:
        #  A- absolute power or ERDS
        power_point = np.average(power[start:end])
        #  B- relative power
        if extractor == 'relative':
            ref_point = np.average(ref_power[start:end])
            power_point = power_point / ref_point
        #  C- segment power into signal power
        #row = np.append(row, np.log10(power_point))
        signal_power = np.append(signal_power, power_point)
        #  D- time window update
        start += noverlap_segment
        end += noverlap_segment
    return signal_power
def SiGCoN(ch,eeg_data,new_layout,reference,bandwidth,bandrejection,DCband,downsample_rate,mode,bad_chs):
    'reference     = [Ref_Type, ch_neg]\
     bandwidth     = [BW,   (Bhp,Ahp),(Blp,Alp)]\
     bandrejection = [Rej50,(B,A)]\
     DCband        = [DCremove, (B,A)]'
    
    num_channels= np.size(eeg_data, axis = 0)
    # ------------------------------------------------------
    if mode == 'spectral':
        # ***** Offline Processing *****
        if eeg_data.ndim == 3:
            num_trials  = np.size(eeg_data, axis = 1)
            for tr in range(num_trials):
                # (a) Spectral Filtering
                # --- DC removal 
                if DCband[0] == 'on':
                    eeg_data[ch,tr,:] = eeg_data[ch,tr,:] - np.mean(eeg_data[ch,tr,:])
                # --- Bandpass filter performance
                if bandwidth[0] == 'on': 
                    # highpass filtering
                    eeg_data[ch,tr,:] = filtfilt(bandwidth[1][0], bandwidth[1][1], eeg_data[ch,tr,:])
                    # lowpass filtering
                    eeg_data[ch,tr,:] = filtfilt(bandwidth[2][0], bandwidth[2][1], eeg_data[ch,tr,:])
                # --- Bandstop filter performance
                if bandrejection[0] == 'on': 
                    eeg_data[ch,tr,:] = filtfilt(bandrejection[1][0],bandrejection[1][1], eeg_data[ch,tr,:])
            return eeg_data[ch,:,:]
        # ***** Online Processing *****
        else:
            # (a) Spectral Filtering
            # --- DC removal 
            if DCband[0] == 'on':
                eeg_data[ch,:] = eeg_data[ch,:] - np.mean(eeg_data[ch,:])
            # --- Bandpass filter performance
            if bandwidth[0] == 'on': 
                # highpass filtering
                eeg_data[ch,:] = filtfilt(bandwidth[1][0],bandwidth[1][1], eeg_data[ch,:])
                # lowpass filtering
                eeg_data[ch,:] = filtfilt(bandwidth[2][0],bandwidth[2][1], eeg_data[ch,:])
            # --- Bandstop filter performance
            if bandrejection[0] == 'on': 
                eeg_data[ch,:] = filtfilt(bandrejection[1][0],bandrejection[1][1], eeg_data[ch,:])
            return eeg_data[ch,:]
    # ------------------------------------------------------        
    if mode == 'spatial':
        # (b) DownSampling (previous requirements)
        # ***** Offline Processing *****
        if eeg_data.ndim == 3:
            num_trials  = np.size(eeg_data, axis = 1)
            num_samples = np.size(eeg_data, axis = 2)//downsample_rate
            dsp_ch = np.zeros((1,num_trials,num_samples))
        # **** Online Processing *****
        else:
            num_samples = np.size(eeg_data, axis = 1)//downsample_rate
            dsp_ch = np.zeros((1,num_samples))
        # (c) Spatial Filtering (previous requirements)       
        # --- monopolar referencing
        if reference[0] == 'Monopolar':           
            ref_idx = replace_refIDX(reference[1], bad_chs, num_channels)
        # --- bipolar referencing
        elif reference[0] == 'Bipolar':
            ref_idx = replace_refIDX(reference[1], bad_chs, num_channels)
        # --- common average referencing
        elif reference[0] == 'CAR':
            ref_idx = replace_refIDX(range(num_channels), bad_chs, num_channels)
        # --- laplacian referencing
        else:           
            (i, j) = np.where(new_layout == (ch+1))
            ref_ch = np.zeros(0, dtype = int)   
            # small laplacian referencing
            if reference[0] == 'SmallLaplacian':  
                if i != 0:  ref_ch = np.append(ref_ch, new_layout[i-1,j])
                if j != 0:  ref_ch = np.append(ref_ch, new_layout[i,j-1])
                if j+1 < 9: ref_ch = np.append(ref_ch, new_layout[i,j+1])
                if i+1 < 9: ref_ch = np.append(ref_ch, new_layout[i+1,j])
            # large laplacian referencing
            elif reference[0] == 'LargeLaplacian':                 
                if i-1 > 0: ref_ch = np.append(ref_ch, new_layout[i-2,j])
                if j-1 > 0: ref_ch = np.append(ref_ch, new_layout[i,j-2])
                if j+2 < 9: ref_ch = np.append(ref_ch, new_layout[i,j+2])
                if i+2 < 9: ref_ch = np.append(ref_ch, new_layout[i+2,j])   
            # remove the zero values & re-assign the channel values (for python processing)
            ref_idx = np.nonzero(ref_ch)
            if ref_idx[0].tolist() != []: 
                ref_ch = ref_ch[ref_idx[0]]
                ref_ch -= 1            
            else:
                ref_ch = np.zeros(0, dtype = int)   
            ref_idx = replace_refIDX(ref_ch, bad_chs, num_channels)
        # (d) Execution of steps b and c
        # ***** Offline Processing *****
        if eeg_data.ndim == 3:            
            for tr in range(num_trials): 
                eeg_tempo = eeg_data[ch,tr,:] - np.mean(eeg_data[ref_idx,tr,:], axis = 0)
                dsp_ch[0,tr,:] = eeg_tempo[0::downsample_rate]  
        # ***** Online Processing *****    
        else:
            eeg_tempo = eeg_data[ch,:] - np.mean(eeg_data[ref_idx,:], axis = 0)
            dsp_ch[0,:] = eeg_tempo[0::downsample_rate]    
        return dsp_ch