Esempio n. 1
0
    def test_example_1(self):
        # Specify streams
        x = Stream('x')
        y = Stream('y')

        # Specify encapsulated functions (if any)
        def f(v):
            return 2 * v

        # Specify agents.
        map_element(func=f, in_stream=x, out_stream=y)

        # Execute a step
        # Put test values in the input streams.
        x.extend(list(range(3)))
        # Execute a step
        run()
        # Look at recent values of output streams.
        assert recent_values(y) == [0, 2, 4]

        # Execute a step
        # Put test values in the input streams.
        x.extend([10, 20, 30])
        # Execute a step
        run()
        # Look at recent values of output streams.
        assert recent_values(y) == [0, 2, 4, 20, 40, 60]

        # Execute a step
        # Put test values in the input streams.
        x.extend([0, -10])
        # Execute a step
        run()
        # Look at recent values of output streams.
        assert recent_values(y) == [0, 2, 4, 20, 40, 60, 0, -20]
Esempio n. 2
0
    def test_timed_window(self):
        x = Stream('x')
        y = Stream('y')

        def f(v):
            return v

        timed_window(func=f,
                     in_stream=x,
                     out_stream=y,
                     window_duration=10,
                     step_time=10)
        x.extend([(1, 'a'), (8, 'b'), (12, 'c')])
        run()
        assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')])])

        x.extend([(14, 'd'), (36, 'e'), (43, 'g'), (75, 'h')])
        run()
        assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')]),
                                     (20, [(12, 'c'), (14, 'd')]),
                                     (40, [(36, 'e')]), (50, [(43, 'g')])])

        x.extend([(79, 'i'), (101, 'j')])
        run()
        assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')]),
                                     (20, [(12, 'c'), (14, 'd')]),
                                     (40, [(36, 'e')]), (50, [(43, 'g')]),
                                     (80, [(75, 'h'), (79, 'i')])])

        return
Esempio n. 3
0
 def test_None_in_stream(self):
     x = Stream('x', discard_None=False)
     y = Stream(name='y', discard_None=False)
     z = Stream(name='z')
     map_element(lambda v: v, x, y)
     map_element(lambda v: v, x, z)
     x.extend([0, None, 1, None, 2, _no_value, 3])
     run()
     assert (recent_values(y) == [0, None, 1, None, 2, 3])
     assert (recent_values(z) == [0, 1, 2, 3])
Esempio n. 4
0
    def test_operators_on_three_streams(self):
        x = Stream('x')
        y = Stream('y')
        z = x + y
        w = x - y + z

        DATA = list(range(5))
        x.extend(DATA)
        y.extend(DATA)
        run()
        assert recent_values(z) == [2 * v for v in DATA]
        assert recent_values(w) == [2 * v for v in DATA]
Esempio n. 5
0
    def test_example_4(self):
        # Illustrates a cycle of agents and also shows use
        # of a class within a wrapper.
        # Specify network: streams, functions, agents
        # (a) Specify streams
        x = Stream('x')
        y = Stream('y')

        # (b) Specify encapsulated functions (if any)
        def f(v, state):
            final, prefinal = state
            next_output = final + prefinal
            # In the next state:
            # prefinal becomes final
            # final becomes next_output
            next_state = next_output, final
            return next_output, next_state

        class G(object):
            def __init__(self):
                self.divisor = 4

            def g(self, v):
                if v % self.divisor == 0:
                    return _no_value
                else:
                    return v

        # (c) Specify agents.
        encapsulator = G()
        map_element(func=f, in_stream=y, out_stream=x, state=(0, 1))
        map_element(func=encapsulator.g, in_stream=x, out_stream=y)

        # Drive the network in steps.
        # Execute a step
        # Put test values in the input streams.
        y.append(0)
        # Execute a step
        run()
        # Look at recent values of output streams.
        assert recent_values(x) == [1, 1, 2, 3, 5, 8]

        # Execute a step after changing agent parameters
        encapsulator.divisor = 2
        # Put test values in the input streams.
        y.append(0)
        # Execute a step
        run()
        # Look at recent values of output streams.
        assert recent_values(x) == \
          [1, 1, 2, 3, 5, 8, 13, 21, 34]
Esempio n. 6
0
 def test_prepend(self):
     x = Stream()
     y = Stream()
     prepend(list(range(10)), x, y)
     z = fprepend(list(range(10)), x)
     x.extend(list(range(100, 105)))
     run()
     assert recent_values(x) == [100, 101, 102, 103, 104]
     assert recent_values(y) == [
         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 101, 102, 103, 104
     ]
     assert recent_values(z) == [
         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 101, 102, 103, 104
     ]
Esempio n. 7
0
    def test_iot_class(self):

        x = StreamArray(name='x', dtype=int)
        y = StreamArray(name='y', dtype=int)
        # Create an agent that wraps np.sum
        sw = self.sliding_window_test(
            func=np.sum, in_stream=x, out_stream=y, window_size=5, step_size=2)
        x.extend(np.arange(10, dtype=int))
        run()
        assert np.array_equal(recent_values(y), np.array([10, 20, 30]))
        x.extend(np.arange(10, 20, dtype=int))
        run()
        assert np.array_equal(recent_values(y),
                              np.array([10, 20., 30, 40, 50, 60, 70, 80]))
Esempio n. 8
0
    def test_multiply_function_with_multidimensional_array(self):
        x = StreamArray(dimension=2, dtype=int)

        # Create a stream array y.
        y = f_mul(x, 2)

        A = np.array([[1, 10], [2, 20], [3, 30]])
        x.extend(A)
        run()
        assert np.array_equal(recent_values(y), [[2, 20], [4, 40], [6, 60]])

        x.append(np.array([4, 40]))
        run()
        assert np.array_equal(recent_values(y),
                              [[2, 20], [4, 40], [6, 60], [8, 80]])
Esempio n. 9
0
 def test_r_sub(self):
     x = Stream()
     y = Stream()
     r_sub(x, y, 2)
     x.extend(list(range(5)))
     run()
     assert recent_values(y) == [-2, -1, 0, 1, 2]
Esempio n. 10
0
    def test_class(self):
        class C(object):
            def __init__(self):
                return

            def f(self, value):
                if value > 0:
                    return self.pos(value)
                else:
                    return self.neg(value)

            def pos(self, value):
                return value * value

            def neg(self, value):
                return value + value

        s = Stream('s')
        t = Stream('t')
        c = C()

        @map_e
        def g(v):
            return c.f(v)

        g(in_stream=s, out_stream=t)
        s.extend(list(range(-4, 4)))
        run()
        assert (recent_values(t) == [-8, -6, -4, -2, 0, 1, 4, 9])
Esempio n. 11
0
 def test_f_add(self):
     x = Stream()
     K = 5
     y = f_add(x, K)
     x.extend(list(range(3)))
     run()
     assert recent_values(y) == [5, 6, 7]
Esempio n. 12
0
 def test_source_file(self, filename='tests/test_source_file_name.txt'):
     s = Stream('s')
     with open(filename, 'r') as input_file:
         for line in input_file:
             s.append(int(line))
             run()
     assert recent_values(s) == [1, 2, 3]
Esempio n. 13
0
 def test_r_mul(self):
     x = Stream()
     y = Stream()
     a = r_mul(x, y, 2)
     x.extend(list(range(5)))
     run()
     assert recent_values(y) == [0, 2, 4, 6, 8]
Esempio n. 14
0
    def test_plus_operator(self):
        x = Stream()
        y = Stream()
        z = x + y

        x.extend(list(range(3)))
        y.extend(list(range(100, 105)))
        run()
        assert recent_values(z) == [100, 102, 104]

        x.extend(list(range(3, 7)))
        run()
        assert recent_values(z) == [100, 102, 104, 106, 108]

        run()
        assert recent_values(z) == [100, 102, 104, 106, 108]
Esempio n. 15
0
 def test_halt_agent(self):
     x = Stream('x')
     y = Stream('y')
     a = map_window(func=sum,
                    in_stream=x,
                    out_stream=y,
                    window_size=2,
                    step_size=2)
     x.extend(list(range(6)))
     run()
     assert recent_values(y) == [1, 5, 9]
     a.halt()
     run()
     assert recent_values(y) == [1, 5, 9]
     x.extend(list(range(10, 15)))
     run()
     assert recent_values(y) == [1, 5, 9]
Esempio n. 16
0
 def test_r_add(self):
     x = Stream()
     y = Stream()
     z = 5
     r_add(x, y, z)
     x.extend(list(range(3)))
     run()
     assert recent_values(y) == [5, 6, 7]
Esempio n. 17
0
 def test_echo(self):
     spoken = Stream('spoken')
     heard = make_echo(spoken, D=1, A=0.5)
     spoken.extend([64, 32, 16, 8, 4, 2, 1, 0, 0, 0, 0])
     run()
     assert recent_values(heard) == [
         64.0, 64.0, 48.0, 32.0, 20.0, 12.0, 7.0, 3.5, 1.75, 0.875, 0.4375
     ]
Esempio n. 18
0
    def test_halt_agent(self):
        def double(v):
            return v * 2

        x = Stream('x')
        y = Stream('y')
        a = map_element(func=double, in_stream=x, out_stream=y)
        x.extend(list(range(5)))
        run()
        assert recent_values(y) == [0, 2, 4, 6, 8]
        a.halt()
        run()
        assert recent_values(y) == [0, 2, 4, 6, 8]
        x.extend(list(range(10, 15)))
        run()
        assert recent_values(y) == [0, 2, 4, 6, 8]
        assert recent_values(x) == list(range(5)) + list(range(10, 15))
Esempio n. 19
0
    def test_fmap_with_stream_array(self):
        x = StreamArray(dimension=2, dtype=int)

        @fmap_e
        def g(v):
            return 2 * v

        y = g(x)

        A = np.array([[1, 10], [2, 20], [3, 30]])
        x.extend(A)
        run()
        assert np.array_equal(recent_values(y), [[2, 20], [4, 40], [6, 60]])

        x.append(np.array([4, 40]))
        run()
        assert np.array_equal(recent_values(y),
                              [[2, 20], [4, 40], [6, 60], [8, 80]])
Esempio n. 20
0
    def test_multiply_operator_with_arrays(self):
        x = StreamArray(dtype=int)
        y = StreamArray(dtype=int)
        z = y * x

        x.extend(np.arange(3))
        y.extend(np.arange(100, 105, 2))
        run()
        assert np.array_equal(recent_values(z), np.array([0, 102, 208]))
Esempio n. 21
0
    def test_minus_operator_with_arrays(self):
        x = StreamArray(dtype=int)
        y = StreamArray(dtype=int)
        z = y - x

        x.extend(np.arange(3))
        y.extend(np.arange(100, 105, 2))
        run()
        assert np.array_equal(recent_values(z), np.array([100, 101, 102]))
Esempio n. 22
0
    def test_map_with_state_and_keyword_arg(self):
        x = Stream()
        y = Stream()

        @map_e
        def f(v, state, k):
            return v + k + state, state + 1

        @fmap_e
        def g(v, state, k):
            return v + k + state, state + 1

        f(x, y, state=0, k=10)
        z = g(x, state=0, k=10)
        x.extend(list(range(5)))
        run()
        assert recent_values(y) == [10, 12, 14, 16, 18]
        assert recent_values(z) == [10, 12, 14, 16, 18]
Esempio n. 23
0
    def test_iot(self):
        x = StreamArray(dtype=int)
        y = StreamArray(dtype=int)
        z = StreamArray(dtype=int)
        u = StreamArray(dtype=int)
        v = StreamArray(dtype=int)
        
        def f(A, y, z):
            """
            Function wrapped by an iot agent. The first parameter
            'A' is an array obtained from the input stream of the
            agent. The other parameters, y and z, are positional
            arguments (*args) of the function.

            The function returns a pointer into the input stream.
            In this example, each call to the function processes
            the entire input array 'A' and so the function returns
            len(A).

            """
            y.extend(2*A)
            z.extend(3*A)
            # Return a pointer into the input array.
            return len(A)

        def g(A, u, v):
            """
            Parameters are similar to f.

            """
            u.extend(A+A)
            v.extend(A**2)
            return len(A)

        # Create agents that wrap functions f and g.
        iot(f, x, y, z)
        iot(g, x, u, v)

        # Extend stream x with an array
        x.extend(np.arange(5, dtype=int))
        run()
        assert np.array_equal(recent_values(y), 2*np.arange(5, dtype=int))
        assert np.array_equal(recent_values(z), 3*np.arange(5, dtype=int))
        assert np.array_equal(recent_values(u), 2*np.arange(5, dtype=int))
        assert np.array_equal(recent_values(v), np.arange(5, dtype=int)**2)

        # Extend stream x with another array
        x.extend(np.arange(5, 10, dtype=int))
        run()
        assert np.array_equal(recent_values(y), 2*np.arange(10, dtype=int))
        assert np.array_equal(recent_values(z), 3*np.arange(10, dtype=int))
        assert np.array_equal(recent_values(u), 2*np.arange(10, dtype=int))
        assert np.array_equal(recent_values(v), np.arange(10, dtype=int)**2)
Esempio n. 24
0
    def test_initial_value(self):
        def double(v):
            return v * 2

        x = Stream('x')
        y = Stream(name='y', initial_value=[0] * 5)
        a = map_element(func=double, in_stream=x, out_stream=y)
        x.extend(list(range(5)))
        run()
        assert recent_values(y) == [0] * 5 + [0, 2, 4, 6, 8]
Esempio n. 25
0
    def test_plus_operator_with_arrays_1(self):
        x = StreamArray(dtype=int)
        y = StreamArray(dtype=int)
        z = x + y

        x.extend(np.arange(3))
        y.extend(np.arange(100, 105))
        run()
        assert isinstance(recent_values(z), np.ndarray)
        assert np.array_equal(recent_values(z), np.array([100, 102, 104]))

        x.extend(np.arange(3, 7))
        run()
        assert np.array_equal(recent_values(z),
                              np.array([100, 102, 104, 106, 108]))

        run()
        assert np.array_equal(recent_values(z),
                              np.array([100, 102, 104, 106, 108]))
Esempio n. 26
0
    def test_simple_merge_1(self):
        scheduler = Stream.scheduler
        x = Stream('x')
        u = Stream('u')
        s = Stream('s')
        d = zip_map(func=sum, in_streams=[x, u], out_stream=s, name='d')
        x.extend(list(range(3)))
        u.extend([10, 15, 18])

        run()
        assert (recent_values(s) == [10, 16, 20])
Esempio n. 27
0
    def test_plus_operator_with_arrays(self):
        x = StreamArray(dimension=2, dtype=int)
        y = StreamArray(dimension=2, dtype=int)
        z = x + y
        A = np.arange(6).reshape((3, 2))
        B = np.arange(100, 110).reshape((5, 2))
        x.extend(A)
        y.extend(B)
        run()
        assert isinstance(z, StreamArray)
        assert np.array_equal(recent_values(z),
                              np.array([[100, 102], [104, 106], [108, 110]]))

        C = np.arange(6, 12).reshape((3, 2))
        x.extend(C)
        run()
        assert np.array_equal(
            recent_values(z),
            np.array([[100, 102], [104, 106], [108, 110], [112, 114],
                      [116, 118]]))
Esempio n. 28
0
 def test_minus_operator_with_arrays_and_dimension(self):
     x = StreamArray(dimension=3, dtype=int)
     y = StreamArray(dimension=3, dtype=int)
     z = y - x
     A = np.array([[10, 20, 30], [40, 50, 60]])
     B = np.array([[100, 100, 100], [200, 200, 200], [300, 300, 00]])
     x.extend(A)
     y.extend(B)
     run()
     assert np.array_equal(recent_values(z),
                           np.array([[90, 80, 70], [160, 150, 140]]))
Esempio n. 29
0
    def test_map_list_with_arrays(self):
        from IoTPy.agent_types.op import map_list
        x = StreamArray(dtype=int)
        y = StreamArray(dtype=int)

        def f(A):
            return 2 * A

        map_list(f, x, y)
        x.extend(np.arange(5))
        run()
        assert np.array_equal(recent_values(y), 2 * np.arange(5))
Esempio n. 30
0
    def test_filter_number_1(self):
        input_list = list(range(-5, 5))
        in_stream = Stream('input')
        out_stream = Stream('output')

        def f(v):
            return v > 0

        filter_element(f, in_stream, out_stream)
        in_stream.extend(input_list)
        run()
        assert recent_values(out_stream) == list(filter(f, input_list))