def test_different_lengths(self): """Check if setting the length works correctly.""" d = data_factory(np.array([1])) testdata = [ # ms, fs, elems [1000, 1000, 1000], [1000, 100, 100], [1000, 10, 10], [1000, 1, 1], [2000, 1000, 2000], [2000, 100, 200], [2000, 10, 20], [2000, 1, 2], [1000, 2000, 2000], [1000, 200, 200], [1000, 20, 20], [1000, 2, 2], [1000, 1000, 1000], [100, 1000, 100], [10, 1000, 10], [1, 1000, 1], ] # 1000ms / 1000hz -> 1000 elements for ms, fs, elems in testdata: rb = RingBuffer(ms) d.fs = fs rb.append(d) self.assertEqual(rb.length, elems)
def test_add_with_markers(self): # add three elements to empty buffer pluss three markers m = [[0, '0'], [20, '2']] d3 = data_factory(np.arange(9).reshape(3, 3)) d3_w_markers = d3.copy(markers=m) self.rb.append(d3_w_markers) data = self.rb.get() self.assertEqual(data.markers, m) # move three more elements into the buffer, the markers should # stay the same, as we don't overfilled the buffer yet self.rb.append(d3) data = self.rb.get() self.assertEqual(data.markers, m) # now we do it again twice and the first one should disappear # the second one should move one position back self.rb.append(d3) self.rb.append(d3) data = self.rb.get() self.assertEqual(data.markers, [[0, '2']]) # again, and no markers should be left self.rb.append(d3) data = self.rb.get() self.assertEqual(data.markers, []) m = [[0, '0'], [90, '9'], [100, '10']] d11_w_markers = data_factory(np.arange(33).reshape(11, 3), markers=m) self.rb.append(d11_w_markers) data = self.rb.get() self.assertEqual(data.markers, [[80, '9'], [90, '10']]) # test overfull add on empty buffer m0 = [[i * 10, i] for i in range(11)] d0_w_markers = data_factory(np.arange(33).reshape(11, 3), markers=m0) self.rb = RingBuffer(100) self.rb.append(d0_w_markers) data = self.rb.get() self.assertEqual(data.markers, [[x[0] - 10, x[1]] for x in m0[-10:]]) # test full add on full buffer self.rb = RingBuffer(100) m0 = [[i, i] for i in range(10)] self.rb.append(data_factory(np.arange(30).reshape(10, 3), markers=m0)) m0 = [[x[0], x[1] + 10] for x in m0] self.rb.append(data_factory(np.arange(30).reshape(10, 3), markers=m0)) data = self.rb.get() self.assertEqual(data.markers, m0)
def test_appending_until_twice_full_w_1hz(self): """Test appending until twice full with 1Hz.""" d = data_factory(np.array([0])) d.fs = 1 rb = RingBuffer(10000) try: for i in range(21): rb.append(d) rb.get() except AssertionError: self.fail()
def test_add(self): """Various adding variations must work.""" d0 = data_factory(np.array([0, 1, 2])) d1 = data_factory(np.array([3, 4, 5])) d2 = data_factory(np.array([6, 7, 8, 9, 10])) d3 = data_factory( np.array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])) # non-full-add self.rb.append(d0) data0 = self.rb.get() self.assertEqual(data0, d0) # non-full-add self.rb.append(d1) data1 = self.rb.get() self.assertEqual(data1, data_factory(np.concatenate([d0.data, d1.data]))) # overfull-add self.rb.append(d2) data2 = self.rb.get() self.assertEqual( data2, data_factory(np.concatenate([d0.data, d1.data, d2.data])[-10:])) self.rb.append(d3) data3 = self.rb.get() self.assertEqual(data3, data_factory(d3.data[-10:])) # test overfull add on empty buffer d0 = data_factory(np.arange(11)) self.rb = RingBuffer(100) self.rb.append(d0) data0 = self.rb.get() self.assertEqual(data0, data_factory(d0.data[-10:])) # test full add on full buffer self.rb = RingBuffer(100) self.rb.append(data_factory(np.arange(10))) d0 = data_factory(np.arange(10) + 10) self.rb.append(d0) data0 = self.rb.get() self.assertEqual(data0, d0)
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()
def runLoop(): alld = dynarray.DynamicArray( (None, len(amp.get_channels()))) # the growing numpy data matrix allm = [] # markers sfreq = amp.get_sampling_frequency() # sampling frequency ch_names = amp.get_channels() # channel names rb = RingBuffer( buffSize * 1000 ) # the buffer containing the last X seconds of data - declared in MILISECONDS totalTime = seed_d.shape[0] / raw_fromfile.info['sfreq'] fig = plt.figure() # plotting... th = fig.suptitle('') ah1 = fig.add_subplot(131) ah2 = fig.add_subplot(132) ah3 = fig.add_subplot(133) l1, = ah1.plot(sx, sy1) l2, = ah2.plot(sx, sy2) l3, = ah3.plot(sx2, sy3) # l=LoopState(); l.start() markeroffset = 0 # needed to store all data in one big mat/vector t0 = time.time() curTime = time.time() markTime = time.time() st = '' i = 0 fnames = [] # for making a movie... while curTime - t0 < totalTime: # l.get_state() != 'Stop': # keep track of time: curTime = time.time() # this is where you get the data data, marker = amp.get_data() if data.shape[0] > 0: # this is crucial for remembering filter state. data2 = hpf.handle(data) data3 = mr.handle(data2) data4 = lpf.handle(data3) data5 = resample.handle(data4) data6 = cwl.handle(data5) # something like this: #filterchain = [HPFilter, MRFilter, LPFilter, ResampleFilter, HPFilter, CWLFilter] #corr_data = ProcessFilters(chain, (data, marker)) # use case -- first using the MR Corrector #mr_data = MRFilter.filter(data) # then -- using the CWL corrector #cwl_mr_data = CWLFilter.filter(data) #dataf, rt_zi_bp = signal.lfilter(rt_b_bp, rt_a_bp, data, axis=0, zi=rt_zi_bp) # how to operate directly on the data cnt = io.convert_mushu_data(data, marker, sfreq, ch_names) mr_cnt = io.convert_mushu_data(data3, marker, sfreq, ch_names) cwl_cnt = io.convert_mushu_data(data6, marker, sfreq, ch_names) # f_cnt, rt_zi_bp = proc.lfilter(cnt, rt_b_bp, rt_a_bp, zi=rt_zi_bp) # real-time data preprocessing... # plotting... sy1.extend(cnt.data[:, channel_to_plot] ) # to visualize/plot -- s1 and s2 are deque's sy2.extend(mr_cnt.data[:, channel_to_plot]) sy3.extend(cwl_cnt.data[:, channel_to_plot]) l1.set_ydata(sy1) l2.set_ydata(sy2) l3.set_ydata(sy3) msy1 = np.mean(sy1) msy2 = np.mean(sy2) msy3 = np.mean(sy3) ah1.set_ylim(-5000 + msy1, 5000 + msy1) ah2.set_ylim(-250 + msy2, 250 + msy2) ah3.set_ylim(-250 + msy3, 250 + msy3) fig.canvas.draw() fig.canvas.flush_events() # i+=1; fname = '_tmp%03d.png' % i; plt.savefig(fname); fnames.append(fname) # currently has no purpose newsamples = cnt.data.shape[0] # append to ringbuffer, so we can calculate features later on on the last N secs/samples of data. rb.append(cwl_cnt) # append it to the big matrix, for saving later on with pickle. alld.extend(data6) for m in marker: allm.append([m[0] + markeroffset, m[1]]) markeroffset += newsamples / float(sfreq) * 1000. # do the following every 0.1 msec - with with the ringbuffer: if curTime - markTime > updateTime: # do Stuff markTime = curTime # 1) obtain last 1-second(s) d = rb.get() # clear_output(wait=True) # write some logging information here # clear_output clear the output of the cell, but if you do that you also remove the figures, it seems # so don't do it! str1 = 'Playing Back - time = %f' % (curTime - t0) str2 = 'Length Markers: %d' % len(allm) str3 = '%d, %d' % data.shape #str4 = 'Feature Value: %f' % feature #str5 = 'Scaled Signal for NF: %f' % signalToSend #print(str1 + '\n' + str2 + '\n' + str3 + '\n' + str4 + '\n' + str5) # print('Length Markers: %d' % len(allm)) # print(data.shape) th.set_text(str1 + '\n' + str2 + '\n' + str3)
def setUp(self): self.rb = RingBuffer(100)
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()
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)