def preprocessing_raw(fpath, subj): # load data and read event channels rawlist = [] evelist = [] # extracting bdf filenames into bdfs bdfs = fnmatch.filter(os.listdir(fpath), subj + '_ITD*.bdf') if len(bdfs) >= 1: for k, bdf in enumerate(bdfs): rawtemp, evestemp = bs.importbdf(fpath + bdf) rawlist += [rawtemp,] evelist += [evestemp,] else: RuntimeError("No bdf files found!") raw, eves = mne.concatenate_raws(rawlist, events_list = evelist) # Things to try if data are too noisy # raw.info['bads'] += ['A20'] # if A7, A6, A24 have been rejected a lot, they can be added manually # raw.set_eeg_reference(ref_channels = ['Average']) # referencing to the average of all channels, do this if channels are too noisy # the returned eves has three columns: sample number, value of onset, value of offset. eves2 = eves.copy() eves2[:, 1] = np.mod(eves2[:, 1], 256) eves2[:, 2] = np.mod(eves2[:, 2], 256) # raw.plot(events=eves2) raw.filter(l_freq = 1, h_freq=50) # if channels are noisy, adjust the filter parameters, high-pass frequency is better set at 1 compared to 0.5 Hz, to get rid of low-frequency drift # SSP for blinks blinks = find_blinks(raw, ch_name = ['A1',], l_trans_bandwidth=0.5, l_freq=1.0) # A1 is the closest electrode to eyebrow, raw.plot(events=blinks) shows the lines at eye blinks, 998 is default event_id epochs_blinks = mne.Epochs(raw, blinks, 998, tmin = -0.25, tmax = 0.25, proj = False, baseline = (-0.25, 0), reject=dict(eeg=500e-6)) n_eeg = 4 blink_projs = compute_proj_epochs(epochs_blinks, n_grad=0, n_mag=0, n_eeg=n_eeg, verbose='DEBUG') if subj in ['S025', 'S031', 'S046', 'S117', 'S123', 'S127', 'S128', 'S132', 'S133', 'S143', 'S149', 'S051', 'S183', 'S185', 'S187', 'S189', 'S193', 'S195', 'S196', 'S043', 'S072', 'S078', 'S191', 'S129', 'S141', 'S190', 'S194']: #raw.add_proj(blink_projs) # adding all projections #raw.del_proj() #raw.plot_projs_topomap() raw.add_proj([blink_projs[0], blink_projs[2]]) elif subj in ['S135', 'S192', 'S197', 'S199', 'S216', 'S218']: raw.add_proj([blink_projs[0], blink_projs[1]]) elif subj in ['S084', 'S075']: raw.add_proj([blink_projs[0]]) return raw, eves2
blinks = find_blinks(data_eeg, ch_name=['A1'], thresh=100e-6, l_trans_bandwidth=0.5, l_freq=1.0) blink_epochs = mne.Epochs(data_eeg, blinks, 998, tmin=-0.25, tmax=0.25, proj=False, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) Projs = compute_proj_epochs(blink_epochs, n_grad=0, n_mag=0, n_eeg=8, verbose='DEBUG') if subject == 'S207': ocular_projs = [Projs[0], Projs[2]] elif subject == 'S228': ocular_projs = [Projs[0], Projs[2]] elif subject == 'S236': ocular_projs = [Projs[0]] elif subject == 'S238': ocular_projs = [Projs[0], Projs[2]] elif subject == 'S239': ocular_projs = [Projs[0], Projs[2]] elif subject == 'S246': ocular_projs = [Projs[0], Projs[2]]
], l_trans_bandwidth='auto') blinkname = (fpath + subj + '_' + para + '_blinks_erp' + ssstag + '-eve.fif') mne.write_events(blinkname, blinks) epochs_blinks = mne.Epochs(raw, blinks, 998, tmin=-0.25, tmax=0.25, proj=True, baseline=(-0.25, 0), reject=dict(grad=8000e-13, mag=8e-12)) blink_projs = compute_proj_epochs(epochs_blinks, n_grad=2, n_mag=2, n_eeg=0, verbose='DEBUG') raw.add_proj(blink_projs) # SSP for cardiac artifact if ('ECG063' in raw.ch_names) and ekg: qrschan = 'ECG063' thresh = 20e-6 else: qrschan = 'MEG1711' thresh = 1e-12 qrs = find_blinks(raw, ch_name=[ qrschan, ],
tmin=-0.25, tmax=0.25, proj=False, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) blinkITD_epochs = mne.Epochs(ITD_eeg, blinks_ITD, 998, tmin=-0.25, tmax=0.25, proj=False, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) Projs_IAC = compute_proj_epochs(blinkIAC_epochs, n_grad=0, n_mag=0, n_eeg=8, verbose='DEBUG') Projs_ITD = compute_proj_epochs(blinkITD_epochs, n_grad=0, n_mag=0, n_eeg=8, verbose='DEBUG') if Subject == 'S211': eye_projsIAC = [Projs_IAC[0], Projs_IAC[2]] eye_projsITD = [Projs_ITD[0], Projs_ITD[2]] elif Subject == 'S203': eye_projsIAC = [Projs_IAC[0], Projs_IAC[1]] eye_projsITD = [Projs_ITD[0], Projs_ITD[1]] elif Subject == 'S204':
tmin=-0.25, tmax=0.25, proj=False, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) blinkITD_epochs = mne.Epochs(ITD_eeg, blinks_ITD, 998, tmin=-0.25, tmax=0.25, proj=False, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) Projs_IAC = compute_proj_epochs(blinkIAC_epochs, n_grad=0, n_mag=0, n_eeg=5) Projs_ITD = compute_proj_epochs(blinkITD_epochs, n_grad=0, n_mag=0, n_eeg=5) if Subject == 'S211': eye_projsIAC = [Projs_IAC[0], Projs_IAC[1]] eye_projsITD = [Projs_ITD[0], Projs_IAC[1]] elif Subject == 'S203': eye_projsIAC = [Projs_IAC[0], Projs_ITD[1]] eye_projsITD = [Projs_ITD[0], Projs_ITD[1]] elif Subject == 'S204': eye_projsIAC = [Projs_IAC[0], Projs_ITD[1]] eye_projsITD = [Projs_ITD[0], Projs_ITD[1]]
#%% Load data and filter datapath = os.path.join(data_loc, subject) data_eeg,data_evnt = EEGconcatenateFolder(datapath+'/',nchans,refchans,exclude) data_eeg.filter(l_freq=1,h_freq=600) #%% Remove bad channels #%% Blink Removal blinks = find_blinks(data_eeg,ch_name = ['A1'],thresh = 100e-6, l_trans_bandwidth = 0.5, l_freq =1.0) blink_epochs = mne.Epochs(data_eeg,blinks,998,tmin=-0.25,tmax=0.25,proj=False, baseline=(-0.25,0),reject=dict(eeg=500e-6)) Projs = compute_proj_epochs(blink_epochs,n_grad=0,n_mag=0,n_eeg=8) if subject == 'S211': ocular_projs = [Projs[0]] #not using Projs[2] for now data_eeg.add_proj(ocular_projs) data_eeg.plot_projs_topomap() #data_eeg.plot(events=blinks,show_options=True) del blinks, blink_epochs, Projs,ocular_projs #%% Plot data fs = data_eeg.info['sfreq'] reject = dict(eeg=1000e-6) epochs = mne.Epochs(data_eeg,data_evnt,1,tmin=-0.3,tmax=np.ceil(mseq.size/fs)+0.4, reject = reject, baseline=(-0.1,0.))
def processing(stimulus, subj, fpath): # load data and read event channels rawlist = [] evelist = [] # extracting bdf filenames into bdfs if stimulus == 'ITD': bdfs = fnmatch.filter(os.listdir(fpath), subj + '_ITD*.bdf') elif stimulus == 'Atten': bdfs = fnmatch.filter(os.listdir(fpath), subj + '_Atten*.bdf') if len(bdfs) >= 1: for k, bdf in enumerate(bdfs): rawtemp, evestemp = bs.importbdf(fpath + bdf) rawlist += [rawtemp,] evelist += [evestemp,] else: RuntimeError("No bdf files found!") # the returned eves has three columns: sample number, value of onset, value of offset. raw, eves = mne.concatenate_raws(rawlist, events_list = evelist) # Things to try if data are too noisy # raw.info['bads'] += ['A7', 'A6', 'A24'] # if A7, A6, A24 have been rejected a lot, they can be added manually # raw.set_eeg_reference(ref_channels=['Average']) # referencing to the average of all channels, do this if channels are too noisy eves2 = eves.copy() # the eves are in 16 bits originally, so this operation only looks at the lower 8 bits. higher 8 bits are always # high, representing button box condition. If interested in what's happening at button press, do np.floor(eves2[:,1]/256) eves2[:, 1] = np.mod(eves2[:, 1], 256) eves2[:, 2] = np.mod(eves2[:, 2], 256) if stimulus == 'ITD' or stimulus == 'Atten': raw.filter(l_freq = 1, h_freq=50) # if channels are noisy, adjust the filter parameters # raw.plot(events=eves2) # SSP for blinks blinks = find_blinks(raw, ch_name = ['A1',], l_trans_bandwidth=0.5, l_freq=1.0) # A1 is the closest electrode to eyebrow # blink and eves2 triggers can be combined using np.concatenate((eves2, blinks), axis = 0) # raw.plot(events=blinks) shows the lines at eye blinks # the trigger for blinks can be chosen to be starting from 1000, just to make sure it doesn't collide with the triggers for conditions epochs_blinks = mne.Epochs(raw, blinks, 998, tmin = -0.25, tmax = 0.25, proj = False, baseline = (-0.25, 0), reject=dict(eeg=500e-6)) # evoked_blinks = epochs_blinks.average() # evoked_blinks_data = evoked_blinks.data[np.arange(32),:] # PCA is only applied to the epochs around eye blinks. Since the eye blinks are # contributing the most to the variance within this chunk of window, # the first PCA (first eigenvector) is going to be due to the eye blink # for sure and removed. If the PCA was performed on the whole samples, we wouldn't # know which vector is going to be linked to the eye blink # for "n_eeg", it's recommended to remove only the biggest projection, which is eye blinks in this case # greater n_eeg removes more nueral data, which is not favorable n_eeg = 4 blink_projs = compute_proj_epochs(epochs_blinks, n_grad=0, n_mag=0, n_eeg=n_eeg, verbose='DEBUG') projN = np.zeros(2) if subj in ['S025', 'S031', 'S046', 'S117', 'S123', 'S127', 'S128', 'S132', 'S133', 'S143', 'S149', 'S051', 'S183', 'S185', 'S187', 'S189', 'S193', 'S195', 'S196', 'S043', 'S072', 'S075', 'S078', 'S191', 'S129', 'S141', 'S190', 'S021', 'S024', 'S028', 'S083', 'S119', 'S121', 'S122', 'S139', 'S140', 'S142', 'S144', 'S145']: #raw.add_proj(blink_projs) # adding all projections raw.add_proj([blink_projs[0], blink_projs[2]]) # raw.del_proj() projN = [1, 3] elif subj in ['S135', 'S192', 'S194', 'S197', 'S199', 'S216', 'S218']: raw.add_proj([blink_projs[0], blink_projs[1]]) # raw.del_proj() projN = [1, 2] elif subj in ['S084']: raw.add_proj([blink_projs[0]]) # raw.del_proj() projN = [1, 0] # raw.plot_projs_topomap() shows the 4 max PCAs (eigenvectors) # raw.plot(events = blinks, show_options = True) could show the options for applying different projections # if channels are too noisy, play with n_eeg, if the second variance is acting more than # the first, that means the channels are contaminated not just by the eye blinks, but also # from other sources # MANUALLY SELECT PROJECTIONS BY PLOTTING raw.plot_projs_topomap # REMOVE EXTRA PROJS USING raw.del_proj -- Remember index starts at 0 return raw, eves2, projN
proj=False, # why is proj true is we havent generated one yet? baseline=(-0.25, 0), # what is point of baseline correction for blinks reject=dict(eeg=500e-6)) epochs_blinksRS = mne.Epochs( rawRS, blinksRS, 998, tmin=-0.25, tmax=0.25, proj=False, # why is proj true is we havent generated one yet? baseline=(-0.25, 0), # what is point of baseline correction for blinks reject=dict(eeg=500e-6)) blink_projsHB = compute_proj_epochs(epochs_blinksHB, n_grad=0, n_mag=0, n_eeg=6, verbose='DEBUG') blink_projsRS = compute_proj_epochs(epochs_blinksRS, n_grad=0, n_mag=0, n_eeg=6, verbose='DEBUG') #All_projs = mne.compute_proj_raw(raw,start=0,stop=None,duration=None,n_grad=0,n_mag=0,n_eeg=10,reject=dict(eeg=500e-6),verbose = 'DEBUG') #ica = mne.preprocessing.ICA() #ica.fit(raw) blink_projsHB = [blink_projsHB[0], blink_projsHB[2]] blink_projsRS = [blink_projsRS[0], blink_projsRS[1]]
#eog_events = mne.preprocessing.find_eog_events(raw,ch_name='EXG5') #eog_epochs = mne.preprocessing.create_eog_epochs(raw,ch_name='EXG5') from mne.preprocessing.ssp import compute_proj_epochs epochs_blinks = mne.Epochs( raw, blinks, 998, tmin=-0.25, tmax=0.25, proj=False, # why is proj true is we havent generated one yet? baseline=(-0.25, 0), # what is point of baseline correction for blinks reject=dict(eeg=500e-6)) blink_projs = compute_proj_epochs( epochs_blinks, n_grad=0, n_mag=0, n_eeg=8, #n_eeg = 2 b/c blinks and horizontal movements verbose='DEBUG') blink_projs = [blink_projs[0], blink_projs[2]] raw.add_proj(blink_projs) #how much do we affect the neural data with this raw.plot_projs_topomap() raw.plot(events=blinks, scalings=scalings, show_options=True) epochsAll = mne.Epochs(raw, eves, [1, 2, 3], tmin=-0.4, tmax=4.4, proj=False,
#eog_epochs = mne.preprocessing.create_eog_epochs(raw,ch_name='EXG5') from mne.preprocessing.ssp import compute_proj_epochs epochs_blinks = mne.Epochs( raw, blinks, 998, tmin=-0.25, tmax=0.25, proj=False, # why is proj true is we havent generated one yet? baseline=(-0.25, 0), # what is point of baseline correction for blinks reject=dict(eeg=500e-6)) blink_projs = compute_proj_epochs(epochs_blinks, n_grad=0, n_mag=0, n_eeg=6, verbose='DEBUG') #All_projs = mne.compute_proj_raw(raw,start=0,stop=None,duration=None,n_grad=0,n_mag=0,n_eeg=10,reject=dict(eeg=500e-6),verbose = 'DEBUG') #ica = mne.preprocessing.ICA() #ica.fit(raw) #blink_projs = [blink_projs[0], blink_projs[2]] <- use this for subject HB blink_projs = [blink_projs[0], blink_projs[1]] raw.add_proj(blink_projs) #raw.add_proj(All_projs) raw.plot_projs_topomap() raw.plot(events=blinks, scalings=scalings, show_options=True)