y = np.array([ valuedict[val] for val in y ]) # 1: detrend data = preproc.detrend(data) # 2: bad-channel removal goodch, badch = preproc.outlierdetection(data); data = data[goodch,:,:]; # 3: apply spatial filter spatialfilter='car' data = preproc.spatialfilter(data,type=spatialfilter) # 4: map to frequencies data,freqs = preproc.powerspectrum(data,dim=1,fSample=fs) # 5 : select the frequency bins we want freqbands =[8,10,28,30] data,freqIdx=preproc.selectbands(data,dim=1,band=freqbands,bins=freqs) # 6 : bad-trial removal goodtr, badtr = preproc.outlierdetection(data,dim=2) data = data[:,:,goodtr] y = y[goodtr] # 7: train classifier, default is a linear-least-squares-classifier clsfr = sklearn.linear_model.LogisticRegressionCV() X2d = np.reshape(data,(-1,data.shape[2])).T # sklearn needs data to be [nTrials x nFeatures] clsfr.fit(X2d,y) print("Best training score=%g"%np.max(np.mean(clsfr.scores_[1],axis=0)))
data = np.array(data) # data contains the signal sampled, 750 samples for 32 channels data_rec = np.copy(data) if verbose: print(events_im[0].value) # Information in terminal, verbose option, for debugging purposes if verbose: print(data.shape) # Information in terminal, verbose option, for debugging purposes data = data[:,:,:] if verbose: print(data.shape) # Information in terminal, verbose option, for debugging purposes data = np.array(data) #data support variable data = np.transpose(data) data = preproc.detrend(data); # Preprocessing operations, more detailed in the report data = preproc.spatialfilter(data, type = 'car') # " data, freqs = preproc.powerspectrum(data,dim = 1,fSample= 250); # " sample information included in the hdr, must be changed if it changes for it to work data,freqIdx = preproc.selectbands(data,dim=1,band=[6,13,15,32],bins=freqs); # " the bands selected here also should be the same as the chosen one in the classifier file if verbose: print(data.shape) # Information in terminal, verbose option, for debugging purposes data = np.reshape(data, (1,-1)) # Data now, is an bunch of array containing the power spectrum of every channel in the selected frequencies (#ch, #freq), in this line all those arrays become one of size (1, #ch * #freq) prediction_right = classifier_right.predict(data) # The freq informations is now passed to the classifier, each one yielding a float value prediction_left = classifier_left.predict(data) pred = [prediction_left, prediction_right] # The predictions are put into an array, for debugging reasons, this isn't what will be passed prediction = [np.round(prediction_left).astype(int), np.round(prediction_right).astype(int)] # In this array the predictions are rounded, as the expected values are 0,1 for each classifier but the outputs are float, they need to be rounded # In the following lunes, the conversion is made from a the array to a single int value, easier to pass as an event value
# stop processing if needed if "stimulus.feedback" in event_types: break # get data in correct format data = np.transpose(data) # 1: detrend data = preproc.detrend(data) # 2: bad-channel removal (as identifed in classifier training) data = data[goodch, :, :] # 3: apply spatial filter (as in classifier training) data = preproc.spatialfilter(data, type=spatialfilter) # 4: map to frequencies (TODO: check fs matches!!) data, freqs = preproc.powerspectrum(data, dim=1, fSample=fs) # 5: select frequency bins we want data, freqIdx = preproc.selectbands(data, dim=1, band=freqbands, bins=freqs) print(data.shape) # 6: apply the classifier, get raw predictions X2d = np.reshape( data, (-1, data.shape[2])).T # sklearn needs data to be [nTrials x nFeatures] fraw = classifier.predict(X2d) # 7: map from fraw to event values # predictions = [ ivaluedict[round(i)] for i in fraw ] predictions = fraw
no_hand_mov = [] for n_exp in range(init, final + 1): # We iterate over all data gathered if verbose: print(' *********** Experiment ' + str(n_exp) + ' *********** ') if verbose: print(np.array(exp[n_exp]['data']).shape) data = np.array( exp[n_exp]['data']) # Support variable that contains the data data = np.transpose(data) data = preproc.detrend(data) # Preprocessing functions, for more info check the report data = preproc.spatialfilter(data, type='car') data, freqs = preproc.powerspectrum(data, dim=1, fSample=exp[i]['hdr'].fSample) data, freqIdx = preproc.selectbands(data, dim=1, band=[6, 13, 15, 32], bins=freqs) if verbose: print(data.shape) n_events = data.shape[2] for event in range(n_events): if verbose: print(exp[n_exp]['events'] [event].value[0]) # Verbose option, for debugging
for element in original: if hasattr(element,"deepcopy"): clone.append(element.deepcopy()) elif hasattr(element,"copy"): clone.append(element.copy()) else: clone.append(element) return clone if __name__=="main": #%pdb import preproc import numpy fs = 40 data = numpy.random.randn(3,fs,5) # 1s of data ppdata=preproc.detrend(data,0) ppdata=preproc.spatialfilter(data,'car') ppdata=preproc.spatialfilter(data,'whiten') ppdata,goodch=preproc.badchannelremoval(data) ppdata,goodtr=preproc.badtrialremoval(data) goodtr,badtr=preproc.outlierdetection(data,-1) Fdata,freqs=preproc.fouriertransform(data, 2, fSamples=1, posFreq=True) Fdata,freqs=preproc.powerspectrum(data, 1, dim=2) filt=preproc.mkFilter(10,[0,4,6,7],1) filt=preproc.mkFilter(numpy.abs(numpy.arange(-10,10,1)),[0,4,6,7]) ppdata=preproc.fftfilter(data,1,[0,3,4,6],fs) ppdata,keep=preproc.selectbands(data,[4,5,10,15],1); ppdata.shape
# 2: bad-channel removal goodch, badch = preproc.outlierdetection(X) print(goodch) X = X[goodch, :, :] # 3: apply spatial filter X = preproc.spatialfilter(X, 'car') # 4 & 5: map to frequencies and select frequencies of interest print(hdr) X, freqs = preproc.powerspectrum(X, dim=1, fSample=hdr.fSample) print(freqs) freqbands = [20, 10, 30, 60] X, freqIdx = preproc.selectbands(X, dim=1, band=freqbands, bins=freqs) freqs = freqs[freqIdx] # 6 : bad-trial removal goodtr, badtr = preproc.outlierdetection(X, dim=2) X = X[:, :, goodtr] labels = labels[goodtr] # 7: train classifier, default is a linear-least-squares-classifier import linear #mapping = {('stimulus.target', 0): 0, ('stimulus.target', 1): 1}
# 2: bad-channel removal goodch, badch = preproc.outlierdetection(X); X = X[goodch,:,:]; Cnames=Cnames[goodch]; Cpos=Cpos[:,goodch]; updatePlots() # 3: apply spatial filter X = preproc.spatialfilter(X,type='car') updatePlots(); # Map to frequency domain, only keep the positive frequencies X,freqs = preproc.powerspectrum(X,dim=1,fSample=fs) yvals = freqs; # ensure the plots use the right x-ticks ylabel='freq (Hz)' updatePlots() # 5 : select the frequency bins we want X,freqIdx=preproc.selectbands(X,dim=1,band=[8,10,28,30],bins=freqs) freqs=freqs[freqIdx] yvals=freqs updatePlots() # 6 : bad-trial removal, trials in dim=2 goodtr, badtr = preproc.outlierdetection(X,dim=2) X = X[:,:,goodtr]