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.from_sampled(ramp_up, ramp_up, None, None), TaborSegment.from_sampled(ramp_down, zero, None, None), TaborSegment.from_sampled(sine, sine, None, None) ] self.zero_segment = TaborSegment.from_sampled(zero, zero, None, None) # program 1 self.sequence_tables_raw = [[(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.sequence_tables = self.to_new_sequencer_tables( self.sequence_tables_raw) self.advanced_sequence_table = self.to_new_advanced_sequencer_table( self.advanced_sequence_table) self.channel_pair = TaborChannelPair(self.instrument, (1, 2), 'tabor_unit_test')
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.from_sampled(ch_a=ch_a, ch_b=ch_b, marker_a=marker_ones, marker_b=marker_random) segment_2 = TaborSegment.from_sampled(ch_a=ch_a, ch_b=ch_a, marker_a=marker_ones, marker_b=marker_random) segment_a0 = TaborSegment.from_sampled(ch_a=ch_a, ch_b=ch_b, marker_a=marker_zeros, marker_b=marker_random) segment_anone = TaborSegment.from_sampled(ch_a=ch_a, ch_b=ch_b, marker_a=None, marker_b=marker_random) segment_none = TaborSegment.from_sampled(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) all_segments = [ segment_1, segment_2, segment_a0, segment_anone, segment_none ] for seg_a, seg_b in itertools.product(all_segments, all_segments): if seg_a == seg_b: self.assertEqual(hash(seg_a), hash(seg_b))
def exec_general(self, data_1, data_2, fill_value=None): tabor_segments = [ TaborSegment.from_sampled(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) 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 __init__(self, tabor_device: TaborAWGRepresentation, channels: Tuple[int, int], identifier: str): super().__init__(identifier) self._device = weakref.ref(tabor_device) self._configuration_guard_count = 0 self._is_in_config_mode = False if channels not in ((1, 2), (3, 4)): raise ValueError('Invalid channel pair: {}'.format(channels)) self._channels = channels self._idle_segment = TaborSegment.from_sampled(voltage_to_uint16(voltage=np.zeros(192), output_amplitude=0.5, output_offset=0., resolution=14), voltage_to_uint16(voltage=np.zeros(192), output_amplitude=0.5, output_offset=0., resolution=14), None, None) self._idle_sequence_table = [(1, 1, 0), (1, 1, 0), (1, 1, 0)] self._known_programs = dict() # type: Dict[str, TaborProgramMemory] self._current_program = None self._segment_lengths = None self._segment_capacity = None self._segment_hashes = None self._segment_references = None self._sequencer_tables = None self._advanced_sequence_table = None self._internal_paranoia_level = 0 self.clear()
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.from_sampled(ch_a=ch_a, ch_b=ch_b, marker_a=None, marker_b=None) np.testing.assert_equal(ts.data_a, ch_a) ts = TaborSegment.from_sampled(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.from_sampled(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.from_sampled(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)
def test_from_sampled(self): with self.assertRaisesRegex(TaborException, 'Empty'): TaborSegment.from_sampled(None, None, None, None) with self.assertRaisesRegex(TaborException, 'same length'): TaborSegment.from_sampled(np.zeros(16, dtype=np.uint16), np.zeros(32, dtype=np.uint16), None, None) ch_a = np.asarray(100 + np.arange(192), dtype=np.uint16) ch_b = np.asarray(1000 + np.arange(192), dtype=np.uint16) marker_a = np.ones(192 // 2, dtype=bool) marker_b = np.arange(192 // 2, dtype=np.uint16) self.assert_from_sampled_consistent(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b)
def assert_from_sampled_consistent(ch_a, ch_b, marker_a, marker_b): ts = TaborSegment.from_sampled(ch_a=ch_a, ch_b=ch_b, marker_a=marker_a, marker_b=marker_b) np.testing.assert_equal(ts.ch_a, ch_a) np.testing.assert_equal(ts.ch_b, ch_b) np.testing.assert_equal(ts.marker_a, marker_a != 0) np.testing.assert_equal(ts.marker_b, marker_b != 0) return ts
def test_upload_segment(self): channel_pair = TaborChannelPair(self.instrument, identifier='asd', channels=(1, 2)) self.reset_instrument_logs() channel_pair._segment_references = np.array([1, 2, 0, 1], dtype=np.uint32) channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32], dtype=np.uint32) channel_pair._segment_lengths = channel_pair._segment_capacity.copy() channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64) # prevent entering and exiting configuration mode channel_pair._configuration_guard_count = 2 segment = TaborSegment.from_sampled( np.ones(192 + 16, dtype=np.uint16), np.zeros(192 + 16, dtype=np.uint16), None, None) segment_binary = segment.get_as_binary() with self.assertRaises(ValueError): channel_pair._upload_segment(3, segment) with self.assertRaises(ValueError): channel_pair._upload_segment(0, segment) channel_pair._upload_segment(2, segment) np.testing.assert_equal( channel_pair._segment_capacity, 192 + np.array([0, 16, 32, 32], dtype=np.uint32)) np.testing.assert_equal( channel_pair._segment_lengths, 192 + np.array([0, 16, 16, 32], dtype=np.uint32)) np.testing.assert_equal( channel_pair._segment_hashes, np.array([1, 2, hash(segment), 4], dtype=np.int64)) expected_commands = [ ':INST:SEL 1', ':INST:SEL 1', ':INST:SEL 1', ':TRAC:DEF 3, 208', ':TRAC:SEL 3', ':TRAC:MODE COMB' ] expected_log = [ ((), dict(cmd_str=cmd, paranoia_level=channel_pair.internal_paranoia_level)) for cmd in expected_commands ] self.assertAllCommandLogsEqual(expected_log) self.assert_written_segment_data(segment_binary)
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.from_sampled(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 exec_general(self, data_1, data_2): tabor_segments = [ TaborSegment.from_sampled(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) self.assertEqual(len(result), expected_length) self.validate_result(tabor_segments, result) destination_array = np.empty(expected_length, dtype=np.uint16) result = make_combined_wave(tabor_segments, destination_array=destination_array) self.validate_result(tabor_segments, result) self.assertEqual(destination_array.data, result.data)
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.from_sampled(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 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.from_sampled(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.from_sampled(np.zeros(32, dtype=np.uint16), np.zeros(32, dtype=np.uint16), None, None).num_points, 32)
def test_amend_segments_iter(self): channel_pair = TaborChannelPair(self.instrument, identifier='asd', channels=(1, 2)) # prevent entering and exiting configuration mode channel_pair._configuration_guard_count = 2 self.instrument.paranoia_level = 0 self.reset_instrument_logs() channel_pair._segment_references = np.array([1, 2, 0, 1], dtype=np.uint32) channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32], dtype=np.uint32) channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16], dtype=np.uint32) channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64) data = np.ones(192, dtype=np.uint16) segments = [ TaborSegment.from_sampled(0 * data, 1 * data, None, None), TaborSegment.from_sampled(1 * data, 2 * data, None, None) ] indices = channel_pair._amend_segments(segments) expected_references = np.array([1, 2, 0, 1, 1, 1], dtype=np.uint32) expected_capacities = 192 + np.array([0, 16, 32, 32, 0, 0], dtype=np.uint32) expected_lengths = 192 + np.array([0, 0, 16, 16, 0, 0], dtype=np.uint32) expected_hashes = np.array( [1, 2, 3, 4, hash(segments[0]), hash(segments[1])], dtype=np.int64) np.testing.assert_equal(channel_pair._segment_references, expected_references) np.testing.assert_equal(channel_pair._segment_capacity, expected_capacities) np.testing.assert_equal(channel_pair._segment_lengths, expected_lengths) np.testing.assert_equal(channel_pair._segment_hashes, expected_hashes) np.testing.assert_equal(indices, np.array([4, 5], dtype=np.int64)) expected_commands = [ ':INST:SEL 1', ':TRAC:DEF 5,{}'.format(2 * 192 + 16), ':TRAC:SEL 5', ':TRAC:MODE COMB', ':TRAC:DEF 5,192', ':TRAC:DEF 6,192' ] expected_log = [ ((), dict(cmd_str=cmd, paranoia_level=channel_pair.internal_paranoia_level)) for cmd in expected_commands ] self.assertAllCommandLogsEqual(expected_log) # no segment lengths written self.assert_written_segment_lengths() expected_bin_blob = make_combined_wave(segments) self.assert_written_segment_data(expected_bin_blob)