def test_const_propagation(self): lhs = MultiChannelWaveform([ DummyWaveform(duration=1.5, defined_channels={'a', 'c', 'd', 'i'}), ConstantWaveform.from_mapping(1.5, {'e': 1.2, 'f': 1.3, 'h': 4.6}) ]) rhs = MultiChannelWaveform([ DummyWaveform(duration=1.5, defined_channels={'a', 'b', 'e'}), ConstantWaveform.from_mapping(1.5, {'f': 2.5, 'g': 3.5, 'i': 6.4}) ]) wf = ArithmeticWaveform(lhs, '-', rhs) assert_constant_consistent(self, wf) expected = {'a': None, 'b': None, 'c': None, 'd': None, 'e': None, 'f': 1.3-2.5, 'g': -3.5, 'h': 4.6, 'i': None} actual = {ch: wf.constant_value(ch) for ch in wf.defined_channels} self.assertEqual(expected, actual)
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 test_from_parallel(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'}) self.assertIs(dwf_a, MultiChannelWaveform.from_parallel([dwf_a])) wf_ab = MultiChannelWaveform.from_parallel([dwf_a, dwf_b]) self.assertEqual(wf_ab, MultiChannelWaveform([dwf_a, dwf_b])) wf_abc = MultiChannelWaveform.from_parallel([wf_ab, dwf_c]) self.assertEqual(wf_abc, MultiChannelWaveform([dwf_a, dwf_b, dwf_c]))
def build_waveform( self, parameters: Dict[str, numbers.Real], channel_mapping: Dict[ChannelID, Optional[ChannelID]] ) -> Optional[Waveform]: self.validate_parameter_constraints(parameters=parameters) sub_waveforms = [] for subtemplate in self.subtemplates: sub_waveform = subtemplate.build_waveform( parameters, channel_mapping=channel_mapping) if sub_waveform is not None: sub_waveforms.append(sub_waveform) if len(sub_waveforms) == 0: return None if len(sub_waveforms) == 1: waveform = sub_waveforms[0] else: waveform = MultiChannelWaveform(sub_waveforms) if self._duration: expected_duration = self._duration.evaluate_numeric(**parameters) if not isclose(expected_duration, waveform.duration): raise ValueError( 'The duration does not ' 'equal the expected duration', expected_duration, waveform.duration) return waveform
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_from_mapping(self): from_single = ConstantWaveform.from_mapping(1., {'A': 2.}) expected_single = ConstantWaveform(duration=1., amplitude=2., channel='A') self.assertEqual(expected_single, from_single) from_multi = ConstantWaveform.from_mapping(1., {'A': 2., 'B': 3.}) expected_from_multi = MultiChannelWaveform([ConstantWaveform(duration=1., amplitude=2., channel='A'), ConstantWaveform(duration=1., amplitude=3., channel='B')]) self.assertEqual(expected_from_multi, from_multi)
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(TimeType.from_float(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 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(TimeType.from_float(1.3), waveform.duration)
def test_init_no_args(self) -> None: with self.assertRaises(ValueError): MultiChannelWaveform(dict()) with self.assertRaises(ValueError): MultiChannelWaveform(None)
def test_const_value(self): mixed_wf = MultiChannelWaveform([DummyWaveform(1.5, defined_channels={'A'}), ConstantWaveform(1.5, 1.1, 'B')]) wf = FunctorWaveform(mixed_wf, {'A': np.negative, 'B': np.negative}) self.assertIsNone(wf.constant_value('A')) self.assertEqual(-1.1, wf.constant_value('B'))