def _get_transformation(
            self, parameters: Mapping[str, Real],
            channel_mapping: Mapping[ChannelID, ChannelID]) -> Transformation:
        transformation = IdentityTransformation()

        scalar_value = self._get_scalar_value(parameters=parameters,
                                              channel_mapping=channel_mapping)

        if self._pulse_template is self._rhs:
            if self._arithmetic_operator == '-':
                # negate the pulse template
                transformation = transformation.chain(
                    ScalingTransformation({
                        channel_mapping[ch]: -1
                        for ch in self.defined_channels if channel_mapping[ch]
                    }))

        else:
            if self._arithmetic_operator == '-':
                for channel, value in scalar_value.items():
                    scalar_value[channel] = -value

            elif self._arithmetic_operator == '/':
                for channel, value in scalar_value.items():
                    scalar_value[channel] = 1 / value

        if self._arithmetic_operator in ('+', '-'):
            return transformation.chain(OffsetTransformation(scalar_value))

        else:
            return transformation.chain(ScalingTransformation(scalar_value))
Ejemplo n.º 2
0
 def test_constant_propagation(self):
     trafo = ChainedTransformation(ScalingTransformation({'a': 1.1}),
                                   OffsetTransformation({'b': 6.6}))
     self.assertTrue(trafo.is_constant_invariant())
     trafo = ChainedTransformation(ScalingTransformation({'a': 1.1}),
                                   TransformationStub())
     self.assertFalse(trafo.is_constant_invariant())
Ejemplo n.º 3
0
    def test_trafo(self):
        trafo = ScalingTransformation(self.scales)

        time = np.asarray([.5, .6])
        in_data = {'A': np.asarray([.1, .2]), 'C': np.asarray([3., 4.])}
        expected = {'A': np.asarray([.1 * 1.5, .2 * 1.5]), 'C': in_data['C']}

        out_data = trafo(time, in_data)

        self.assertIs(expected['C'], out_data['C'])
        np.testing.assert_equal(expected, out_data)
Ejemplo n.º 4
0
    def test_get_transformation(self):
        pulse_template = DummyPulseTemplate(defined_channels={'u', 'v', 'w'})
        scalar = dict(a=1., b=2.)
        neg_scalar = dict(a=-1., b=-2.)
        inv_scalar = dict(a=1., b=1/2.)
        neg_trafo = ScalingTransformation(dict(a=-1, b=-1))

        parameters = dict(x=3, y=5, z=8)
        channel_mapping = dict(u='a', v='b', w=None)

        # (PT + scalar)
        arith = ArithmeticPulseTemplate(pulse_template, '+', 'd')
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = OffsetTransformation(scalar)
            self.assertEqual(expected_trafo, trafo)

        # (scalar + PT)
        arith = ArithmeticPulseTemplate('d', '+', pulse_template)
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = OffsetTransformation(scalar)
            self.assertEqual(expected_trafo, trafo)

        # (PT - scalar)
        arith = ArithmeticPulseTemplate(pulse_template, '-', 'd')
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = OffsetTransformation(neg_scalar)
            self.assertEqual(expected_trafo, trafo)

        # (scalar - PT)
        arith = ArithmeticPulseTemplate('d', '-', pulse_template)
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = neg_trafo.chain(OffsetTransformation(scalar))
            self.assertEqual(expected_trafo, trafo)

        # (PT * scalar)
        arith = ArithmeticPulseTemplate(pulse_template, '*', 'd')
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = ScalingTransformation(scalar)
            self.assertEqual(expected_trafo, trafo)

        # (scalar * PT)
        arith = ArithmeticPulseTemplate('d', '*', pulse_template)
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = ScalingTransformation(scalar)
            self.assertEqual(expected_trafo, trafo)

        # (PT / scalar)
        arith = ArithmeticPulseTemplate(pulse_template, '/', 'd')
        with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value:
            trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping)
            get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)

            expected_trafo = ScalingTransformation(inv_scalar)
            self.assertEqual(expected_trafo, trafo)
Ejemplo n.º 5
0
 def test_constant_propagation(self):
     trafo = ScalingTransformation(self.scales)
     self.assertTrue(trafo.is_constant_invariant())
Ejemplo n.º 6
0
 def test_scalar_trafo_works(self):
     trafo = ScalingTransformation(self.scales)
     assert_scalar_trafo_works(self, trafo, {'A': 0., 'B': 0.3, 'c': 0.6})
Ejemplo n.º 7
0
 def test_get_input_channels(self):
     trafo = ScalingTransformation(self.scales)
     channels = {'A', 'C'}
     self.assertIs(channels, trafo.get_input_channels(channels))
     self.assertIs(channels, trafo.get_output_channels(channels))
Ejemplo n.º 8
0
 def test_init(self):
     trafo = ScalingTransformation(self.scales)
     # test copy
     self.assertIsNot(trafo._factors, self.scales)
     self.assertEqual(trafo._factors, self.scales)
Ejemplo n.º 9
0
 def test_repr(self):
     trafo = ChainedTransformation(ScalingTransformation({'a': 1.1}),
                                   OffsetTransformation({'b': 6.6}))
     self.assertEqual(trafo, eval(repr(trafo)))