Example #1
0
 def test_append_empty(self):
     """Appending several emtpy dats must not modify the Block Buffer."""
     b = BlockBuffer(5)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     self.assertEqual(self.empty_dat, b.get())
Example #2
0
 def test_append_until_full(self):
     """Appending fractions of block_length, must accumulate in the buffer until block_length is reached."""
     b = BlockBuffer(5)
     for i in range(4):
         b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(self.empty_dat, ret)
     b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(self.dat_5, ret)
Example #3
0
 def test_append_empty(self):
     """Appending several emtpy dats must not modify the Block Buffer."""
     b = BlockBuffer(5)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     b.append(self.empty_dat)
     self.assertEqual(self.empty_dat, b.get())
Example #4
0
 def test_append_with_markers(self):
     """Check if markers are handled correctly."""
     markers = [[i, 'x'] for i in range(5)]
     b = BlockBuffer(5)
     for i in range(4):
         b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(self.empty_dat, ret)
     b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(ret.markers, markers)
Example #5
0
 def test_append_until_full(self):
     """Appending fractions of block_length, must accumulate in the buffer until block_length is reached."""
     b = BlockBuffer(5)
     for i in range(4):
         b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(self.empty_dat, ret)
     b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(self.dat_5, ret)
Example #6
0
 def test_append_with_markers(self):
     """Check if markers are handled correctly."""
     markers = [[i, 'x'] for i in range(5)]
     b = BlockBuffer(5)
     for i in range(4):
         b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(self.empty_dat, ret)
     b.append(self.dat_1)
     ret = b.get()
     self.assertEqual(ret.markers, markers)
Example #7
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()
Example #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)
Example #9
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)