def testcase(): import sys sessfn = '../../resources/example_data/mindaffectBCI.txt' from load_mindaffectBCI import load_mindaffectBCI print("Loading: {}".format(sessfn)) X, Y, coords = load_mindaffectBCI(sessfn, ofs=60) times = coords[1]['coords'] fs = coords[1]['fs'] ch_names = coords[2]['coords'] print("X({}){}".format([c['name'] for c in coords], X.shape)) print("Y={}".format(Y.shape)) print("fs={}".format(fs)) # visualize the dataset from analyse_datasets import debug_test_dataset debug_test_dataset(X, Y, coords, tau_ms=100, evtlabs=('re', 'fe'), rank=1, model='cca')
def datasettest(): # N.B. imports in function to avoid import loop.. from datasets import get_dataset from model_fitting import MultiCCA, BwdLinearRegression, FwdLinearRegression from analyse_datasets import debug_test_dataset from scoreOutput import plot_outputscore from decodingCurveSupervised import decodingCurveSupervised if True: tau_ms = 300 offset_ms = 0 rank = 1 evtlabs = None l, f, _ = get_dataset('twofinger') oX, oY, coords = l(f[0]) else: tau_ms = 20 offset_ms = 0 rank = 8 evtlabs = None l, f, _ = get_dataset('mark_EMG') X, Y, coords = l(f[1], whiten=True, stopband=((0, 10), (45, 55), (200, -1)), filterbank=((10, 20), (20, 45), (55, 95), (105, 200))) oX = X.copy() oY = Y.copy() X = oX.copy() Y = oY.copy() # test with reduced number classes? nY = 8 Y = Y[:, :, :nY + 1, :nY] plt.close('all') debug_test_dataset(X, Y, coords, tau_ms=tau_ms, offset_ms=offset_ms, evtlabs=evtlabs, rank=8, outputscore='ip', model='cca') fs = coords[1]['fs'] tau = min(X.shape[-2], int(tau_ms * fs / 1000)) offset = int(offset_ms * fs / 1000) cca = MultiCCA(tau=tau, offset=offset, rank=rank, evtlabs=evtlabs) cca.fit(X, Y) Fy = cca.predict(X, Y, dedup0=True) (_) = decodingCurveSupervised(Fy) W = cca.W_ R = cca.R_ b = cca.b_ plot_outputscore(X[0, ...], Y[0, ...], W, R)
def testcase(): datadir = '~/data/bci/external_data/dryad/Cocktail Party' datadir = 'D://external_data/dryad/Cocktail Party' sessdir = 'EEG/Subject1' sessfn = '' X, Y, coords = load_cocktail(datadir, sessdir, sessfn, subtriallen=15) from analyse_datasets import debug_test_dataset debug_test_dataset(X, Y, coords, tau_ms=500, rank=4, evtlabs=None)
def testcase(): import sys from readCapInf import getPosInfo datadir = '/home/jadref/data/bci/own_experiments/visual/p300_prn_2' sessdir = '' sessfn = 'peter/20100714/jf_prep/peter_rc_5_flash.mat' sessfn = 'peter/20100714/jf_prep/peter_prn_5_flip.mat' #sessfn = 'alex/20100722/jf_prep/alex_prn_10_flip.mat' from offline.load_p300_prn import load_p300_prn X, Y, coords = load_p300_prn(datadir, sessdir, sessfn, ofs=32, stopband=((0,1),(12,-1)), order=6); oX=X.copy(); oY=Y.copy(); fs = coords[1]['fs'] ch_names = coords[2]['coords'] # attach the electrode positions cnames, xy, xyz, iseeg =getPosInfo(ch_names) coords[2]['pos2d'] = xy # set the analysis paramters tau = int(.6*fs) evtlabs = ('re','anyre') # ('re', 'ntre') #('re', 'fe') # times = [i/fs for i in range(tau)] rank = 10 from analyse_datasets import debug_test_dataset debug_test_dataset(X, Y, coords, tau_ms=800, evtlabs=evtlabs, rank=rank, regy=.1, regx=.1) # try with cross-validation res = cca.cv_fit(X, Y) Fy=res['estimator'] (_) = decodingCurveSupervised(Fy) print('FWD') fwd = FwdLinearRegression(tau=tau, evtlabs=evtlabs) fwd.fit(X, Y) print('fwd = {}'.format(fwd)) Fy = fwd.predict(X, Y, dedup0=True) print("Fy={}".format(Fy.shape)) (_) = decodingCurveSupervised(Fy) plot_erp(factored2full(fwd.W_, fwd.R_), evtlabs, times, ch_names) print('BWD') bwd = BwdLinearRegression(tau=tau, evtlabs=evtlabs) bwd.fit(X, Y) print('bwd = {}'.format(bwd)) Fy = bwd.predict(X, Y, dedup0=True) print("Fy={}".format(Fy.shape)) (_) = decodingCurveSupervised(Fy)
def testcase(): import sys sessfn = '~/data/bci/own_experiments/emg/facial/training_data_SV_6.mat' # command-line, for testing if len(sys.argv) > 1: sessfn = sys.argv[1] #from offline.load_mark_EMG import load_mark_EMG oX, oY, coords = load_mark_EMG(sessfn, ofs=125, stopband=((0,10),(45,55),(95,105),(145,-1)), plot=False) fs = coords[1]['fs'] ch_names = coords[2]['coords'] X=oX.copy() Y=oY.copy() print("X({}){}".format([c['name'] for c in coords],X.shape)) print("Y={}".format(Y.shape)) print("fs={}".format(fs)) # try as simple linear classification problem from sklearn.linear_model import Ridge, LogisticRegression, LogisticRegressionCV from sklearn.svm import LinearSVR, LinearSVC # trial average Xc = np.sum(X,axis=-2) Yc = np.argmax(Y[:,0,0,:],axis=-1) clsfr = LogisticRegression(C=1e8) # turn off regularization clsfr.fit(Xc,Yc) Yest = clsfr.predict(Xc) acc = sum([i==j for i, j in zip(Yc,Yest)]) / len(Yc); print("Acc:{}".format(acc)) W = clsfr.coef_ b = clsfr.intercept_ Fe = np.einsum("Td,ed->Te",Xc,W) + b Fy = Fe Yest = np.argmax(Fy,-1) acc = sum([i==j for i, j in zip(Yc,Yest)]) / len(Yc); print("Acc:{}".format(acc)) # per-sample clsfr = LogisticRegression(C=1e8) Xc = X.reshape((-1,X.shape[-1])) Yc = np.argmax(Y[...,0,:],axis=-1).reshape((-1,)) clsfr.fit(Xc,Yc) W = clsfr.coef_ b = clsfr.intercept_ Fe = np.einsum("Td,ed->Te",Xc,W) + b Fy = Fe Yest = np.argmax(Fy,-1) acc = sum([i==j for i, j in zip(Yc,Yest)]) / len(Yc); print('Acc: {}'.format(acc)) # visualize the dataset from analyse_datasets import debug_test_dataset debug_test_dataset(X, Y, coords, tau_ms=20, evtlabs=None, rank=15, model='cca')
def testcase(): import sys if os.path.isdir('D:\external_data'): sessfn = 'D:\external_data\gigadb\openBMI\session1\s1\sml_sess01_subj01_EEG_SSVEP.mat' sessfn = 'D:\external_data\gigadb\openBMI\session1\s1\sml_sess01_subj01_EEG_ERP.mat' sessfn = 'D:\external_data\gigadb\openBMI\session1\s2\sml_sess01_subj02_EEG_MI.mat' else: sessfn = '/home/jadref/data/bci/external_data/gigadb/openBMI/session1/s2/sml_sess01_subj02_EEG_MI.mat' # command-line, for testing if len(sys.argv) > 1: sessfn = sys.argv[1] from load_openBMI import load_openBMI X, Y, coords = load_openBMI(sessfn, CAR=True, offset_ms=(-400, 1000), sessfn=sessfn, ofs=60, stopband=((0, 1), (30, -1))) fs = coords[1]['fs'] # CAR=False,ofs=60,stopband=((0,3),(29,-1)),rcond=1e-3 : audc=36 Perr[-1]=.30 # CAR=True,ofs=60,stopband=((0,3),(29,-1)),rcond=1e-3 : audc=36 Perr[-1]=.30 # CAR=True,ofs=60,stopband=((0,3),(29,-1)),rcond=1e-8 : audc=36 Perr[-1]=.30 if 'SSVEP' in sessfn: tau_ms = 300 evtlabs = ('re') rank = 1 elif 'ERP' in sessfn: tau_ms = 700 evtlabs = ('re', 'ntre') rank = 10 elif 'MI' in sessfn: tau_ms = 20 evtlabs = None rank = 5 # visualize the dataset from analyse_datasets import debug_test_dataset debug_test_dataset(X, Y, coords, rank=rank, evtlabs=evtlabs, tau_ms=tau_ms, offset_ms=0, model='cca', rcond=1e-2)
def testcase(): try: from offline.load_ninapro_db2 import load_ninapro_db2 except: pass datadir = '/home/jadref/data/bci/external_data/ninapro/DB2_s1/S1_E1_A1.mat' ofs = 30 X, Y, coords = load_ninapro_db2(datadir, ofs=ofs, envelopeband=((0, .01), (5, -1)), log=True) from analyse_datasets import debug_test_dataset debug_test_dataset(X, Y, coords, rank=5, evtlabs=None, tau_ms=120, offset_ms=0, model='cca')
# get normally pre-processed X, Y, coords = load_mindaffectBCI(savefile, stopband=((0, 5), (25, -1)), order=6, ofs=200) # output is: X=eeg, Y=stimulus, coords=meta-info about dimensions of X and Y print("EEG: X({}){} @{}Hz".format([c['name'] for c in coords], X.shape, coords[1]['fs'])) print("STIMULUS: Y({}){}".format([c['name'] for c in coords[:1]] + ['output'], Y.shape)) plt.close('all') debug_test_dataset(X, Y, coords, tau_ms=350, evtlabs=('re', 'fe'), rank=1, model='cca') # check the electrode qualities computation ppfn = butterfilt_and_downsample( order=6, stopband='butter_stopband((0, 5), (25, -1))_fs200.pk', fs_out=100) X = X.reshape((-1, X.shape[-1])) ppfn.fit(X[:10, :], fs=200) Xp = ppfn.transform(X) plt.clf() plt.subplot(211) plt.plot(X) plt.subplot(212) plt.plot(Xp)