def test_sequence_as_expression(self):
        def get_sympy(v):
            return v.sympified_expression

        t = sympy.Dummy('t')

        times = {
            t: 0.5,
            't0': 0.3,
            't1': 0.7,
            't2': 1.3,
        }

        entries = [TableEntry(0, 0, None), TableEntry(1, 0, 'hold')]
        self.assertEqual(
            ExpressionScalar(0),
            TableEntry._sequence_as_expression(
                entries, get_sympy, t, pre_value=None,
                post_value=None).sympified_expression.subs(times))

        entries = [TableEntry(0, 1, None), TableEntry(1, 1, 'hold')]
        self.assertEqual(
            ExpressionScalar(1),
            TableEntry._sequence_as_expression(
                entries, get_sympy, t, pre_value=None,
                post_value=None).sympified_expression.subs(times))

        entries = [TableEntry(0, 0, None), TableEntry(1, 1, 'linear')]
        self.assertEqual(
            ExpressionScalar(.5),
            TableEntry._sequence_as_expression(
                entries, get_sympy, t, pre_value=None,
                post_value=None).sympified_expression.subs(times))

        entries = [
            TableEntry('t0', 'a', 'linear'),
            TableEntry('t1', 'b', 'linear'),
            TableEntry('t2', 'c', 'hold')
        ]
        self.assertEqual(
            ExpressionScalar('(a+b)*.5'),
            TableEntry._sequence_as_expression(
                entries, get_sympy, t, pre_value=None,
                post_value=None).sympified_expression.subs(times))
예제 #2
0
    def _as_expression(self) -> Dict[ChannelID, ExpressionScalar]:
        t = self._AS_EXPRESSION_TIME
        shape = (len(self.defined_channels),)
        expressions = {}

        for i, channel in enumerate(self._channels):
            def value_trafo(v):
                try:
                    return v.underlying_expression[i]
                except TypeError:
                    return IndexedBroadcast(v.underlying_expression, shape, i)
            pre_value = value_trafo(self._entries[0].v)
            post_value = value_trafo(self._entries[-1].v)
            pw = TableEntry._sequence_as_expression(self._entries,
                                                    expression_extractor=value_trafo,
                                                    t=t,
                                                    post_value=post_value,
                                                    pre_value=pre_value)
            expressions[channel] = pw
        return expressions