def test_unsafe_sample(self) -> None: sample_times = numpy.linspace(98.5, 103.5, num=11) samples_a = numpy.linspace(4, 5, 11) samples_b = numpy.linspace(2, 3, 11) dwf_a = DummyWaveform(duration=3.2, sample_output=samples_a, defined_channels={'A'}) dwf_b = DummyWaveform(duration=3.2, sample_output=samples_b, defined_channels={'B', 'C'}) waveform = MultiChannelWaveform((dwf_a, dwf_b)) result_a = waveform.unsafe_sample('A', sample_times) numpy.testing.assert_equal(result_a, samples_a) result_b = waveform.unsafe_sample('B', sample_times) numpy.testing.assert_equal(result_b, samples_b) self.assertEqual(len(dwf_a.sample_calls), 1) self.assertEqual(len(dwf_b.sample_calls), 1) numpy.testing.assert_equal(sample_times, dwf_a.sample_calls[0][1]) numpy.testing.assert_equal(sample_times, dwf_b.sample_calls[0][1]) self.assertEqual('A', dwf_a.sample_calls[0][0]) self.assertEqual('B', dwf_b.sample_calls[0][0]) self.assertIs(dwf_a.sample_calls[0][2], None) self.assertIs(dwf_b.sample_calls[0][2], None) reuse_output = numpy.empty_like(samples_a) result_a = waveform.unsafe_sample('A', sample_times, reuse_output) self.assertEqual(len(dwf_a.sample_calls), 2) self.assertIs(result_a, reuse_output) self.assertIs(result_a, dwf_a.sample_calls[1][2]) numpy.testing.assert_equal(result_b, samples_b)
def test_equality(self) -> None: dwf_a = DummyWaveform(duration=246.2, num_channels=2) waveform_a1 = MultiChannelWaveform([(dwf_a, [0, 1])]) waveform_a2 = MultiChannelWaveform([(dwf_a, [0, 1])]) waveform_a3 = MultiChannelWaveform([(dwf_a, [1, 0])]) self.assertEqual(waveform_a1, waveform_a1) self.assertEqual(waveform_a1, waveform_a2) self.assertNotEqual(waveform_a1, waveform_a3)
def get_two_chan_test_block(wfg=WaveformGenerator(2)): generate_waveform = wfg.generate_single_channel_waveform generate_multi_channel_waveform = wfg.generate_multi_channel_waveform loop_block11 = InstructionBlock() loop_block11.add_instruction_exec(generate_multi_channel_waveform()) loop_block1 = InstructionBlock() loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(loop_block11)) loop_block21 = InstructionBlock() loop_block21.add_instruction_exec(generate_multi_channel_waveform()) loop_block21.add_instruction_exec(generate_multi_channel_waveform()) loop_block2 = InstructionBlock() loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(loop_block21)) loop_block2.add_instruction_exec(generate_multi_channel_waveform()) loop_block3 = InstructionBlock() loop_block3.add_instruction_exec(generate_multi_channel_waveform()) loop_block3.add_instruction_exec(generate_multi_channel_waveform()) loop_block411 = InstructionBlock() loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) loop_block412 = InstructionBlock() loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) loop_block41 = InstructionBlock() loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(loop_block411)) loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(loop_block412)) loop_block421 = InstructionBlock() loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) loop_block422 = InstructionBlock() loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) loop_block42 = InstructionBlock() loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(loop_block421)) loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(loop_block422)) chan_block4A = InstructionBlock() chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(loop_block41)) chan_block4B = InstructionBlock() chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(loop_block42)) loop_block4 = InstructionBlock() loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(chan_block4A), frozenset('B'): ImmutableInstructionBlock(chan_block4B)}) root_block = InstructionBlock() root_block.add_instruction_exec(generate_multi_channel_waveform()) root_block.add_instruction_repj(10, ImmutableInstructionBlock(loop_block1)) root_block.add_instruction_repj(17, ImmutableInstructionBlock(loop_block2)) root_block.add_instruction_repj(3, ImmutableInstructionBlock(loop_block3)) root_block.add_instruction_repj(4, ImmutableInstructionBlock(loop_block4)) return root_block
def test_init_single_channel(self) -> None: dwf = DummyWaveform(duration=1.3) with self.assertRaises(ValueError): MultiChannelWaveform([(dwf, [1])]) with self.assertRaises(ValueError): MultiChannelWaveform([(dwf, [-1])]) waveform = MultiChannelWaveform([(dwf, [0])]) self.assertEqual(1, waveform.num_channels) self.assertEqual(1.3, waveform.duration)
def test_equality(self) -> None: dwf_a = DummyWaveform(duration=246.2, defined_channels={'A'}) dwf_b = DummyWaveform(duration=246.2, defined_channels={'B'}) dwf_c = DummyWaveform(duration=246.2, defined_channels={'C'}) waveform_a1 = MultiChannelWaveform([dwf_a, dwf_b]) waveform_a2 = MultiChannelWaveform([dwf_a, dwf_b]) waveform_a3 = MultiChannelWaveform([dwf_a, dwf_c]) self.assertEqual(waveform_a1, waveform_a1) self.assertEqual(waveform_a1, waveform_a2) self.assertNotEqual(waveform_a1, waveform_a3)
def get_marker_data(waveform: MultiChannelWaveform, time): marker_data = np.zeros(len(time), dtype=np.uint16) for marker_index, markerID in enumerate(self._markers): if markerID is not None: marker_data |= (waveform.get_sampled(channel=markerID, sample_times=time) != 0).\ astype(dtype=np.uint16) << marker_index+14 return marker_data
def test_sample(self) -> None: sample_times = numpy.linspace(98.5, 103.5, num=11) samples_a = numpy.array([ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5] # [0, 0.5], [1, 0.6], [2, 0.7], [3, 0.8], [4, 0.9], [5, 1.0], # [6, 1.1], [7, 1.2], [8, 1.3], [9, 1.4], [10, 1.5] ]) samples_b = numpy.array([ [-10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20] # [-10], [-11], [-12], [-13], [-14], [-15], [-16], [-17], [-18], [-19], [-20] ]) dwf_a = DummyWaveform(duration=3.2, sample_output=samples_a, num_channels=2) dwf_b = DummyWaveform(duration=3.2, sample_output=samples_b, num_channels=1) waveform = MultiChannelWaveform([(dwf_a, [2, 0]), (dwf_b, [1])]) self.assertEqual(3, waveform.num_channels) self.assertEqual(3.2, waveform.duration) result = waveform.sample(sample_times, 0.7) self.assertEqual([(list(sample_times), 0.7)], dwf_a.sample_calls) self.assertEqual([(list(sample_times), 0.7)], dwf_b.sample_calls) # expected = numpy.array([ # [0.5, -10, 0], # [0.6, -11, 1], # [0.7, -12, 2], # [0.8, -13, 3], # [0.9, -14, 4], # [1.0, -15, 5], # [1.1, -16, 6], # [1.2, -17, 7], # [1.3, -18, 8], # [1.4, -19, 9], # [1.5, -20, 10], # ]) expected = numpy.array( [[0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5], [-10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]) self.assertTrue(numpy.all(expected == result))
def test_init_several_channels(self) -> None: dwf_a = DummyWaveform(duration=2.2, defined_channels={'A'}) dwf_b = DummyWaveform(duration=2.2, defined_channels={'B'}) dwf_c = DummyWaveform(duration=2.3, defined_channels={'C'}) waveform = MultiChannelWaveform([dwf_a, dwf_b]) self.assertEqual({'A', 'B'}, waveform.defined_channels) self.assertEqual(2.2, waveform.duration) with self.assertRaises(ValueError): MultiChannelWaveform([dwf_a, dwf_c]) with self.assertRaises(ValueError): MultiChannelWaveform([waveform, dwf_c]) with self.assertRaises(ValueError): MultiChannelWaveform((dwf_a, dwf_a)) dwf_c_valid = DummyWaveform(duration=2.2, defined_channels={'C'}) waveform_flat = MultiChannelWaveform((waveform, dwf_c_valid)) self.assertEqual(len(waveform_flat.compare_key), 3)
def test_get_item(self): dwf_a = DummyWaveform(duration=2.2, defined_channels={'A'}) dwf_b = DummyWaveform(duration=2.2, defined_channels={'B'}) dwf_c = DummyWaveform(duration=2.2, defined_channels={'C'}) wf = MultiChannelWaveform([dwf_a, dwf_b, dwf_c]) self.assertIs(wf['A'], dwf_a) self.assertIs(wf['B'], dwf_b) self.assertIs(wf['C'], dwf_c) with self.assertRaises(KeyError): wf['D']
def test_unsafe_get_subset_for_channels(self): dwf_a = DummyWaveform(duration=246.2, defined_channels={'A'}) dwf_b = DummyWaveform(duration=246.2, defined_channels={'B'}) dwf_c = DummyWaveform(duration=246.2, defined_channels={'C'}) mcwf = MultiChannelWaveform((dwf_a, dwf_b, dwf_c)) with self.assertRaises(KeyError): mcwf.unsafe_get_subset_for_channels({'D'}) with self.assertRaises(KeyError): mcwf.unsafe_get_subset_for_channels({'A', 'D'}) self.assertIs(mcwf.unsafe_get_subset_for_channels({'A'}), dwf_a) self.assertIs(mcwf.unsafe_get_subset_for_channels({'B'}), dwf_b) self.assertIs(mcwf.unsafe_get_subset_for_channels({'C'}), dwf_c) sub_ab = mcwf.unsafe_get_subset_for_channels({'A', 'B'}) self.assertEqual(sub_ab.defined_channels, {'A', 'B'}) self.assertIsInstance(sub_ab, MultiChannelWaveform) self.assertIs(sub_ab.unsafe_get_subset_for_channels({'A'}), dwf_a) self.assertIs(sub_ab.unsafe_get_subset_for_channels({'B'}), dwf_b)
def build_waveform( self, parameters: Dict[str, Real], channel_mapping: Dict[ChannelID, Optional[ChannelID]] ) -> Optional[Waveform]: self.validate_parameter_constraints(parameters) if all(channel_mapping[channel] is None for channel in self.defined_channels): return None if self.duration.evaluate_numeric(**parameters) == 0: return None mapped_channels = tuple(channel_mapping[c] for c in self._channels) waveform_entries = list([] for _ in range(len(self._channels))) for entry in self._entries: instantiated_entries = entry.instantiate(parameters, len(self._channels)) for ch_entries, wf_entry in zip(waveform_entries, instantiated_entries): ch_entries.append(wf_entry) if waveform_entries[0][0].t > 0: for ch_entries in waveform_entries: ch_entries[:0] = [ PointWaveformEntry(0, ch_entries[0].v, ch_entries[0].interp) ] # filter mappings to None channel_entries = [ (ch, ch_entries) for (ch, ch_entries) in zip(mapped_channels, waveform_entries) if ch is not None ] mapped_channels, waveform_entries = zip(*channel_entries) waveforms = [ PointWaveform(mapped_channel, ch_entries) for mapped_channel, ch_entries in zip( mapped_channels, waveform_entries) ] if len(waveforms) == 1: return waveforms.pop() else: return MultiChannelWaveform(waveforms)
def test_build_sequence(self) -> None: measurement_windows = [('M', 0, 5)] single_wf = DummyWaveform(duration=6, defined_channels={'A'}) wf = MultiChannelWaveform([single_wf]) sequencer = DummySequencer() block = DummyInstructionBlock() template = AtomicPulseTemplateStub(waveform=wf, measurements=measurement_windows) template.build_sequence(sequencer, {}, {}, measurement_mapping={'M': 'N'}, channel_mapping={}, instruction_block=block) self.assertEqual(len(block.instructions), 2) meas, exec = block.instructions self.assertIsInstance(meas, MEASInstruction) self.assertEqual(meas.measurements, [('N', 0, 5)]) self.assertIsInstance(exec, EXECInstruction) self.assertEqual(exec.waveform.defined_channels, {'A'})
def test_init_several_channels(self) -> None: dwfa = DummyWaveform(duration=4.2, num_channels=2) dwfb = DummyWaveform(duration=4.2, num_channels=3) dwfc = DummyWaveform(duration=2.3) with self.assertRaises(ValueError): MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, 5, 1])]) with self.assertRaises(ValueError): MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, -1, 1])]) with self.assertRaises(ValueError): MultiChannelWaveform([(dwfa, [0, 1]), (dwfc, [2])]) with self.assertRaises(ValueError): MultiChannelWaveform([(dwfa, [0, 0]), (dwfb, [3, 4, 1])]) with self.assertRaises(ValueError): MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, 4, 1])]) waveform = MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, 0, 1])]) self.assertEqual(5, waveform.num_channels) self.assertEqual(4.2, waveform.duration)
def test_build_sequence(self) -> None: dummy_wf1 = DummyWaveform(duration=2.3, num_channels=2) dummy_wf2 = DummyWaveform(duration=2.3, num_channels=1) dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, waveform=dummy_wf1) dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1, waveform=dummy_wf2) pulse = MultiChannelPulseTemplate([(dummy1, { 'foo': '2*bar' }, [2, 1]), (dummy2, {}, [0])], {'bar'}) result = pulse.build_waveform({'bar': ConstantParameter(3)}) expected = MultiChannelWaveform([(dummy_wf1, [2, 1]), (dummy_wf2, [0])]) self.assertEqual(expected, result) self.assertEqual([{ 'foo': MappedParameter(Expression("2*bar"), {'bar': ConstantParameter(3)}) }], dummy1.build_waveform_calls) self.assertEqual([{}], dummy2.build_waveform_calls)
def build_waveform(self, parameters: Dict[str, numbers.Real], channel_mapping: Dict[ChannelID, Optional[ChannelID]]) -> Optional['Waveform']: self.validate_parameter_constraints(parameters) if all(channel_mapping[channel] is None for channel in self.defined_channels): return None instantiated = [(channel_mapping[channel], instantiated_channel) for channel, instantiated_channel in self.get_entries_instantiated(parameters).items() if channel_mapping[channel] is not None] if self.duration.evaluate_numeric(**parameters) == 0: return None waveforms = [TableWaveform(*ch_instantiated) for ch_instantiated in instantiated] if len(waveforms) == 1: return waveforms.pop() else: return MultiChannelWaveform(waveforms)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) wf = DummyWaveform() self.descriptionA = \ """\ LOOP 1 times: ->EXEC {} 1 times ->EXEC {} 50 times ->LOOP 17 times: ->LOOP 2 times: ->EXEC {} 1 times ->EXEC {} 1 times ->EXEC {} 1 times ->LOOP 3 times: ->EXEC {} 1 times ->EXEC {} 1 times ->LOOP 24 times: ->EXEC {} 7 times ->EXEC {} 8 times""" self.descriptionB = \ """\ LOOP 1 times: ->EXEC {} 1 times ->EXEC {} 50 times ->LOOP 17 times: ->LOOP 2 times: ->EXEC {} 1 times ->EXEC {} 1 times ->EXEC {} 1 times ->LOOP 3 times: ->EXEC {} 1 times ->EXEC {} 1 times ->LOOP 36 times: ->EXEC {} 10 times ->EXEC {} 11 times""" def generate_waveform(channel): return DummyWaveform(sample_output=None, duration=None, defined_channels={channel}) def generate_multi_channel_waveform(): return MultiChannelWaveform([generate_waveform('A'), generate_waveform('B')]) self.loop_block11 = InstructionBlock() self.loop_block11.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block1 = InstructionBlock() self.loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(self.loop_block11)) self.loop_block21 = InstructionBlock() self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block2 = InstructionBlock() self.loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(self.loop_block21)) self.loop_block2.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block3 = InstructionBlock() self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block411 = InstructionBlock() self.loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) self.loop_block412 = InstructionBlock() self.loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) self.loop_block41 = InstructionBlock() self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411)) self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412)) self.loop_block421 = InstructionBlock() self.loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) self.loop_block422 = InstructionBlock() self.loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) self.loop_block42 = InstructionBlock() self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421)) self.loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(self.loop_block422)) self.chan_block4A = InstructionBlock() self.chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(self.loop_block41)) self.chan_block4B = InstructionBlock() self.chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(self.loop_block42)) self.loop_block4 = InstructionBlock() self.loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(self.chan_block4A), frozenset('B'): ImmutableInstructionBlock(self.chan_block4B)}) self.root_block = InstructionBlock() self.root_block.add_instruction_exec(generate_multi_channel_waveform()) self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1)) self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2)) self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3)) self.root_block.add_instruction_repj(4, ImmutableInstructionBlock(self.loop_block4)) self.maxDiff = None
def test_init_single_channel(self) -> None: dwf = DummyWaveform(duration=1.3, defined_channels={'A'}) waveform = MultiChannelWaveform([dwf]) self.assertEqual({'A'}, waveform.defined_channels) self.assertEqual(1.3, waveform.duration)
def test_init_no_args(self) -> None: with self.assertRaises(ValueError): MultiChannelWaveform([]) with self.assertRaises(ValueError): MultiChannelWaveform(None)
def generate_multi_channel_waveform(): return MultiChannelWaveform([generate_waveform('A'), generate_waveform('B')])
def generate_multi_channel_waveform(self): return MultiChannelWaveform([self.generate_single_channel_waveform(self.channel_names[ch_i]) for ch_i in range(self.num_channels)])