def test_exg_register(self): reg1 = bytes([3, 160, 16, 64, 71, 0, 0, 0, 2, 1]) reg2 = bytes([0, 171, 16, 21, 21, 0, 0, 0, 2, 1]) exg_reg1 = ExGRegister(reg1) exg_reg2 = ExGRegister(reg2) self.assertEqual(exg_reg1.ch1_gain, 4) self.assertEqual(exg_reg1.ch2_gain, 4) self.assertEqual(exg_reg1.ch1_mux, ExGMux.NORMAL) self.assertEqual(exg_reg1.get_ch_mux_bin(0), 0b0000) self.assertEqual(exg_reg1.ch2_mux, ExGMux.RLD_DRM) self.assertEqual(exg_reg1.get_ch_mux_bin(1), 0b0111) self.assertEqual(exg_reg1.ch1_powerdown, False) self.assertEqual(exg_reg1.ch2_powerdown, False) self.assertEqual(exg_reg1.data_rate, 1000) self.assertEqual(exg_reg1.binary, reg1) self.assertEqual(exg_reg2.ch1_gain, 1) self.assertEqual(exg_reg2.ch2_gain, 1) self.assertEqual(exg_reg2.ch1_mux, ExGMux.TEST_SIGNAL) self.assertEqual(exg_reg2.ch2_mux, ExGMux.TEST_SIGNAL) self.assertEqual(exg_reg2.ch1_powerdown, False) self.assertEqual(exg_reg2.ch2_powerdown, False) self.assertEqual(exg_reg2.data_rate, 125) self.assertEqual(exg_reg2.binary, reg2) self.assertRaises(ValueError, exg_reg1.get_ch_mux, 2) self.assertRaises(ValueError, exg_reg1.get_ch_mux, -1)
def test_exg_register_rld_ref(self): reg_bin = bytes( [0x03, 0xA8, 0x10, 0x40, 0x40, 0x2D, 0x00, 0x00, 0x02, 0x03]) reg = ExGRegister(reg_bin) self.assertEqual(reg.rld_ref, ERLDRef.INTERNAL) reg_bin = bytes( [0x03, 0xA8, 0x10, 0x40, 0x40, 0x2D, 0x00, 0x00, 0x02, 0x01]) reg = ExGRegister(reg_bin) self.assertEqual(reg.rld_ref, ERLDRef.EXTERNAL)
def test_exg_register_rld_channels(self): reg_bin = bytes( [0x03, 0xA8, 0x10, 0x40, 0x40, 0x2D, 0x00, 0x00, 0x02, 0x03]) reg = ExGRegister(reg_bin) self.assertEqual( reg.rld_channels, [ExGRLDLead.RLD1P, ExGRLDLead.RLD2P, ExGRLDLead.RLD2N]) reg_bin = bytes( [0x03, 0xA8, 0x10, 0x40, 0x40, 0x00, 0x00, 0x00, 0x02, 0x03]) reg = ExGRegister(reg_bin) self.assertEqual(reg.rld_channels, [])
def test_parsing_w_sync(self): fpath, _ = get_synced_bin_vs_consensys_pair_fpath() with open(fpath, 'rb') as f: reader = ShimmerBinaryReader(f) exp_dr = 64 exp_sensors = [ESensorGroup.CH_A13] exp_channels = [ EChannelType.TIMESTAMP, EChannelType.INTERNAL_ADC_13 ] exp_offsets = np.array([372, 362, 364, 351]) exp_sync_ts = np.array([3725366, 4071094, 4397558, 4724022]) exp_exg_reg1 = ExGRegister( b'\x00\x80\x10\x00\x00\x00\x00\x00\x02\x01') exp_exg_reg2 = ExGRegister( b'\x00\x80\x10\x00\x00\x00\x00\x00\x02\x01') sample_size = sum([dt.size for dt in get_ch_dtypes(exp_channels)]) samples_per_block = int((512 - 9) / sample_size) block_size = samples_per_block * sample_size + 9 self.assertEqual(reader.has_global_clock, True) self.assertEqual(reader.has_sync, True) self.assertEqual(reader.is_sync_master, False) self.assertEqual(reader.enabled_sensors, exp_sensors) self.assertEqual(reader.enabled_channels, exp_channels) self.assertEqual(reader.sample_rate, exp_dr) self.assertEqual(reader.samples_per_block, samples_per_block) self.assertEqual(reader.start_timestamp, 3085110) self.assertEqual(reader.block_size, block_size) self.assertEqual(reader.get_exg_reg(0), exp_exg_reg1) self.assertEqual(reader.get_exg_reg(1), exp_exg_reg2) self.assertEqual(reader.exg_reg1, exp_exg_reg1) self.assertEqual(reader.exg_reg2, exp_exg_reg2) samples, (off_index, sync_off) = reader.read_data() ts = samples[EChannelType.TIMESTAMP] np.testing.assert_equal(ts[off_index], exp_sync_ts) np.testing.assert_equal(sync_off, exp_offsets) # Sanity check on the timestamps: they should all be spaced equally apart with a stride that is equal # to the sampling rate. ts = samples[EChannelType.TIMESTAMP] ts_diff = np.diff(ts) correct_diff = np.sum(ts_diff == exp_dr) self.assertTrue(correct_diff / len(ts_diff) > 0.98)
def test_exg_register_powerdown(self): pd = 0x1 << 7 reg_bin = bytes([3, 160, 16, pd, pd, 0, 0, 0, 2, 1]) reg = ExGRegister(reg_bin) self.assertEqual(reg.ch1_powerdown, True) self.assertEqual(reg.ch2_powerdown, True)
def test_exg_register_print(self): reg_bin = bytes( [0x03, 0xA8, 0x10, 0x40, 0x40, 0x2D, 0x00, 0x00, 0x02, 0x03]) reg = ExGRegister(reg_bin) str_repr = str(reg) self.assertTrue('Data Rate: 1000' in str_repr) self.assertTrue('RLD Powerdown: False' in str_repr)
def receive(self, ser: BluetoothSerial) -> any: rlen = ser.read_response(EXG_REGS_RESPONSE, arg_format='B') if not rlen == self._rlen: raise ValueError( 'Response does not contain required amount of bytes') reg_data = ser.read(rlen) return ExGRegister(reg_data)
def test_post_process_exg_signal(self): exg_reg1 = ExGRegister(b'\x03\x80\x10\x40\x40\x00\x00\x00\x02\x01') exg1_gain = 4 exg_reg2 = ExGRegister(b'\x03\x80\x10\x20\x20\x00\x00\x00\x02\x01') exg2_gain = 2 chip_gain = { 0: exg1_gain, 1: exg2_gain, } samples = { EChannelType.EXG_ADS1292R_1_CH1_24BIT: np.random.randn(1000), EChannelType.EXG_ADS1292R_2_CH2_24BIT: np.random.randn(1000), EChannelType.EXG_ADS1292R_1_CH1_16BIT: np.random.randn(1000), EChannelType.EXG_ADS1292R_2_CH2_16BIT: np.random.randn(1000), } samples_w_ts = {**samples, EChannelType.TIMESTAMP: np.arange(1000)} m_br = Mock(spec=ShimmerBinaryReader) m_br.get_exg_reg.side_effect = lambda x: exg_reg1 if x == 0 else exg_reg2 m_br.read_data.side_effect = lambda: (dict(samples_w_ts), ((), ())) type(m_br).sample_rate = PropertyMock(return_value=1) type(m_br).enabled_sensors = PropertyMock(return_value=[]) type(m_br).has_sync = PropertyMock(return_value=False) type(m_br).has_global_clock = PropertyMock(return_value=False) type(m_br).start_timestamp = PropertyMock(return_value=0) type(m_br).exg_reg1 = PropertyMock(return_value=exg_reg1) type(m_br).exg_reg2 = PropertyMock(return_value=exg_reg2) reader = ShimmerReader(bin_reader=m_br, post_process=False) reader.load_file_data() for ch_type in samples: np.testing.assert_equal(samples[ch_type], reader[ch_type]) reader = ShimmerReader(bin_reader=m_br, post_process=True) reader.load_file_data() for ch in samples: bit = 16 if '16' in ch.name else 24 gain = chip_gain[get_exg_ch(ch)[0]] expected = (samples[ch] - 0) * 2.420 / (2**(bit - 1) - 1) / gain actual = reader[ch] np.testing.assert_almost_equal(actual, expected)
def get_exg_register(self, chip_id: int) -> ExGRegister: if not 0 <= chip_id <= 1: raise ValueError('Parameter chip_id must be 0 or 1') offset = UART_INFOMEM_EXG_OFFSET + chip_id * 0x0A dlen = 0x0A reg_data = self.get_infomem(offset, dlen) return ExGRegister(reg_data)
def test_reader_exg_register(self): exp_reg1_content = bytes(range(10)) exp_reg1 = ExGRegister(exp_reg1_content) exp_reg2_content = bytes(range(10, 0, -1)) exp_reg2 = ExGRegister(exp_reg2_content) exp_regs = [exp_reg1, exp_reg2] m_br = Mock(spec=ShimmerBinaryReader) m_br.get_exg_reg.side_effect = lambda x: exp_regs[x] type(m_br).exg_reg1 = PropertyMock(return_value=exp_reg1) type(m_br).exg_reg2 = PropertyMock(return_value=exp_reg2) reader = ShimmerReader(bin_reader=m_br) for i in range(2): self.assertEqual(reader.get_exg_reg(i), exp_regs[i]) actual_reg1 = reader.exg_reg1 actual_reg2 = reader.exg_reg2 self.assertEqual(actual_reg1, exp_reg1) self.assertEqual(actual_reg2, exp_reg2)
def get_exg_reg(self, chip_id: int) -> ExGRegister: reg_content = self._exg_regs[chip_id] return ExGRegister(reg_content)
def do_assert(a: bytes, b: bytes, result: bool) -> None: self.assertEqual(ExGRegister(a) == ExGRegister(b), result)
def test_exg_register_rld_powerdown(self): pd = 0x01 << 5 reg_bin = bytes([0, 0, 0, 0, 0, pd, 0, 0, 0, 0]) reg = ExGRegister(reg_bin) self.assertEqual(reg.rld_powerdown, False)
def get_exg_reg(self, chip_id: int) -> ExGRegister: return ExGRegister(self._bin_reader.get_exg_reg(chip_id))