def test_init(self): with self.assertRaises(TaborException): TaborSegment(None, None, None, None) with self.assertRaises(TaborException): TaborSegment(np.zeros(5), np.zeros(4), None, None) with self.assertRaises(TaborException): TaborSegment(np.zeros(4), np.zeros(4), np.zeros(4), None) with self.assertRaises(TaborException): TaborSegment(np.zeros(4), np.zeros(4), None, np.zeros(4)) ch_a = np.asarray(100 + np.arange(6), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(6), dtype=np.uint16) marker_a = np.ones(3, dtype=bool) marker_b = np.arange(3, dtype=np.uint16) ts = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b) self.assertIs(ts.ch_a, ch_a) self.assertIs(ts.ch_b, ch_b) self.assertIs(ts.marker_a, marker_a) self.assertIsNot(ts.marker_b, marker_b) np.testing.assert_equal(ts.marker_b, marker_b != 0)
def setUp(self): super().setUp() ramp_up = np.linspace(0, 2**14 - 1, num=192, dtype=np.uint16) ramp_down = ramp_up[::-1] zero = np.ones(192, dtype=np.uint16) * 2**13 sine = ((np.sin(np.linspace(0, 2 * np.pi, 192 + 64)) + 1) / 2 * (2**14 - 1)).astype(np.uint16) self.segments = [ TaborSegment(ramp_up, ramp_up, None, None), TaborSegment(ramp_down, zero, None, None), TaborSegment(sine, sine, None, None) ] self.zero_segment = TaborSegment(zero, zero, None, None) # program 1 self.sequence_tables = [[(10, 0, 0), (10, 1, 0), (10, 0, 0), (10, 1, 0)], [(1, 0, 0), (1, 1, 0), (1, 0, 0), (1, 1, 0)]] self.advanced_sequence_table = [(1, 1, 0), (1, 2, 0)] self.channel_pair = TaborChannelPair(self.instrument, (1, 2), 'tabor_unit_test')
def test_data_a(self): ch_a = np.asarray(100 + np.arange(32), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(32), dtype=np.uint16) marker_a = np.ones(16, dtype=bool) marker_b = np.asarray(list(range(5)) + list(range(6)) + list(range(5)), dtype=np.uint16) on = 1 << 14 off = 0 marker_a_data = np.asarray([0] * 8 + [on] * 8 + [0] * 8 + [on] * 8, dtype=np.uint16) on = 1 << 15 off = 0 marker_b_data = np.asarray([0] * 8 + [off] + [on] * 4 + [off] + [on] * 2 + [0] * 8 + [on] * 3 + [off] + [on] * 4) ts = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=None, marker_b=None) self.assertIs(ts.data_a, ch_a) ts = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=None) expected_data = ch_a + marker_a_data np.testing.assert_equal(ts.data_a, expected_data) ts = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=None, marker_b=marker_b) expected_data = ch_a + marker_b_data np.testing.assert_equal(ts.data_a, expected_data) ts = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b) expected_data = ch_a + marker_b_data + marker_a_data np.testing.assert_equal(ts.data_a, expected_data) with self.assertRaises(NotImplementedError): TaborSegment(ch_a=None, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b).data_a
def exec_general(self, data_1, data_2, fill_value=None): tabor_segments = [ TaborSegment(d1, d2, None, None) for d1, d2 in zip(data_1, data_2) ] expected_length = (sum(segment.num_points for segment in tabor_segments) + 16 * (len(tabor_segments) - 1)) * 2 offset = 0 pyte_result = 15000 * np.ones(expected_length, dtype=np.uint16) for i, segment in enumerate(tabor_segments): offset = pytabor.make_combined_wave(segment.ch_a, segment.ch_b, dest_array=pyte_result, dest_array_offset=offset, add_idle_pts=i > 0) self.assertEqual(expected_length, offset) result = make_combined_wave(tabor_segments, fill_value=15000) np.testing.assert_equal(pyte_result, result) dest_array = 15000 * np.ones(expected_length, dtype=np.uint16) result = make_combined_wave(tabor_segments, destination_array=dest_array) np.testing.assert_equal(pyte_result, result) # test that the destination array data is not copied self.assertEqual(dest_array.__array_interface__['data'], result.__array_interface__['data']) with self.assertRaises(ValueError): make_combined_wave(tabor_segments, destination_array=np.ones(16))
def test_from_binary_segment(self): ch_a = np.asarray(100 + np.arange(32), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(32), dtype=np.uint16) marker_a = np.ones(16, dtype=bool) marker_b = np.asarray(list(range(5)) + list(range(6)) + list(range(5)), dtype=np.uint16) segment = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b) binary = segment.get_as_binary() reconstructed = TaborSegment.from_binary_segment(binary) self.assertEqual(segment, reconstructed)
def test_from_binary_data(self): ch_a = np.asarray(100 + np.arange(32), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(32), dtype=np.uint16) marker_a = np.ones(16, dtype=bool) marker_b = np.asarray(list(range(5)) + list(range(6)) + list(range(5)), dtype=np.uint16) segment = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b) data_a = segment.data_a data_b = segment.data_b reconstructed = TaborSegment.from_binary_data(data_a, data_b) self.assertEqual(segment, reconstructed)
def test_eq(self): ch_a = np.asarray(100 + np.arange(32), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(32), dtype=np.uint16) marker_ones = np.ones(16, dtype=bool) marker_random = np.asarray(list(range(5)) + list(range(6)) + list(range(5)), dtype=np.uint16) marker_zeros = np.zeros(16, dtype=bool) segment_1 = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_ones, marker_b=marker_random) segment_2 = TaborSegment(ch_a=ch_a, ch_b=ch_a, marker_a=marker_ones, marker_b=marker_random) segment_a0 = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_zeros, marker_b=marker_random) segment_anone = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=None, marker_b=marker_random) segment_none = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=None, marker_b=None) self.assertEqual(segment_1, segment_1) self.assertNotEqual(segment_1, segment_2) self.assertEqual(segment_a0, segment_anone) self.assertEqual(segment_anone, segment_a0) self.assertEqual(segment_anone, segment_anone) self.assertNotEqual(segment_anone, segment_none) self.assertEqual(segment_none, segment_none) self.assertNotEqual(segment_a0, segment_1)
def test_invalid_segment_length(self): gen = itertools.count() data_1 = [ np.fromiter(gen, count=32, dtype=np.uint16), np.fromiter(gen, count=15, dtype=np.uint16), np.fromiter(gen, count=192, dtype=np.uint16) ] data_2 = [ np.fromiter(gen, count=32, dtype=np.uint16), np.fromiter(gen, count=16, dtype=np.uint16), np.fromiter(gen, count=193, dtype=np.uint16) ] tabor_segments = [TaborSegment(d, d, None, None) for d in data_1] with self.assertRaises(ValueError): make_combined_wave(tabor_segments) tabor_segments = [TaborSegment(d, d, None, None) for d in data_2] with self.assertRaises(ValueError): make_combined_wave(tabor_segments)
def test_data_b(self): ch_a = np.asarray(100 + np.arange(6), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(6), dtype=np.uint16) marker_a = np.ones(3, dtype=bool) marker_b = np.arange(3, dtype=np.uint16) ts = TaborSegment(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b) self.assertIs(ts.data_b, ch_b)
def test_read_waveforms(self): self.channel_pair._amend_segments(self.segments) waveforms = self.channel_pair.read_waveforms() segments = [ TaborSegment.from_binary_segment(waveform) for waveform in waveforms ] expected = [self.zero_segment, *self.segments] for ex, r in zip(expected, segments): ex1, ex2 = ex.data_a, ex.data_b r1, r2 = r.data_a, r.data_b np.testing.assert_equal(ex1, r1) np.testing.assert_equal(ex2, r2) self.assertEqual(expected, segments)
def exec_general(self, data_1, data_2, fill_value=None): tabor_segments = [ TaborSegment(d1, d2, None, None) for d1, d2 in zip(data_1, data_2) ] expected_length = (sum(segment.num_points for segment in tabor_segments) + 16 * (len(tabor_segments) - 1)) * 2 result = make_combined_wave(tabor_segments, fill_value=fill_value) self.assertEqual(len(result), expected_length) validate_result(tabor_segments, result, fill_value=fill_value) destination_array = np.empty(expected_length, dtype=np.uint16) result = make_combined_wave(tabor_segments, fill_value=fill_value, destination_array=destination_array) validate_result(tabor_segments, result, fill_value=fill_value) self.assertEqual(destination_array.__array_interface__['data'], result.__array_interface__['data'], 'Data was copied')
def setUp(self): self.ch_a = [ np.arange(16, dtype=np.uint16), np.arange(32, 64, dtype=np.uint16) ] self.ch_b = [ 1000 + np.arange(16, dtype=np.uint16), 1000 + np.arange(32, 64, dtype=np.uint16) ] self.marker_a = [np.ones(8, bool), np.array([0, 1] * 8, dtype=bool)] self.marker_b = [ np.array([0, 0, 0, 1] * 2, bool), np.array([1, 0, 1, 1] * 4, dtype=bool) ] self.segments = [ TaborSegment(ch_a, ch_b, marker_a, marker_b) for ch_a, ch_b, marker_a, marker_b in zip( self.ch_a, self.ch_b, self.marker_a, self.marker_b) ] self.sequencer_tables = [[(1, 1, 0), (1, 2, 0)], [(1, 1, 0), (2, 2, 0), (1, 1, 0)]] self.adv_sequencer_table = [(1, 1, 0), (1, 2, 0), (2, 1, 0)] self.read_segments = [ segment.get_as_binary() for segment in self.segments ] self.read_sequencer_tables = [ (np.array([1, 1]), np.array([1, 2]), np.array([0, 0])), (np.array([1, 2, 1]), np.array([1, 2, 1]), np.array([0, 0, 0])) ] self.read_adv_sequencer_table = (np.array([1, 1, 2]), np.array([1, 2, 1]), np.array([0, 0, 0])) self.selection_order = [0, 1, 0, 1, 1, 0, 0, 1, 0, 1] self.selection_order_without_repetition = [0, 1, 0, 1, 0, 0, 1, 0, 1]
def test_num_points(self): self.assertEqual( TaborSegment(np.zeros(6), np.zeros(6), None, None).num_points, 6)