Exemple #1
0
def preprocess(data, events, matlab=False, spectral=True):
    '''
    preprocessing pipeline for EEG data from the
    buffer
    Parameters
    ----------
    data : a list of datapoints (numpy arrays)
    events : a list of fieldtrip events or a numpy array
    spectral: Whether you want to compute a spectrogram (default False)
    matlab: Whether the data comes from a .mat file (default False)
    
    Examples
    --------
    >>> data, events = ftc.getData(0,100)
    >>> data, events = preprocess(data, events)
    >>> data, events = ftc.getData(0,100)
    >>> data, events = preprocess(data, events, spectral=True)
    '''
    if matlab:
        data, events = reformat(data, events)
    data = preproc.detrend(data, dim=0)

    data, badch = preproc.badchannelremoval(data)
    data = preproc.spatialfilter(data, type='whiten')
    if spectral:
        data = preproc.spectralfilter(data, (1, 40), fSample)

    data, events, badtrials = preproc.badtrailremoval(data, events)

    return data, events
Exemple #2
0
events = f['events']
hdr = f['hdr']


# -------------------------------------------------------------------
#  Run the standard pre-processing and analysis pipeline
# 1: detrend
data = [d[:, :10] for d in data]
data = preproc.detrend(data)
# 2: bad-channel removal
data, bad_channels = preproc.badchannelremoval(data)
print(f'Removed channels: {bad_channels}')
# 3: apply spatial filter
data = preproc.spatialfilter(data, type='car')
# 4 & 5: map to frequencies and select frequencies of interest
data = preproc.spectralfilter(data, (5, 6, 31, 32), hdr.fSample)
# 6 : bad-trial removal
data, events, bad_trials = preproc.badtrialremoval(data, events)
print(f'Removed trials: {bad_trials}')

with open('processed_data.pkl', 'wb') as f:
    pickle.dump({'X': data, 'events': events, 'hdr': hdr}, f)

# Reduce the number of features per channel
freqs = np.linspace(0, hdr.fSample/2, len(data[0]))
data = [d[(5 <= freqs) & (freqs <= 32)] for d in data]
data = [util.subsample_frequencies(d, 4) for d in data]

# 7: train classifier
import linear
# mapping = {('stimulus.target', 0): 0, ('stimulus.target', 1): 1}
Exemple #3
0
while run:
    e = bufhelp.waitforevent("startPhase.cmd",1000, True)
    print("Got startPhase event: %s"%e)
    if e is not None:

        if e.value == "calibration":
            print("Calibration phase")
            data, events, stopevents = bufhelp.gatherdata("stimulus.tgtFlash",trlen_ms,("stimulus.training","end"), milliseconds=True)
            pickle.dump({"events":events,"data":data}, open("subject_data", "w"))

        elif e.value == "train":
            print("Training classifier")
            data = preproc.detrend(data)
            data, badch = preproc.badchannelremoval(data)
            data = preproc.spatialfilter(data)
            data = preproc.spectralfilter(data, (0, .1, 10, 12), bufhelp.fSample)
            data, events, badtrials = preproc.badtrailremoval(data, events)
            mapping = {('stimulus.tgtFlash', '0'): 0, ('stimulus.tgtFlash', '1'): 1}
            linear.fit(data,events,mapping)
            bufhelp.update()
            bufhelp.sendevent("sigproc.training","done")

        elif e.value =="testing":
            print("Feedback phase")
            while True:
                data, events, stopevents = bufhelp.gatherdata(["stimulus.columnFlash","stimulus.rowFlash"],trlen_ms,[("stimulus.feedback","end"), ("stimulus.sequence","end")], milliseconds=True)

                if isinstance(stopevents, list):
                    if any(["stimulus.feedback" in x.type for x in stopevents]):
                        break
                else:
Exemple #4
0
    f     = loadmat(dname+'.mat')
    data  =f['data']
    events=f['events']
    hdr   =f['hdr']


#-------------------------------------------------------------------
#  Run the standard pre-processing and analysis pipeline
# 1: detrend
data        = preproc.detrend(data)
# 2: bad-channel removal
data, badch = preproc.badchannelremoval(data)
# 3: apply spatial filter
data        = preproc.spatialfilter(data,type='car')
# 4 & 5: map to frequencies and select frequencies of interest
data        = preproc.spectralfilter(data, (8,10,28,30), hdr.fSample)
# 6 : bad-trial removal
data, events, badtrials = preproc.badtrailremoval(data, events)
# 7: train classifier, default is a linear-least-squares-classifier
import linear
#mapping = {('stimulus.target', 0): 0, ('stimulus.target', 1): 1}
classifier = linear.fit(data,events)#,mapping)

# save the trained classifer
print('Saving clsfr to : %s'%(cname+'.pk'))
pickle.dump({'classifier':classifier},open(cname+'.pk','wb'))
# # Unfortunately doesn't work for objects...
# # also as hdf5 / mat -v7.3
# f = h5py.File(cname+'.mat','w')
# f.create_dataset('classifier',data=classifier)
# f.create_dataset('mapping',data=mapping)
Exemple #5
0
            print("Calibration phase")
            data, events, stopevents = bufhelp.gatherdata(
                "stimulus.tgtFlash",
                trlen_ms, ("stimulus.training", "end"),
                milliseconds=True)
            pickle.dump({
                "events": events,
                "data": data
            }, open("subject_data", "w"))

        elif e.value == "train":
            print("Training classifier")
            data = preproc.detrend(data)
            data, badch = preproc.badchannelremoval(data)
            data = preproc.spatialfilter(data)
            data = preproc.spectralfilter(data, (0, .1, 10, 12),
                                          bufhelp.fSample)
            data, events, badtrials = preproc.badtrailremoval(data, events)
            mapping = {
                ('stimulus.tgtFlash', '0'): 0,
                ('stimulus.tgtFlash', '1'): 1
            }
            linear.fit(data, events, mapping)
            bufhelp.update()
            bufhelp.sendevent("sigproc.training", "done")

        elif e.value == "testing":
            print("Feedback phase")
            while True:
                data, events, stopevents = bufhelp.gatherdata(
                    ["stimulus.columnFlash", "stimulus.rowFlash"],
                    trlen_ms, [("stimulus.feedback", "end"),
Exemple #6
0
                "stimulus.hybrid",
                trlen_ms, ("stimulus.training", "end"),
                milliseconds=True)

            pickle.dump({
                "events": events,
                "data": data
            }, open("subject_data", "wb"))

        elif e.value == "training":
            print("Training classifier")

            data = preproc.detrend(data)
            data, badch = preproc.badchannelremoval(data)
            data = preproc.spatialfilter(data)
            data = preproc.spectralfilter(data, (1, 10, 15, 25),
                                          bufhelp.fSample)
            data, events, badtrials = preproc.badtrailremoval(data, events)
            mapping = {
                ('stimulus.hybrid', 'left'): -1,
                ('stimulus.hybrid', 'right'): 1
            }
            classifier = linear.fit(data, events, mapping)
            bufhelp.update()
            bufhelp.sendEvent("sigproc.training", "done")

        elif e.value == "contfeedback":
            print("Feedback phase")
            while True:
                test_data, test_events, stopevents = bufhelp.gatherdata(
                    "stimulus.hybrid",
                    trlen_ms,
    from scipy.io import loadmat
    f = loadmat(dname + '.mat')
    data = f['data']
    events = f['events']
    hdr = f['hdr']

#-------------------------------------------------------------------
#  Run the standard pre-processing and analysis pipeline
# 1: detrend
data = preproc.detrend(data)
# 2: bad-channel removal
data, badch = preproc.badchannelremoval(data)
# 3: apply spatial filter
data = preproc.spatialfilter(data, type='car')
# 4 & 5: map to frequencies and select frequencies of interest
data = preproc.spectralfilter(data, (0, 0, 10, 15), hdr.fSample)
# 6 : bad-trial removal
data, events, badtrials = preproc.badtrailremoval(data, events)
# 7: train classifier, default is a linear-least-squares-classifier
import linear
#mapping = {('stimulus.target', 0): 0, ('stimulus.target', 1): 1}
classifier = linear.fit(data, events)  #,mapping)

# save the trained classifer
print('Saving clsfr to : %s' % (cname + '.pk'))
pickle.dump({'classifier': classifier}, open(cname + '.pk', 'wb'))
# # Unfortunately doesn't work for objects...
# # also as hdf5 / mat -v7.3
# f = h5py.File(cname+'.mat','w')
# f.create_dataset('classifier',data=classifier)
# f.create_dataset('mapping',data=mapping)