def train(filename): dat = io.load_bcicomp3_ds2(filename) fs_n = dat.fs / 2 b, a = proc.signal.butter(16, [30 / fs_n], btype='low') dat = proc.lfilter(dat, b, a) b, a = proc.signal.butter(5, [.4 / fs_n], btype='high') dat = proc.lfilter(dat, b, a) dat = proc.subsample(dat, 60) epo = proc.segment_dat(dat, MARKER_DEF_TRAIN, SEG_IVAL) #from wyrm import plot #plot.plot_spatio_temporal_r2_values(proc.sort_channels(epo)) #print JUMPING_MEANS_IVALS #plot.plt.show() fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS) fv = proc.create_feature_vectors(fv) clf = proc.lda_train(fv) return clf
def train(filename): cnt = io.load_bcicomp3_ds2(filename) fs_n = cnt.fs / 2 b, a = proc.signal.butter(5, [38 / fs_n], btype='low') cnt = proc.lfilter(cnt, b, a) b, a = proc.signal.butter(5, [.1 / fs_n], btype='high') cnt = proc.lfilter(cnt, b, a) cnt = proc.subsample(cnt, 60) epo = proc.segment_dat(cnt, MARKER_DEF_TRAIN, SEG_IVAL) # from wyrm import plot # logger.debug('Ploting channels...') # plot.plot_spatio_temporal_r2_values(proc.sort_channels(epo)) # print JUMPING_MEANS_IVALS # plot.plt.show() fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS) fv = proc.create_feature_vectors(fv) cfy = proc.lda_train(fv) return cfy
def train(filename): cnt = io.load_bcicomp3_ds2(filename) fs_n = cnt.fs / 2 b, a = proc.signal.butter(5, [30 / fs_n], btype='low') cnt = proc.lfilter(cnt, b, a) b, a = proc.signal.butter(5, [.4 / fs_n], btype='high') cnt = proc.lfilter(cnt, b, a) cnt = proc.subsample(cnt, 60) epo = proc.segment_dat(cnt, MARKER_DEF_TRAIN, SEG_IVAL) #from wyrm import plot #plot.plot_spatio_temporal_r2_values(proc.sort_channels(epo)) #print JUMPING_MEANS_IVALS #plot.plt.show() fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS) fv = proc.create_feature_vectors(fv) cfy = proc.lda_train(fv) return cfy
def train(filename_): cnt = io.load_bcicomp3_ds2(filename_) fs_n = cnt.fs / 2 b, a = proc.signal.butter(5, [HIGH_CUT / fs_n], btype='low') cnt = proc.lfilter(cnt, b, a) b, a = proc.signal.butter(5, [LOWER_CUT / fs_n], btype='high') cnt = proc.lfilter(cnt, b, a) print("Filtragem aplicada em [{} Hz ~ {} Hz]".format(LOWER_CUT, HIGH_CUT)) cnt = proc.subsample(cnt, SUBSAMPLING) print("Sub-amostragem em {} Hz".format(SUBSAMPLING)) epo = proc.segment_dat(cnt, MARKER_DEF_TRAIN, SEG_IVAL) print("Dados segmentados em intervalos de [{} ~ {}]".format( SEG_IVAL[0], SEG_IVAL[1])) fv = proc.jumping_means(epo, JUMPING_MEANS_INTERVALS) fv = proc.create_feature_vectors(fv) print("Iniciando treinamento da LDA...") cfy = proc.lda_train(fv) print("Treinamento concluido!") return cfy
def test_correct_classlabels(self): """lda_train must throw an error if the class labels are not exactly [0, 1].""" data = np.random.random((50, 100)) labels = np.zeros(50) # only 0s -> fail fv = Data(data=data, axes=[labels, np.arange(100)], units=['x', 'y'], names=['foo', 'bar']) with self.assertRaises(ValueError): lda_train(fv) # 0s and 1s -> ok labels[1] = 1 fv = Data(data=data, axes=[labels, np.arange(100)], units=['x', 'y'], names=['foo', 'bar']) try: lda_train(fv) except ValueError: self.fail() # 0s, 1s, and 2s -> fail labels[2] = 2 fv = Data(data=data, axes=[labels, np.arange(100)], units=['x', 'y'], names=['foo', 'bar']) with self.assertRaises(ValueError): lda_train(fv)
def test_lda_apply_w_shrinkage(self): """trivial lda application must work.""" # this is not a proper test for LDA data = np.random.random((50, 100)) labels = np.zeros(50) data[::2] += 1 labels[::2] += 1 fv = Data(data=data, axes=[labels, np.arange(100)], units=["x", "y"], names=["foo", "bar"]) clf = lda_train(fv, shrink=True) out = lda_apply(fv, clf) # map projections back to 0s and 1s out[out > 0] = 1 out[out < 0] = 0 np.testing.assert_array_equal(out, labels)
def test_lda_apply_w_shrinkage(self): """trivial lda application must work.""" # this is not a proper test for LDA data = np.random.random((50, 100)) labels = np.zeros(50) data[::2] += 1 labels[::2] += 1 fv = Data(data=data, axes=[labels, np.arange(100)], units=['x', 'y'], names=['foo', 'bar']) clf = lda_train(fv, shrink=True) out = lda_apply(fv, clf) # map projections back to 0s and 1s out[out > 0] = 1 out[out < 0] = 0 np.testing.assert_array_equal(out, labels)
def test_correct_classlabels(self): """lda_train must throw an error if the class labels are not exactly [0, 1].""" data = np.random.random((50, 100)) labels = np.zeros(50) # only 0s -> fail fv = Data(data=data, axes=[labels, np.arange(100)], units=["x", "y"], names=["foo", "bar"]) with self.assertRaises(ValueError): lda_train(fv) # 0s and 1s -> ok labels[1] = 1 fv = Data(data=data, axes=[labels, np.arange(100)], units=["x", "y"], names=["foo", "bar"]) try: lda_train(fv) except ValueError: self.fail() # 0s, 1s, and 2s -> fail labels[2] = 2 fv = Data(data=data, axes=[labels, np.arange(100)], units=["x", "y"], names=["foo", "bar"]) with self.assertRaises(ValueError): lda_train(fv)
jumping_means_ivals = JUMPING_MEANS_IVALS_A else: training_set = TRAIN_B testing_set = TEST_B labels = TRUE_LABELS_B jumping_means_ivals = JUMPING_MEANS_IVALS_B # load the training set print "before loading" dat = load_bcicomp3_ds2(training_set) print "after loading " fv_train, epo[subject] = preprocessing(dat, MARKER_DEF_TRAIN, jumping_means_ivals) # train the lda print "before training" cfy = proc.lda_train(fv_train) print "after training" # load the testing set dat = load_bcicomp3_ds2(testing_set) fv_test, _ = preprocessing(dat, MARKER_DEF_TEST, jumping_means_ivals) # predict lda_out_prob = proc.lda_apply(fv_test, cfy) # unscramble the order of stimuli unscramble_idx = fv_test.stimulus_code.reshape(100, 15, 12).argsort() static_idx = np.indices(unscramble_idx.shape) lda_out_prob = lda_out_prob.reshape(100, 15, 12) lda_out_prob = lda_out_prob[static_idx[0], static_idx[1], unscramble_idx]
labels = fv_train.axes[0] y_as_categorical = to_categorical(labels) lstm_model.fit(epo[subject].data, y_as_categorical, verbose=1, show_accuracy=1, validation_split=0.1, nb_epoch=20, class_weight={ 0: 1, 1: 50 }) # train the lda print "before training" cfy = proc.lda_train(fv_train) print "after training" # load the testing set dat = load_bcicomp3_ds2(testing_set) fv_test, epo_test = preprocessing(dat, MARKER_DEF_TEST, jumping_means_ivals) # predict print "-----" lda_out_prob = proc.lda_apply(fv_test, cfy) print lda_out_prob.shape lda_out_prob_2 = lstm_model.predict(epo_test.data)[:, 1] print lda_out_prob.shape