def stream_buffer_02(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'off' b.insert(usb_packet_factory(0)) b.insert(usb_packet_factory(2)) b.process() return b
def _create_file_insert(self, packet_index, count_incr, count_total): stream_buffer = StreamBuffer(10.0, [10], 1000.0) stream_buffer.suppress_mode = 'off' if packet_index > 0: data = usb_packet_factory(0, packet_index - 1) stream_buffer.insert(data) stream_buffer.process() fh = io.BytesIO() d = DataRecorder(fh) sample_id = stream_buffer.sample_id_range[1] for _ in range(0, count_total, count_incr): data = usb_packet_factory(packet_index, count_incr) stream_buffer.insert(data) stream_buffer.process() sample_id_next = stream_buffer.sample_id_range[1] d.insert(stream_buffer.samples_get(sample_id, sample_id_next)) sample_id = sample_id_next packet_index += count_incr d.close() fh.seek(0) # dfr = datafile.DataFileReader(fh) # dfr.pretty_print() # fh.seek(0) return fh
def _create_file(self, packet_index, count=None, signals=None): d = FakeJS110() stream_buffer = StreamBuffer(60.0, [10], 1000.0) stream_buffer.suppress_mode = 'off' if packet_index > 0: data = usb_packet_factory(0, packet_index - 1) stream_buffer.insert(data) stream_buffer.process() with JlsWriter(d, self._filename1, signals=signals) as wr: wr.stream_notify(stream_buffer) data = usb_packet_factory(packet_index, count) stream_buffer.insert(data) stream_buffer.process() wr.stream_notify(stream_buffer) self.assertTrue(os.path.isfile(self._filename1))
def stream_buffer_01(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() return b
def test_insert_process(self): b = StreamBuffer(1.0, [100, 10], 1000.0) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) self.assertEqual(0, b.status()['device_sample_id']['value']) b.insert(frame) self.assertEqual(126, b.status()['device_sample_id']['value']) self.assertEqual(0, b.status()['sample_id']['value']) b.process() self.assertEqual((0, 126), b.sample_id_range) self.assertEqual((0.0, 1.0), b.limits_time) self.assertEqual((-874, 126), b.limits_samples) self.assertEqual(126, b.time_to_sample_id(1.0)) self.assertEqual(1.0, b.sample_id_to_time(126)) self.assertEqual(0.999, b.sample_id_to_time(125)) self.assertEqual(b.limits_time[0], b.sample_id_to_time(b.limits_samples[0])) self.assertEqual(b.limits_time[1], b.sample_id_to_time(b.limits_samples[1])) self.assertEqual(126, b.status()['sample_id']['value']) samples = b.samples_get(0, 126, ['raw']) raw_data = b.samples_get(0, 126, 'raw') np.testing.assert_allclose(samples['signals']['raw']['value'], raw_data) expect = np.arange(126 * 2, dtype=np.uint16).reshape((126, 2)) np.testing.assert_allclose(expect, np.right_shift(raw_data, 2)) np.testing.assert_allclose(expect, b.data_buffer[0:126 * 2].reshape( (126, 2))) data = b.data_get(0, 126) np.testing.assert_allclose(expect[:, 0], data[:, 0]['mean'])
def test_get_over_reduction_direct_with_raw_nan(self): b = StreamBuffer(2.0, [10, 10], 1000.0) frame = usb_packet_factory(0, 1) frame[8 + 11 * 4:8 + 15 * 4:] = 0xff b.insert(frame) b.process() r = b.get_reduction(0, 0, 126) self.assertTrue(all(np.isfinite(r[:, 0]['mean'])))
def test_wrap_unaligned(self): frame = usb_packet_factory(0, 4) b = StreamBuffer((2 * SAMPLES_PER + 2) / 1000.0, [], 1000.0) b.insert(frame) b.process() self.assertEqual(SAMPLES_PER * 4, b.sample_id_range[1]) data = np.right_shift(b.samples_get(SAMPLES_PER * 2, SAMPLES_PER * 4, 'raw'), 2) expect = np.arange(SAMPLES_PER * 4, SAMPLES_PER * 8, dtype=np.uint16).reshape((SAMPLES_PER * 2, 2)) np.testing.assert_allclose(expect, data)
def _create_file(self, packet_index, count=None): stream_buffer = StreamBuffer(401.0, [200, 100], 1000.0) stream_buffer.suppress_mode = 'off' if packet_index > 0: data = usb_packet_factory(0, packet_index - 1) stream_buffer.insert(data) stream_buffer.process() fh = io.BytesIO() d = DataRecorder(fh) d.stream_notify(stream_buffer) data = usb_packet_factory(packet_index, count) stream_buffer.insert(data) stream_buffer.process() d.stream_notify(stream_buffer) d.close() fh.seek(0) return fh
def test_get_over_reduction_direct_with_reduction0_nan(self): b = StreamBuffer(2.0, [10, 10], 1000.0) frame = usb_packet_factory(0, 1) frame[8+10*4:8+22*4:] = 0xff b.insert(frame) b.process() r0 = b.get_reduction(0, 0, 126) self.assertFalse(np.isfinite(r0[1, 0]['mean'])) r1 = b.get_reduction(1, 0, 126) self.assertTrue(np.isfinite(r0[0, 0]['mean']))
def test_bulk(self): r = RawProcessor() r.suppress_mode = 'off' self.assertEqual('off', r.suppress_mode) r.calibration_set([-10.0] * 7, [2.0] * 7, [-2.0, 1.0], [4.0, 1.0]) raw = usb_packet_factory(0, 1)[8:].view(dtype=np.uint16) cal, bits = r.process_bulk(raw) self.assertEqual((126, 2), cal.shape) np.testing.assert_allclose([[-20.0, -4.0], [-16, 4], [-12, 12]], cal[0:3, :]) np.testing.assert_allclose([0x20, 0x20, 0x20], bits[0:3])
def test_get_over_reduction(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 40, 20) self.assertEqual((2, STATS_FIELD_COUNT), data.shape) assert_single_stat_close([20, 19.0, 2660.0, 0.0, 38.0], data[0, 0]) assert_single_stat_close([20, 20.0, 2660.0, 1.0, 39.0], data[0, 1]) assert_single_stat_close([20, 59.0, 2660.0, 40.0, 78.0], data[1, 0])
def test_get_over_reduction(self): b = StreamBuffer(2000, [10, 10], 1000.0) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 40, 20) self.assertEqual((2, STATS_FIELDS, STATS_VALUES), data.shape) np.testing.assert_allclose([19.0, 133.0, 0.0, 38.0], data[0, 0, :]) np.testing.assert_allclose([20.0, 133.0, 1.0, 39.0], data[0, 1, :]) np.testing.assert_allclose([59.0, 133.0, 40.0, 78.0], data[1, 0, :])
def test_get_over_reduction_direct(self): b = StreamBuffer(2000, [10, 10]) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 20, 10) self.assertEqual((2, 3, 4), data.shape) np.testing.assert_allclose([9.0, 33.0, 0.0, 18.0], data[0, 0, :]) np.testing.assert_allclose([29.0, 33.0, 20.0, 38.0], data[1, 0, :]) np.testing.assert_allclose([10.0, 33.0, 1.0, 19.0], data[0, 1, :]) np.testing.assert_allclose([30.0, 33.0, 21.0, 39.0], data[1, 1, :])
def test_calibration(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'off' self.assertEqual('off', b.suppress_mode) b.calibration_set([-10.0] * 7, [2.0] * 7, [-2.0, 1.0], [4.0, 1.0]) frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 10, 1) self.assertEqual((10, STATS_FIELD_COUNT), data.shape) np.testing.assert_allclose([-20.0, -4.0, 80.0, 0, 0, 1], data[0, :]['mean']) np.testing.assert_allclose([12., 60., 720., 0, 0, 1], data[8, :]['mean'])
def test_get_over_reduction_direct(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 20, 10) self.assertEqual((2, STATS_FIELD_COUNT), data.shape) np.testing.assert_allclose([10, 9.0, 330.0, 0.0, 18.0], single_stat_as_array(data[0, 0])) np.testing.assert_allclose([10, 29.0, 330.0, 20.0, 38.0], single_stat_as_array(data[1, 0])) np.testing.assert_allclose([10, 10.0, 330.0, 1.0, 19.0], single_stat_as_array(data[0, 1])) np.testing.assert_allclose([10, 30.0, 330.0, 21.0, 39.0], single_stat_as_array(data[1, 1]))
def test_get_over_samples(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 21, 5) self.assertEqual((4, STATS_FIELD_COUNT), data.shape) np.testing.assert_allclose(np.arange(10), b.data_buffer[0:10]) # processed correctly np.testing.assert_allclose([4.0, 14.0, 24.0, 34.0], data[:, 0]['mean']) np.testing.assert_allclose([5, 4.0, 40.0, 0.0, 8.0], single_stat_as_array(data[0, 0])) np.testing.assert_allclose([5.0, 15.0, 25.0, 35.0], data[:, 1]['mean'])
def test_get_over_samples(self): b = StreamBuffer(2000, [10, 10]) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) b.insert(frame) b.process() data = b.data_get(0, 21, 5) self.assertEqual((4, 3, 4), data.shape) np.testing.assert_allclose(np.arange(10), b.data_buffer[0:10]) # processed correctly np.testing.assert_allclose([4.0, 14.0, 24.0, 34.0], data[:, 0, 0]) np.testing.assert_allclose([4.0, 8.0, 0.0, 8.0], data[0, 0, :]) np.testing.assert_allclose([5.0, 15.0, 25.0, 35.0], data[:, 1, 0])
def _create_file(self, packet_index, count=None): stream_buffer = StreamBuffer(2000, [10]) stream_buffer.suppress_mode = 'off' if packet_index > 0: data = usb_packet_factory(0, packet_index - 1) stream_buffer.insert(data) stream_buffer.process() fh = io.BytesIO() d = DataRecorder(fh, sampling_frequency=1000) d.process(stream_buffer) data = usb_packet_factory(packet_index, count) stream_buffer.insert(data) stream_buffer.process() d.process(stream_buffer) d.close() fh.seek(0) # from joulescope import datafile # dfr = datafile.DataFileReader(fh) # dfr.pretty_print() # fh.seek(0) return fh
def _create_file(self, packet_index, count=None): data_recorder.SAMPLES_PER_REDUCTION = 1000 data_recorder.REDUCTIONS_PER_TLV = 2 stream_buffer = StreamBuffer(60.0, [10], 1000.0) stream_buffer.suppress_mode = 'off' if packet_index > 0: data = usb_packet_factory(0, packet_index - 1) stream_buffer.insert(data) stream_buffer.process() fh = io.BytesIO() d = data_recorder.DataRecorder(fh) d.stream_notify(stream_buffer) data = usb_packet_factory(packet_index, count) stream_buffer.insert(data) stream_buffer.process() d.stream_notify(stream_buffer) d.close() fh.seek(0) # dfr = datafile.DataFileReader(fh) # dfr.pretty_print() # fh.seek(0) return fh
def test_wrap_aligned(self): frame = usb_packet_factory(0, 4) b = StreamBuffer(2.0 * SAMPLES_PER / 1000.0, [], 1000.0) b.suppress_mode = 'off' b.insert(frame) b.process() self.assertEqual((SAMPLES_PER * 2, SAMPLES_PER * 4), b.sample_id_range) data = b.samples_get(SAMPLES_PER * 2, SAMPLES_PER * 4, 'raw') data = np.right_shift(data, 2) expect = np.arange(SAMPLES_PER * 4, SAMPLES_PER * 8, dtype=np.uint16).reshape((SAMPLES_PER * 2, 2)) np.testing.assert_allclose(expect, data) np.testing.assert_allclose(expect[:, 0], b.data_buffer[::2]) data = b.data_get(SAMPLES_PER * 2, SAMPLES_PER * 4) np.testing.assert_allclose(expect[:, 0], data[:, 0]['mean']) np.testing.assert_allclose(expect[:, 1], data[:, 1]['mean'])
def test_insert_process(self): b = StreamBuffer(1000000, [100, 100, 100]) b.suppress_mode = 'off' frame = usb_packet_factory(0, 1) self.assertEqual(0, b.status()['device_sample_id']['value']) b.insert(frame) self.assertEqual(126, b.status()['device_sample_id']['value']) self.assertEqual(0, b.status()['sample_id']['value']) b.process() self.assertEqual(126, b.status()['sample_id']['value']) data = b.raw_get(0, 126) expect = np.arange(126*2, dtype=np.uint16).reshape((126, 2)) np.testing.assert_allclose(expect, np.right_shift(data, 2)) np.testing.assert_allclose(expect, b.data_buffer[0:126*2].reshape((126, 2))) data = b.data_get(0, 126) np.testing.assert_allclose(expect[:, 0], data[:, 0, 0])
def test_truncated(self): stream_buffer = StreamBuffer(400.0, [10], 1000.0) stream_buffer.suppress_mode = 'off' fh = io.BytesIO() d = DataRecorder(fh) d.stream_notify(stream_buffer) count = 16 for idx in range(0, 160, count): data = usb_packet_factory(idx, count) stream_buffer.insert(data) stream_buffer.process() d.stream_notify(stream_buffer) fh.seek(0) #r = datafile.DataFileReader(fh) #r.pretty_print() r = DataReader().open(fh)
def setUp(self): self.b = StreamBuffer(2000, [10, 10], sampling_frequency=1000) self.v = View(stream_buffer=self.b, calibration=None) self.b.insert(usb_packet_factory(0, 2)) self.b.process() self.v.open()