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 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 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_raw_simple(self): b = StreamBuffer(1000, [100, 100, 100]) b.suppress_mode = 'off' expect = np.arange(126 * 2, dtype=np.uint16).reshape((126, 2)) b.insert_raw(np.left_shift(expect, 2)) b.process() data = b.data_get(0, 126) np.testing.assert_allclose(expect[:, 0], data[:, 0, 0])
def test_get_over_reduction_direct_with_reduction0_nan_mean(self): b = StreamBuffer(2.0, [10, 10], 1000.0) b.suppress_mode = 'mean_1_n_1' 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(r1[0, 0]['mean']))
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_insert_raw_simple(self): b = StreamBuffer(1.0, [100, 100, 100], 1000.0) b.suppress_mode = 'off' expect = np.arange(126 * 2, dtype=np.uint16).reshape((126, 2)) raw = np.left_shift(expect, 2) raw[1::2, 1] = np.bitwise_or(raw[1::2, 1], 0x0002) b.insert_raw(raw) b.process() data = b.data_get(0, 126) np.testing.assert_allclose(expect[:, 0], data[:, 0]['mean']) self.assertEqual(0, b.status()['skip_count']['value'])
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_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_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_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, 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 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 _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