def preprocess(loadedraw, events,\ APPLY_CAR,\ l_freq,\ h_freq,\ filter_method,\ tmin,\ tmax,\ tlow,\ thigh,\ n_jobs,\ picks_feat,\ baselineRange,\ verbose=False): # Load raw, apply bandpass (if applicable), epoch raw = loadedraw.copy() # Di # %% Spatial filter - Common Average Reference (CAR) if APPLY_CAR: raw._data[1:] = raw._data[1:] - np.mean(raw._data[1:], axis=0) # print('Preprocess: CAR done') # %% Properties initialization tdef = trigger_def('triggerdef_errp.ini') sfreq = raw.info['sfreq'] event_id = dict(correct=tdef.by_key['FEEDBACK_CORRECT'], wrong=tdef.by_key['FEEDBACK_WRONG']) # %% Bandpass temporal filtering b, a, zi = pu.butter_bandpass(h_freq, l_freq, sfreq, raw._data.shape[0] - 1) # raw._data.shape[0]- 1 because channel 0 is trigger if filter_method is 'NC' and cv_container is None: raw.filter(l_freq=2, filter_length='10s', h_freq=h_freq, n_jobs=n_jobs, picks=picks_feat, method='fft', iir_params=None) # method='iir'and irr_params=None -> filter with a 4th order Butterworth # print('Preprocess: NC_bandpass filtering done') if filter_method is 'LFILT': # print('Preprocess: LFILT filtering done') for x in range(1, raw._data.shape[0]): # range starting from 1 because channel 0 is trigger # raw._data[x,:] = lfilter(b, a, raw._data[x,:]) raw._data[x, :], zi[:, x - 1] = lfilter(b, a, raw._data[x, :], -1, zi[:, x - 1]) # self.eeg[:,x], self.zi[:,x] = lfilter(b, a, self.eeg[:,x], -1,zi[:,x]) # %% Epoching and baselining # = tmin-paddingLength # t_upper = tmax+paddingLength t_lower = 0 t_upper = thigh # t_lower = 0 # t_upper = tmax+paddingLength epochs = mne.Epochs(raw, events=events, event_id=event_id, tmin=t_lower, tmax=t_upper, baseline=baselineRange, picks=picks_feat, preload=True, proj=False, verbose=verbose) total_wframes = epochs.get_data().shape[2] print('Preprocess: Epoching done') # if tmin != tmin_bkp: # # if the baseline range was before the initial tmin, epochs was tricked to # # to select this range (it expects that baseline is witin [tmin,tmax]) # # this part restore the initial tmin and prune the data # epochs.tmin = tmin_bkp # epochs._data = epochs._data[:,:,int((tmin_bkp-tmin)*sfreq):] return tdef, sfreq, event_id, b, a, zi, t_lower, t_upper, epochs, total_wframes
# Spatial filter - Common Average Reference (CAR) if APPLY_CAR: raw._data[1:] = raw._data[1:] - np.mean(raw._data[1:], axis=0) processing_steps.append('Car') tdef = trigger_def('triggerdef_errp.ini') sfreq = raw.info['sfreq'] paddingIdx = int(paddingLength * sfreq) event_id = dict(correct=tdef.by_name['FEEDBACK_CORRECT'], wrong=tdef.by_name['FEEDBACK_WRONG']) # %% Dataset wide processing # Bandpass temporal filtering b, a, zi = pu.butter_bandpass( h_freq, l_freq, sfreq, raw._data.shape[0] - 1) # raw._data.shape[0]- 1 because channel 0 is trigger if FILTER_METHOD is 'NC': raw.filter( l_freq=l_freq, h_freq=h_freq, n_jobs=n_jobs, picks=picks_feat, method='iir', iir_params=None ) # method='iir'and irr_params=None -> filter with a 4th order Butterworth if FILTER_METHOD is 'LFILT': for x in range(1, raw._data.shape[0] ): # range starting from 1 because channel 0 is trigger # raw._data[x,:] = lfilter(b, a, raw._data[x,:]) raw._data[x, :], zi[:, x - 1] = lfilter(b, a, raw._data[x, :], -1,