Beispiel #1
0
X = preproc.detrend(X)
updatePlots()

# 2: bad-channel removal, channels in dim=0
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()

# 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
    # get all event type labels
    event_types = [e.type[0] for e in events] 
    
    # stop processing if needed
    if "stimulus.feedback" in event_types:
        break

    print("Applying classifier to %d events"%(len(events)))
    
    # get data in correct format
    data = np.transpose(data) # make it [d x tau]
    
    # 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 & 5: spectral filter (TODO: check fs matches!!)
    data = preproc.fftfilter(data, 1, freqbands, fs)
    # 6 : bad-trial removal
    # 7: 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)
    # 8: map from fraw to event values (note probably not necessary here!)    
    #predictions = [ ivaluedict[round(i)] for i in fraw ]
    # send the prediction events
    for i,f in enumerate(fraw):
        print("%d) {%s}=%f(raw)\n"%(i,str(events[i]),f))
        bufhelp.sendEvent("classifier.prediction",f)
# 0: Remove extra channels
print('number of channels:', NUM_OF_CHANNELS)
print('data shaped as:', data.shape)
data = data[:, :, :NUM_OF_CHANNELS]
# 1: detrend
data = preproc.detrend(data)
# 2: bad-channel removal; channels are last
goodch, badch = preproc.outlierdetection(data, dim=2)
#data = data[:,:,goodch]
data[:, :, badch] *= 0
print('data shape after bad channesls', badch)
print('data shaped as:', data.shape)
# 3: apply spatial filter
data = preproc.spatialfilter(data, type='car')
# 4: frequency filtering
data = preproc.fftfilter(data, 1, [0, 0.1, 30, 32], hdr.fSample)
# 5 : bad-trial removal
goodtr, badtr = preproc.outlierdetection(data, dim=0)
data = data[goodtr]
print('data after good trails:', data.shape)
y = y[goodtr]
print('good trails:', y, y.shape)
# Squash data into 2 dimensions
data = np.reshape(data, (data.shape[0], data.shape[1] * data.shape[2]))
print('data shaped after dimension ordering:', data.shape)
# Fit classifier

from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
Beispiel #4
0
    # stop processing if needed
    if "stimulus.feedback" in event_types:
        break

    print("Applying classifier to %d events" % (len(events)))

    # get data in correct format
    data = np.transpose(data)  # make it [d x tau]

    # 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 & 5: spectral filter (TODO: check fs matches!!)
    data = preproc.fftfilter(data, 1, freqbands, fs)
    # 6 : bad-trial removal
    # 7: 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)
    # 8: map from fraw to event values (note probably not necessary here!)
    #predictions = [ ivaluedict[round(i)] for i in fraw ]
    # send the prediction events
    for i, f in enumerate(fraw):
        print("%d) {%s}=%f(raw)\n" % (i, str(events[i]), f))
        bufhelp.sendEvent("classifier.prediction", f)
Beispiel #5
0
    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
#-------------------------------------------------------------------
#  Run the standard pre-processing and analysis pipeline
# 1: detrend
X = preproc.detrend(X)

# 2: bad-channel removal
goodch, badch = preproc.outlierdetection(X)
X = X[goodch, :, :]
Cnames = Cnames[goodch]
Cpos = Cpos[:, goodch]

# 3: apply spatial filter
X = preproc.spatialfilter(X, type='car')

X = preproc.fftfilter(X, dim=1, band=[0, 0, 10, 15], fSample=fs)

# 4 : bad-trial removal, trials in dim=2
goodtr, badtr = preproc.outlierdetection(X, dim=2)
X = X[:, :, goodtr]
Y = Y[goodtr]

# 8: train SVM

from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.utils.multiclass import unique_labels

X2d = np.reshape(
        xnew = np.linspace(0, source_samples, target_samples)
        d = np.zeros((data.shape[0], target_samples, data.shape[2]))
        for c in range(data.shape[0]):
            d[c] = np.expand_dims(np.interp(xnew, np.arange(0, source_samples),
                                            data[c, :, 0]),
                                  axis=-1)
        data = d

        # 1: detrend
        data = preproc.detrend(data)
        # 2: bad-channel removal
        data = data[goodch, :, :]
        # 3: apply spatial filter
        data = preproc.spatialfilter(data, type=spatialfilter)
        # 4 & 5: spectral filter
        data = preproc.fftfilter(data, 1, freqbands,
                                 200)  # fs of downsampled/training data
        # 6 : bad-trial removal
        ##data = data[:, :, goodtr]
        # 7: load classifier and make prediction
        # sklearn needs x to be [nTrials x nFeatures]
        X2d = np.transpose(np.reshape(data, (-1, data.shape[2])))
        fraw = classifier.predict_proba(X2d)
        # 8: map from fraw to event values
        ##predictions = [ivaluedict[round(i)] for i in fraw]
        probability = fraw[0, 1]
        prediction = np.rint(probability)
        # send the prediction event
        print("ErrP prediction:", probability, "\n")
        bufhelp.sendEvent('errp_clsfr.prediction', probability.tolist())

    except Exception:
# Data preprocessing
for n_exp in range(len(exp)):
    print(' ***********   Experiment  ' + exp_name[n_exp] + '   *********** ')
    data = np.array(exp[n_exp]['data'])  #data support variable
    data = np.transpose(data)
    # Take only the first 1s of EEG recording (We originally took 2s but 1s is enough)
    data = data[:, 0:250, :]
    # 1: detrend
    data = preproc.detrend(data)
    # 2: bad-channel removal
    goodch = np.arange(31)  # all plugged-in electrodes of the cap
    data = data[goodch, :, :]
    # 3: apply spatial filter
    data = preproc.spatialfilter(data, type=spatialfilter)
    # 4 & 5: spectral filter
    data = preproc.fftfilter(data, 1, freqbands, exp[n_exp]['hdr'].fSample)
    # 6 : bad-trial removal
    goodtr, badtr = preproc.outlierdetection(data, dim=2)
    data = data[:, :, goodtr]

    n_events = goodtr
    for i, event in enumerate(n_events):

        if exp[n_exp]['events'][event].value[0] == '0':
            bad_count = bad_count + 1
            bad.append(data[:, :, i])

        if exp[n_exp]['events'][event].value[0] == '1':
            good_count = good_count + 1
            good.append(data[:, :, i])