Пример #1
0
def map_array_f(func, in_stream, state=None, *args, **kwargs):
    out_stream = StreamArray(func.__name__ + in_stream.name)
    map_list(func, in_stream, out_stream, state, None, None, *args, **kwargs)
    return out_stream
Пример #2
0
def make_out_stream(in_stream):
    if isinstance(in_stream, Stream):
        return Stream()
    if isinstance(in_stream, StreamArray):
        return StreamArray(dimension=in_stream.dimension,
                           dtype=in_stream.dtype)
Пример #3
0
def stream_test():
    # Numpy type for testing stream array.
    txyz_dtype = np.dtype([('time', 'int'), ('data', '3float')])

    #--------------------------------------------
    # Testing StreamArray with positive dimension
    s = StreamArray(name='s', dimension=3)
    # Each element of s is a numpy array with with 3 elements
    # Initially s is empty. So s.stop == 0
    assert s.stop == 0
    # If num_in_memory is not specified in the declaration for
    # s, the default value, DEFAULT_NUM_IN_MEMORY,  is used.
    # The length of s.recent is twice num_in_memory
    assert len(s.recent) == 2 * DEFAULT_NUM_IN_MEMORY

    # Append a numpy array with 3 zeros to s
    s.append(np.zeros(3))
    assert (s.stop == 1)
    # Thus s.recent[:s.stop] is an array with 1 row and 3 columns.
    assert (np.array_equal(s.recent[:s.stop], np.array([[0.0, 0.0, 0.0]])))

    # Extend s by an array with 2 rows and 3 columns. The number of
    # columns must equal the dimension of the stream array.
    s.extend(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]))
    # s.stop is incremented to account for the addition of two elements.
    assert s.stop == 3
    # s.recent[:s.stop] includes all the elements added to s.
    # Thus s.recent[:s.stop] is an array with 3 rows and 3 columns.
    assert (np.array_equal(
        s.recent[:s.stop],
        np.array([[0.0, 0.0, 0.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])))

    # Extend s by an array with 1 row and 3 columns. The number of
    # columns must equal the dimension of the stream array.
    s.extend(np.array([[7.0, 8.0, 9.0]]))
    # s.stop is incremented to account for the addition of a single row.
    assert s.stop == 4
    # Thus s.recent[:s.stop] is an array with 4 rows and 3 columns.
    assert np.array_equal(
        s.recent[:s.stop],
        np.array([[0.0, 0.0, 0.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0],
                  [7.0, 8.0, 9.0]]))
    # Note the difference between EXTENDING s with an array consisting
    # of 1 row and 3 columns, versus APPENDING a rank-1 array consisting
    # of 3 elements, as in the following example.
    s.append(np.array([10.0, 11.0, 12.0]))
    # s.stop is incremented to account for the addition of a single row.
    assert s.stop == 5
    # Thus s.recent[:s.stop] is an array with 5 rows and 3 columns.
    assert np.array_equal(
        s.recent[:s.stop],
        np.array([[0.0, 0.0, 0.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0],
                  [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]))

    #---------------------------------------------------------------
    # Testing StreamArray with zero dimension and user-defined dtype
    t = StreamArray(name='t', dimension=0, dtype=txyz_dtype)
    # Each element of t is an object of dtype txyz_dtype
    # t[i]['time'] is an int.
    # t[i]['data'] is a 3-tuple consisting of 3 floats.
    # Nothing has been appended to t, and so t.stop == 0.
    assert (t.stop == 0)

    # Append an object with 'time' = 1, and 'data' = [0.0, 1.0, 2.0].
    t.append(np.array((1, [0.0, 1.0, 2.0]), dtype=txyz_dtype))
    # Increase t.stop to account for the element that has just been
    # added to t.
    assert t.stop == 1
    # t.recent[:t.stop] contains all elements appended to t.
    assert t.recent[:t.stop] == np.array([(1, [0.0, 1.0, 2.0])],
                                         dtype=txyz_dtype)
    assert t.recent[0]['time'] == np.array(1)
    assert np.array_equal(t.recent[0]['data'], np.array([0., 1., 2.]))

    # Append another element to t.
    t.append(np.array((2, [11.0, 12.0, 13.0]), dtype=txyz_dtype))
    # Increase t.stop to account for the element that has just been
    # added to t.
    assert (t.stop == 2)
    # t.recent[:t.stop] contains all elements appended to t.
    a = np.array([(1, [0.0, 1.0, 2.0]), (2, [11.0, 12.0, 13.0])],
                 dtype=txyz_dtype)
    assert np.array_equal(t.recent[:t.stop], a)

    # Extend t by a list of 2 elements each of which consists of
    # zeroes of txyz_dtype
    t.extend(np.zeros(2, dtype=txyz_dtype))
    # Increase t.stop to account for the element that has just been
    # added to t.
    assert (t.stop == 4)
    # t.recent[:t.stop] contains all elements appended to t.
    a = np.array([(1, [0.0, 1.0, 2.0]), (2, [11.0, 12.0, 13.0]),
                  (0, [0.0, 0.0, 0.0]), (0, [0.0, 0.0, 0.0])],
                 dtype=txyz_dtype)
    assert np.array_equal(t.recent[:t.stop], a)

    #---------------------------------------------------------------
    # Testing simple Stream
    u = Stream('u')
    v = Stream('v')
    # Add elements 0, 1, 2, 3 to stream u.
    u.extend(list(range(4)))
    # Increase u.stop to account for the element that has just been
    # added to u.
    assert u.stop == 4
    # u.recent[:t.stop] contains all elements appended to u.
    assert u.recent[:u.stop] == [0, 1, 2, 3]
    # No change to v.
    assert v.stop == 0

    # Append element 10 to v and then append the list [40, 50]
    v.append(10)
    v.append([40, 50])
    # Increase v.stop by 2 to account for the 2 new elements appended
    # to v.
    assert v.stop == 2
    # v.recent[:v.stop] contains all elements appended to v.
    assert v.recent[:v.stop] == [10, [40, 50]]

    # Extend stream v
    v.extend([60, 70, 80])
    # Increase v.stop by 3 to account for the 3 new elements appended
    # to v.
    assert v.stop == 5
    # v.recent[:v.stop] contains all elements appended to v.
    assert v.recent[:v.stop] == [10, [40, 50], 60, 70, 80]

    #------------------------------------------
    # Test helper functions: get_contents_after_column_value()
    # Also test StreamArray
    y = StreamArray(name='y', dimension=0, dtype=txyz_dtype, num_in_memory=64)
    # y[i]['time'] is a time (int).
    # y[i]['data'] is 3-tuple usually with directional data
    # for x, y, z.
    # y.recent length is twice num_in_memory
    assert len(y.recent) == 128
    # y has no elements, so y.stop == 0
    assert y.stop == 0
    # Test data for StreamArray with user-defined data type.
    test_data = np.zeros(128, dtype=txyz_dtype)
    assert len(test_data) == 128

    # Put random numbers for test_data[i]['time'] and
    # test_data[i]['data'][xyx] for xyz in [0, 1, 2]
    for i in range(len(test_data)):
        test_data[i]['time'] = random.randint(0, 1000)
        for j in range(3):
            test_data[i]['data'][j] = random.randint(2000, 9999)

    # ordered_test_data has time in increasing order.
    ordered_test_data = np.copy(test_data)
    for i in range(len(ordered_test_data)):
        ordered_test_data[i]['time'] = i
    y.extend(ordered_test_data[:60])
    # extending y does not change length of y.recent
    assert (len(y.recent) == 128)
    # y.stop increases to accommodate the extension of y by 60.
    assert (y.stop == 60)
    # y.recent[:y.stop] now contains all the values put into y.
    assert np.array_equal(y.recent[:y.stop], ordered_test_data[:60])

    assert np.array_equal(
        y.get_contents_after_column_value(column_number=0, value=50),
        ordered_test_data[50:60])
    assert np.array_equal(y.get_contents_after_time(start_time=50),
                          ordered_test_data[50:60])
    assert (y.get_index_for_column_value(column_number=0, value=50) == 50)

    yz = StreamArray(name='yz',
                     dimension=0,
                     dtype=txyz_dtype,
                     num_in_memory=64)
    c = np.array((1, [0., 1., 2.]), dtype=txyz_dtype)
    yz.append(c)
    assert np.array_equal(yz.recent[:yz.stop],
                          np.array([(1, [0., 1., 2.])], dtype=txyz_dtype))
    d = np.array([(2, [3., 4., 5.]), (3, [6., 7., 8.])], dtype=txyz_dtype)
    yz.extend(d)
    assert np.array_equal(
        yz.recent[:yz.stop],
        np.array([(1, [0., 1., 2.]), (2, [3., 4., 5.]), (3, [6., 7., 8.])],
                 dtype=txyz_dtype))

    #------------------------------------------
    # TESTING regular Stream class
    x = Stream(name='x', num_in_memory=8)
    # The length of x.recent is twice num_in_memory
    assert (len(x.recent) == 16)
    # No values have been appended to stream x; so x.stop == 0
    assert (x.stop == 0)

    # Test append
    x.append(10)
    # Appending values to x does not change len(x.recent)
    assert (len(x.recent) == 16)
    # x.stop increases to accomodate the value appended.
    assert (x.stop == 1)
    # x.recent[:x.stop] includes the latest append
    assert (x.recent[:x.stop] == [10])
    x.append(20)
    assert (len(x.recent) == 16)
    # x.stop increases to accomodate the value appended.
    assert (x.stop == 2)
    # x.recent[:x.stop] includes the latest append
    assert (x.recent[:2] == [10, 20])

    # Test extend
    x.extend([30, 40])
    assert (len(x.recent) == 16)
    # x.stop increases to accomodate the values extended.
    assert (x.stop == 4)
    # x.recent[:x.stop] includes the latest extend
    assert (x.recent[:x.stop] == [10, 20, 30, 40])
    # Checking extension with the empty list.
    x.extend([])
    assert (len(x.recent) == 16)
    # extending a stream with the empty list does not change
    # the stream.
    assert (x.stop == 4)
    assert (x.recent[:4] == [10, 20, 30, 40])
    # Checking extending a stream with a singleton list
    x.extend([50])
    assert (len(x.recent) == 16)
    assert (x.stop == 5)
    assert (x.recent[:5] == [10, 20, 30, 40, 50])

    # Check registering a reader.
    # Register a reader called 'a' for stream x starting
    # to read from x[3] onwards.
    x.register_reader('a', 3)
    # Register a reader called 'b' for stream x starting
    # to read from x[4] onwards.
    x.register_reader('b', 4)
    # x.start is a dict which identifies the readers of x
    # and where they are starting to read from.
    assert (x.start == {'a': 3, 'b': 4})

    x.extend([1, 2, 3, 4, 5])
    assert (len(x.recent) == 16)
    assert (x.stop == 10)
    assert (x.recent[:10] == [10, 20, 30, 40, 50, 1, 2, 3, 4, 5])
    assert (x.start == {'a': 3, 'b': 4})

    x.register_reader('a', 7)
    x.register_reader('b', 7)
    assert (x.start == {'a': 7, 'b': 7})

    #------------------------------------------
    # Test helper functions
    assert (x.get_last_n(n=2) == [4, 5])

    v = StreamArray(dimension=(3, 4), dtype=int)
    v.append(np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]))
    a = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])
    np.array_equal(v.recent[:v.stop],
                   np.array([[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]]))
    v.extend(
        np.array([[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]],
                  [[24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35]]]))
    np.array_equal(
        v.recent[:v.stop],
        np.array([[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]],
                  [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]],
                  [[24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35]]]))

    u = StreamArray(name='u', dimension=2, dtype=int)
    a = np.array([0, 1])
    u.append(a)
    np.array_equal(u.recent[:u.stop], np.array([[0, 1]]))
    u.extend(np.array([[2, 3], [4, 5], [6, 7]]))
    np.array_equal(u.recent[:u.stop], np.array([[0, 1], [2, 3], [4, 5], [6,
                                                                         7]]))

    t = StreamArray('t')
    t.append(np.array(1.0))
    t.extend(np.array([2.0, 3.0]))
    np.array_equal(t.recent[:t.stop], np.array([1.0, 2.0, 3.0]))
Пример #4
0
def test_window_agents():
    scheduler = Stream.scheduler

    q = Stream('q')
    qq = Stream('qq')
    r = Stream('r')
    s = Stream('s')
    t = Stream('t')
    u = Stream('u')
    v = Stream('v')
    w = Stream('w')
    x = Stream('x')
    y = Stream('y')
    z = Stream('z')
    a = Stream('a')
    b = Stream('b')
    c = Stream('c')
    yy = Stream('yy')
    zz = Stream('zz')

    #----------------------------------------------------------------
    # Test simple window map agent with the same window size and step size
    smap = map_window_f(func=sum, in_stream=r, window_size=4, step_size=4)
    map_window(func=sum, in_stream=r, out_stream=s, window_size=4, step_size=4)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test simple window list agent with the same window size and step size
    def f_map_window_list(lst):
        return [max(lst)] * len(lst)

    s_list = Stream('s list')
    map_window_list(func=f_map_window_list,
                    in_stream=r,
                    out_stream=s_list,
                    window_size=4,
                    step_size=4)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window map agent with different window and step sizes
    map_window(func=sum, in_stream=r, out_stream=t, window_size=3, step_size=2)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window map agent with a NumPy function
    map_window(func=np.mean,
               in_stream=r,
               out_stream=q,
               window_size=3,
               step_size=2,
               name='bb')

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window map agent with arguments
    def map_with_args(window, addend):
        return np.mean(window) + addend

    map_window(func=map_with_args,
               in_stream=r,
               out_stream=qq,
               window_size=3,
               step_size=2,
               addend=1)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window map agent with user-defined function and no state
    map_window(func=lambda v: sum(v) + 1,
               in_stream=r,
               out_stream=u,
               window_size=4,
               step_size=4)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window map agent with state
    def g(lst, state):
        return sum(lst) + state, sum(lst) + state

    map_window(func=g,
               in_stream=r,
               out_stream=v,
               window_size=4,
               step_size=4,
               state=0)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window merge agent with no state
    def h(list_of_windows):
        return sum([sum(window) for window in list_of_windows])

    merge_window(func=h,
                 in_streams=[r, w],
                 out_stream=x,
                 window_size=3,
                 step_size=3)
    merge_stream = merge_window_f(func=h,
                                  in_streams=[r, w],
                                  window_size=3,
                                  step_size=3)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window merge agent with state
    def h_with_state(list_of_windows, state):
        return (sum([sum(window)
                     for window in list_of_windows]) + state, state + 1)

    merge_window(func=h_with_state,
                 in_streams=[r, w],
                 out_stream=a,
                 window_size=3,
                 step_size=3,
                 state=0)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window split agent with no state
    def splt(window):
        return sum(window), max(window)

    split_window(func=splt,
                 in_stream=r,
                 out_streams=[y, z],
                 window_size=3,
                 step_size=3)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window split agent with state
    def split_with_state(window, state):
        return (sum(window) + state, max(window) + state), state + 1

    split_window(func=split_with_state,
                 in_stream=r,
                 out_streams=[yy, zz],
                 window_size=3,
                 step_size=3,
                 state=0)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window many-to-many with state and args
    def func_multi_window_with_state_and_args(windows, state, cutoff):
        return ((max(max(windows[0]), max(windows[1]), cutoff, state),
                 min(min(windows[0]), min(windows[1]), cutoff,
                     state)), state + 2)

    multi_window(func=func_multi_window_with_state_and_args,
                 in_streams=[r, w],
                 out_streams=[b, c],
                 state=0,
                 window_size=3,
                 step_size=3,
                 cutoff=15)
    multi_window_b, multi_window_c = multi_window_f(
        func=func_multi_window_with_state_and_args,
        in_streams=[r, w],
        num_out_streams=2,
        state=0,
        window_size=3,
        step_size=3,
        cutoff=15)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    r.extend(range(16))
    scheduler.step()
    assert recent_values(r) == [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
    ]
    assert recent_values(s) == [
        0 + 1 + 2 + 3, 4 + 5 + 6 + 7, 8 + 9 + 10 + 11, 12 + 13 + 14 + 15
    ]
    assert recent_values(smap) == recent_values(s)
    assert recent_values(t) == [
        0 + 1 + 2, 2 + 3 + 4, 4 + 5 + 6, 6 + 7 + 8, 8 + 9 + 10, 10 + 11 + 12,
        12 + 13 + 14
    ]
    assert recent_values(q) == [1, 3, 5, 7, 9, 11, 13]
    assert recent_values(qq) == [2, 4, 6, 8, 10, 12, 14]
    assert recent_values(u) == [
        0 + 1 + 2 + 3 + 1, 4 + 5 + 6 + 7 + 1, 8 + 9 + 10 + 11 + 1,
        12 + 13 + 14 + 15 + 1
    ]
    assert recent_values(v) == [6, 28, 66, 120]
    assert recent_values(w) == []
    assert recent_values(x) == []
    assert recent_values(merge_stream) == recent_values(x)
    # y is sum of windows of r with window and step size of 3
    assert recent_values(y) == [3, 12, 21, 30, 39]
    assert recent_values(yy) == [3, 13, 23, 33, 43]
    #  y is max of windows of r with window and step size of 3
    assert recent_values(z) == [2, 5, 8, 11, 14]
    assert recent_values(zz) == [2, 6, 10, 14, 18]
    assert recent_values(a) == []
    assert recent_values(b) == []
    assert recent_values(c) == []

    #----------------------------------------------------------------
    #----------------------------------------------------------------
    # Step through the scheduler
    #----------------------------------------------------------------
    #----------------------------------------------------------------
    w.extend([10, 12, 14, 16, 18])
    scheduler.step()
    assert recent_values(r) == [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
    ]
    assert recent_values(s) == [
        0 + 1 + 2 + 3, 4 + 5 + 6 + 7, 8 + 9 + 10 + 11, 12 + 13 + 14 + 15
    ]
    assert recent_values(s_list) == [
        3, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 15
    ]
    assert recent_values(smap) == recent_values(s)
    assert recent_values(t) == [
        0 + 1 + 2, 2 + 3 + 4, 4 + 5 + 6, 6 + 7 + 8, 8 + 9 + 10, 10 + 11 + 12,
        12 + 13 + 14
    ]
    assert recent_values(q) == [1, 3, 5, 7, 9, 11, 13]
    assert recent_values(qq) == [2, 4, 6, 8, 10, 12, 14]
    assert recent_values(u) == [
        0 + 1 + 2 + 3 + 1, 4 + 5 + 6 + 7 + 1, 8 + 9 + 10 + 11 + 1,
        12 + 13 + 14 + 15 + 1
    ]
    assert recent_values(v) == [6, 28, 66, 120]
    assert recent_values(w) == [10, 12, 14, 16, 18]
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14)]
    assert recent_values(merge_stream) == recent_values(x)
    assert recent_values(y) == [3, 12, 21, 30, 39]
    assert recent_values(yy) == [3, 13, 23, 33, 43]
    assert recent_values(z) == [2, 5, 8, 11, 14]
    assert recent_values(zz) == [2, 6, 10, 14, 18]
    assert recent_values(a) == [39]
    assert recent_values(b) == [15]
    assert recent_values(c) == [0]

    #----------------------------------------------------------------
    r.extend([10, -10, 21, -20])
    scheduler.step()
    assert recent_values(s) == [6, 22, 38, 54, 1]
    assert recent_values(s_list) == \
      [3, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 15, 21, 21, 21, 21]

    assert recent_values(smap) == recent_values(s)
    assert recent_values(t) == [
        0 + 1 + 2, 2 + 3 + 4, 4 + 5 + 6, 6 + 7 + 8, 8 + 9 + 10, 10 + 11 + 12,
        12 + 13 + 14, 14 + 15 + 10, 10 + (-10) + 21
    ]
    assert recent_values(q) == [1, 3, 5, 7, 9, 11, 13, 13, 7]
    assert recent_values(qq) == [2, 4, 6, 8, 10, 12, 14, 14, 8]
    assert recent_values(u) == [
        0 + 1 + 2 + 3 + 1, 4 + 5 + 6 + 7 + 1, 8 + 9 + 10 + 11 + 1,
        12 + 13 + 14 + 15 + 1, 10 + (-10) + 21 + (-20) + 1
    ]
    assert recent_values(v) == [6, 28, 66, 120, 121]
    assert recent_values(w) == [10, 12, 14, 16, 18]
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14)]
    assert recent_values(merge_stream) == recent_values(x)
    assert recent_values(y) == [3, 12, 21, 30, 39, 15]
    assert recent_values(yy) == [3, 13, 23, 33, 43, 20]
    assert recent_values(z) == [2, 5, 8, 11, 14, 15]
    assert recent_values(zz) == [2, 6, 10, 14, 18, 20]
    assert recent_values(a) == [39]
    assert recent_values(b) == [15]
    assert recent_values(c) == [0]

    #----------------------------------------------------------------
    w.append(20)
    scheduler.step()
    assert recent_values(s) == [6, 22, 38, 54, 1]
    assert recent_values(smap) == recent_values(s)
    assert recent_values(t) == [
        0 + 1 + 2, 2 + 3 + 4, 4 + 5 + 6, 6 + 7 + 8, 8 + 9 + 10, 10 + 11 + 12,
        12 + 13 + 14, 14 + 15 + 10, 10 + (-10) + 21
    ]
    assert recent_values(q) == [1, 3, 5, 7, 9, 11, 13, 13, 7]
    assert recent_values(qq) == [2, 4, 6, 8, 10, 12, 14, 14, 8]
    assert recent_values(u) == [
        0 + 1 + 2 + 3 + 1, 4 + 5 + 6 + 7 + 1, 8 + 9 + 10 + 11 + 1,
        12 + 13 + 14 + 15 + 1, 10 + (-10) + 21 + (-20) + 1
    ]
    assert recent_values(v) == [6, 28, 66, 120, 121]
    assert recent_values(w) == [10, 12, 14, 16, 18, 20]
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14),
                                (3 + 4 + 5) + (16 + 18 + 20)]
    assert recent_values(merge_stream) == recent_values(x)
    assert recent_values(y) == [3, 12, 21, 30, 39, 15]
    assert recent_values(yy) == [3, 13, 23, 33, 43, 20]
    assert recent_values(z) == [2, 5, 8, 11, 14, 15]
    assert recent_values(zz) == [2, 6, 10, 14, 18, 20]
    assert recent_values(a) == [39, 67]
    assert recent_values(b) == [15, 20]
    assert recent_values(multi_window_b) == recent_values(b)
    assert recent_values(c) == [0, 2]
    assert recent_values(multi_window_c) == recent_values(c)

    #----------------------------------------------------------------
    r.extend([-1, 1, 0])
    scheduler.step()
    assert recent_values(s) == [6, 22, 38, 54, 1]
    assert recent_values(smap) == recent_values(s)
    assert recent_values(t) == [
        0 + 1 + 2, 2 + 3 + 4, 4 + 5 + 6, 6 + 7 + 8, 8 + 9 + 10, 10 + 11 + 12,
        12 + 13 + 14, 14 + 15 + 10, 10 + (-10) + 21, 21 - 20 - 1, -1 + 1 + 0
    ]
    assert recent_values(q) == [1, 3, 5, 7, 9, 11, 13, 13, 7, 0, 0]
    assert recent_values(qq) == [2, 4, 6, 8, 10, 12, 14, 14, 8, 1, 1]
    assert recent_values(u) == [7, 23, 39, 55, 2]
    assert recent_values(v) == [6, 28, 66, 120, 121]
    assert recent_values(w) == [10, 12, 14, 16, 18, 20]
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14),
                                (3 + 4 + 5) + (16 + 18 + 20)]
    assert recent_values(merge_stream) == recent_values(x)
    assert recent_values(y) == [3, 12, 21, 30, 39, 15, 0]
    assert recent_values(yy) == [3, 13, 23, 33, 43, 20, 6]
    assert recent_values(z) == [2, 5, 8, 11, 14, 15, 21]
    assert recent_values(zz) == [2, 6, 10, 14, 18, 20, 27]
    assert recent_values(a) == [39, 67]
    assert recent_values(b) == [15, 20]
    assert recent_values(multi_window_b) == recent_values(b)
    assert recent_values(c) == [0, 2]
    assert recent_values(multi_window_c) == recent_values(c)

    #----------------------------------------------------------------
    #----------------------------------------------------------------
    # TEST WINDOW WITH STREAM ARRAY
    #----------------------------------------------------------------
    #----------------------------------------------------------------
    # Simple linear arrays
    x = StreamArray('x')
    y = StreamArray('y')

    #----------------------------------------------------------------
    # Test window map agent with stream arrays and a NumPy function
    map_window(func=np.mean,
               in_stream=x,
               out_stream=y,
               window_size=3,
               step_size=3,
               name='window map agent for arrays')
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    x.extend(np.linspace(0.0, 11.0, 12))
    scheduler.step()
    assert np.array_equal(recent_values(x), np.linspace(0.0, 11.0, 12))
    # y[0] = (0+1+2)/3.0, y[1] = (3+4+5)/3.0
    assert np.array_equal(recent_values(y), np.array([1.0, 4.0, 7.0, 10.0]))

    x = StreamArray('x', dimension=2)
    y = StreamArray('y', dimension=2)
    z = StreamArray('z', dimension=2)
    a = StreamArray('a', dimension=2)
    b = StreamArray('b', dimension=2)
    c = StreamArray('c', dimension=2)
    d = StreamArray('d', dimension=2)
    p = StreamArray('p', dimension=2)
    q = StreamArray('q', dimension=2)
    r = StreamArray('r', dimension=2)
    s = StreamArray('s', dimension=2)

    #----------------------------------------------------------------
    # Test window map agent with stream arrays and a NumPy function
    def f(input_array):
        return np.mean(input_array, axis=0)

    map_window(func=f,
               in_stream=x,
               out_stream=y,
               window_size=2,
               step_size=2,
               name='window map agent for arrays')

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window sink with stream arrays
    def sum_array(input_array, output_list):
        output_list.append(sum(input_array))

    sum_array_list = []
    sink_window(func=sum_array,
                in_stream=x,
                window_size=2,
                step_size=2,
                name='sum array',
                output_list=sum_array_list)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window map agent with state
    def g(lst, state):
        return sum(lst) + state, state + 1

    map_window(func=g,
               in_stream=x,
               out_stream=z,
               window_size=2,
               step_size=2,
               state=0)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window merge agent with state
    def h_array(list_of_windows, state):
        return (sum([sum(window)
                     for window in list_of_windows]) + state, state + 1)

    merge_window(func=h_array,
                 in_streams=[x, a],
                 out_stream=b,
                 window_size=2,
                 step_size=2,
                 state=0)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window split agent with state
    def split_with_state(window, state):
        return [np.sum(window, axis=0)+state, np.max(window, axis=0)+state], \
          state+1.0

    split_window(func=split_with_state,
                 in_stream=x,
                 out_streams=[c, d],
                 window_size=2,
                 step_size=2,
                 state=0.0)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window many-to-many with state and args
    def func_multi_window_with_state_and_args(windows, state, cutoff):
        max_value = np.maximum(np.max(windows[0], axis=0),
                               np.max(windows[1], axis=0))
        max_value = np.maximum(max_value, cutoff) + state

        min_value = np.minimum(np.min(windows[0], axis=0),
                               np.min(windows[1], axis=0))
        min_value = np.minimum(min_value, cutoff) + state

        return (max_value, min_value), state + 1

    multi_window(func=func_multi_window_with_state_and_args,
                 in_streams=[x, a],
                 out_streams=[r, s],
                 state=0,
                 window_size=2,
                 step_size=2,
                 cutoff=10)
    #----------------------------------------------------------------

    x.extend(np.array([[1., 5.], [7., 11.]]))
    a.extend(np.array([[0., 1.], [2., 3.]]))
    scheduler.step()
    # sum_array_list is the sum of x with window size, step size of 2
    assert np.array_equal(sum_array_list, [np.array([1. + 7., 5. + 11.])])
    # y is the mean of x with window size and step size of 2
    assert np.array_equal(recent_values(x), np.array([[1., 5.], [7., 11.]]))
    assert np.array_equal(recent_values(y),
                          np.array([[(1. + 7.) / 2.0, (5. + 11.) / 2.]]))
    assert np.array_equal(recent_values(z), np.array([[1. + 7., 5. + 11]]))
    assert np.array_equal(recent_values(b),
                          [np.array([1. + 7. + 0. + 2., 5. + 11. + 1. + 3.])])
    assert np.array_equal(recent_values(c), np.array([[8., 16.]]))
    assert np.array_equal(recent_values(d), np.array([[7., 11.]]))
    assert np.array_equal(recent_values(r), np.array([[10., 11.]]))
    assert np.array_equal(recent_values(s), np.array([[0., 1.]]))

    a.extend(np.array([[0., 1.], [1., 0.]]))
    scheduler.step()
    assert np.array_equal(recent_values(y),
                          np.array([[(1. + 7.) / 2.0, (5. + 11.) / 2.]]))
    assert np.array_equal(recent_values(z), np.array([[1. + 7., 5. + 11]]))
    assert np.array_equal(recent_values(b),
                          [np.array([1. + 7. + 0. + 2., 5. + 11. + 1. + 3.])])
    assert np.array_equal(recent_values(c), np.array([[8., 16.]]))
    assert np.array_equal(recent_values(d), np.array([[7., 11.]]))
    assert np.array_equal(recent_values(r), np.array([[10., 11.]]))
    assert np.array_equal(recent_values(s), np.array([[0., 1.]]))

    x.extend(np.array([[14., 18.], [18., 30.], [30., 38.], [34., 42.]]))
    scheduler.step()
    assert np.array_equal(recent_values(y),
                          np.array([[4., 8.], [16., 24.], [32., 40.]]))
    assert np.array_equal(recent_values(z),
                          np.array([[8., 16.], [33., 49.], [66., 82.]]))
    assert np.array_equal(recent_values(c),
                          np.array([[8., 16.], [33., 49.], [66., 82.]]))
    assert np.array_equal(recent_values(d),
                          np.array([[7., 11.], [19., 31.], [36., 44.]]))
    assert np.array_equal(recent_values(r), np.array([[10., 11.], [19., 31.]]))
    assert np.array_equal(recent_values(s), np.array([[0., 1.], [1., 1.]]))

    print 'TEST OF OP (WINDOW) IS SUCCESSFUL'

    return
Пример #5
0
def test_some_merge_agents():
    import numpy as np
    scheduler = Stream.scheduler

    #----------------------------------------------------
    # Declare streams
    s = Stream('s')
    t = Stream('t')
    u = Stream('u')
    v_stream = Stream('v')
    x = Stream('x')

    #----------------------------------------------------
    # Define functions
    def g(lst):
        return sum(lst)

    def g_args(lst, multiplier):
        return sum(lst) * multiplier

    def general_f(lst, f):
        return f(lst)

    def fff(lst, f, addend):
        return f(lst, addend)

    def hhh(lst, addend):
        return sum(lst) + addend

    #----------------------------------------------------
    # Define agents
    d = zip_map(func=sum, in_streams=[x, u], out_stream=s, name='d')

    def magnitude(vector):
        return math.sqrt(sum([w * w for w in vector]))

    ssssss = Stream()
    ddd = zip_map(func=magnitude, in_streams=[x, u], out_stream=ssssss)
    zipxu = zip_stream_f([x, u])
    zip_map_xu = zip_map_f(sum, [x, u])
    zip_map_xu_merge = Stream('zip map xu merge')
    zip_map(sum, [x, u], zip_map_xu_merge)
    zip_map_g_args = zip_map_f(g_args, [x, u], multiplier=2)
    dd = zip_map(func=general_f,
                 in_streams=[x, u],
                 out_stream=t,
                 name='dd',
                 f=np.mean)
    zip_map_ss = zip_map_f(np.mean, [x, u])
    dddd = zip_map(func=fff,
                   in_streams=[x, u],
                   out_stream=v_stream,
                   name='dddd',
                   f=hhh,
                   addend=10)

    #----------------------------------------------------
    #----------------------------------------------------
    # Append values to stream
    x.extend(list(range(3)))
    u.extend([10, 15, 18])
    scheduler.step()
    assert recent_values(s) == [10, 16, 20]
    assert recent_values(zip_map_g_args) == [2 * v for v in recent_values(s)]
    assert recent_values(zipxu) == [(0, 10), (1, 15), (2, 18)]
    assert recent_values(t) == [5, 8, 10]
    assert recent_values(zip_map_ss) == [5.0, 8.0, 10.0]
    assert recent_values(v_stream) == [20, 26, 30]
    assert recent_values(zip_map_xu) == s.recent[:s.stop]
    assert recent_values(zip_map_xu) == recent_values(zip_map_xu_merge)

    #----------------------------------------------------
    u.append(37)
    x.extend(list(range(3, 5, 1)))
    scheduler.step()
    assert recent_values(s) == [10, 16, 20, 40]
    assert recent_values(zip_map_g_args) == [2 * v for v in recent_values(s)]
    assert recent_values(zipxu) == [(0, 10), (1, 15), (2, 18), (3, 37)]
    assert recent_values(t) == [5, 8, 10, 20]
    assert recent_values(v_stream) == [20, 26, 30, 50]
    assert recent_values(zip_map_xu) == recent_values(zip_map_xu_merge)
    assert recent_values(ssssss) == [
        10.0, 15.033296378372908, 18.110770276274835, 37.12142238654117
    ]

    #----------------------------------------------------
    u.extend([96, 95])
    scheduler.step()
    assert recent_values(s) == [10, 16, 20, 40, 100]
    assert recent_values(zipxu) == [(0, 10), (1, 15), (2, 18), (3, 37),
                                    (4, 96)]
    assert recent_values(t) == [5, 8, 10, 20, 50]
    assert recent_values(v_stream) == [20, 26, 30, 50, 110]
    assert recent_values(zip_map_xu) == recent_values(zip_map_xu_merge)

    #----------------------------------------------------
    # TEST MERGE_ASYNCH AND MIX
    #----------------------------------------------------

    x = Stream('x')
    y = Stream('y')
    z = Stream('z')
    w = Stream('w')

    def g_asynch(pair):
        index, value = pair
        if index == 0:
            return value * 10
        elif index == 1:
            return value * 2
        else:
            raise Exception()

    merge_asynch(func=lambda v: v, in_streams=[x, y], out_stream=z)
    merge_asynch(func=g_asynch, in_streams=[x, y], out_stream=w)
    mix_z = mix_f([x, y])
    scheduler.step()
    assert recent_values(z) == []
    assert recent_values(mix_z) == []
    assert recent_values(w) == []

    x.append(10)
    scheduler.step()
    assert recent_values(z) == [(0, 10)]
    assert recent_values(mix_z) == recent_values(z)
    assert recent_values(w) == [100]

    y.append('A')
    scheduler.step()
    assert recent_values(z) == [(0, 10), (1, 'A')]
    assert recent_values(mix_z) == recent_values(z)
    assert recent_values(w) == [100, 'AA']

    y.append('B')
    scheduler.step()
    assert recent_values(z) == [(0, 10), (1, 'A'), (1, 'B')]
    assert recent_values(mix_z) == recent_values(z)
    assert recent_values(w) == [100, 'AA', 'BB']

    x.append(20)
    scheduler.step()
    assert recent_values(z) == [(0, 10), (1, 'A'), (1, 'B'), (0, 20)]
    assert recent_values(z) == recent_values(mix_z)
    assert recent_values(w) == [100, 'AA', 'BB', 200]

    fahrenheit = Stream('fahrenheit')
    celsius = Stream('celsius')

    def fahrenheit_and_celsius(pair):
        index, value = pair
        if index == 0:
            return (value - 32.0) / 1.8
        elif index == 1:
            return value
        else:
            raise Exception()

    fahrenheit_stream = Stream('fahrenheit temperatures')
    celsius_stream = Stream('celsius temperatures')
    centigrade_stream = Stream('centigrade temperatures')

    merge_asynch(func=fahrenheit_and_celsius,
                 in_streams=[fahrenheit_stream, celsius_stream],
                 out_stream=centigrade_stream)

    fahrenheit_stream.append(32)
    scheduler.step()
    assert recent_values(centigrade_stream) == [0.0]

    fahrenheit_stream.append(50)
    scheduler.step()
    assert recent_values(centigrade_stream) == [0.0, 10.0]

    fahrenheit_stream.append(68)
    scheduler.step()
    assert recent_values(centigrade_stream) == [0.0, 10.0, 20.0]

    celsius_stream.append(-10.0)
    scheduler.step()
    assert recent_values(centigrade_stream) == [0.0, 10.0, 20.0, -10.0]

    #----------------------------------------------------
    # TEST BLEND
    #----------------------------------------------------

    x = Stream('x')
    y = Stream('y')
    z = Stream('z')
    z_addend = Stream('z_addend')

    def double(v):
        return 2 * v

    def double_add(v, addend):
        return 2 * v + addend

    blend(func=double, in_streams=[x, y], out_stream=z)
    blend(func=double, in_streams=[x, y], out_stream=z_addend)
    blend_z = blend_f(double, [x, y])
    blend_add_z = blend_f(double_add, [x, y], addend=10)

    x.append(1)
    scheduler.step()
    assert recent_values(z) == [2]
    assert recent_values(blend_z) == recent_values(z)
    assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)]

    x.extend(list(range(2, 4)))
    scheduler.step()
    assert recent_values(z) == [2, 4, 6]
    assert recent_values(blend_z) == recent_values(z)
    assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)]

    y.extend(list(range(100, 102)))
    scheduler.step()
    assert recent_values(z) == [2, 4, 6, 200, 202]
    assert recent_values(blend_z) == recent_values(z)
    assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)]

    x.extend([10, 20])
    scheduler.step()
    assert recent_values(z) == [2, 4, 6, 200, 202, 20, 40]
    assert recent_values(blend_z) == recent_values(z)
    assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)]

    #----------------------------------------------------
    # TEST MANY
    #----------------------------------------------------

    # func operates on a list with one element for each input stream.
    # func returns a list with one element for each output stream.
    def f_many(lst):
        return [sum(lst), sum(lst) + 1]

    u_stream = Stream(name='u_stream')
    v_stream = Stream(name='v_stream')
    w_stream = Stream(name='w_stream')
    x_stream = Stream(name='x_stream')

    multi_agent = multi_element(func=f_many,
                                in_streams=[u_stream, v_stream],
                                out_streams=[w_stream, x_stream],
                                name='multi_agent')
    ww_stream, xx_stream = multi_element_f(func=f_many,
                                           in_streams=[u_stream, v_stream],
                                           num_out_streams=2)

    u_stream.extend(list(range(5)))
    v_stream.extend(list(range(0, 40, 4)))
    scheduler.step()
    assert recent_values(w_stream) == [0, 5, 10, 15, 20]
    assert recent_values(x_stream) == [1, 6, 11, 16, 21]
    assert recent_values(ww_stream) == recent_values(w_stream)
    assert recent_values(xx_stream) == recent_values(x_stream)

    # ------------------------------------
    # Test many with args and kwargs
    # func operates on a list with one element for each input stream.
    # func returns a list with one element for each output stream.
    def f_multi_args_kwargs(lst, multiplicand, addend):
        return sum(lst) * multiplicand, sum(lst) + addend

    u_args_kwargs_stream = Stream(name='u_args_kwargs_stream')
    v_args_kwargs_stream = Stream(name='v_args_kwargs_stream')
    w_args_kwargs_stream = Stream(name='w_args_kwargs_stream')
    x_args_kwargs_stream = Stream(name='x_args_kwargs_stream')

    multi_args_kwargs_agent = multi_element(
        func=f_multi_args_kwargs,
        in_streams=[u_args_kwargs_stream, v_args_kwargs_stream],
        out_streams=[w_args_kwargs_stream, x_args_kwargs_stream],
        name='multi_args_kwargs_agent',
        multiplicand=2,
        addend=10)
    ww_args_kwargs_stream, xx_args_kwargs_stream = multi_element_f(
        func=f_multi_args_kwargs,
        in_streams=[u_args_kwargs_stream, v_args_kwargs_stream],
        num_out_streams=2,
        multiplicand=2,
        addend=10)
    assert (recent_values(ww_args_kwargs_stream) == recent_values(
        w_args_kwargs_stream))
    assert (recent_values(xx_args_kwargs_stream) == recent_values(
        x_args_kwargs_stream))

    u_args_kwargs_stream.extend(list(range(5)))
    v_args_kwargs_stream.extend(list(range(0, 40, 4)))
    scheduler.step()
    assert recent_values(w_args_kwargs_stream) == [0, 10, 20, 30, 40]
    assert recent_values(x_args_kwargs_stream) == [10, 15, 20, 25, 30]
    assert (recent_values(ww_args_kwargs_stream) == recent_values(
        w_args_kwargs_stream))
    assert (recent_values(xx_args_kwargs_stream) == recent_values(
        x_args_kwargs_stream))

    u_args_kwargs_stream.append(100)
    v_args_kwargs_stream.extend(list(range(40, 80, 4)))
    scheduler.step()
    assert recent_values(w_args_kwargs_stream) == \
      [0, 10, 20, 30, 40, 240]
    assert recent_values(x_args_kwargs_stream) == \
      [10, 15, 20, 25, 30, 130]
    assert (recent_values(ww_args_kwargs_stream) == recent_values(
        w_args_kwargs_stream))
    assert (recent_values(xx_args_kwargs_stream) == recent_values(
        x_args_kwargs_stream))

    u_args_kwargs_stream.extend([200, 300])
    scheduler.step()
    v_args_kwargs_stream.append(100)
    scheduler.step()
    assert recent_values(w_args_kwargs_stream) == \
      [0, 10, 20, 30, 40, 240, 448, 656]
    assert recent_values(x_args_kwargs_stream) == \
      [10, 15, 20, 25, 30, 130, 234, 338]
    assert (recent_values(ww_args_kwargs_stream) == recent_values(
        w_args_kwargs_stream))
    assert (recent_values(xx_args_kwargs_stream) == recent_values(
        x_args_kwargs_stream))

    #----------------------------------------------------
    #----------------------------------------------------
    # TEST STREAM ARRAY
    #----------------------------------------------------
    #----------------------------------------------------

    #----------------------------------------------------
    # Test zip_map with StreamArray
    #----------------------------------------------------
    x = StreamArray('x')
    y = StreamArray('y')
    z = StreamArray('z')
    a = StreamArray('a')

    def sum_array_axis_0(a_list_of_arrays):
        return np.sum(a_list_of_arrays, axis=0)

    merge_list(func=sum_array_axis_0, in_streams=[x, y], out_stream=z)

    def mean_array_axis_0(a_list_of_arrays):
        return np.mean(a_list_of_arrays, axis=0)

    zip_map_list(func=mean_array_axis_0, in_streams=[x, y], out_stream=a)

    x.extend(np.linspace(0.0, 9.0, 10))
    scheduler.step()
    y.extend(np.linspace(0.0, 4.0, 5))
    scheduler.step()
    expected_array = np.sum(
        [np.linspace(0.0, 4.0, 5),
         np.linspace(0.0, 4.0, 5)], axis=0)
    assert isinstance(z, StreamArray)
    assert np.array_equal(recent_values(z), expected_array)
    expected_means = np.linspace(0.0, 4.0, 5)
    assert np.array_equal(recent_values(a), expected_means)

    #----------------------------------------------------
    # Test blend with StreamArray
    #----------------------------------------------------
    x = StreamArray('x')
    y = StreamArray('y')
    z = StreamArray('z')
    a = StreamArray('a')

    def double(v):
        return 2 * v

    def double_add(v, addend):
        return 2 * v + addend

    ## blend(func=double, in_streams=[x, y], out_stream=z)
    ## blend(func=double_add, in_streams=[x, y], out_stream=a, addend=10.0)

    ## x.append(np.array(1.0))
    ## scheduler.step()
    ## assert np.array_equal(recent_values(z), np.array([2.0]))
    ## assert np.array_equal(recent_values(a), recent_values(z)+10.0)

    ## x.extend(np.linspace(2.0, 3.0, 2))
    ## scheduler.step()
    ## assert np.array_equal(recent_values(z), np.array([2., 4., 6.]))
    ## assert np.array_equal(recent_values(a), recent_values(z)+10.0)

    ## y.extend(np.linspace(100.0, 101.0, 2))
    ## scheduler.step()
    ## assert np.array_equal(recent_values(z), [2., 4., 6., 200., 202.])
    ## assert np.array_equal(recent_values(a), recent_values(z)+10.0)

    ## x.extend([10., 20.])
    ## scheduler.step()
    ## assert np.array_equal(recent_values(z), [2., 4., 6., 200., 202., 20., 40.])
    ## assert np.array_equal(recent_values(a), recent_values(z)+10.0)

    #----------------------------------------------------
    # Test merge_asynch with StreamArray
    #----------------------------------------------------

    x = StreamArray('x')
    y = StreamArray('y')
    dt_0 = np.dtype([('time', int), ('value', float)])
    z = StreamArray('z', dimension=2)

    merge_asynch(func=lambda v: v, in_streams=[x, y], out_stream=z)
    scheduler.step()
    assert np.array_equal(recent_values(z), np.empty(shape=(0, 2)))

    x.append(np.array(10.0))
    scheduler.step()
    assert np.array_equal(recent_values(z), np.array([(0, 10.0)]))

    y.append(np.array(1.0))
    scheduler.step()
    assert np.array_equal(recent_values(z), [(0, 10.), (1, 1.0)])

    y.append(np.array(2.0))
    scheduler.step()
    assert np.array_equal(recent_values(z), [(0, 10.), (1, 1.0), (1, 2.0)])

    x.append(np.array(20.0))
    scheduler.step()
    assert np.array_equal(recent_values(z), [(0, 10.), (1, 1.), (1, 2.),
                                             (0, 20.)])

    #----------------------------------------------------------------
    # Test window merge agent with no state
    r = Stream('r')
    w = Stream('w')
    x = Stream('x')
    a = Stream('a')

    def h(list_of_windows):
        return sum([sum(window) for window in list_of_windows])

    merge_window(func=h,
                 in_streams=[r, w],
                 out_stream=x,
                 window_size=3,
                 step_size=3)
    merge_stream = merge_window_f(func=h,
                                  in_streams=[r, w],
                                  window_size=3,
                                  step_size=3)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test window merge agent with state
    def h_with_state(list_of_windows, state):
        return (sum([sum(window)
                     for window in list_of_windows]) + state, state + 1)

    merge_window(func=h_with_state,
                 in_streams=[r, w],
                 out_stream=a,
                 window_size=3,
                 step_size=3,
                 state=0)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    r.extend(list(range(16)))
    scheduler.step()
    assert recent_values(r) == list(range(16))
    assert recent_values(x) == []
    assert recent_values(merge_stream) == recent_values(x)
    assert recent_values(a) == []

    w.extend([10, 12, 14, 16, 18])
    scheduler.step()
    assert recent_values(r) == list(range(16))
    assert recent_values(w) == [10, 12, 14, 16, 18]
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14)]
    assert recent_values(a) == [39]

    #----------------------------------------------------------------
    r.extend([10, -10, 21, -20])
    scheduler.step()
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14)]
    assert recent_values(a) == [39]

    #----------------------------------------------------------------
    w.append(20)
    scheduler.step()
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14),
                                (3 + 4 + 5) + (16 + 18 + 20)]
    assert recent_values(a) == [39, 67]

    #----------------------------------------------------------------
    r.extend([-1, 1, 0])
    scheduler.step()
    assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14),
                                (3 + 4 + 5) + (16 + 18 + 20)]
    assert recent_values(a) == [39, 67]

    #----------------------------------------------------------------
    # TEST MERGE_WINDOW WITH STREAM ARRAY
    #----------------------------------------------------------------
    x = StreamArray('x', dimension=2)
    b = StreamArray('b', dimension=2)
    a = StreamArray('a', dimension=2)

    #----------------------------------------------------------------
    # Test window merge agent with state
    def h_array(list_of_windows, state):
        return (sum([sum(window)
                     for window in list_of_windows]) + state, state + 1)

    merge_window(func=h_array,
                 in_streams=[x, a],
                 out_stream=b,
                 window_size=2,
                 step_size=2,
                 state=0)
    #----------------------------------------------------------------
    x.extend(np.array([[1., 5.], [7., 11.]]))
    a.extend(np.array([[0., 1.], [2., 3.]]))
    scheduler.step()
    np.array_equal(recent_values(b), np.empty(shape=(0, 2)))

    a.extend(np.array([[0., 1.], [1., 0.]]))
    scheduler.step()
    np.array_equal(recent_values(b), np.empty(shape=(0, 2)))

    x.extend(np.array([[14., 18.], [18., 30.], [30., 38.], [34., 42.]]))
    scheduler.step()

    #-------------------------------------------------------------------
    # TEST MERGE_LIST
    #-------------------------------------------------------------------
    # Function g  operates on a list of lists, one list for each input
    # stream, to return a single list for the output stream.
    x = Stream('x list merge')
    u = Stream('u list merge')
    s = Stream('s list merge')

    def g(list_of_lists):
        return [sum(snapshot) for snapshot in list(zip(*list_of_lists))]

    d = merge_list(func=g, in_streams=[x, u], out_stream=s, name='d')
    ss = merge_list_f(g, [x, u])
    x.extend(list(range(4)))
    u.extend(list(range(10, 20, 2)))
    scheduler.step()
    assert recent_values(x) == [0, 1, 2, 3]
    assert recent_values(u) == [10, 12, 14, 16, 18]
    assert recent_values(s) == [10, 13, 16, 19]

    x = StreamArray()
    y = StreamArray()
    z = StreamArray(dtype='bool')

    def f(two_lists):
        return np.array(two_lists[0]) > np.array(two_lists[1])

    merge_list(f, [x, y], z)
    x.extend(np.array([3.0, 5.0, 7.0]))
    y.extend(np.array([4.0, 3.0, 10.0]))
    run()
Пример #6
0
def test_element_simple():
    m = Stream('m')
    n = Stream('n')
    o = Stream('o')
    q = Stream('q')
    r = Stream('r')
    s = Stream('s')
    t = Stream('t')
    u = Stream('u')
    v = Stream('v')
    w = Stream('w')
    x = Stream('x')
    y = Stream('y')
    z = Stream('z')

    #----------------------------------------------------------------
    # Test simple map using map_element
    # func operates on an element of the input stream and returns an element of
    # the output stream.
    def double(v):
        return 2 * v

    a = map_element(func=double, in_stream=x, out_stream=y, name='a')
    ymap = map_element_f(func=double, in_stream=x)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test filtering
    def filtering(v):
        return v <= 2

    # yfilter is a stream consisting of those elements in stream x with
    # values greater than 2.
    # The elements of stream x that satisfy the boolean, filtering(), are
    # filtered out.
    yfilter = filter_element_f(func=filtering, in_stream=x)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test map with state using map_element
    # func operates on an element of the input stream and state and returns an
    # element of the output stream and the new state.
    def f(x, state):
        return x + state, state + 2

    b = map_element(func=f, in_stream=x, out_stream=z, state=0, name='b')
    bmap = map_element_f(func=f, in_stream=x, state=0)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test map with call streams
    # The agent executes a state transition when a value is added to call_streams.
    c = map_element(func=f,
                    in_stream=x,
                    out_stream=v,
                    state=10,
                    call_streams=[w],
                    name='c')

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test _no_value
    # func returns _no_value to indicate that no value
    # is placed on the output stream.
    def f_no_value(v):
        """ Filters out odd values
        """
        if v % 2:
            # v is odd. So filter it out.
            return _no_value
        else:
            # v is even. So, keep it in the output stream.
            return v

    no_value_stream = Stream(name='no_value_stream')
    no_value_agent = map_element(func=f_no_value,
                                 in_stream=x,
                                 out_stream=no_value_stream,
                                 name='no_value_agent')

    no_value_map = map_element_f(func=f_no_value, in_stream=x)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test _multivalue
    # func returns _multivalue(output_list) to indicate that
    # the list of elements in output_list should be placed in the
    # output stream.
    def f_multivalue(v):
        if v % 2:
            return _no_value
        else:
            return _multivalue([v, v * 2])

    multivalue_stream = Stream('multivalue_stream')
    multivalue_agent = map_element(func=f_multivalue,
                                   in_stream=x,
                                   out_stream=multivalue_stream,
                                   name='multivalue_agent')
    multivalue_map = map_element_f(func=f_multivalue, in_stream=x)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test map_element with args
    def function_with_args(x, multiplicand, addition):
        return x * multiplicand + addition

    ## EXPLANATION FOR agent BELOW
    ## agent_test_args = map_element(
    ##     func=function_with_args, in_stream = x, out_stream=r,
    ##     state=None, call_streams=None, name='agent_test_args',
    ##     multiplicand=2, addition=10)

    agent_test_args = map_element(function_with_args, x, r, None, None,
                                  'agent_test_args', 2, 10)
    stream_test_args = map_element_f(function_with_args, x, None, 2, 10)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test map_element with kwargs
    agent_test_kwargs = map_element(func=function_with_args,
                                    in_stream=x,
                                    out_stream=u,
                                    state=None,
                                    call_streams=None,
                                    name='agent_test_kwargs',
                                    multiplicand=2,
                                    addition=10)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test map_element with state and kwargs
    # func operates on an element of the input stream and state and returns an
    # element of the output stream and the new state.
    def f_map_args_kwargs(u, state, multiplicand, addend):
        return u * multiplicand + addend + state, state + 2

    agent_test_kwargs_and_state = map_element(
        func=f_map_args_kwargs,
        in_stream=x,
        out_stream=s,
        state=0,
        name='agent_test_kwargs_and_state',
        multiplicand=2,
        addend=10)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test map_element with state and args
    aa_map_args_agent = map_element(f_map_args_kwargs, x, t, 0, None,
                                    'aa_map_args_agent', 2, 10)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test filter_element
    def is_even_number(v):
        return not v % 2

    filter_element(func=is_even_number, in_stream=x, out_stream=q)

    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test filter_element with state
    def less_than_n(v, state):
        return v <= state, state + 1

    x0 = Stream('x0')
    q0 = Stream('q0')
    # state[i] = i
    # Discard elements in x0 where x0[i] <= state[i]
    filter_element(func=less_than_n, in_stream=x0, out_stream=q0, state=0)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test filter_element_stream
    # p is a stream consisting of odd-numbered elements of x
    # Even-numbered elements are filtered out.
    p = filter_element_f(is_even_number, x)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # Test cycles in the module connection graph
    filter_element(func=lambda v: v >= 5, in_stream=o, out_stream=n)
    map_element(func=lambda v: v + 2, in_stream=n, out_stream=o)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    # PUT VALUES INTO STREAMS
    #----------------------------------------------------------------
    #   FIRST STEP
    x.extend(range(3))
    x0.extend([0, 1, 3, 3, 6, 8])
    n.append(0)
    scheduler = Stream.scheduler
    scheduler.step()
    assert recent_values(x) == [0, 1, 2]
    assert recent_values(y) == [0, 2, 4]
    assert recent_values(q0) == [3, 6, 8]
    assert recent_values(ymap) == recent_values(y)
    assert recent_values(yfilter) == []
    assert recent_values(z) == [0, 3, 6]
    assert recent_values(bmap) == recent_values(z)
    assert recent_values(v) == []
    assert recent_values(no_value_stream) == [0, 2]
    assert recent_values(no_value_map) == recent_values(no_value_stream)
    assert recent_values(multivalue_stream) == [0, 0, 2, 4]
    assert recent_values(multivalue_map) == recent_values(multivalue_stream)
    assert recent_values(r) == [10, 12, 14]
    assert recent_values(stream_test_args) == recent_values(r)
    assert recent_values(u) == recent_values(r)
    assert recent_values(s) == [10, 14, 18]
    assert recent_values(s) == recent_values(t)
    assert recent_values(q) == [1]
    assert recent_values(q) == recent_values(p)
    assert recent_values(n) == [0, 2, 4]
    assert recent_values(o) == [2, 4, 6]
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    x.extend(range(3, 5, 1))
    scheduler.step()
    assert recent_values(x) == [0, 1, 2, 3, 4]
    assert recent_values(y) == [0, 2, 4, 6, 8]
    assert recent_values(ymap) == recent_values(y)
    assert recent_values(yfilter) == [3, 4]
    assert recent_values(z) == [0, 3, 6, 9, 12]
    assert recent_values(bmap) == recent_values(z)
    assert recent_values(no_value_stream) == [0, 2, 4]
    assert recent_values(no_value_map) == recent_values(no_value_stream)
    assert recent_values(multivalue_stream) == [0, 0, 2, 4, 4, 8]
    assert recent_values(multivalue_map) == recent_values(multivalue_stream)
    assert recent_values(r) == [10, 12, 14, 16, 18]
    assert recent_values(stream_test_args) == recent_values(r)
    assert recent_values(u) == recent_values(r)
    assert recent_values(s) == [10, 14, 18, 22, 26]
    assert recent_values(s) == recent_values(t)
    assert recent_values(q) == [1, 3]
    assert recent_values(q) == recent_values(p)
    #----------------------------------------------------------------

    #----------------------------------------------------------------
    w.append(0)
    scheduler.step()
    assert recent_values(x) == [0, 1, 2, 3, 4]
    assert recent_values(y) == [0, 2, 4, 6, 8]
    assert recent_values(ymap) == recent_values(y)
    assert recent_values(yfilter) == [3, 4]
    assert recent_values(z) == [0, 3, 6, 9, 12]
    assert recent_values(bmap) == recent_values(z)
    assert recent_values(v) == [10, 13, 16, 19, 22]
    assert recent_values(no_value_stream) == [0, 2, 4]
    assert recent_values(no_value_map) == recent_values(no_value_stream)
    assert recent_values(multivalue_stream) == [0, 0, 2, 4, 4, 8]
    assert recent_values(multivalue_map) == recent_values(multivalue_stream)
    assert recent_values(r) == [10, 12, 14, 16, 18]
    assert recent_values(stream_test_args) == recent_values(r)
    assert recent_values(u) == recent_values(r)
    assert recent_values(s) == [10, 14, 18, 22, 26]
    assert recent_values(s) == recent_values(t)
    assert recent_values(q) == [1, 3]
    assert recent_values(q) == recent_values(p)
    #----------------------------------------------------------------

    #------------------------------------------------------------------------------------------------
    #                                     ELEMENT AGENT TESTS FOR STREAM ARRAY
    #------------------------------------------------------------------------------------------------
    import numpy as np

    m = StreamArray('m')
    n = StreamArray('n')
    o = StreamArray('o')

    map_element(func=np.sin, in_stream=m, out_stream=n)
    filter_element(func=lambda v: v <= 0.5, in_stream=n, out_stream=o)
    input_array = np.linspace(0.0, 2 * np.pi, 20)
    m.extend(input_array)
    scheduler.step()
    expected_output = np.sin(input_array)
    assert np.array_equal(recent_values(n), expected_output)
    expected_output = expected_output[expected_output > 0.5]
    assert np.array_equal(recent_values(o), expected_output)
    return