Пример #1
0
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}
classifier = linear.fit(data, events)  # ,mapping
print(f'number of features: {classifier.coef_.shape}')

# save the trained classifer
print('Saving clsfr to : %s' % (cname + '.pk'))
pickle.dump({'classifier': classifier,
             'bad_channels': bad_channels}, open(cname + '.pk', 'wb'))
Пример #2
0
import numpy as np

import bufhelp
import preproc
import linear

if __name__ == '__main__':
    f = pickle.load(open('training_data.pkl', 'rb'))
    data = f['data']
    events = f['events']
    hdr = f['hdr']

    data = preproc.detrend(data)
    # data, _ = preproc.badchannelremoval(data)

    # this line causes memory errors
    # data is of shape (1300, 60, 4)
    # spatial filtering tries to create eye(78_000)
    # data = preproc.spatialfilter(data)

    data = preproc.spectralfilter(data, (0, 1, 14, 15), hdr.fSample)

    data, events, _ = preproc.badtrailremoval(data, events)
    data = np.array(data).reshape(len(data), -1)
    print('data shape:', data.shape)
    events = np.array([e.value[0] for e in events])
    events[events == 0] = -1
    classifier = linear.fit(data, events)
    pickle.dump(classifier, open('classifier.pkl', 'wb'))
Пример #3
0
    f     = h5py.File(dname+'.mat','r')
    data  =f['data']
    events=f['events']
    hdr   =f['hdr']
# try the .mat file if all else fails
if not 'data' in dir() and os.path.exists(dname+'.mat'):
    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 using the preproc class
# 1: detrend
data        = preproc.detrend(data)
# 2: bad-channel removal
# 3: apply spatial filter
# 4 & 5: map to frequencies and select frequencies of interest
# 6 : bad-trial removal
# 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'))

Пример #4
0
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}
X2d = np.reshape(
    X, (-1, X.shape[2])).T  # sklearn needs data to be [nTrials x nFeatures]
classifier = linear.fit(X2d, labels.astype(int))  #,mapping)
print(X2d[labels].mean(axis=0) - X2d[~labels].mean(axis=0))
print(labels.astype(int))

# save the trained classifer
print('Saving clsfr to : %s' % (cname + '.pk'))
pickle.dump(
    {
        'classifier': classifier,
        'goodch': goodch,
        'freqbands': freqbands,
        'classifier': classifier,
        'fSample': hdr.fSample
    }, open(cname + '.pk', 'wb'))

print(classifier.intercept_, classifier.coef_)