Пример #1
0
    def testDependency(self):
        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 1, 10, 11, 13)

        engine = Engine(keep_history=True)
        data1 = DataSource(engine, [[t1, 1], [t2, 2]], 'data1')
        data2 = DataSource(engine, [[t1, 3], [t2, 4]], 'data2')

        class Transform(Flow):
            input1 = Input()
            input2 = Input()

            output1 = Output()
            output2 = Output()

            def __init__(self, input1, input2, name):
                super().__init__(name)

            @when(input1)
            def handle(self):
                self.output1 = self.input1()

            @when(input2)
            def handle(self):
                self.output2 = self.input2()

        tx1 = Transform(data1, data2, 'transform1')
        tx2 = Transform(tx1.output1, tx1.output2, 'transform2')

        # engine.show_graph('dep', show_edge_label=True)
        engine.start(t1, t2)

        self.assertEqual(tx2.get_output_value()[tx2.output1], [(t1, 1), (t2, 2)])
        self.assertEqual(tx2.get_output_value()[tx2.output2], [(t1, 3), (t2, 4)])
Пример #2
0
    def testToDict(self):
        t0 = datetime(2016, 1, 1, 1, 1, 0)
        t1 = datetime(2016, 1, 1, 1, 1, 1)
        t2 = datetime(2016, 1, 1, 1, 1, 2)
        t3 = datetime(2016, 1, 1, 1, 1, 3)
        t4 = datetime(2016, 1, 1, 1, 1, 4)
        t5 = datetime(2016, 1, 1, 1, 1, 5)

        engine = Engine(keep_history=True)

        d1 = DataSource(engine, [
            (t0, 0.5),
            (t1, 1),
            (t2, 2),
            (t4, 5)
        ])

        d2 = DataSource(engine, [
            (t1, 3),
            (t3, 4),
            (t4, 6)
        ])

        d3 = DataSource(engine, [
            (t1, 9),
            (t3, 10),
            (t5, 11),
        ])

        out = flow_to_dict({'d1': d1, 'd2': d2, 'd3': d3})
        engine.start(t0, t5)
        o = out()

        t, v = o[0]
        self.assertEqual(t, t0)
        self.assertEqual(dict(d1=0.5), v)

        t, v = o[1]
        self.assertEqual(t, t1)
        self.assertEqual(dict(d1=1, d2=3, d3=9), v)

        t, v = o[2]
        self.assertEqual(t, t2)
        self.assertEqual(dict(d1=2, d2=3, d3=9), v)

        t, v = o[3]
        self.assertEqual(t, t3)
        self.assertEqual(dict(d1=2, d2=4, d3=10), v)

        t, v = o[4]
        self.assertEqual(t, t4)
        self.assertEqual(dict(d1=5, d2=6, d3=10), v)

        t, v = o[5]
        self.assertEqual(t, t5)
        self.assertEqual(dict(d1=5, d2=6, d3=11), v)
Пример #3
0
    def testSnap(self):
        engine = Engine(keep_history=True)
        snap_time = datetime(2020, 11, 8, 4, 45, 30)
        values = DataSource(engine, [
            (datetime(2020, 11, 8, 4, 45), 1),
            (datetime(2020, 11, 8, 4, 46), 2),
            (datetime(2020, 11, 8, 4, 47), 3),
        ])
        s = values.snap(snap_time)
        engine.start(datetime(2020, 11, 8, 4, 44), datetime(2020, 11, 8, 4, 47))
        self.assertEqual(s(), [(datetime(2020, 11, 8, 4, 45, 30), 1)])

        engine = Engine(keep_history=True)
        snap_time = datetime(2020, 11, 8, 4, 46)
        values = DataSource(engine, [
            (datetime(2020, 11, 8, 4, 45), 1),
            (datetime(2020, 11, 8, 4, 46), 2),
            (datetime(2020, 11, 8, 4, 47), 3),
        ])
        s = values.snap(snap_time)
        engine.start(datetime(2020, 11, 8, 4, 44), datetime(2020, 11, 8, 4, 48))
        self.assertEqual(s(), [(datetime(2020, 11, 8, 4, 46), 2)])

        engine = Engine(keep_history=True)
        snap_time = datetime(2020, 11, 8, 4, 43)
        values = DataSource(engine, [
            (datetime(2020, 11, 8, 4, 45), 1),
            (datetime(2020, 11, 8, 4, 46), 2),
            (datetime(2020, 11, 8, 4, 47), 3),
        ])
        s = values.snap(snap_time)
        engine.start(datetime(2020, 11, 8, 4, 41), datetime(2020, 11, 8, 4, 48))
        # print(s())
        self.assertEqual(s(), None)
Пример #4
0
    def testFlatten3(self):
        t0 = datetime(2016, 1, 1, 1, 1, 0)
        t1 = datetime(2016, 1, 1, 1, 1, 1)
        t2 = datetime(2016, 1, 1, 1, 1, 2)
        t3 = datetime(2016, 1, 1, 1, 1, 3)
        t4 = datetime(2016, 1, 1, 1, 1, 4)
        t5 = datetime(2016, 1, 1, 1, 1, 5)

        engine = Engine(keep_history=True)

        d1 = DataSource(engine, [
            (t0, 0.5),
            (t1, 1),
            (t2, 2),
            (t4, 5)
        ])

        d2 = DataSource(engine, [
            (t1, 3),
            (t3, 4),
            (t4, 6)
        ])

        d3 = DataSource(engine, [
            (t1, 9),
            (t3, 10),
            (t5, 11),
        ])

        out = flatten([d1, d2, d3], is_fill_empty=True)
        engine.start(t0, t5)
        o = out()
        print(o)

        t, v = o[1]
        self.assertEqual(t, t1)
        self.assertEqual([1, 3, 9], v)

        t, v = o[2]
        self.assertEqual(t, t2)
        self.assertEqual([2, None, None], v)

        t, v = o[3]
        self.assertEqual(t, t3)
        self.assertEqual([None, 4, 10], v)

        t, v = o[4]
        self.assertEqual(t, t4)
        self.assertEqual([5, 6, None], v)

        t, v = o[5]
        self.assertEqual(t, t5)
        self.assertEqual([None, None, 11], v)
Пример #5
0
    def testShift(self):
        values = [(datetime(2016, 1, 1, 1, 1, i), i) for i in range(1, 10)]

        engine = Engine(keep_history=True)

        d = DataSource(engine, values)
        s = d.shift(2)

        engine.start(values[0][0], values[-1][0])

        result = s()
        for t, v in result:
            self.assertEqual(t.second, v + 2)
Пример #6
0
    def testLifting(self):
        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 1, 10, 11, 13)

        engine = Engine(keep_history=True)

        @lift()
        def f(p):
            return True if p > 1 else False

        data = DataSource(engine, [
            (t1, 1),
            (t2, 2),
        ])

        z = f(data)

        # engine.show_graph('test')
        engine.start(t1, t2)

        self.assertEqual(len(z()), 2)

        (ts0, v0) = z()[0]
        (ts1, v1) = z()[1]

        self.assertEqual(ts0, t1)
        self.assertEqual(v0, False)

        self.assertEqual(ts1, t2)
        self.assertEqual(v1, True)
Пример #7
0
    def testSanity(self):
        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 1, 10, 11, 13)

        engine = Engine(keep_history=True)
        input_series = [(t1, 1), (t2, 2)]

        data = DataSource(engine, input_series)

        # class Transform(Flow):
        #     input = Input()
        #
        #     def __init__(self, input, name):
        #         super().__init__(name)
        #
        #     @when(input)
        #     def handle(self):
        #         self << self.input() + 1

        @lift()
        def add(i):
            return i + 1

        tx1 = add(data)
        tx2 = add(tx1)

        engine.start(t1, t2)

        self.assertEqual(tx1(), [(datetime(2016, 8, 1, 10, 11, 12), 2), (datetime(2016, 8, 1, 10, 11, 13), 3)])
        self.assertEqual(tx2(), [(datetime(2016, 8, 1, 10, 11, 12), 3), (datetime(2016, 8, 1, 10, 11, 13), 4)])
Пример #8
0
    def testFlatten1(self):
        t1 = datetime(2016, 1, 1, 1, 1, 1)
        t2 = datetime(2016, 1, 1, 1, 1, 2)
        t4 = datetime(2016, 1, 1, 1, 1, 4)

        engine = Engine(keep_history=True)

        d1 = DataSource(engine, [
            (t1, 1),
            (t2, 2),
            (t4, 5)
        ])

        out = flatten([d1])

        engine.start(t1, t4)

        t, v = out()[0]
        self.assertEqual(t, t1)
        self.assertEqual(v, [1])

        t, v = out()[1]
        self.assertEqual(t, t2)
        self.assertEqual(v, [2])

        t, v = out()[2]
        self.assertEqual(t, t4)
        self.assertEqual(v, [5])
Пример #9
0
    def test(self):
        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 1, 10, 11, 13)

        engine = Engine(keep_history=True)

        data1 = DataSource(engine, [
            (t1, 0),
            (t2, 2),
        ])

        data = data1 + 1
        data = 1 + data
        data = data * 1
        data = 1 * data
        data = data / 1
        data = 1 / data
        data = -data
        data = abs(data)

        # engine.show_graph('dep', show_edge_label=True)
        engine.start(t1, t2)
        self.assertEqual(len(data()), 2)

        t, v = data()[0]
        self.assertEqual(t, t1)
        self.assertEqual(v, 0.5)

        t, v = data()[1]
        self.assertEqual(t, t2)
        self.assertEqual(v, 0.25)
Пример #10
0
    def testTimer2(self):
        engine = Engine(keep_history=True)

        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 2, 10, 11, 13)

        input1 = [(t1, 1)]
        input2 = [(t2, 2)]

        input1 = DataSource(engine, input1)
        input2 = DataSource(engine, input2)

        class Transform(Flow):
            input1 = Input()
            input2 = Input()

            timer = Timer()

            def __init__(self, input1, input2, default_value):
                super().__init__("transform")
                self.default_value = default_value
                self.done = False

            @when(input1)
            def handle(self):
                self.timer = self.now() + timedelta(minutes=60)

            @when(input2)
            def handle(self):
                if not self.done:
                    self << self.input1() * self.input2()

            @when(timer)
            def do_timer(self):
                if not self.input2 and not self.done:
                    self << self.input1() * self.default_value
                    self.done = True

        t = Transform(input1, input2, 10)

        engine.start(t1, t2)
        # engine.show_graph('timer')
        self.assertEqual(t(), [(t1 + timedelta(minutes=60), 10)])
Пример #11
0
    def testLifting2(self):
        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 1, 10, 11, 13)

        engine = Engine(keep_history=True)

        @lift()
        def sum(p1, p2):
            return p1 + p2

        @lift()
        def sum3(p1, p2, p3):
            return p1 + p2 + p3

        data_1 = DataSource(engine, [
            (t1, 1),
            (t2, 2),
        ])

        data_2 = DataSource(engine, [
            (t1, 1),
            (t2, 2),
        ])

        z = sum(data_1, data_2)
        result = sum3(z, z, z)

        # engine.show_graph('test')
        engine.start(t1, t2)

        result = result()

        self.assertEqual(len(result), 2)

        (ts0, v0) = result[0]
        (ts1, v1) = result[1]

        self.assertEqual(ts0, t1)
        self.assertEqual(v0, 6)

        self.assertEqual(ts1, t2)
        self.assertEqual(v1, 12)
Пример #12
0
    def testSampleFirstWithStartTime(self):
        values = [(datetime(2016, 1, 1, 1, 1, i * 2), i * 2) for i in range(1, 20)]

        engine = Engine(keep_history=True)

        d = DataSource(engine, values)
        s = d.sample(timedelta(seconds=7), method=SampleMethod.First, start_time=values[2][0])

        engine.start(values[0][0], values[-1][0])

        first_result = s()

        first_expected = [
            (datetime(2016, 1, 1, 1, 1, 6, 1), 6),
            (datetime(2016, 1, 1, 1, 1, 13, 1), 8),
            (datetime(2016, 1, 1, 1, 1, 20, 1), 14),
            (datetime(2016, 1, 1, 1, 1, 27, 1), 22),
            (datetime(2016, 1, 1, 1, 1, 34, 1), 28),
        ]
        self.assertEqual(first_expected, first_result)
Пример #13
0
    def testSampleFirst(self):
        values = [(datetime(2016, 1, 1, 1, 1, i * 2), i * 2) for i in range(1, 20)]

        engine = Engine(keep_history=True)

        d = DataSource(engine, values)
        s = d.sample(timedelta(seconds=7), method=SampleMethod.First)

        engine.start(values[0][0], values[-1][0])

        first_result = s()

        first_expected = [
            (datetime(2016, 1, 1, 1, 1, 9), 2),
            (datetime(2016, 1, 1, 1, 1, 16), 10),
            (datetime(2016, 1, 1, 1, 1, 23), 16),
            (datetime(2016, 1, 1, 1, 1, 30), 24),
            (datetime(2016, 1, 1, 1, 1, 37), 30),
        ]
        self.assertEqual(first_expected, first_result)
Пример #14
0
    def testFlatten2(self):
        t1 = datetime(2016, 1, 1, 1, 1, 1)
        t2 = datetime(2016, 1, 1, 1, 1, 2)
        t3 = datetime(2016, 1, 1, 1, 1, 3)
        t4 = datetime(2016, 1, 1, 1, 1, 4)

        engine = Engine(keep_history=True)

        d1 = DataSource(engine, [
            (t1, 1),
            (t2, 2),
            (t4, 5)
        ])

        d2 = DataSource(engine, [
            (t1, 3),
            (t3, 4),
            (t4, 6)
        ])

        out = flatten([d1, d2])

        engine.start(t1, t4)

        t, v = out()[0]
        self.assertEqual(t, t1)
        self.assertEqual(v, [1, 3])

        t, v = out()[1]
        self.assertEqual(t, t2)
        self.assertEqual(v, [2])

        t, v = out()[2]
        self.assertEqual(t, t3)
        self.assertEqual(v, [4])

        t, v = out()[3]
        self.assertEqual(t, t4)
        self.assertEqual(v, [5, 6])
Пример #15
0
    def testLifting3(self):
        t1 = datetime(2016, 8, 1, 10, 11, 12)
        t2 = datetime(2016, 8, 1, 10, 11, 13)
        t3 = datetime(2016, 8, 1, 10, 11, 14)

        engine = Engine(keep_history=True)

        @lift(timed=True, passive='p2')
        def sum(ts, p1, p2, p3):
            return ts, p1 + p2 + p3

        data_1 = DataSource(engine, [
            (t1, 1),
        ])

        data_2 = DataSource(engine, [
            (t2, 2),
        ])

        data_3 = DataSource(engine, [
            (t1, 1),
            (t3, 3),
        ])

        result = sum(data_1, data_2, data_3)

        # engine.show_graph('test')
        engine.start(t1, t3)

        result = result()

        self.assertEqual(len(result), 1)

        (ts0, (ts, v0)) = result[0]

        self.assertEqual(ts0, t3)
        self.assertEqual(v0, 6)
        self.assertEqual(ts, t3)
Пример #16
0
    def testRolling(self):
        t0 = datetime(2016, 1, 1, 1, 1, 0)
        t1 = datetime(2016, 1, 1, 1, 1, 1)
        t2 = datetime(2016, 1, 1, 1, 1, 2)
        t3 = datetime(2016, 1, 1, 1, 1, 3)
        t4 = datetime(2016, 1, 1, 1, 1, 4)
        t5 = datetime(2016, 1, 1, 1, 1, 5)
        t6 = datetime(2016, 1, 1, 1, 1, 8)
        t7 = datetime(2016, 1, 1, 1, 1, 8, 5)

        engine = Engine(keep_history=True)

        d = DataSource(engine, [
            (t0, 0),
            (t1, 1),
            (t2, 2),
            (t3, 3),
            (t4, 4),
            (t5, 5),
            (t6, 6),
            (t7, 7),
        ])

        window = timedelta(seconds=2)
        out = d.rolling(window)
        engine.start(t0, t7)
        o = out()

        self.assertEqual(len(o), 8)

        t, v = o[0]
        self.assertEqual(t0, t)
        self.assertEqual([0], v)

        t, v = o[1]
        self.assertEqual(t1, t)
        self.assertEqual([0, 1], v)

        t, v = o[2]
        self.assertEqual(t2, t)
        self.assertEqual([0, 1, 2], v)

        t, v = o[3]
        self.assertEqual(t3, t)
        self.assertEqual([1, 2, 3], v)

        t, v = o[4]
        self.assertEqual(t4, t)
        self.assertEqual([2, 3, 4], v)

        t, v = o[5]
        self.assertEqual(t5, t)
        self.assertEqual([3, 4, 5], v)

        t, v = o[6]
        self.assertEqual(t6, t)
        self.assertEqual([6], v)

        t, v = o[7]
        self.assertEqual(t7, t)
        self.assertEqual([6, 7], v)