def plotERP(X, Y): 'plot the class averages' global Cnames, Cpos, ylabel, yvals erp = np.stack((X[:, :, Y > 0].mean(2), X[:, :, Y <= 0].mean(2)), 2) #compute the ERP image3d(erp, 0, plotpos=Cpos, xvals=Cnames, ylabel=ylabel, yvals=yvals) # plot the ERPs
def plotTrials(X, trls=range(3)): 'Plot the single-trial data' global Cnames, Cpos, ylabel, yvals image3d(X[:, :, 1:3], 0, plotpos=Cpos, xvals=Cnames, ylabel=ylabel, yvals=yvals)
Cpos = Cpos[:, goodch] updatePlots() # 3: apply spatial filter X = preproc.spatialfilter(X, type='car') updatePlots() # 4 & 5: map to frequencies and select frequencies of interest X = preproc.fftfilter(X, 1, [8, 10, 28, 30], fs) updatePlots() # 6 : bad-trial removal, trials in dim=2 goodtr, badtr = preproc.outlierdetection(X, dim=2) X = X[:, :, goodtr] Y = Y[goodtr] updatePlots() # 7: train linear least squares classifier, with cross-validation from sklearn.linear_model import LinearRegression clsfr = sklearn.linear_model.RidgeCV() X2d = np.reshape( X, (-1, X.shape[2])).T # sklearn needs x to be [nTrials x nFeatures] clsfr.fit(X2d, Y) clsfr.score #8: plot the classifier weights # plot the classifier weight vector W = clsfr.coef_ W = np.reshape(clsfr.coef_, (X.shape[1], X.shape[0])) image3d(W, 0, plotpos=Cpos, xvals=Cnames)
#load the datafile, and extract the variables data=loadmat('ERPdata.mat') X =data['X'] Y =data['Y'].reshape(-1) # ensure is 1d fs =data['fs'][0] # ensure is scalar Cnames=data['Cnames'] Cpos =data['Cpos'] ylabel='time (s)' yvals =np.range(0,X.shape[1])/fs # element labels for 2nd dim of X def plotTrials(X,trls=0:3): 'Plot the single-trial data' global Cnames, Cpos, ylabel, yvals image3d(X[:,:,1:3],0,plotpos=Cpos,xvals=Cnames,ylabel=ylabel,yvals=yvals) def plotERP(X,Y): 'plot the class averages' global Cnames, Cpos, ylabel, yvals erp=np.stack((X[:,:,Y>0].mean(2),X[:,:,Y<=0].mean(2)),2) #compute the ERP image3d(erp,0,plotpos=Cpos,xvals=Cnames,ylabel=ylabel,yvals=yvals) # plot the ERPs def updatePlots: global X,Y # Plot the raw data fig=plt.figure() plotTrials(X) # single trial fig=plt.figure() plotERP(X,Y) # class averages
from image3d import * # init connection to the buffer ftc, hdr = bufhelp.connect() #define variables trlen_samp = 50 nSymbols = 2 nr_channels = 4 # in debug mode erp = np.zeros((nr_channels, trlen_samp, nSymbols)) nTarget = np.zeros((nSymbols, 1)) # read in the capfile Cname, latlong, xy, xyz, capfile = readCapInf.readCapInf('sigproxy_with_TRG') fig = plt.figure() # grab data after every t:'stimulus' event until we get a {t:'stimulus.training' v:'end'} event data, events, stopevents = bufhelp.gatherdata(["stimulus", "experiment"], trlen_samp, ("sequence", "end"), milliseconds=False) # loop through all recorded events (last to first) for ei in np.arange(len(events) - 1, -1, -1): # detrend erp, so we can see stuff erp = preproc.detrend(erp) image3d(erp) # plot the ERPs plt.show()
# init connection to the buffer ftc,hdr=bufhelp.connect(); #define variables trlen_samp = 50 nSymbols = 2 nr_channels = 4 # in debug mode erp = np.zeros((nr_channels,trlen_samp,nSymbols)) nTarget = np.zeros((nSymbols,1)) # read in the capfile Cname,latlong,xy,xyz,capfile= readCapInf.readCapInf('sigproxy_with_TRG') fig=plt.figure() # grab data after every t:'stimulus' event until we get a {t:'stimulus.training' v:'end'} event data, events, stopevents = bufhelp.gatherdata(["stimulus","experiment"],trlen_samp,("sequence","end"), milliseconds=False) # loop through all recorded events (last to first) for ei in np.arange(len(events)-1,-1,-1): # detrend erp, so we can see stuff erp = preproc.detrend(erp) image3d(erp) # plot the ERPs plt.show()
print("Waiting for triggers: %s and endtrigger: %s"%(triggerevents[0],stopevent[0])) while endTest is False: # grab data after every t:'stimulus' event until we get a {t:'stimulus.training' v:'end'} event #data, events, stopevents, state = bufhelp.gatherdata(triggerevents,trlen_samp,stopevent, state, milliseconds=False) data, events, stopevents, state = bufhelp.gatherdata(triggerevents,trlen_samp,[], state, milliseconds=False) for ei in np.arange(len(events)-1,-1,-1): ev = events[ei] # check for exit event if (ev.type == "experiment") and (ev.value == "end"): endTest = True print("end experiment") break # update ERP if ev.value is '+': classlabel = 1 else: classlabel = 0 erp[:,:,classlabel] = (erp[:,:,classlabel]*nTarget[classlabel] + np.transpose(data[ei]))/(nTarget[classlabel]+1); nTarget[classlabel]= nTarget[classlabel]+1; # detrend erp, so we can see stuff in the viewer plterp = preproc.detrend(erp) image3d(plterp,plotpos=xy,xvals=Cnames) # plot the ERPs drawnow()
def plotERP(X,Y): 'plot the class averages' global Cnames, Cpos, ylabel, yvals erp=np.stack((X[:,:,Y>0].mean(2),X[:,:,Y<=0].mean(2)),2) #compute the ERP image3d(erp,0,plotpos=Cpos,xvals=Cnames,ylabel=ylabel,yvals=yvals) # plot the ERPs
def plotTrials(X,trls): 'Plot the single-trial data' global Cnames, Cpos, ylabel, yvals image3d(X[:,:,1:3],0,plotpos=Cpos,xvals=Cnames,ylabel=ylabel,yvals=yvals)
from image3d import * from scipy.io import loadmat #load the datafile, and extract the variables data=loadmat('ERSPdata.mat') X =data['X']; print("X= [channels x timepoints x trials]");print(X.shape) Y =data['Y'].reshape(-1); print("Y=[trials]");print(Y.shape) fs =float(data['fs'][0]) Cnames=data['Cnames'].reshape(-1); Cnames=np.array([item for sublist in Cnames for item in sublist]) #flatten the list of lists of names and make array print("Channel names : ");print(Cnames) Cpos =data['Cpos']; print("Cpos= [ 3 x channels]");print(Cpos.shape); # plot the data image3d(X[:,:,1:3],0,plotpos=Cpos); # plot the class averages erp=np.stack((X[:,:,Y>0].mean(2),X[:,:,Y<=0].mean(2)),2) #compute the ERP image3d(erp,0,plotpos=Cpos,xvals=Cnames); # plot the ERPs # define some utility functions to simplify plotting of data ylabel='time (s)' yvals =np.arange(0,X.shape[1])/fs # element labels for 2nd dim of X def plotTrials(X,trls): 'Plot the single-trial data' global Cnames, Cpos, ylabel, yvals image3d(X[:,:,1:3],0,plotpos=Cpos,xvals=Cnames,ylabel=ylabel,yvals=yvals) def plotERP(X,Y):