示例#1
0
    def predict_outputs(self):
        n_folds = len(self.binary_csp.folds)
        n_class_pairs = len(self.binary_csp.class_pairs)
        result_shape = (n_folds, n_class_pairs)
        self.train_accuracy = np.empty(result_shape, dtype=float)
        self.test_accuracy = np.empty(result_shape, dtype=float)
        self.train_pred_full_fold = np.empty(result_shape, dtype=object)
        self.test_pred_full_fold = np.empty(result_shape, dtype=object)
        for fold_i in range(n_folds):
            log.info("Fold Nr: {:d}".format(fold_i + 1))
            for class_i, class_pair in enumerate(self.binary_csp.class_pairs):
                clf = self.clf[fold_i, class_i]
                class_pair_plus_one = (np.array(class_pair) + 1).tolist()
                log.info("Class {:d} vs {:d}".format(*class_pair_plus_one))
                train_feature = self.train_feature[fold_i, class_i]
                train_out = lda_apply(train_feature, clf)
                correct_train = train_feature.axes[0] == class_pair[1]
                predicted_train = train_out >= 0
                train_accuracy = (sum(correct_train == predicted_train) /
                                  float(len(predicted_train)))
                self.train_accuracy[fold_i, class_i] = train_accuracy

                test_feature = self.test_feature[fold_i, class_i]
                test_out = lda_apply(test_feature, clf)
                correct_test = test_feature.axes[0] == class_pair[1]
                predicted_test = test_out >= 0
                test_accuracy = (sum(correct_test == predicted_test) /
                                 float(len(predicted_test)))

                self.test_accuracy[fold_i, class_i] = test_accuracy

                train_feature_full_fold = self.train_feature_full_fold[fold_i,\
                     class_i]
                train_out_full_fold = lda_apply(train_feature_full_fold, clf)
                self.train_pred_full_fold[fold_i,
                                          class_i] = train_out_full_fold
                test_feature_full_fold = self.test_feature_full_fold[fold_i,\
                     class_i]
                test_out_full_fold = lda_apply(test_feature_full_fold, clf)
                self.test_pred_full_fold[fold_i, class_i] = test_out_full_fold

                log.info("Train: {:4.2f}%".format(train_accuracy * 100))
                log.info("Test:  {:4.2f}%".format(test_accuracy * 100))
示例#2
0
 def cross_validate_lda(features):
     folds = KFold(features.data.shape[0], n_folds=5, shuffle=False)
     test_accuracies = []
     for train_inds, test_inds in folds:
         train_features = features.copy(data=features.data[train_inds], axes=[features.axes[0][train_inds]])
         test_features = features.copy(data=features.data[test_inds], axes=[features.axes[0][test_inds]])
         clf = lda_train_scaled(train_features, shrink=True)
         test_out = lda_apply(test_features, clf)
         second_class_test = test_features.axes[0] == np.max(test_features.axes[0])
         predicted_2nd_class_test = test_out >= 0
         test_accuracy = sum(second_class_test == predicted_2nd_class_test) / float(len(predicted_2nd_class_test))
         test_accuracies.append(test_accuracy)
     return np.mean(test_accuracies)
示例#3
0
    def predict_outputs(self):
        n_folds = len(self.binary_csp.folds)
        n_class_pairs = len(self.binary_csp.class_pairs)
        result_shape = (n_folds, n_class_pairs)
        self.train_accuracy = np.empty(result_shape, dtype=float)
        self.test_accuracy = np.empty(result_shape, dtype=float)
        self.train_pred_full_fold = np.empty(result_shape, dtype=object)
        self.test_pred_full_fold = np.empty(result_shape, dtype=object)
        for fold_i in range(n_folds):
            log.info("Fold Nr: {:d}".format(fold_i + 1))
            for class_i, class_pair in enumerate(self.binary_csp.class_pairs):
                clf = self.clf[fold_i, class_i]
                class_pair_plus_one = (np.array(class_pair) + 1).tolist()
                log.info("Class {:d} vs {:d}".format(*class_pair_plus_one))
                train_feature = self.train_feature[fold_i, class_i]
                train_out = lda_apply(train_feature, clf)
                correct_train = train_feature.axes[0] == class_pair[1]
                predicted_train = train_out >= 0
                train_accuracy = sum(correct_train == predicted_train) / float(len(predicted_train))
                self.train_accuracy[fold_i, class_i] = train_accuracy

                test_feature = self.test_feature[fold_i, class_i]
                test_out = lda_apply(test_feature, clf)
                correct_test = test_feature.axes[0] == class_pair[1]
                predicted_test = test_out >= 0
                test_accuracy = sum(correct_test == predicted_test) / float(len(predicted_test))

                self.test_accuracy[fold_i, class_i] = test_accuracy

                train_feature_full_fold = self.train_feature_full_fold[fold_i, class_i]
                train_out_full_fold = lda_apply(train_feature_full_fold, clf)
                self.train_pred_full_fold[fold_i, class_i] = train_out_full_fold
                test_feature_full_fold = self.test_feature_full_fold[fold_i, class_i]
                test_out_full_fold = lda_apply(test_feature_full_fold, clf)
                self.test_pred_full_fold[fold_i, class_i] = test_out_full_fold

                log.info("Train: {:4.2f}%".format(train_accuracy * 100))
                log.info("Test:  {:4.2f}%".format(test_accuracy * 100))
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
 def cross_validate_lda(features):
     folds = KFold(features.data.shape[0], n_folds=5, shuffle=False)
     test_accuracies = []
     for train_inds, test_inds in folds:
         train_features = features.copy(data=features.data[train_inds],
                                        axes=[features.axes[0][train_inds]])
         test_features = features.copy(data=features.data[test_inds],
                                       axes=[features.axes[0][test_inds]])
         clf = lda_train_scaled(train_features, shrink=True)
         test_out = lda_apply(test_features, clf)
         second_class_test = test_features.axes[0] == np.max(
             test_features.axes[0])
         predicted_2nd_class_test = test_out >= 0
         test_accuracy = (
             sum(second_class_test == predicted_2nd_class_test) /
             float(len(predicted_2nd_class_test)))
         test_accuracies.append(test_accuracy)
     return np.mean(test_accuracies)
示例#7
0
def offline_experiment(filename_, cfy_, true_labels_):
    print("\n")
    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.filtfilt(cnt, b, a)

    b, a = proc.signal.butter(5, [LOWER_CUT / fs_n], btype='high')
    cnt = proc.filtfilt(cnt, b, a)

    cnt = proc.subsample(cnt, SUBSAMPLING)

    epo = proc.segment_dat(cnt, MARKER_DEF_TEST, SEG_IVAL)

    fv = proc.jumping_means(epo, JUMPING_MEANS_INTERVALS)
    fv = proc.create_feature_vectors(fv)

    lda_out = proc.lda_apply(fv, cfy_)
    markers = [fv.class_names[cls_idx] for cls_idx in fv.axes[0]]
    result = zip(markers, lda_out)
    endresult = []
    markers_processed = 0
    letter_prob = {i: 0 for i in 'abcdefghijklmnopqrstuvwxyz123456789_'}
    for s, score in result:
        if markers_processed == 180:
            endresult.append(
                sorted(letter_prob.items(), key=lambda x: x[1])[-1][0])
            letter_prob = {
                i: 0
                for i in 'abcdefghijklmnopqrstuvwxyz123456789_'
            }
            markers_processed = 0
        for letter in s:
            letter_prob[letter] += score
        markers_processed += 1

    print('Letras Encontradas-: %s' % "".join(endresult))
    print('Letras Corretas----: %s' % true_labels_)
    acc = np.count_nonzero(
        np.array(endresult) == np.array(
            list(true_labels_.lower()[:len(endresult)]))) / len(endresult)
    print("Acertividade Final : %d" % (acc * 100))
示例#8
0
def online_erp(fs, n_channels, subsample):
    logger.debug('Running Online ERP with {fs}Hz, and {channels}channels'.format(fs=fs, channels=n_channels))

    target_fs = 100
    # blocklen in ms
    blocklen = 1000 * 1 / target_fs
    # blocksize given the original fs and blocklen
    blocksize = fs * (blocklen / 1000)


    MRK_DEF = {'target': 'm'}
    SEG_IVAL = [0, 700]
    JUMPING_MEANS_IVALS = [150, 220], [200, 260], [310, 360], [550, 660]
    RING_BUFFER_CAP = 1000

    cfy = [0, 0]

    fs_n = fs / 2

    b_l, a_l = proc.signal.butter(5, [30 / fs_n], btype='low')
    b_h, a_h = proc.signal.butter(5, [.4 / fs_n], btype='high')
    zi_l = proc.lfilter_zi(b_l, a_l, n_channels)
    zi_h = proc.lfilter_zi(b_h, a_h, n_channels)

    ax_channels = np.array([str(i) for i in range(n_channels)])

    names = ['time', 'channel']
    units = ['ms', '#']

    blockbuf = BlockBuffer(blocksize)
    ringbuf = RingBuffer(RING_BUFFER_CAP)

    times = []

    # time since the last data was acquired
    t_last = time.time()

    # time since the last marker
    t_last_marker = time.time()

    # time since the experiment started
    t_start = time.time()

    full_iterations = 0
    while full_iterations < 500:

        t0 = time.time()

        dt = time.time() - t_last
        samples = int(dt * fs)
        if samples == 0:
            continue
        t_last = time.time()

        # get data
        data = np.random.random((samples, n_channels))
        ax_times = np.linspace(0, 1000 * (samples / fs), samples, endpoint=False)
        if t_last_marker + .01 < time.time():
            t_last_marker = time.time()
            markers = [[ax_times[-1], 'm']]
        else:
            markers = []

        cnt = Data(data, axes=[ax_times, ax_channels], names=names, units=units)
        cnt.fs = fs
        cnt.markers = markers

        # blockbuffer
        blockbuf.append(cnt)
        cnt = blockbuf.get()
        if not cnt:
            continue

        # filter
        cnt, zi_l = proc.lfilter(cnt, b_l, a_l, zi=zi_l)
        cnt, zi_h = proc.lfilter(cnt, b_h, a_h, zi=zi_h)

        # subsample
        if subsample:
            cnt = proc.subsample(cnt, target_fs)
        newsamples = cnt.data.shape[0]

        # ringbuffer
        ringbuf.append(cnt)
        cnt = ringbuf.get()

        # epoch
        epo = proc.segment_dat(cnt, MRK_DEF, SEG_IVAL, newsamples=newsamples)
        if not epo:
            continue

        # feature vectors
        fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS)
        rv = proc.create_feature_vectors(fv)

        # classification
        proc.lda_apply(fv, cfy)

        # don't measure in the first second, where the ringbuffer is not
        # full yet.
        if time.time() - t_start < (RING_BUFFER_CAP / 1000):
            continue

        dt = time.time() - t0
        times.append(dt)

        full_iterations += 1

    return np.array(times)
示例#9
0
def online_experiment(amp, cfy):
    amp_fs = amp.get_sampling_frequency()
    amp_channels = amp.get_channels()

    # buf = BlockBuffer(4)
    rb = RingBuffer(5000)
    fn = amp_fs / 2
    b_low, a_low = proc.signal.butter(5, [38 / fn], btype='low')
    b_high, a_high = proc.signal.butter(5, [.1 / fn], btype='high')
    zi_low = proc.lfilter_zi(b_low, a_low, len(amp_channels))
    zi_high = proc.lfilter_zi(b_high, a_high, len(amp_channels))

    amp.start()
    print("Iniciando simulacao em 5s...")
    for x in xrange(4, 0, -1):
        time.sleep(1)
        print("%ds" % x)
        pass
    markers_processed = 0
    current_letter_idx = 0
    current_letter = TRUE_LABELS[current_letter_idx].lower()

    letter_prob = {i: 0 for i in 'abcdefghijklmnopqrstuvwxyz123456789_'}
    endresult = []
    t0 = time.time()

    while True:
        t0 = time.time()

        # get fresh data from the amp
        data, markers = amp.get_data()
        if len(data) == 0:
            continue

        # we should rather wait for a specific end-of-experiment marker
        if len(data) == 0:
            break

        # convert to cnt
        cnt = io.convert_mushu_data(data, markers, amp_fs, amp_channels)

        # enter the block buffer
        # buf.append(cnt)
        # cnt = buf.get()
        # if not cnt:
        #    continue

        # band-pass and subsample
        cnt, zi_low = proc.lfilter(cnt, b_low, a_low, zi=zi_low)
        cnt, zi_high = proc.lfilter(cnt, b_high, a_high, zi=zi_high)

        cnt = proc.subsample(cnt, 60)

        newsamples = cnt.data.shape[0]

        # enter the ringbuffer
        rb.append(cnt)
        cnt = rb.get()

        # segment
        epo = proc.segment_dat(cnt,
                               MARKER_DEF_TEST,
                               SEG_IVAL,
                               newsamples=newsamples)
        if not epo:
            continue

        fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS)
        fv = proc.create_feature_vectors(fv)
        print("\n")
        logger.info('Step : %d' % markers_processed)

        lda_out = proc.lda_apply(fv, cfy)
        markers = [fv.class_names[cls_idx] for cls_idx in fv.axes[0]]
        result = zip(markers, lda_out)
        for s, score in result:
            if markers_processed == 180:
                endresult.append(
                    sorted(letter_prob.items(), key=lambda x: x[1])[-1][0])
                letter_prob = {
                    i: 0
                    for i in 'abcdefghijklmnopqrstuvwxyz123456789_'
                }
                markers_processed = 0
                current_letter_idx += 1
                current_letter = TRUE_LABELS[current_letter_idx].lower()
            for letter in s:
                letter_prob[letter] += score
            markers_processed += 1

        print('Letra Atual Correta-:  %s  ' % current_letter)
        print("Letra provavel--: %s" % "".join([
            i[0] for i in sorted(
                letter_prob.items(), key=lambda x: x[1], reverse=True)
        ]).replace(current_letter, " '%s' " % current_letter))
        print('Letras Corretas----: %s' % TRUE_LABELS)
        # discovered = BuildDiscoveredString(endresult)
        # print('Letras Encontradas-: %s' % discovered)
        print('Letras Encontradas-: %s' % "".join(endresult))

        # calculate the current accuracy
        if len(endresult) > 0:
            acc = np.count_nonzero(
                np.array(endresult) == np.array(
                    list(TRUE_LABELS.lower()[:len(endresult)]))) / len(
                        endresult)
            print('Acertividade Atual : %d' % (acc * 100))

        if len(endresult) == len(TRUE_LABELS) - 1:
            break

        # logger.debug('Resultado : %s' % result)
        timeValue = 1000 * (time.time() - t0)
        print('Tempo consumido por ciclo : %d' % timeValue)

    acc = np.count_nonzero(
        np.array(endresult) == np.array(
            list(TRUE_LABELS.lower()[:len(endresult)]))) / len(endresult)
    print("Acertividade Final : %d" % (acc * 100))

    amp.stop()
示例#10
0
def online_erp(fs, n_channels, subsample):
    logger.debug('Running Online ERP with {fs}Hz, and {channels}channels'.format(fs=fs, channels=n_channels))

    target_fs = 100
    # blocklen in ms
    blocklen = 1000 * 1 / target_fs
    # blocksize given the original fs and blocklen
    blocksize = fs * (blocklen / 1000)


    MRK_DEF = {'target': 'm'}
    SEG_IVAL = [0, 700]
    JUMPING_MEANS_IVALS = [150, 220], [200, 260], [310, 360], [550, 660]
    RING_BUFFER_CAP = 1000

    cfy = [0, 0]

    fs_n = fs / 2

    b_l, a_l = proc.signal.butter(5, [30 / fs_n], btype='low')
    b_h, a_h = proc.signal.butter(5, [.4 / fs_n], btype='high')
    zi_l = proc.lfilter_zi(b_l, a_l, n_channels)
    zi_h = proc.lfilter_zi(b_h, a_h, n_channels)

    ax_channels = np.array([str(i) for i in range(n_channels)])

    names = ['time', 'channel']
    units = ['ms', '#']

    blockbuf = BlockBuffer(blocksize)
    ringbuf = RingBuffer(RING_BUFFER_CAP)

    times = []

    # time since the last data was acquired
    t_last = time.time()

    # time since the last marker
    t_last_marker = time.time()

    # time since the experiment started
    t_start = time.time()

    full_iterations = 0
    while full_iterations < 500:

        t0 = time.time()

        dt = time.time() - t_last
        samples = int(dt * fs)
        if samples == 0:
            continue
        t_last = time.time()

        # get data
        data = np.random.random((samples, n_channels))
        ax_times = np.linspace(0, 1000 * (samples / fs), samples, endpoint=False)
        if t_last_marker + .01 < time.time():
            t_last_marker = time.time()
            markers = [[ax_times[-1], 'm']]
        else:
            markers = []

        cnt = Data(data, axes=[ax_times, ax_channels], names=names, units=units)
        cnt.fs = fs
        cnt.markers = markers

        # blockbuffer
        blockbuf.append(cnt)
        cnt = blockbuf.get()
        if not cnt:
            continue

        # filter
        cnt, zi_l = proc.lfilter(cnt, b_l, a_l, zi=zi_l)
        cnt, zi_h = proc.lfilter(cnt, b_h, a_h, zi=zi_h)

        # subsample
        if subsample:
            cnt = proc.subsample(cnt, target_fs)
        newsamples = cnt.data.shape[0]

        # ringbuffer
        ringbuf.append(cnt)
        cnt = ringbuf.get()

        # epoch
        epo = proc.segment_dat(cnt, MRK_DEF, SEG_IVAL, newsamples=newsamples)
        if not epo:
            continue

        # feature vectors
        fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS)
        rv = proc.create_feature_vectors(fv)

        # classification
        proc.lda_apply(fv, cfy)

        # don't measure in the first second, where the ringbuffer is not
        # full yet.
        if time.time() - t_start < (RING_BUFFER_CAP / 1000):
            continue

        dt = time.time() - t0
        times.append(dt)

        full_iterations += 1

    return np.array(times)
示例#11
0
def online_experiment(amp, cfy):
    amp_fs = amp.get_sampling_frequency()
    amp_channels = amp.get_channels()

    #buf = BlockBuffer(4)
    rb = RingBuffer(5000)

    fn = amp_fs / 2
    b_low, a_low = proc.signal.butter(5, [30 / fn], btype='low')
    b_high, a_high = proc.signal.butter(5, [.4 / fn], btype='high')

    zi_low = proc.lfilter_zi(b_low, a_low, len(amp_channels))
    zi_high = proc.lfilter_zi(b_high, a_high, len(amp_channels))

    amp.start()
    markers_processed = 0
    current_letter_idx = 0
    current_letter = TRUE_LABELS[current_letter_idx].lower()

    letter_prob = {i : 0 for i in 'abcdefghijklmnopqrstuvwxyz123456789_'}
    endresult = []
    t0 = time.time()
    while True:
        t0 = time.time()

        # get fresh data from the amp
        data, markers = amp.get_data()
        if len(data) == 0:
            continue

        # we should rather wait for a specific end-of-experiment marker
        if len(data) == 0:
            break

        # convert to cnt
        cnt = io.convert_mushu_data(data, markers, amp_fs, amp_channels)

        ## enter the block buffer
        #buf.append(cnt)
        #cnt = buf.get()
        #if not cnt:
        #    continue

        # band-pass and subsample
        cnt, zi_low = proc.lfilter(cnt, b_low, a_low, zi=zi_low)
        cnt, zi_high = proc.lfilter(cnt, b_high, a_high, zi=zi_high)

        cnt = proc.subsample(cnt, 60)

        newsamples = cnt.data.shape[0]

        # enter the ringbuffer
        rb.append(cnt)
        cnt = rb.get()

        # segment
        epo = proc.segment_dat(cnt, MARKER_DEF_TEST, SEG_IVAL, newsamples=newsamples)
        if not epo:
            continue

        fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS)
        fv = proc.create_feature_vectors(fv)
        logger.debug(markers_processed)

        lda_out = proc.lda_apply(fv, cfy)
        markers = [fv.class_names[cls_idx] for cls_idx in fv.axes[0]]
        result = zip(markers, lda_out)
        for s, score in result:
            if markers_processed == 180:
                endresult.append(sorted(letter_prob.items(), key=lambda x: x[1])[-1][0])
                letter_prob = {i : 0 for i in 'abcdefghijklmnopqrstuvwxyz123456789_'}
                markers_processed = 0
                current_letter_idx += 1
                current_letter = TRUE_LABELS[current_letter_idx].lower()
            for letter in s:
                letter_prob[letter] += score
            markers_processed += 1
        logger.debug("".join([i[0] for i in sorted(letter_prob.items(), key=lambda x: x[1], reverse=True)]).replace(current_letter, " %s " % current_letter))
        logger.debug(TRUE_LABELS)
        logger.debug("".join(endresult))
        # calculate the current accuracy
        if len(endresult) > 0:
            acc = np.count_nonzero(np.array(endresult) == np.array(list(TRUE_LABELS.lower()[:len(endresult)]))) / len(endresult)
            print "Current accuracy:", acc * 100
        if len(endresult) == len(TRUE_LABELS):
            break
        #logger.debug("Result: %s" % result)
        print 1000 * (time.time() - t0)

    acc = np.count_nonzero(np.array(endresult) == np.array(list(TRUE_LABELS.lower()[:len(endresult)]))) / len(endresult)
    print "Accuracy:", acc * 100

    amp.stop()
def preprocess(data, filt=None):
    dat = data.copy()
    fs_n = 250 # sample rate is 250 for us

    b, a = proc.signal.butter(5, [13 / fs_n], btype='low')
    dat = proc.filtfilt(dat, b, a)

    b, a = proc.signal.butter(5, [9 / fs_n], btype='high')
    dat = proc.filtfilt(dat, b, a)

    dat = proc.subsample(dat, 50)

    if filt is None:
        filt, pattern, _ = proc.calculate_csp(dat)
        plot_csp_pattern(pattern)
    dat = proc.apply_csp(dat, filt)

    dat = proc.variance(dat)
    dat = proc.logarithm(dat)
    return dat, filt

fv_train, filt = preprocess(dat_train)
fv_test, _ = preprocess(dat_test, filt)

cfy = proc.lda_train(fv_train)
result = proc.lda_apply(fv_test, cfy)
result = (np.sign(result) + 1) / 2
print 'LDA Accuracy %.2f%%' % ((result == true_labels).sum() / len(result))

plt.show()
            test_data_i = proc.select_epochs(train_data, rs[1])
            train_data_i = proc.select_epochs(train_data, rs[0])
            #expected = test_data_i.axes[0]
            expected = train_data_i.axes[0]

            # creating a CSP filter, preprocessing train data
            fv_train, filt = preprocess(train_data_i)

            # training LDA
            cfy = proc.lda_train(fv_train)

            # preprocess test data
            #fv_test, _ = preprocess(test_data_i, filt)

            # predicting result of the test data
            result = proc.lda_apply(fv_train, cfy)
            result = (np.sign(result) + 1) / 2

            fpr, tpr, thresholds = metrics.roc_curve(expected, result, pos_label=1)
            accuracy[state] = metrics.auc(fpr, tpr)

        Accuracy[j][i] = accuracy.mean()

        log('LDA Accuracy on start %3i, window size %3i train data %.8f%%' % (start, window_size, Accuracy[j][i]))
        #log('LDA Accuracy on start %3i, window size %3i train data %.8f%%' % (start, window_size, accuracy))

        try:
            data = go.Data([go.Contour(z=Accuracy, x=starts, y=sizes)])
            #plot_url = py.plot(data, filename='prediction-roc-accuracy-8', fileopt='overwrite', auto_open=False)
        except BaseException as e:
            print 'error occurred when trying to load data from .mat: ' + e.message
示例#14
0
dat_train, dat_test=load_graz('dataset_BCIcomp1.mat')

labels=scio.loadmat('labels_data_set_iii.mat')
'''print type(labels)
print labels.shape'''
y_test=labels['y_test']
y_test[y_test == 2] = 0
print "true labels\n"
print y_test.swapaxes(1,0)

#dat_test=graz_data['x_test']
fv_train, filt = preprocess(dat_train)
fv_test, _ = preprocess(dat_test, filt)

cfy= proc.lda_train(fv_train)
result=proc.lda_apply(fv_test,cfy)
result = (np.sign(result) + 1) / 2

print "generated labels\n"
print result


#print y_test.shape
#print result.shape



sum=0.0
for i in range(len(result)):
	if result[i]==y_test[i]:
		sum=sum+1
示例#15
0
                   })

    # 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
    # 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]

    #lda_out_prob = lda_out_prob[:, :5, :]

    # destil the result of the 15 runs
    #lda_out_prob = lda_out_prob.prod(axis=1)
    lda_out_prob = lda_out_prob.sum(axis=1)
示例#16
0
    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]
    
    #lda_out_prob = lda_out_prob[:, :5, :]
    
    # destil the result of the 15 runs
    #lda_out_prob = lda_out_prob.prod(axis=1)
    lda_out_prob = lda_out_prob.sum(axis=1)
        
    # 
    lda_out_prob = lda_out_prob.argsort()
示例#17
0
    def run_pair(self, epo_train, epo_test, bp_nr, fold_nr, pair_nr):
        class_pair = self.class_pairs[pair_nr]
        self.print_class_pair(class_pair)

        ### Run Training
        epo_train_pair = select_classes(epo_train, class_pair)
        epo_test_pair = select_classes(epo_test, class_pair)

        if self.ival_optimizer is not None:
            best_segment_ival = self.ival_optimizer.optimize(epo_train_pair)
            log.info("Ival {:.0f}ms - {:.0f}ms".format(*best_segment_ival))
            epo_train_pair = select_ival(epo_train_pair, best_segment_ival)
            epo_test_pair = select_ival(epo_test_pair, best_segment_ival)
            epo_train = select_ival(epo_train, best_segment_ival)
            epo_test = select_ival(epo_test, best_segment_ival)

        self.train_labels[fold_nr][pair_nr] = epo_train_pair.axes[0]
        self.test_labels[fold_nr][pair_nr] = epo_test_pair.axes[0]

        ## Calculate CSP
        filters, patterns, variances = calculate_csp(epo_train_pair)
        ## Apply csp, calculate features
        if self.n_filters is not None:
            # take topmost and bottommost filters, e.g.
            # for n_filters=3 0,1,2,-3,-2,-1
            columns = range(0, self.n_filters) + \
                range(-self.n_filters, 0)
        else:  # take all possible filters
            columns = range(len(filters))
        train_feature = apply_csp_var_log(epo_train_pair, filters, columns)

        ## Calculate LDA
        clf = lda_train_scaled(train_feature, shrink=True)
        assert not np.any(np.isnan(clf[0]))
        assert not np.isnan(clf[1])
        ## Apply LDA to train
        train_out = lda_apply(train_feature, clf)
        correct_train = train_feature.axes[0] == class_pair[1]
        predicted_train = train_out >= 0
        train_accuracy = (sum(correct_train == predicted_train) /
                          float(len(predicted_train)))

        ### Feature Computation and LDA Application for test
        test_feature = apply_csp_var_log(epo_test_pair, filters, columns)
        test_out = lda_apply(test_feature, clf)
        correct_test = test_feature.axes[0] == class_pair[1]
        predicted_test = test_out >= 0
        test_accuracy = (sum(correct_test == predicted_test) /
                         float(len(predicted_test)))

        ### Feature Computations for full fold (for later multiclass)
        train_feature_full_fold = apply_csp_var_log(epo_train, filters,
                                                    columns)
        test_feature_full_fold = apply_csp_var_log(epo_test, filters, columns)
        ### Store results
        # only store used patterns filters variances
        # to save memory space on disk
        self.store_results(bp_nr, fold_nr, pair_nr, filters[:, columns],
                           patterns[:, columns], variances[columns],
                           train_feature, test_feature,
                           train_feature_full_fold, test_feature_full_fold,
                           clf, train_accuracy, test_accuracy)
        if self.ival_optimizer is not None:
            self.best_ival[bp_nr, fold_nr, pair_nr] = best_segment_ival

        self.print_results(bp_nr, fold_nr, pair_nr)
示例#18
0
def online_experiment(amp, clf):
    amp_fs = amp.get_sampling_frequency()
    amp_channels = amp.get_channels()

    buf = BlockBuffer(4)
    rb = RingBuffer(5000)

    fn = amp.get_sampling_frequency() / 2
    b_low, a_low = proc.signal.butter(16, [30 / fn], btype='low')
    b_high, a_high = proc.signal.butter(5, [.4 / fn], btype='high')

    zi_low = proc.lfilter_zi(b_low, a_low, len(amp_channels))
    zi_high = proc.lfilter_zi(b_high, a_high, len(amp_channels))

    amp.start()
    markers_processed = 0
    current_letter_idx = 0
    current_letter = TRUE_LABELS[current_letter_idx].lower()

    letter_prob = {i: 0 for i in 'abcdefghijklmnopqrstuvwxyz123456789_'}
    endresult = []
    while True:
        # turn on for 'real time'
        #time.sleep(0.01)

        # get fresh data from the amp
        data, markers = amp.get_data()

        # we should rather wait for a specific end-of-experiment marker
        if len(data) == 0:
            break

        # convert to cnt
        cnt = io.convert_mushu_data(data, markers, amp_fs, amp_channels)

        # enter the block buffer
        buf.append(cnt)
        cnt = buf.get()
        if not cnt:
            continue

        # band-pass and subsample
        cnt, zi_low = proc.lfilter(cnt, b_low, a_low, zi=zi_low)
        cnt, zi_high = proc.lfilter(cnt, b_high, a_high, zi=zi_high)

        cnt = proc.subsample(cnt, 60)

        newsamples = cnt.data.shape[0]

        # enter the ringbuffer
        rb.append(cnt)
        cnt = rb.get()

        # segment
        epo = proc.segment_dat(cnt,
                               MARKER_DEF_TEST,
                               SEG_IVAL,
                               newsamples=newsamples)
        if not epo:
            continue

        fv = proc.jumping_means(epo, JUMPING_MEANS_IVALS)
        fv = proc.create_feature_vectors(fv)
        logger.debug(markers_processed)

        lda_out = proc.lda_apply(fv, clf)
        markers = [fv.class_names[cls_idx] for cls_idx in fv.axes[0]]
        result = zip(markers, lda_out)
        for s, score in result:
            if markers_processed == 180:
                endresult.append(
                    sorted(letter_prob.items(), key=lambda x: x[1])[-1][0])
                letter_prob = {
                    i: 0
                    for i in 'abcdefghijklmnopqrstuvwxyz123456789_'
                }
                markers_processed = 0
                current_letter_idx += 1
                current_letter = TRUE_LABELS[current_letter_idx].lower()
            for letter in s:
                letter_prob[letter] += score
            markers_processed += 1
        logger.debug("".join([
            i[0] for i in sorted(
                letter_prob.items(), key=lambda x: x[1], reverse=True)
        ]).replace(current_letter, " %s " % current_letter))
        logger.debug(TRUE_LABELS)
        logger.debug("".join(endresult))
        # calculate the current accuracy
        if len(endresult) > 0:
            acc = np.count_nonzero(
                np.array(endresult) == np.array(
                    list(TRUE_LABELS.lower()[:len(endresult)]))) / len(
                        endresult)
            print "Current accuracy:", acc * 100
        if len(endresult) == len(TRUE_LABELS):
            break
        #logger.debug("Result: %s" % result)

    acc = np.count_nonzero(
        np.array(endresult) == np.array(
            list(TRUE_LABELS.lower()[:len(endresult)]))) / len(endresult)
    print "Accuracy:", acc * 100

    amp.stop()
示例#19
0
    def run_pair(self, epo_train, epo_test, bp_nr, fold_nr, pair_nr):
        class_pair = self.class_pairs[pair_nr]
        self.print_class_pair(class_pair)

        ### Run Training
        epo_train_pair = select_classes(epo_train, class_pair)
        epo_test_pair = select_classes(epo_test, class_pair)
        if self.ival_optimizer is not None:
            best_segment_ival = self.ival_optimizer.optimize(epo_train_pair)
            log.info("Ival {:.0f}ms - {:.0f}ms".format(*best_segment_ival))
            epo_train_pair = select_ival(epo_train_pair, best_segment_ival)
            epo_test_pair = select_ival(epo_test_pair, best_segment_ival)
            epo_train = select_ival(epo_train, best_segment_ival)
            epo_test = select_ival(epo_test, best_segment_ival)

        self.train_labels[fold_nr][pair_nr] = epo_train_pair.axes[0]
        self.test_labels[fold_nr][pair_nr] = epo_test_pair.axes[0]

        ## Calculate CSP
        filters, patterns, variances = calculate_csp(epo_train_pair)
        ## Apply csp, calculate features
        if self.n_filters is not None:
            # take topmost and bottommost filters, e.g.
            # for n_filters=3 0,1,2,-3,-2,-1
            columns = range(0, self.n_filters) + range(-self.n_filters, 0)
        else:  # take all possible filters
            columns = range(len(filters))
        train_feature = apply_csp_var_log(epo_train_pair, filters, columns)

        ## Calculate LDA
        clf = lda_train_scaled(train_feature, shrink=True)
        assert not np.any(np.isnan(clf[0]))
        assert not np.isnan(clf[1])
        ## Apply LDA to train
        train_out = lda_apply(train_feature, clf)
        correct_train = train_feature.axes[0] == class_pair[1]
        predicted_train = train_out >= 0
        train_accuracy = sum(correct_train == predicted_train) / float(len(predicted_train))

        ### Feature Computation and LDA Application for test
        test_feature = apply_csp_var_log(epo_test_pair, filters, columns)
        test_out = lda_apply(test_feature, clf)
        correct_test = test_feature.axes[0] == class_pair[1]
        predicted_test = test_out >= 0
        test_accuracy = sum(correct_test == predicted_test) / float(len(predicted_test))

        ### Feature Computations for full fold (for later multiclass)
        train_feature_full_fold = apply_csp_var_log(epo_train, filters, columns)
        test_feature_full_fold = apply_csp_var_log(epo_test, filters, columns)
        ### Store results
        # only store used patterns filters variances
        # to save memory space on disk
        self.store_results(
            bp_nr,
            fold_nr,
            pair_nr,
            filters[:, columns],
            patterns[:, columns],
            variances[columns],
            train_feature,
            test_feature,
            train_feature_full_fold,
            test_feature_full_fold,
            clf,
            train_accuracy,
            test_accuracy,
        )
        if self.ival_optimizer is not None:
            self.best_ival[bp_nr, fold_nr, pair_nr] = best_segment_ival

        self.print_results(bp_nr, fold_nr, pair_nr)