def test_cleanup(self): channel_pair = TaborChannelPair(self.instrument, identifier='asd', channels=(1, 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) channel_pair.cleanup() np.testing.assert_equal(channel_pair._segment_references, np.array([1, 2, 0, 1], dtype=np.uint32)) 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, 0, 16, 16], dtype=np.uint32)) np.testing.assert_equal(channel_pair._segment_hashes, np.array([1, 2, 3, 4], dtype=np.int64)) channel_pair._segment_references = np.array([1, 2, 0, 1, 0], dtype=np.uint32) channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32, 32], dtype=np.uint32) channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16, 0], dtype=np.uint32) channel_pair._segment_hashes = np.array([1, 2, 3, 4, 5], dtype=np.int64) channel_pair.cleanup() np.testing.assert_equal(channel_pair._segment_references, np.array([1, 2, 0, 1], dtype=np.uint32)) 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, 0, 16, 16], dtype=np.uint32)) np.testing.assert_equal(channel_pair._segment_hashes, np.array([1, 2, 3, 4], dtype=np.int64))
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_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)
def test_find_place_for_segments_in_memory(self): def hash_based_on_dir(ch): hash_list = [] for d in dir(ch): o = getattr(ch, d) if isinstance(o, np.ndarray): hash_list.append(hash(o.tobytes())) else: try: hash_list.append(hash(o)) except TypeError: pass return hash(tuple(hash_list)) channel_pair = TaborChannelPair(self.instrument, identifier='asd', channels=(1, 2)) # empty segments = np.asarray([-5, -6, -7, -8, -9]) segment_lengths = 192 + np.asarray([32, 16, 64, 32, 16]) hash_before = hash_based_on_dir(channel_pair) w2s, ta, ti = channel_pair._find_place_for_segments_in_memory( segments, segment_lengths) self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(ta.tolist(), [True, True, True, True, True]) self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) # all new segments channel_pair._segment_capacity = 192 + np.asarray([0, 16, 32, 16, 0], dtype=np.uint32) channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5], dtype=np.int64) channel_pair._segment_references = np.asarray([1, 1, 1, 2, 1], dtype=np.int32) hash_before = hash_based_on_dir(channel_pair) w2s, ta, ti = channel_pair._find_place_for_segments_in_memory( segments, segment_lengths) self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(ta.tolist(), [True, True, True, True, True]) self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) # some known segments channel_pair._segment_capacity = 192 + np.asarray( [0, 16, 32, 64, 0, 16], dtype=np.uint32) channel_pair._segment_hashes = np.asarray([1, 2, 3, -7, 5, -9], dtype=np.int64) channel_pair._segment_references = np.asarray([1, 1, 1, 2, 1, 3], dtype=np.int32) hash_before = hash_based_on_dir(channel_pair) w2s, ta, ti = channel_pair._find_place_for_segments_in_memory( segments, segment_lengths) self.assertEqual(w2s.tolist(), [-1, -1, 3, -1, 5]) self.assertEqual(ta.tolist(), [True, True, False, True, False]) self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) # insert some segments with same length channel_pair._segment_capacity = 192 + np.asarray( [0, 16, 32, 64, 0, 16], dtype=np.uint32) channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5, 6], dtype=np.int64) channel_pair._segment_references = np.asarray([1, 0, 1, 0, 1, 3], dtype=np.int32) hash_before = hash_based_on_dir(channel_pair) w2s, ta, ti = channel_pair._find_place_for_segments_in_memory( segments, segment_lengths) self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(ta.tolist(), [True, False, False, True, True]) self.assertEqual(ti.tolist(), [-1, 1, 3, -1, -1]) self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) # insert some segments with smaller length channel_pair._segment_capacity = 192 + np.asarray( [0, 80, 32, 64, 96, 16], dtype=np.uint32) channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5, 6], dtype=np.int64) channel_pair._segment_references = np.asarray([1, 0, 1, 1, 0, 3], dtype=np.int32) hash_before = hash_based_on_dir(channel_pair) w2s, ta, ti = channel_pair._find_place_for_segments_in_memory( segments, segment_lengths) self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) self.assertEqual(ta.tolist(), [True, True, False, False, True]) self.assertEqual(ti.tolist(), [-1, -1, 4, 1, -1]) self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) # mix everything segments = np.asarray([-5, -6, -7, -8, -9, -10, -11]) segment_lengths = 192 + np.asarray([32, 16, 64, 32, 16, 0, 0]) channel_pair._segment_capacity = 192 + np.asarray( [0, 80, 32, 64, 32, 16], dtype=np.uint32) channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, -8, 6], dtype=np.int64) channel_pair._segment_references = np.asarray([1, 0, 1, 0, 1, 0], dtype=np.int32) hash_before = hash_based_on_dir(channel_pair) w2s, ta, ti = channel_pair._find_place_for_segments_in_memory( segments, segment_lengths) self.assertEqual(w2s.tolist(), [-1, -1, -1, 4, -1, -1, -1]) self.assertEqual(ta.tolist(), [False, True, False, False, True, True, True]) self.assertEqual(ti.tolist(), [1, -1, 3, -1, -1, -1, -1]) self.assertEqual(hash_before, hash_based_on_dir(channel_pair))