Exemple #1
0
 def test_unsafe_get_subset_for_channels(self):
     interp = DummyInterpolationStrategy()
     entries = [TableWaveformEntry(0, 0, interp),
                TableWaveformEntry(2.1, -33.2, interp),
                TableWaveformEntry(5.7, 123.4, interp)]
     waveform = TableWaveform('A', entries)
     self.assertIs(waveform.unsafe_get_subset_for_channels({'A'}), waveform)
Exemple #2
0
    def test_unsafe_sample(self) -> None:
        interp = DummyInterpolationStrategy()
        entries = [
            TableWaveformEntry(0, 0, interp),
            TableWaveformEntry(2.1, -33.2, interp),
            TableWaveformEntry(5.7, 123.4, interp)
        ]
        waveform = TableWaveform('A', entries)
        sample_times = numpy.linspace(.5, 5.5, num=11)

        expected_interp_arguments = [
            ((0, 0), (2.1, -33.2), [0.5, 1.0, 1.5, 2.0]),
            ((2.1, -33.2), (5.7, 123.4), [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5])
        ]
        expected_result = numpy.copy(sample_times)

        result = waveform.unsafe_sample('A', sample_times)

        self.assertEqual(expected_interp_arguments, interp.call_arguments)
        numpy.testing.assert_equal(expected_result, result)

        output_expected = numpy.empty_like(expected_result)
        output_received = waveform.unsafe_sample('A',
                                                 sample_times,
                                                 output_array=output_expected)
        self.assertIs(output_expected, output_received)
        numpy.testing.assert_equal(expected_result, output_received)
Exemple #3
0
 def test_duration(self) -> None:
     entries = [
         TableWaveformEntry(0, 0, HoldInterpolationStrategy()),
         TableWaveformEntry(5, 1, HoldInterpolationStrategy())
     ]
     waveform = TableWaveform('A', entries)
     self.assertEqual(5, waveform.duration)
    def test_from_table(self):
        expected = ConstantWaveform(0.1, 0.2, 'A')

        for interp in (HoldInterpolationStrategy(), JumpInterpolationStrategy(), LinearInterpolationStrategy()):
            wf = TableWaveform.from_table('A',
                                          [TableWaveformEntry(0.0, 0.2, interp),
                                           TableWaveformEntry(0.1, 0.2, interp)])
            self.assertEqual(expected, wf)
Exemple #5
0
    def test_simple_properties(self):
        interp = DummyInterpolationStrategy()
        entries = [TableWaveformEntry(0, 0, interp),
                   TableWaveformEntry(2.1, -33.2, interp),
                   TableWaveformEntry(5.7, 123.4, interp)]
        chan = 'A'
        waveform = TableWaveform(chan, entries)

        self.assertEqual(waveform.defined_channels, {chan})
        self.assertIs(waveform.unsafe_get_subset_for_channels({'A'}), waveform)
Exemple #6
0
    def test_validate_input_duplicate_removal(self):
        validated = TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                                   TableWaveformEntry(0.1, 0.2, LinearInterpolationStrategy()),
                                                   TableWaveformEntry(0.1, 0.3, JumpInterpolationStrategy()),
                                                   TableWaveformEntry(0.1, 0.3, HoldInterpolationStrategy()),
                                                   TableWaveformEntry(0.2, 0.3, LinearInterpolationStrategy()),
                                                   TableWaveformEntry(0.3, 0.3, JumpInterpolationStrategy())])

        self.assertEqual(validated, (TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                     TableWaveformEntry(0.1, 0.2, LinearInterpolationStrategy()),
                                     TableWaveformEntry(0.1, 0.3, HoldInterpolationStrategy()),
                                     TableWaveformEntry(0.3, 0.3, JumpInterpolationStrategy())))
    def test_simple_properties(self):
        interp = DummyInterpolationStrategy()
        entries = [TableWaveformEntry(0, 0, interp),
                   TableWaveformEntry(2.1, -33.2, interp),
                   TableWaveformEntry(5.7, 123.4, interp)]
        chan = 'A'
        waveform = TableWaveform.from_table(chan, entries)

        self.assertEqual(waveform.defined_channels, {chan})
        self.assertIs(waveform.unsafe_get_subset_for_channels({'A'}), waveform)
        assert_constant_consistent(self, waveform)

        evaled = eval(repr(waveform))
        self.assertEqual(evaled, waveform)
    def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \
            -> Dict[ChannelID, List[TableWaveformEntry]]:
        """Compute an instantiated list of the table's entries.

        Args:
            parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects.
        Returns:
             (float, float)-list of all table entries with concrete values provided by the given
                parameters.
        """
        if not (self.table_parameters <= set(parameters.keys())):
            raise ParameterNotProvidedException(
                (self.table_parameters - set(parameters.keys())).pop())

        instantiated_entries = dict(
        )  # type: Dict[ChannelID,List[TableWaveformEntry]]

        for channel, channel_entries in self._entries.items():
            instantiated = [
                entry.instantiate(parameters) for entry in channel_entries
            ]

            # Add (0, v) entry if wf starts at finite time
            if instantiated[0].t > 0:
                instantiated.insert(
                    0,
                    TableWaveformEntry(
                        0, instantiated[0].v,
                        TablePulseTemplate.interpolation_strategies['hold']))
            instantiated_entries[channel] = instantiated

        duration = max(instantiated[-1].t
                       for instantiated in instantiated_entries.values())

        # ensure that all channels have equal duration
        for channel, instantiated in instantiated_entries.items():
            final_entry = instantiated[-1]
            if final_entry.t < duration:
                instantiated.append(
                    TableWaveformEntry(
                        duration, final_entry.v,
                        TablePulseTemplate.interpolation_strategies['hold']))
            instantiated_entries[channel] = instantiated
        return instantiated_entries
Exemple #9
0
    def test_validate_input_errors(self):
        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy())])

        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.0, 0.3, HoldInterpolationStrategy())])

        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.2, 0.2, HoldInterpolationStrategy())])

        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.2, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy())])
 def instantiate(self,
                 parameters: Dict[str, numbers.Real]) -> TableWaveformEntry:
     return TableWaveformEntry(
         self.t.evaluate_with_exact_rationals(parameters),
         self.v.evaluate_in_scope(parameters), self.interp)
 def instantiate(self,
                 parameters: Dict[str, numbers.Real]) -> TableWaveformEntry:
     return TableWaveformEntry(self.t.evaluate_numeric(**parameters),
                               self.v.evaluate_numeric(**parameters),
                               self.interp)
Exemple #12
0
 def test_interpolation_exception(self):
     with self.assertRaises(TypeError):
         TableWaveformEntry(1, 2, 3)
Exemple #13
0
 def test_few_entries(self) -> None:
     with self.assertRaises(ValueError):
         TableWaveform('A', [[]])
     with self.assertRaises(ValueError):
         TableWaveform(
             'A', [TableWaveformEntry(0, 0, HoldInterpolationStrategy())])
Exemple #14
0
 def test_repr(self):
     interpolation = DummyInterpolationStrategy()
     self.assertEqual(f"TableWaveformEntry(t={1.}, v={2.}, interp={interpolation})",
                      repr(TableWaveformEntry(t=1., v=2., interp=interpolation)))
Exemple #15
0
    def test_validate_input_errors(self):
        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy())])

        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.0, 0.3, HoldInterpolationStrategy())])

        with self.assertRaises(ValueError):
            TableWaveform._validate_input([TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.2, 0.2, HoldInterpolationStrategy())])

        with self.assertRaisesRegex(ValueError, "not increasing"):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.2, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy())])

        with self.assertRaisesRegex(ValueError, "Negative"):
            TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(-0.2, 0.2, HoldInterpolationStrategy()),
                                           TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy())])