def get_erp(subID, run): currentSub = subID currentRun = 'run-'+ run print('Current Subject: ', currentSub) print('Current Run:', currentRun) datadict = read_mat(path + subID + '/EEG/' + 'EEG_data_'+ subID + '_' + currentRun + '.mat') eventsdict = read_mat(path + subID + '/EEG/' + 'EEG_events_' + subID + '_' + currentRun + '.mat') data = np.array(datadict['EEGdata']['Y']) sr = np.array(datadict['fs']) tresp = np.array(eventsdict['tresp']) tstim = np.array(eventsdict['tstim']) # construct a time x channel x trial matrix for each run channelnum = data.shape[0] trialnum = tresp.shape[0] trialdata = np.zeros((5000,channelnum, trialnum)) data = np.transpose(data) for i in np.arange(trialnum): time = tstim[i] trialdata[:,:, i] = data[time-2000: time+3000,:] erp = np.mean(trialdata[:,:,:],axis = 2) # make a lowpass filter sos, w, h = timeop.makefiltersos(sr, 10, 20) erpfilt = signal.sosfiltfilt(sos, erp, axis=0, padtype='odd') erpfiltbase = timeop.baselinecorrect(erpfilt, np.arange(1849,1998,1)) # Identify an optimal set of weights to estimate a single erp peak. u,s,vh = linalg.svd(erpfiltbase[2150:2375,:]) weights = np.zeros((channelnum,1)) weights[:,0] = np.matrix.transpose(vh[0,:]) erpfiltproject = np.matmul(erpfiltbase,weights) erpmin = np.amin(erpfiltproject[2150:2375]) erpmax = np.amax(erpfiltproject[2150:2375]) if abs(erpmin) < abs(erpmax): weights = -weights erpfiltproject = -erpfiltproject erp_peaktiming = np.argmin(erpfiltproject[2150:2375]) + 2150 indices = np.arange(erp_peaktiming - 10, erp_peaktiming + 10, 1) erp_peakvalue = np.mean(erpfiltproject[indices]) return erp, erpfiltproject
relative_peakvalue = relative_peakvalue[finalgoodtrials] relative_peaktime = relative_peaktime[finalgoodtrials] n200_param = np.vstack((relative_peakvalue, relative_peaktime)).T Data = np.vstack((Data, n200_param)) np.save('/home/ramesh/pdmattention/ssvep/test/data_n200raw', Data) def get_unTransMatrix(): Target = [] Data = np.empty((0,242)) Target_rt = [] Target_rawrt =[] for index, sub in enumerate(subIDs): print(index) StimSnr30, finalgoodtrials, acc, rt, rt_class = trial_ssvep(sub, 30) StimSnr40, _, _, _, _ = trial_ssvep(sub, 40) StimSnrall = np.hstack((StimSnr30.T, StimSnr40.T)) StimSnrall = StimSnrall[finalgoodtrials,:] Data = np.vstack((Data, StimSnrall)) Target = np.append(Target, acc) Target_rt = np.append(Target_rt, rt_class) Target_rawrt = np.append(Target_rt, rt) np.save('/home/ramesh/pdmattention/ssvep/test/data_real', Data) # make a lowpass filter sr = 1000 sos, w, h = timeop.makefiltersos(sr, 8, 10) erpfilt = signal.sosfiltfilt(sos, n200, axis=1, padtype='odd')
# open up indices artifact0 = artifact.sum(axis=0) artifact1 = artifact.sum(axis=1) # identify goodtrials and good channels. goodtrials = np.squeeze(np.array(np.where(artifact0 < 20))) goodchans = np.squeeze(np.array(np.where(artifact1 < 40))) # BehEEG_int = list(set(beh_ind) & set(goodtrials)) finalgoodtrials = np.array(diffusion.compLists(beh_ind, goodtrials)) # finalgoodtrials = np.array(BehEEG_int) return data, sr, finalgoodtrials, goodchans #%% #globals path = '/home/ramesh/pdmattention/task3/' subID = 's195_ses2' data, sr, goodtrials, goodchans = getdata(path, subID) sos, w, h = timeop.makefiltersos( sr, 15, 20 ) #lowpass filter at 15 hz, needs high pass too which depends on window - discuss. sos_hi, w_h1, h_h1 = timeop.makefiltersos(sr, 5, 2.5) trialnumber = goodtrials[10] trialdata = np.squeeze(data[:, :, trialnumber]) trialdatafilt = signal.sosfiltfilt(sos_hi, trialdata, axis=0, padtype='odd') trialdatafilt = signal.sosfiltfilt(sos, trialdatafilt, axis=0, padtype='odd') #resample at 100 Hz. factor of 10 time = np.linspace(0, 3990, 400) trialdataresample = trialdatafilt[time.astype(int), :]
elif trialcond > 1 and int(whichkey) == int(keyRight): acc[i] = 1 blockcond[blockcond == 3] = 5 blockcond[blockcond == 0] = 5 blockcond[blockcond == 1] = 6 blockcond[blockcond == 2] = 6 blockcond[blockcond == 5] = 0 # congruent blockcond[blockcond == 6] = 1 # incongruent ######### segment data ######### # filter the data # make a lowpass filter sr = 2000 sos, w, h = timeop.makefiltersos(sr, 50, 60) eegfilt = signal.sosfiltfilt(sos, eeg, axis=1, padtype='odd') # make a highpass filter sos, w, h = timeop.makefiltersos(sr, 0.5, 0.25) neweeg = signal.sosfiltfilt(sos, eegfilt, axis=1, padtype='odd') ################# segment the data into trials ###################### prestimdur = 2 poststimdur = 7 trialdata = np.zeros((24, (prestimdur + poststimdur) * sr, 128)) for i in range(0, ntrial): trialdata[i, :, :] = neweeg[:, trial_t[i] - prestimdur * sr:trial_t[i] + poststimdur * sr].T
def main(): for subject in subIDs: #import data currentSub = subject[0:4] print('Current Subject: ', currentSub) datadict = read_mat(path + subject + 'task3_final.mat') behavdict = read_mat(path + subject + '_behavior_final.mat') expdict = read_mat(path + subject + 'task3_expinfo.mat') #This bit was for importing ica files. we should just calculate internally #ica = numpy.array(datadict['ica']) #organize data data = numpy.array(datadict['data']) #mix = numpy.array(datadict['mix']) artifact = numpy.array(datadict['artifact']) sr = numpy.array(datadict['sr']) beh_ind = numpy.array(behavdict['trial']) rt = numpy.array(expdict['rt']) # open up indices artifact0 = artifact.sum(axis=0) artifact1 = artifact.sum(axis=1) #identify goodtrials and good channels. goodtrials = numpy.squeeze(numpy.array(numpy.where(artifact0 < 20))) goodchan = numpy.squeeze(numpy.array(numpy.where(artifact1 < 20))) BehEEG_int = list(set(beh_ind) & set(goodtrials)) print('Good Trials: ', goodtrials) finalgoodtrials = numpy.array(BehEEG_int) print('finalgoodtrials', finalgoodtrials) #average erp. erp = numpy.mean(data[:, :, finalgoodtrials], axis=2) #make a lowpass filter sos, w, h = timeop.makefiltersos(sr, 10, 20) erpfilt = signal.sosfiltfilt(sos, erp, axis=0, padtype='odd') #lets do a SVD, limiting the window in time, and taking the goodchannels. u, s, vh = linalg.svd(erpfilt[1400:1625, goodchan]) weights = numpy.zeros((129, 1)) #This is an optimal set of weights to estimate a single erp peak. weights[goodchan, 0] = numpy.matrix.transpose(vh[0, :]) # Lets test it on the average time series. erpfiltproject = numpy.matmul(erpfilt, weights) plt.figure(1, figsize=[3.5, 1.5]) plt.clf() plt.plot(erpfiltproject) plt.xlabel('Sample') #now we need to apply it to every sample in the data set. trialestimate = numpy.zeros((4000, 360)) for trial in finalgoodtrials: trialdata = numpy.squeeze(data[:, :, trial]) trialproject = numpy.matmul(trialdata, weights) trialestimate[:, trial] = trialproject[:, 0] #now we filter the single trials. trialestimatefilt = signal.sosfiltfilt(sos, trialestimate, axis=0, padtype='odd') #lets make a plot of the average of the single trials. plt.figure(2, figsize=[3.5, 1.5]) plt.clf() plt.plot(numpy.mean(trialestimatefilt, axis=1)) #lets extract two features of interest - peak value and peak timing. peakvalue = numpy.zeros((360, 1)) peaktiming = numpy.zeros((360, 1)) for j in finalgoodtrials: minval = numpy.amin(trialestimatefilt[1400:1625, j]) index = numpy.where(trialestimatefilt[:, j] == minval) peaktiming[j, 0] = numpy.array(index[0]) indices = numpy.arange(index[0] - 10, index[0] + 10, 1) peakvalue[j, 0] = numpy.mean(trialestimatefilt[indices, j], axis=0) #lets make scatter plots of these parameters versus rt plt.figure(3, figsize=[3.5, 1.5]) plt.clf() plt.scatter(peaktiming[finalgoodtrials, 0], rt[finalgoodtrials], marker='o') plt.figure(4, figsize=[3.5, 1.5]) plt.clf() plt.scatter(peakvalue[finalgoodtrials, 0], rt[finalgoodtrials], marker='o') plt.show()