コード例 #1
0
ファイル: basics.py プロジェクト: yongkyunlee/IoTPy
def test_source_file(filename):
    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]
コード例 #2
0
def colorCapuaFictaTest():
    (n11,n12,n13,n14) = (Note(), Note(), Note(), Note())
    (n21,n22,n23,n24) = (Note(), Note(), Note(), Note())
    n11.duration.type = "quarter"
    n11.name = "D"
    n12.duration.type = "quarter"
    n12.name = "E"
    n13.duration.type = "quarter"
    n13.name = "F"
    n14.duration.type = "quarter"
    n14.name = "G"

    n21.name = "C"
    n21.duration.type = "quarter"
    n22.name = "C"
    n22.duration.type = "quarter"
    n23.name = "B"
    n23.octave = 3
    n23.duration.type = "quarter"
    n24.name = "C"
    n24.duration.type = "quarter"

    stream1 = Stream()
    stream1.append([n11, n12, n13, n14])
    stream2 = Stream()
    stream2.append([n21, n22, n23, n24])


    ### Need twoStreamComparer to Work
    capua.evaluateWithoutFicta(stream1, stream2)
    assert n13.editorial.harmonicInterval.name == "d5", n13.editorial.harmonicInterval.name
    capua.evaluateCapuaTwoStreams(stream1, stream2)

    capua.colorCapuaFicta(stream1, stream2, "both")
    assert n13.editorial.harmonicInterval.name == "P5", n13.editorial.harmonicInterval.name

    assert n11.editorial.color == "yellow"
    assert n12.editorial.color == "yellow"
    assert n13.editorial.color == "green"
    assert n14.editorial.color == "yellow"

    assert n11.editorial.harmonicInterval.name == "M2"
    assert n21.editorial.harmonicInterval.name == "M2"

    assert n13.editorial.harmonicInterval.name == "P5"
    assert n13.editorial.misc["noFictaHarmony"] == "perfect cons"
    assert n13.editorial.misc["capua2FictaHarmony"] == "perfect cons"
    assert n13.editorial.misc["capua2FictaInterval"].name == "P5"
    assert n13.editorial.color == "green"
    assert stream1.lily.strip() == r'''\clef "treble" \color "yellow" d'4 \color "yellow" e'4 \ficta \color "green" fis'!4 \color "yellow" g'4'''
コード例 #3
0
ファイル: pattern.py プロジェクト: stackdynamic/coral
 def match(self, token_stream):
     groups = Stream()
     while True:
         e = token_stream.peek()
         if self.filter_func(e):
             groups.append(e)
             next(token_stream)
         else:
             break
         d = token_stream.peek()
         if d == self.delimiter:
             next(token_stream)
         else:
             break
     return Match(True, groups=groups)
コード例 #4
0
ファイル: pattern.py プロジェクト: stackdynamic/coral
 def match(self, token_stream):
     groups = Stream()
     try:
         for i, p in enumerate(self.pattern):
             token = token_stream[i]
             if isinstance(p, TokenType) and token.token_type == p:
                 groups.append(token)
             elif isinstance(p, str) and token == Token(p):
                 continue
             else:
                 break
         else:
             token_stream.consume(self.pattern_len)
             return Match(True, groups=groups)
     except StopIteration:
         pass
     return Match(False)
コード例 #5
0
def sort(lst):

    def flip(I, L):
        i = I[0]
        if lst[i] > lst[i+1]:
            lst[i], lst[i+1] = lst[i+1], lst[i]
            return (1)
        else:
            return (_no_value)

    x = Stream('x')

    for i in range(len(lst) - 1):
        signal_element(func=flip, in_stream=x, out_stream=x, name=i, I=[i], L=lst)
    scheduler = Stream.scheduler
    x.append(1)
    scheduler.step()
コード例 #6
0
def shortest_path(D):
    def triangle_inequality(triple, D):
        i, j, k = triple
        if D[i][j] + D[j][k] < D[i][k]:
            D[i][k] = D[i][j] + D[j][k]
            D[k][i] = D[i][k]
            return(1)
        else:
            return (_no_value)

    x = Stream('x')
    size = len(D)
    for i in range(size):
        for j in range(i):
            for k in range(size):
                signal_element(func=triangle_inequality,
                               in_stream=x, out_stream=x,
                               name=str(i)+"_"+str(j)+"_"+str(k),
                               triple=[i, j, k], D=D)
    scheduler = Stream.scheduler
    x.append(1)
    scheduler.step()
    
    return D
コード例 #7
0
ファイル: window_test.py プロジェクト: yongkyunlee/IoTPy
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(list(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)
    t = StreamArray('t', dimension=2)

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

    def ff(input_array):
        return np.mean(input_array, axis=1)

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

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

    #----------------------------------------------------------------
    # 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(t),
                          np.array([[(1. + 5.) // 2.0, (7. + 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
コード例 #8
0
ファイル: functional_sieve.py プロジェクト: yongkyunlee/IoTPy
def primes_example_2(N):
    """
    Agent used in example 2 in which prime_stream is the sequence of
    primes up to the N-th prime

    Parameters
    ----------
    N: int
       positive integer

    Returns: first_N, prime_stream
    -------
       first_N: list
         The first N primes
       prime_stream: Stream
         Stream of prime numbers. May have more than N primes
      
    
    Notes
    -----
    sieve creates a single sink agent. The sink agent has a single
    input stream, in_stream. The agent encapsulates stateful function
    f which has an initial state of 0. (Sinks have no output streams.)
    
    Let the first element of in_stream be p. This agent assumes that p
    is a prime number. So, the agent appends p to prime_stream. Many
    agents append prime numbers to prime_stream, but at most one agent
    can do so at a time.

    When the agent discovers an element of in_stream that is not a
    multiple of p, the agent creates a new sieve agent which takes a
    new stream out_stream as its input stream. out_stream consists of
    elements of in_stream that are not multiples of p.

    """
    def execute_until_stop_message(v, state, function):
        function_state, finished_execution = state
        if finished_execution:
            return (_no_value, True)
        index, input_value = v
        if index == 1:
            # This value is from stop_stream
            # Make finished_execution become True because a message
            # was received on stop_stream.
            finished_execution = True
            # From now onwards, no messages are appended to the output
            # stream, and finished_execution remains True forever.
            return (_no_value, (function_state, True))
        # index is 0. So, this value is from state_stream.
        output_value, next_function_state = function(input_value,
                                                     function_state)
        # next_state = (next_function_state, finished_execution)
        return output_value, (next_function_state, finished_execution)

    def generate_numbers_until_stop_message(index_and_value, state):
        # state is initially False and switches to True if a message
        # is received in stop_stream. If state becomes True then it
        # remains True thereafter. After state becomes True no values
        # are appended to the output stream.
        # The elements of the input stream are tuples: index and
        # value.
        # index is 0 for state_stream and 1 for stop_stream.
        index, value = index_and_value
        if index == 1:
            # This value is from stop_stream
            # Make state True because a message was received on
            # stop_stream.
            # From now onwards, no messages are appended to the output
            # stream, and state remains True.
            return (_no_value, True)
        # index is 0. So, this value is from state_stream.
        if state:
            # Do not append values to the output stream, and state
            # remains True
            return (_no_value, state)
        else:
            # Append the next value to the output stream, and state
            # remains False.
            return (value + 1, state)

    def detect_finished_then_send_stop(v, state, N):
        length, stop = state
        # If stop is True then computation must stop
        length += 1
        if length >= N and not stop:
            stop = True
            return (True, (length, stop))
        else:
            return (_no_value, (length, stop))

    def first_N_elements(in_stream, N, first_N):
        def first_N_elements_of_stream(v, state, N, first_N):
            if state < N:
                first_N.append(v)
                state += 1
            return state

        sink(func=first_N_elements_of_stream,
             in_stream=in_stream,
             state=0,
             N=N,
             first_N=first_N)

    #-----------------------------------------------------------------
    # Define streams
    #-----------------------------------------------------------------
    state_stream = Stream(name='numbers 2, 3, 4, ...')
    stop_stream = Stream(name='stop!')
    prime_stream = Stream(name='prime numbers')
    first_N = []

    #-----------------------------------------------------------------
    # Define agents
    #-----------------------------------------------------------------
    # Create agent that generates 2, 3, 4... until it receives a
    # message on stop_stream

    ## merge_asynch(func=generate_numbers_until_stop_message,
    ##              in_streams=[state_stream, stop_stream],
    ##              out_stream=state_stream, state=False)
    def g(v, state):
        return v + 1, state

    merge_asynch(func=execute_until_stop_message,
                 in_streams=[state_stream, stop_stream],
                 out_stream=state_stream,
                 state=(None, False),
                 function=g)
    # Create an agent that sieves state_stream to create prime_stream
    # which is a sequence of primes.
    # We do this by creating a sink agent that encapsulates a stateful
    # function f with an initial state of 0. Pass parameters
    # prime_stream and out_stream from the sink agent to its
    # encapsulated function f.
    sieve(in_stream=state_stream, prime_stream=prime_stream)

    # Create an agent that sends a message on stop_stream when the
    # length of prime_stream exceeds N.
    map_element(func=detect_finished_then_send_stop,
                in_stream=prime_stream,
                out_stream=stop_stream,
                state=(0, False),
                N=N)

    first_N_elements(in_stream=prime_stream, N=N, first_N=first_N)

    state_stream.append(2)

    return first_N, prime_stream
コード例 #9
0
def test_list():
    scheduler = Stream.scheduler

    n = Stream('n')
    o = Stream('o')
    p = Stream('p')
    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
    def simple(lst):
        return [2 * v for v in lst]

    a = map_list(func=simple, in_stream=x, out_stream=y, name='a')
    yy = map_list_f(simple, x)

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

    #-------------------------------------------------------------------
    # Test map with state
    # Function that operates on an element and state and returns an
    # element and state.
    def f(input_list, state):
        output_list = [[]] * len(input_list)
        for i in range(len(input_list)):
            output_list[i] = input_list[i] + state
            state += 2
        return output_list, state

    b = map_list(func=f, in_stream=x, out_stream=z, state=0, name='b')
    zz = map_list_f(f, x, 0)
    #-------------------------------------------------------------------

    #-------------------------------------------------------------------
    # Test map with call streams
    c = map_list(func=f,
                 in_stream=x,
                 out_stream=v,
                 state=10,
                 call_streams=[w],
                 name='c')

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

    #-------------------------------------------------------------------
    # Test sink with state
    def sink_with_state(input_list, output_list):
        # sink has no output stream.
        # This function only returns the next state.
        return output_list.extend(input_list)

    out_list = []
    # In this simple example, out_list will be the same as the input
    # stream.
    sink_agent = sink_list(func=sink_with_state,
                           in_stream=x,
                           name='sink_agent',
                           state=out_list)
    out_list_stream = []
    # Function version of the previous agent example
    sink_list_f(sink_with_state, x, out_list_stream)

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

    #-------------------------------------------------------------------
    # Test merge
    # Function that operates on a list of lists
    def g(list_of_lists):
        return [sum(snapshot) for snapshot in 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])

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

    #-------------------------------------------------------------------
    # Test split
    def h(input_list):
        return [[element + 1 for element in input_list],
                [element * 2 for element in input_list]]

    e = split_list(func=h, in_stream=x, out_streams=[r, t], name='e')
    rr, tt = split_list_f(h, x, num_out_streams=2)

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

    #-------------------------------------------------------------------
    # Test split with state
    def h_state(input_list, state):
        length = len(input_list)
        output_list_0 = [[]] * length
        output_list_1 = [[]] * length
        for i in range(length):
            output_list_0[i] = input_list[i] + state
            output_list_1[i] = input_list[i] * state
            state += 1
        return ([output_list_0, output_list_1], state)

    split_list(func=h_state, in_stream=x, out_streams=[p, q], state=0)
    pp, qq = split_list_f(h_state, x, num_out_streams=2, state=0)

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

    #-------------------------------------------------------------------
    # Test many
    def f_many(list_of_lists):
        snapshots = zip(*list_of_lists)
        return [[max(snapshot) for snapshot in snapshots],
                [min(snapshot) for snapshot in snapshots]]

    multi_agent = multi_list(func=f_many,
                             in_streams=[x, u],
                             out_streams=[n, o],
                             name='multi_agent')
    nn, oo = multi_list_f(func=f_many, in_streams=[x, u], num_out_streams=2)
    #-------------------------------------------------------------------

    #-------------------------------------------------------------------
    #-------------------------------------------------------------------
    x.extend(range(5))
    scheduler.step()
    assert recent_values(x) == range(5)
    assert recent_values(y) == [0, 2, 4, 6, 8]
    assert recent_values(z) == [0, 3, 6, 9, 12]
    assert recent_values(v) == []
    assert out_list == range(5)
    assert out_list == out_list_stream
    assert recent_values(s) == []
    assert recent_values(r) == [1, 2, 3, 4, 5]
    assert recent_values(t) == [0, 2, 4, 6, 8]
    assert recent_values(p) == [0, 2, 4, 6, 8]
    assert recent_values(q) == [0, 1, 4, 9, 16]
    assert recent_values(n) == []
    assert recent_values(o) == []
    assert recent_values(y) == recent_values(yy)
    assert recent_values(z) == recent_values(zz)
    assert recent_values(s) == recent_values(ss)
    assert recent_values(r) == recent_values(rr)
    assert recent_values(t) == recent_values(tt)
    assert recent_values(p) == recent_values(pp)
    assert recent_values(q) == recent_values(qq)
    assert recent_values(n) == recent_values(nn)
    assert recent_values(o) == recent_values(oo)

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

    #-------------------------------------------------------------------
    w.append(0)
    scheduler.step()

    assert recent_values(x) == range(5)
    assert recent_values(y) == [0, 2, 4, 6, 8]
    assert recent_values(z) == [0, 3, 6, 9, 12]
    assert recent_values(v) == [10, 13, 16, 19, 22]
    assert out_list == range(5)
    assert recent_values(s) == []
    assert recent_values(r) == [1, 2, 3, 4, 5]
    assert recent_values(t) == [0, 2, 4, 6, 8]
    assert recent_values(p) == [0, 2, 4, 6, 8]
    assert recent_values(q) == [0, 1, 4, 9, 16]
    assert recent_values(n) == []
    assert recent_values(o) == []
    assert recent_values(y) == recent_values(yy)
    assert recent_values(z) == recent_values(zz)
    assert recent_values(s) == recent_values(ss)
    assert recent_values(r) == recent_values(rr)
    assert recent_values(t) == recent_values(tt)
    assert recent_values(p) == recent_values(pp)
    assert recent_values(q) == recent_values(qq)
    assert recent_values(n) == recent_values(nn)
    assert recent_values(o) == recent_values(oo)
    #-------------------------------------------------------------------

    #-------------------------------------------------------------------
    u.extend([10, 15, 18])
    scheduler.step()
    assert recent_values(s) == [10, 16, 20]
    assert recent_values(n) == [10, 15, 18]
    assert recent_values(o) == [0, 1, 2]

    u.append(37)
    scheduler.step()
    assert recent_values(s) == [10, 16, 20, 40]
    assert recent_values(n) == [10, 15, 18, 37]
    assert recent_values(o) == [0, 1, 2, 3]

    u.extend([96, 95])
    scheduler.step()
    assert recent_values(x) == range(5)
    assert recent_values(y) == [0, 2, 4, 6, 8]
    assert recent_values(z) == [0, 3, 6, 9, 12]
    assert recent_values(v) == [10, 13, 16, 19, 22]
    assert out_list == range(5)
    assert recent_values(s) == [10, 16, 20, 40, 100]
    assert recent_values(r) == [1, 2, 3, 4, 5]
    assert recent_values(t) == [0, 2, 4, 6, 8]
    assert recent_values(p) == [0, 2, 4, 6, 8]
    assert recent_values(q) == [0, 1, 4, 9, 16]
    assert recent_values(n) == [10, 15, 18, 37, 96]
    assert recent_values(o) == [0, 1, 2, 3, 4]

    assert recent_values(y) == recent_values(yy)
    assert recent_values(z) == recent_values(zz)
    assert recent_values(s) == recent_values(ss)
    assert recent_values(r) == recent_values(rr)
    assert recent_values(t) == recent_values(tt)
    assert recent_values(p) == recent_values(pp)
    assert recent_values(q) == recent_values(qq)
    assert recent_values(n) == recent_values(nn)
    assert recent_values(o) == recent_values(oo)

    #------------------------------------------------------------------
    #------------------------------------------------------------------
    # Test NumPy arrays: StreamArray
    #------------------------------------------------------------------
    #------------------------------------------------------------------
    # Test list map on StreamArray (dimension is 0).
    a_stream_array = StreamArray(name='a_stream_array')
    b_stream_array = StreamArray(name='b_stream_array')

    def f_np(input_array):
        return input_array + 1

    a_np_agent = map_list(func=f_np,
                          in_stream=a_stream_array,
                          out_stream=b_stream_array,
                          name='a_np_agent')
    bb_stream_array = map_array_f(f_np, a_stream_array)

    scheduler.step()
    assert np.array_equal(recent_values(b_stream_array),
                          np.array([], dtype=np.float64))
    assert np.array_equal(recent_values(b_stream_array),
                          recent_values(bb_stream_array))

    a_stream_array.extend(np.arange(5.0))
    scheduler.step()
    assert np.array_equal(recent_values(b_stream_array), np.arange(5.0) + 1)
    assert np.array_equal(recent_values(b_stream_array),
                          recent_values(bb_stream_array))

    a_stream_array.extend(np.arange(5.0, 10.0, 1.0))
    scheduler.step()
    assert np.array_equal(recent_values(b_stream_array), np.arange(10.0) + 1)
    assert np.array_equal(recent_values(b_stream_array),
                          recent_values(bb_stream_array))

    # Test list map with state on StreamArray (dimension is 0)
    c_stream_array = StreamArray(name='c_stream_array')
    d_stream_array = StreamArray(name='d_stream_array')

    def f_np_state(input_array, state):
        return np.cumsum(input_array) + state, np.sum(input_array)

    b_np_agent = map_list(func=f_np_state,
                          in_stream=c_stream_array,
                          out_stream=d_stream_array,
                          state=0.0,
                          name='b_np_agent')
    dd_stream_array = map_array_f(f_np_state, c_stream_array, state=0.0)
    scheduler.step()
    assert np.array_equal(recent_values(d_stream_array),
                          np.array([], dtype=np.float64))
    assert np.array_equal(recent_values(d_stream_array),
                          recent_values(dd_stream_array))

    c_stream_array.extend(np.arange(5.0))
    scheduler.step()
    assert np.array_equal(d_stream_array.recent[:d_stream_array.stop],
                          np.cumsum(np.arange(5.0)))
    assert np.array_equal(recent_values(d_stream_array),
                          recent_values(dd_stream_array))

    c_stream_array.extend(np.arange(5.0, 10.0, 1.0))
    scheduler.step()
    assert np.array_equal(d_stream_array.recent[:d_stream_array.stop],
                          np.cumsum(np.arange(10.0)))
    assert np.array_equal(recent_values(d_stream_array),
                          recent_values(dd_stream_array))

    # Test list map with positive integer dimension on StreamArray
    e_stream_array = StreamArray(name='e_stream_array', dimension=3)
    f_stream_array = StreamArray(name='f_stream_array', dimension=2)

    def f_np_dimension(input_array):
        output_array = np.zeros([len(input_array), 2])
        output_array[:, 0] = input_array[:, 0] + input_array[:, 1]
        output_array[:, 1] = input_array[:, 2]
        return output_array

    c_np_agent = map_list(func=f_np_dimension,
                          in_stream=e_stream_array,
                          out_stream=f_stream_array,
                          name='c_np_agent')
    e_stream_array.extend(np.array([[1.0, 2.0, 3.0]]))
    scheduler.step()
    assert np.array_equal(f_stream_array.recent[:f_stream_array.stop],
                          np.array([[3.0, 3.0]]))

    e_stream_array.extend(np.array([[4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]))
    scheduler.step()
    assert np.array_equal(f_stream_array.recent[:f_stream_array.stop],
                          np.array([[3.0, 3.0], [9.0, 6.0], [15.0, 9.0]]))

    # Test list map with a dimension which is a tuple of integers.
    g_stream_array = StreamArray(name='g_stream_array', dimension=(2, 2))
    h_stream_array = StreamArray(name='h_stream_array', dimension=(2, 2))

    def f_np_tuple_dimension(input_array):
        return input_array * 2

    d_np_agent = map_list(func=f_np_tuple_dimension,
                          in_stream=g_stream_array,
                          out_stream=h_stream_array,
                          name='d_np_agent')
    a_array = np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]])
    g_stream_array.extend(a_array)
    scheduler.step()
    assert np.array_equal(h_stream_array.recent[:h_stream_array.stop],
                          a_array * 2)

    b_array = np.array([[[9.0, 10.0], [11.0, 12.0]]])
    g_stream_array.extend(b_array)
    scheduler.step()
    assert np.array_equal(h_stream_array.recent[:h_stream_array.stop],
                          np.vstack((a_array, b_array)) * 2)

    # Test list map with a datatype and dimension of 0.
    dt_0 = np.dtype([('time', int), ('value', (float, 3))])
    dt_1 = np.dtype([('time', int), ('value', float)])
    i_stream_array = StreamArray(name='i_stream_array', dtype=dt_0)
    j_stream_array = StreamArray(name='j_stream_array', dtype=dt_1)

    def f_datatype(input_array):
        output_array = np.zeros(len(input_array), dtype=dt_1)
        output_array['time'] = input_array['time']
        output_array['value'] = np.sum(input_array['value'], axis=1)
        return output_array

    e_np_agent = map_list(func=f_datatype,
                          in_stream=i_stream_array,
                          out_stream=j_stream_array,
                          name='e_np_agent')
    c_array = np.array([(1, [2.0, 3.0, 4.0])], dtype=dt_0)
    assert j_stream_array.stop == 0

    i_stream_array.extend(c_array)
    scheduler.step()
    assert np.array_equal(j_stream_array.recent[:j_stream_array.stop],
                          f_datatype(c_array))

    d_array = np.array([(10, [6.0, 7.0, 8.0]), (20, [10.0, 11.0, 12.0])],
                       dtype=dt_0)
    i_stream_array.extend(d_array)
    scheduler.step()
    assert np.array_equal(j_stream_array.recent[:j_stream_array.stop],
                          f_datatype(np.hstack((c_array, d_array))))

    # Test list map with a datatype and positive integer dimension.
    k_stream_array = StreamArray(name='k_stream_array',
                                 dtype=dt_0,
                                 dimension=2)
    l_stream_array = StreamArray(name='l_stream_array', dtype=dt_1)

    def f_datatype_int_dimension(input_array):
        m = len(input_array)
        output_array = np.zeros(m, dtype=dt_1)
        for i in range(m):
            output_array[i]['time'] = np.max(input_array[i]['time'])
            output_array[i]['value'] = np.sum(input_array[i]['value'])
        return output_array

    f_np_agent = map_list(func=f_datatype_int_dimension,
                          in_stream=k_stream_array,
                          out_stream=l_stream_array,
                          name='f_np_agent')
    e_array = np.array([[(1, [2.0, 3.0, 4.0]), (2, [5.0, 6.0, 7.0])]],
                       dtype=dt_0)
    assert l_stream_array.stop == 0

    k_stream_array.extend(e_array)
    scheduler.step()
    assert np.array_equal(l_stream_array.recent[:l_stream_array.stop],
                          f_datatype_int_dimension(e_array))

    f_array = np.array([[(3, [8.0, 9.0, 10.0]), (4, [11.0, 12.0, 13.0])],
                        [(5, [-1.0, 0.0, 1.0]), (6, [-2.0, 2.0, -2.0])]],
                       dtype=dt_0)
    k_stream_array.extend(f_array)
    scheduler.step()
    assert np.array_equal(
        l_stream_array.recent[:l_stream_array.stop],
        f_datatype_int_dimension(np.vstack((e_array, f_array))))

    # Test list map with a datatype and a dimension which is a tuple
    m_stream_array = StreamArray(name='m_stream_array',
                                 dtype=dt_0,
                                 dimension=(2, 2))
    n_stream_array = StreamArray(name='n_stream_array', dtype=dt_1)
    g_np_agent = map_list(func=f_datatype_int_dimension,
                          in_stream=m_stream_array,
                          out_stream=n_stream_array,
                          name='g_np_agent')
    assert n_stream_array.stop == 0

    g_array = np.array(
        [
            # zeroth 2x2 array
            [[(1, [2.0, 3.0, 4.0]), (2, [5.0, 6.0, 7.0])],
             [(3, [8.0, 9.0, 10.0]), (4, [11.0, 12.0, 13.0])]],
            # first 2x2 array
            [[(5, [12.0, 13.0, 14.0]), (6, [15.0, 16.0, 17.0])],
             [(7, [18.0, 19.0, 20.0]), (8, [21.0, 22.0, 23.0])]]
        ],
        dtype=dt_0)
    m_stream_array.extend(g_array)
    scheduler.step()
    assert np.array_equal(n_stream_array.recent[:n_stream_array.stop],
                          f_datatype_int_dimension(g_array))

    h_array = np.array([[[(9, [0.0, 1.0, -1.0]), (10, [2.0, 2.0, -4.0])],
                         [(11, [80.0, -71.0, -9.0]), (15, [0.0, 0.0, 0.0])]]],
                       dtype=dt_0)
    m_stream_array.extend(h_array)
    scheduler.step()
    assert np.array_equal(
        n_stream_array.recent[:n_stream_array.stop],
        f_datatype_int_dimension(np.vstack((g_array, h_array))))

    # Test list merge with StreamArray and no dimension and no data type
    a_in_0 = StreamArray(name='a_in_0')
    a_in_1 = StreamArray(name='a_in_1')
    a_out = StreamArray(name='a_out')

    def a_merge(list_of_lists):
        array_0, array_1 = list_of_lists
        return array_0 + array_1

    a_s_agent = merge_list(func=a_merge,
                           in_streams=[a_in_0, a_in_1],
                           out_stream=a_out,
                           name='a_s_agent')

    assert a_out.stop == 0

    #a_in_0.extend(np.array([1.0, 2.0, 3.0]))
    a_in_0.extend(np.array([1.0, 2.0, 3.0]))
    scheduler.step()
    assert a_out.stop == 0

    a_in_0.extend(np.array([4.0, 5.0, 6.0]))
    scheduler.step()
    assert a_out.stop == 0

    a_in_1.extend(np.array([10.0, 20.0]))
    scheduler.step()
    assert np.array_equal(a_out.recent[:a_out.stop], np.array([11.0, 22.0]))

    a_in_1.extend(np.array([30.0, 40.0]))
    scheduler.step()
    assert np.array_equal(a_out.recent[:a_out.stop],
                          np.array([11.0, 22.0, 33.0, 44.0]))

    # Test list merge with StreamArray and no dimension and data type
    a_in_dt_0 = StreamArray(name='a_in_dt_0', dtype=dt_0)
    a_in_dt_1 = StreamArray(name='a_in_dt_1', dtype=dt_0)
    a_out_dt = StreamArray(name='out', dtype=dt_0)

    def a_merge_dtype(list_of_arrays):
        input_array_0, input_array_1 = list_of_arrays
        output_array = np.zeros(len(input_array_0), dtype=dt_0)
        output_array['time'] = \
          np.max((input_array_0['time'], input_array_1['time']), axis=0)
        output_array['value'] = input_array_0['value'] + input_array_1['value']
        return output_array

    a_s_dt_agent = merge_list(func=a_merge_dtype,
                              in_streams=[a_in_dt_0, a_in_dt_1],
                              out_stream=a_out_dt,
                              name='a_s_dt_agent')
    a_in_dt_0.extend(np.array([(1, [1.0, 2.0, 3.0])], dtype=dt_0))
    scheduler.step()
    assert a_out_dt.stop == 0

    a_in_dt_1.extend(np.array([(2, [10.0, 20.0, 30.0])], dtype=dt_0))
    scheduler.step()
    assert np.array_equal(a_out_dt.recent[:a_out_dt.stop],
                          np.array([(2, [11.0, 22.0, 33.0])], dtype=dt_0))

    a_in_dt_0.extend(
        np.array([(5, [21.0, 23.0, 32.0]), (9, [27.0, 29.0, 31.0])],
                 dtype=dt_0))
    scheduler.step()
    assert np.array_equal(a_out_dt.recent[:a_out_dt.stop],
                          np.array([(2, [11.0, 22.0, 33.0])], dtype=dt_0))

    a_in_dt_1.extend(
        np.array([(6, [19.0, 17.0, 8.0]), (8, [13.0, 11.0, 9.0]),
                  (10, [3.0, 1.0, 5.0])],
                 dtype=dt_0))
    scheduler.step()
    assert np.array_equal(
        a_out_dt.recent[:a_out_dt.stop],
        np.array([(2, [11.0, 22.0, 33.0]), (6, [40.0, 40.0, 40.0]),
                  (9, [40.0, 40.0, 40.0])],
                 dtype=dt_0))

    # Test list split with StreamArray and positive integer dimension and no data type
    dim = 2
    b_in = StreamArray(name='b_in', dimension=dim)
    b_out_0 = StreamArray(name='b_out_0', dimension=dim)
    b_out_1 = StreamArray(name='b_out_1')

    def b_split(array_of_arrays):
        length = len(array_of_arrays)
        output_array_0 = np.zeros((
            length,
            dim,
        ))
        output_array_1 = np.zeros(length)
        for i in range(length):
            input_array = array_of_arrays[i]
            output_array_0[i] = np.array(
                [[np.max(input_array),
                  np.min(input_array)]])
            output_array_1[i] = np.array([np.sum(input_array)])
        return output_array_0, output_array_1

    b_split_agent = split_list(func=b_split,
                               in_stream=b_in,
                               out_streams=[b_out_0, b_out_1],
                               name='b_split_agent')

    b_array_0 = np.array([[1.0, 9.0]])
    b_in.extend(b_array_0)
    scheduler.step()
    assert np.array_equal(b_out_0.recent[:b_out_0.stop], np.array([[9.0,
                                                                    1.0]]))
    assert np.array_equal(b_out_1.recent[:b_out_1.stop], np.array([10.0]))

    b_array_1 = np.array([[98.0, 2.0]])
    b_in.extend(b_array_1)
    scheduler.step()
    assert np.array_equal(b_out_0.recent[:b_out_0.stop],
                          np.array([[9.0, 1.0], [98.0, 2.0]]))
    assert np.array_equal(b_out_1.recent[:b_out_1.stop],
                          np.array([10.0, 100.0]))

    b_array_3 = np.array([[10.0, 20.0], [3.0, 37.0], [55.0, 5.0]])
    b_in.extend(b_array_3)
    scheduler.step()
    assert np.array_equal(
        b_out_0.recent[:b_out_0.stop],
        np.array([[9.0, 1.0], [98.0, 2.0], [20.0, 10.0], [37.0, 3.0],
                  [55.0, 5.0]]))
    assert np.array_equal(b_out_1.recent[:b_out_1.stop],
                          np.array([10.0, 100.0, 30.0, 40.0, 60.0]))

    # Test list many with StreamArray and no dimension and no data type
    c_in_0 = StreamArray(name='c_in_0')
    c_in_1 = StreamArray(name='c_in_1')
    c_out_0 = StreamArray(name='c_out_0')
    c_out_1 = StreamArray(name='c_out_1')

    def c_many(list_of_arrays):
        length = len(list_of_arrays)
        input_array_0, input_array_1 = list_of_arrays
        output_array_0 = np.zeros(length)
        output_array_1 = np.zeros(length)
        output_array_0 = input_array_0 + input_array_1
        output_array_1 = input_array_0 - input_array_1
        return [output_array_0, output_array_1]

    c_multi_agent = multi_list(func=c_many,
                               in_streams=[c_in_0, c_in_1],
                               out_streams=[c_out_0, c_out_1],
                               name='c_multi_agent')
    c_array_0_0 = np.arange(3.0) * 3
    c_array_1_0 = np.arange(3.0)
    c_in_0.extend(c_array_0_0)
    scheduler.step()
    c_in_1.extend(c_array_1_0)
    scheduler.step()
    assert np.array_equal(c_out_0.recent[:c_out_0.stop],
                          np.array([0.0, 4.0, 8.0]))
    assert np.array_equal(c_out_1.recent[:c_out_1.stop],
                          np.array([0.0, 2.0, 4.0]))

    c_array_0_1 = np.array([100.0])
    c_array_1_1 = np.array([4.0, 5.0, 6.0])
    c_in_0.extend(c_array_0_1)
    c_in_1.extend(c_array_1_1)
    scheduler.step()
    assert np.array_equal(c_out_0.recent[:c_out_0.stop],
                          np.array([0.0, 4.0, 8.0, 104.0]))
    assert np.array_equal(c_out_1.recent[:c_out_1.stop],
                          np.array([0.0, 2.0, 4.0, 96.0]))

    ## # Test list many with StreamArray and no dimension and no data type
    ## z_in_0 = StreamArray(name='z_in_0')
    ## z_in_1 = StreamArray(name='z_in_1')
    ## z_out_0 = StreamArray(name='z_out_0')
    ## z_out_1 = StreamArray(name='z_out_1')
    ## def execute_list_of_np_func(v, list_of_np_func):
    ##     length = len(list_of_arrays)
    ##     input_array_0, input_array_1 = list_of_arrays
    ##     output_array_0 = np.zeros(length)
    ##     output_array_1 = np.zeros(length)
    ##     output_array_0 = input_array_0 + input_array_1
    ##     output_array_1 = input_array_0 - input_array_1
    ##     return [output_array_0, output_array_1]

    # Test list many with StreamArray and positive integer dimension and no data type
    dim = 2
    d_in_0 = StreamArray(name='d_in_0', dimension=dim)
    d_in_1 = StreamArray(name='d_in_1', dimension=dim)
    d_out_0 = StreamArray(name='d_out_0', dimension=dim)
    d_out_1 = StreamArray(name='d_out_1')

    def d_many(list_of_arrays):
        length = len(list_of_arrays)
        input_array_0, input_array_1 = list_of_arrays
        output_array_0 = input_array_0 + input_array_1
        output_array_1 = np.array([np.sum(input_array_0 + input_array_1)])
        return output_array_0, output_array_1

    d_multi_agent = multi_list(func=d_many,
                               in_streams=[d_in_0, d_in_1],
                               out_streams=[d_out_0, d_out_1],
                               name='d_multi_agent')

    d_array_0_0 = np.array([[1.0, 2.0]])
    d_array_1_0 = np.array([[0.0, 10.0]])
    d_in_0.extend(d_array_0_0)
    scheduler.step()
    d_in_1.extend(d_array_1_0)
    scheduler.step()
    assert np.array_equal(d_out_0.recent[:d_out_0.stop], np.array([[1.0,
                                                                    12.0]]))
    assert np.array_equal(d_out_1.recent[:d_out_1.stop], np.array([13.0]))

    d_array_0_1 = np.array([[4.0, 8.0]])
    d_array_1_1 = np.array([[2.0, 4.0]])
    d_in_0.extend(d_array_0_1)
    d_in_1.extend(d_array_1_1)
    scheduler.step()
    assert np.array_equal(d_out_0.recent[:d_out_0.stop],
                          np.array([[1.0, 12.0], [6.0, 12.0]]))
    assert np.array_equal(d_out_1.recent[:d_out_1.stop], np.array([13.0,
                                                                   18.0]))

    d_array_0_2 = np.array([[20.0, 30.0], [40.0, 50.0]])
    d_array_1_2 = np.array([[-10.0, -20.0]])
    d_in_0.extend(d_array_0_2)
    d_in_1.extend(d_array_1_2)
    scheduler.step()
    assert np.array_equal(d_out_0.recent[:d_out_0.stop],
                          np.array([[1.0, 12.0], [6.0, 12.0], [10.0, 10.0]]))
    assert np.array_equal(d_out_1.recent[:d_out_1.stop],
                          np.array([13.0, 18.0, 20.0]))

    # Test list many with StreamArray and tuple dimension and no data type
    dim = (2, 2)
    e_in_0 = StreamArray(name='e_in_0', dimension=dim)
    e_in_1 = StreamArray(name='e_in_1', dimension=dim)
    e_out_0 = StreamArray(name='e_out_0', dimension=dim)
    e_out_1 = StreamArray(name='e_out_1')

    def e_many(list_of_arrays):
        input_array_0, input_array_1 = list_of_arrays
        output_array_0 = input_array_0 + input_array_1
        output_array_1 = \
          np.array([np.sum(input_array_0[i]+ input_array_1[i])
                    for i in range(len(input_array_0))])
        return output_array_0, output_array_1

    e_multi_agent = multi_list(func=e_many,
                               in_streams=[e_in_0, e_in_1],
                               out_streams=[e_out_0, e_out_1],
                               name='e_multi_agent')

    e_array_0_0 = np.array([[[10.0, 20.0], [30.0, 40.0]]])
    e_in_0.extend(e_array_0_0)
    e_array_1_0 = np.array([[[1.0, 2.0], [3.0, 4.0]]])
    e_in_1.extend(e_array_1_0)
    scheduler.step()
    assert np.array_equal(e_out_0.recent[:e_out_0.stop],
                          np.array([[[11.0, 22.0], [33.0, 44.0]]]))
    assert np.array_equal(e_out_1.recent[:e_out_1.stop], np.array([110.0]))

    e_array_0_1 = np.array([[[11.0, 13.0], [17.0, 19.0]],
                            [[2.0, 4.0], [6.0, 8.0]]])
    e_in_0.extend(e_array_0_1)
    scheduler.step()
    assert np.array_equal(e_out_0.recent[:e_out_0.stop],
                          np.array([[[11.0, 22.0], [33.0, 44.0]]]))
    assert np.array_equal(e_out_1.recent[:e_out_1.stop], np.array([110.0]))

    e_array_1_1 = np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0,
                                                                    8.0]]])
    e_in_1.extend(e_array_1_1)
    scheduler.step()
    assert np.array_equal(
        e_out_0.recent[:e_out_0.stop],
        np.array([[[11.0, 22.0], [33.0, 44.0]], [[12.0, 15.0], [20.0, 23.0]],
                  [[7.0, 10.0], [13.0, 16.0]]]))
    assert np.array_equal(e_out_1.recent[:e_out_1.stop],
                          np.array([110.0, 70.0, 46.0]))

    e_array_1_2 = np.array([[[11.0, 12.0], [13.0, 14.0]],
                            [[15.0, 16.0], [17.0, 18.0]]])
    e_in_1.extend(e_array_1_2)
    scheduler.step()
    e_array_0_2 = np.array([[[-10.0, -11.0], [12.0, 16.0]],
                            [[-14.0, -15.0], [-16.0, -17.0]]])
    e_in_0.extend(e_array_0_2)
    scheduler.step()
    assert np.array_equal(
        e_out_0.recent[:e_out_0.stop],
        np.array([[[11.0, 22.0], [33.0, 44.0]], [[12.0, 15.0], [20.0, 23.0]],
                  [[7.0, 10.0], [13.0, 16.0]], [[1.0, 1.0], [25.0, 30.0]],
                  [[1.0, 1.0], [1.0, 1.0]]]))
    assert np.array_equal(e_out_1.recent[:e_out_1.stop],
                          np.array([110.0, 70.0, 46.0, 57.0, 4.0]))

    #------------------------------------------------------------------
    #------------------------------------------------------------------
    # Test args and kwargs
    #------------------------------------------------------------------
    #------------------------------------------------------------------
    # Test map

    def map_args(lst, multiplicand):
        return [multiplicand * element for element in lst]

    in_stream_map_args_stream = Stream('in_stream_map_args_stream')
    out_stream_map_args_stream = Stream('out_stream_map_args_stream')
    out_stream_map_kwargs_stream = Stream('out_stream_map_kwargs_stream')

    map_args_agent = map_list(map_args, in_stream_map_args_stream,
                              out_stream_map_args_stream, None, None,
                              'map_args_agent', 2)

    map_kwargs_agent = map_list(func=map_args,
                                in_stream=in_stream_map_args_stream,
                                out_stream=out_stream_map_kwargs_stream,
                                name='map_args_agent',
                                multiplicand=2)
    scheduler.step()
    assert out_stream_map_args_stream.recent[:out_stream_map_args_stream.stop] == \
      []
    assert out_stream_map_kwargs_stream.recent[:out_stream_map_kwargs_stream.stop] == \
      []

    in_stream_map_args_stream.extend(range(5))
    scheduler.step()
    assert out_stream_map_args_stream.recent[:out_stream_map_args_stream.stop] == \
      [0, 2, 4, 6, 8]
    assert out_stream_map_kwargs_stream.recent[:out_stream_map_kwargs_stream.stop] == \
      [0, 2, 4, 6, 8]

    in_stream_map_args_stream.append(5)
    scheduler.step()
    assert out_stream_map_args_stream.recent[:out_stream_map_args_stream.stop] == \
      [0, 2, 4, 6, 8, 10]
    assert out_stream_map_kwargs_stream.recent[:out_stream_map_kwargs_stream.stop] == \
      [0, 2, 4, 6, 8, 10]

    # Test list map on StreamArray (dimension is 0).
    a_stream_array_args = StreamArray(name='a_stream_array_args')
    b_stream_array_args = StreamArray(name='b_stream_array_args')
    c_stream_array_args_kwargs = StreamArray(name='c_stream_array_args_kwargs')

    def f_np_args(input_array_args, addend):
        return input_array_args + addend

    def f_np_args_kwargs(input_array_args_kwargs, multiplicand, addend):
        return input_array_args_kwargs * multiplicand + addend

    a_np_agent_args = map_list(f_np_args, a_stream_array_args,
                               b_stream_array_args, None, None,
                               'a_np_agent_args', 1)

    a_np_agent_args_kwargs = map_list(f_np_args_kwargs,
                                      a_stream_array_args,
                                      c_stream_array_args_kwargs,
                                      None,
                                      None,
                                      'a_np_agent_args_kwargs',
                                      2,
                                      addend=10)
    scheduler.step()
    assert np.array_equal(
        b_stream_array_args.recent[:b_stream_array_args.stop], np.array([]))
    assert np.array_equal(
        c_stream_array_args_kwargs.recent[:c_stream_array_args_kwargs.stop],
        np.array([]))

    a_stream_array_args.extend(np.arange(5.0))
    scheduler.step()
    assert np.array_equal(
        b_stream_array_args.recent[:b_stream_array_args.stop],
        np.arange(5.0) + 1)
    assert np.array_equal(
        c_stream_array_args_kwargs.recent[:c_stream_array_args_kwargs.stop],
        np.arange(5.0) * 2 + 10)

    a_stream_array_args.extend(np.arange(5.0, 10.0, 1.0))
    scheduler.step()
    assert np.array_equal(
        b_stream_array_args.recent[:b_stream_array_args.stop],
        np.arange(10.0) + 1)
    assert np.array_equal(
        c_stream_array_args_kwargs.recent[:c_stream_array_args_kwargs.stop],
        np.arange(10.0) * 2 + 10)

    print 'TEST OF OP (LISTS) IS SUCCESSFUL'
コード例 #10
0
ファイル: kmeans_stream.py プロジェクト: yongkyunlee/IoTPy
def test_kmeans_streams():
    s = Stream()
    t = Stream()
    km = kmeans_stream(n_clusters=2)

    @map_e
    def g(v):
        return km.process_element(v)

    g(in_stream=s, out_stream=t)
    s.append(('add', [1, 2]))
    s.append(('add', [1, 4]))
    s.append(('add', [1, 0]))
    s.append(('add', [10, 4]))
    s.append(('add', [10, 0]))
    s.append(('add', [10, 2]))
    s.append('cluster')
    s.append('show')

    ## s.extend([('add', [1, 2]), ('add', [1, 4]), ('add', [1, 0]),
    ##           ('add', [10, 4]), ('add', [10, 0]), ('add', [10, 2])])
    run()
    print(recent_values(t))
コード例 #11
0
def stop_agent_when_enough_elements(N):
    """
    Shows how shared variables can be used to stop agents.
    One agent generates a sequence until stopped by another agent.

    Parameters
    ----------
    N: int (positive)

    """

    #----------------------------------------------------------------
    # STEP 1. DEFINE FUNCTIONS TO BE ENCAPSULATED
    def generate_numbers(v, state, stop):
        """
        This function generates the sequence 0, 1, 2, ... starting
        with the specified initial state. The function stops execution
        when stop becomes True.

        Parameters
        ----------
        v: The element in the sequence, 0,1,2,.. read from the input
           stream.
        state: The last element of the sequence
        stop: array of length 1. This is a shared variable of the agent.

        """
        if not stop[0]:
            return state, state + 1
        else:
            return _no_value, state

    def call_halt(v, N, stop):
        if v > N:
            stop[0] = True

    #----------------------------------------------------------------
    # STEP 2. CREATE STREAMS AND SHARED VARIABLES
    # stop is a variable shared by both agents that are created
    # below. It is initially False and set to True and then remains
    # True.
    stop = [False]
    numbers = Stream('numbers')

    #----------------------------------------------------------------
    # STEP 3. CREATE AGENTS
    # Create an agent that reads and writes the same stream: numbers.
    # The agent executes its action when a new value appears on
    # numbers. The action puts the next value on numbers if stop is
    # False. The action has no effect (it is a skip operation) if stop
    # is True.
    map_element(func=generate_numbers,
                in_stream=numbers,
                out_stream=numbers,
                state=1,
                stop=stop)
    # Create an agent that sets stop to True after it reads more than
    # N values.
    N = 3
    sink(func=call_halt, in_stream=numbers, N=N, stop=stop)

    #----------------------------------------------------------------
    #STEP 4. START COMPUTATION
    # Get the scheduler and execute a step.
    scheduler = Stream.scheduler
    # Start the computation by putting a value into the numbers stream.
    numbers.append(0)
    scheduler.step()
    # The stream numbers will be 0, 1, ... up to N-1 and possibly may
    # contain additional values. For example, if N = 3 then numbers
    # could be 0, 1, 2 or 0, 1, 2, 3, 4, 5.
    return numbers
    assert list(range(N)) == recent_values(numbers)[:N]
コード例 #12
0
def test_split_agents():
    import numpy as np
    scheduler = Stream.scheduler
    
    s = Stream('s')
    
    u = Stream('u')
    v = Stream('v')
    w = Stream('w')
    
    y = Stream('y')
    z = Stream('z')


    # Test split
    # func operates on a single element of the single input stream and
    # return a list of elements, one for each output stream.
    def h(element):
        return [element+1, element*2]
    def h_args(element, addend, multiplier):
        return [element+addend, element*multiplier]

    in_stream_split = Stream('in_stream_split')
    r = Stream('r')
    t = Stream('t')
    e = split_element(func=h, in_stream=in_stream_split,
                            out_streams=[r, t], name='e')
    r_split, t_split = split_element_f(function=h, in_stream=in_stream_split,
                                    num_out_streams=2, )
    r_args, t_args = split_element_f(
        h_args, in_stream_split, 2, addend=1, multiplier=2)

    scheduler.step()
    assert recent_values(r) == []
    assert recent_values(t) == []
    assert recent_values(r_split) == recent_values(r)
    assert recent_values(t_split) == recent_values(t)
    assert recent_values(r_args) == recent_values(r)
    assert recent_values(t_args) == recent_values(t)

    in_stream_split.extend(range(5))
    scheduler.step()
    assert recent_values(r) == [1, 2, 3, 4, 5]
    assert recent_values(t) == [0, 2, 4, 6, 8]
    assert recent_values(r_split) == recent_values(r)
    assert recent_values(t_split) == recent_values(t)
    assert recent_values(r_args) == recent_values(r)
    assert recent_values(t_args) == recent_values(t)

    in_stream_split.append(10)
    scheduler.step()
    assert recent_values(r) == [1, 2, 3, 4, 5, 11]
    assert recent_values(t) == [0, 2, 4, 6, 8, 20]

    in_stream_split.extend([20, 100])
    scheduler.step()
    assert recent_values(r) == [1, 2, 3, 4, 5, 11, 21, 101]
    assert recent_values(t) == [0, 2, 4, 6, 8, 20, 40, 200]
    assert recent_values(r_split) == recent_values(r)
    assert recent_values(t_split) == recent_values(t)
    assert recent_values(r_args) == recent_values(r)
    assert recent_values(t_args) == recent_values(t)

    # Test split with kwargs
    def f_list(element, list_of_functions):
        return [f(element) for f in list_of_functions]

    def f_0(element):
        return element*2
    def f_1(element):
        return element+10

    x = Stream('x')
    rr = Stream('rr')
    tt = Stream('tt')
    ee = split_element(func=f_list, in_stream=x, out_streams=[rr, tt], name='ee',
                             list_of_functions=[f_0, f_1])
    x.extend(range(5))
    scheduler.step()
    assert recent_values(rr) == [0, 2, 4, 6, 8]
    assert recent_values(tt) == [10, 11, 12, 13, 14]

    # ------------------------------------
    # Test split with state
    # func operates on an element of the single input stream and state.
    # func returns a list with one element for each output stream.
    def h_state(element, state):
        return ([element+state, element*state], state+1)
    r_state = Stream(name='r_state')
    t_state = Stream(name='t_state')
    in_stream_split_state = Stream('in_stream_split_state')
    
    e_state = split_element(
        func=h_state, in_stream=in_stream_split_state,
         out_streams=[r_state, t_state], name='e', state=0)

    scheduler.step()
    assert recent_values(r_state) == []
    assert recent_values(t_state) == []

    in_stream_split_state.extend(range(5))
    scheduler.step()
    assert recent_values(r_state) == [0, 2, 4, 6, 8]
    assert recent_values(t_state) == [0, 1, 4, 9, 16]

    in_stream_split_state.append(20)
    scheduler.step()
    assert recent_values(r_state) == [0, 2, 4, 6, 8, 25]
    assert recent_values(t_state) == [0, 1, 4, 9, 16, 100]

    in_stream_split_state.extend([44, 93])
    scheduler.step()
    assert recent_values(r_state) == [0, 2, 4, 6, 8, 25, 50, 100]
    assert recent_values(t_state) == [0, 1, 4, 9, 16, 100, 264, 651]

    # ------------------------------------
    # Test split with state and args
    
    def hh_state(element, state, increment):
        return ([element+state, element*state], state+increment)
    
    rr_state = Stream(name='rr_state')
    tt_state = Stream(name='tt_state')
    in_stream_split_state_funcargs = Stream('in_stream_split_state_funcargs')

    ee_state_agent = split_element(
        func=hh_state,
        in_stream=in_stream_split_state_funcargs,
        out_streams=[rr_state, tt_state],
        name='ee_state_agent', state=0, increment=10)

    scheduler.step()
    assert recent_values(rr_state) == []
    assert recent_values(tt_state) == []

    in_stream_split_state_funcargs.extend(range(5))
    scheduler.step() 
    assert recent_values(rr_state) == [0, 11, 22, 33, 44]
    assert recent_values(tt_state) == [0, 10, 40, 90, 160]

#------------------------------------------------------------------------------------------------
#                                     UNZIP AGENT TESTS
#------------------------------------------------------------------------------------------------

    s_unzip = Stream('s_unzip')
    u_unzip = Stream('u_unzip')
    x_unzip = Stream('x_unzip')
 
    # ------------------------------------
    # Test unzip
    unzip(in_stream=s_unzip, out_streams=[x_unzip, u_unzip])
    d_unzip_fn = unzip_f(s_unzip, 2) 
 
 
    s_unzip.extend([(1,10), (2,15), (3,18)])
    scheduler.step()
    assert recent_values(x_unzip) == [1, 2, 3]
    assert recent_values(u_unzip) == [10, 15, 18]
    assert recent_values(d_unzip_fn[0]) == x_unzip.recent[:3]
    assert recent_values(d_unzip_fn[1]) == u_unzip.recent[:3]
 
    s_unzip.extend([(37,96)])
    scheduler.step()
    assert recent_values(x_unzip) == [1, 2, 3, 37]
    assert recent_values(u_unzip) == [10, 15, 18, 96]
    assert recent_values(d_unzip_fn[0]) == x_unzip.recent[:4]
    assert recent_values(d_unzip_fn[1]) == u_unzip.recent[:4]


    #------------------------------------------------------------------------------------------------
    #                                     SEPARATE AGENT TESTS
    #------------------------------------------------------------------------------------------------
    s_separate = Stream('s separate')
    u_separate = Stream('u separate')
    x_separate = Stream('x separate')

    d_separate = separate(
        in_stream=s_separate, out_streams=[x_separate,u_separate],
        name='d separate')
    x_sep_func, u_sep_func = separate_f(s_separate, 2)

    s_separate.extend([(0,10), (1,15), (0,20)])
    scheduler.step()
    assert recent_values(x_separate) == [10, 20]
    assert recent_values(u_separate) == [15]
    assert x_sep_func.recent == x_separate.recent
    assert u_sep_func.recent == u_separate.recent

    s_separate.extend([(1,96)])
    scheduler.step()
    assert recent_values(x_separate) == [10, 20]
    assert recent_values(u_separate) == [15, 96]
    assert recent_values(x_sep_func) == recent_values(x_separate)
    assert recent_values(u_sep_func) == recent_values(u_separate)

    #------------------------------------------------------------------------------------------------
    #                                     TIMED_UNZIP TESTS
    #------------------------------------------------------------------------------------------------
    # timed_unzip tests
    t_unzip = Stream()
    a_unzip = Stream('a_unzip')
    b_unzip = Stream('b_unzip')

    timed_unzip(t_unzip, [a_unzip, b_unzip])
    t_unzip_0, t_unzip_1 = timed_unzip_f(in_stream=t_unzip, num_out_streams=2)

    t_unzip.extend(
        [(1, ["A", None]), (5, ["B", "a"]), (7, [None, "b"]),
         (9, ["C", "c"]), (10, [None, "d"])])

    
    scheduler.step()
    assert recent_values(t_unzip_0) == [(1, 'A'), (5, 'B'), (9, 'C')]
    assert recent_values(t_unzip_1) == [(5, 'a'), (7, 'b'), (9, 'c'), (10, 'd')]
    assert recent_values(a_unzip) == recent_values(t_unzip_0)
    assert recent_values(b_unzip) == recent_values(t_unzip_1)


    #------------------------------------------------------------------------------------------------
    #                               TEST SPLIT WITH STREAM_ARRAY
    #------------------------------------------------------------------------------------------------
    # Test split_element with StreamArray
    x = StreamArray('x')
    y = StreamArray('y')
    z = StreamArray('z')

    def h_args(element, addend, multiplier):
            return [element+addend, element*multiplier]

    this_agent = split_element(func=h_args, in_stream=x, out_streams=[y,z],
                                     addend=1.0 , multiplier=2.0, name='this_agent')

    add_to_x = np.linspace(0.0, 4.0, 5)
    x.extend(add_to_x)
    scheduler.step()
    assert np.array_equal(recent_values(y), add_to_x+1.0)
    assert np.array_equal(recent_values(z), add_to_x*2.0)

    # Test separate with StreamArray
    x = StreamArray('x', dimension=2)
    y = StreamArray('y')
    z = StreamArray('z')

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

    x.extend(np.array([[0.0, 2.0], [1.0, 20.0], [0.0, 4.0]]))
    scheduler.step()
    assert np.array_equal(recent_values(z), np.array([10.0, 20.0]))
    assert np.array_equal(recent_values(y), np.array([2.0, 4.0]))

    # ------------------------------------------------------
    # TEST split_list
    # ------------------------------------------------------
    x = Stream('x')
    y = Stream('y')
    z = Stream('z')

    def f(lst):
        return [v*2 for v in lst], [v*10 for v in lst]

    split_list(f, x, [y, z])

    x.extend(range(3))
    scheduler.step()
    assert recent_values(y) == [v*2 for v in recent_values(x)]
    assert recent_values(z) == [v*10 for v in recent_values(x)]

    x.append(100)
    scheduler.step()
    assert recent_values(y) == [v*2 for v in recent_values(x)]
    assert recent_values(z) == [v*10 for v in recent_values(x)]
    

    # ------------------------------------------------------
    # TEST split_window
    # ------------------------------------------------------
    def f(window):
        return max(window), min(window)

    x = Stream('x')
    y = Stream('y')
    z = Stream('z')
    
    split_window(
        func=f, in_stream=x, out_streams=[y, z], window_size=2, step_size=2)

    x.extend(range(7))
    scheduler.step()
    assert recent_values(y) == [1, 3, 5]
    assert recent_values(z) == [0, 2, 4]

    # ------------------------------------------------------
    # TEST split_tuple
    # ------------------------------------------------------
    x = Stream('x')
    y = Stream('y')
    z = Stream('z')
    split_tuple(in_stream=x, out_streams=[y, z])
    x.append((0, 'A'))
    x.extend([(1, 'B'), (2, 'C')])
    scheduler.step()

    print 'TEST OF SPLIT IS SUCCESSFUL'
コード例 #13
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]))
コード例 #14
0
ファイル: merge_test.py プロジェクト: yongkyunlee/IoTPy
def test_timed_mix_agents():
    scheduler = Stream.scheduler

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

    timed_mix([x, y], z)

    x.append((0, 'a'))
    scheduler.step()
    assert recent_values(z) == [(0, (0, 'a'))]

    x.append((1, 'b'))
    scheduler.step()
    assert recent_values(z) == [(0, (0, 'a')), (1, (0, 'b'))]

    y.append((2, 'A'))
    scheduler.step()
    assert recent_values(z) == \
      [(0, (0, 'a')), (1, (0, 'b')), (2, (1, 'A'))]

    y.append((5, 'B'))
    scheduler.step()
    assert recent_values(z) == \
      [(0, (0, 'a')), (1, (0, 'b')), (2, (1, 'A')), (5, (1, 'B'))]

    x.append((3, 'c'))
    scheduler.step()
    assert recent_values(z) == \
      [(0, (0, 'a')), (1, (0, 'b')), (2, (1, 'A')), (5, (1, 'B'))]

    x.append((4, 'd'))
    scheduler.step()
    assert recent_values(z) == \
      [(0, (0, 'a')), (1, (0, 'b')), (2, (1, 'A')), (5, (1, 'B'))]

    x.append((8, 'e'))
    scheduler.step()
    assert recent_values(z) == \
      [(0, (0, 'a')), (1, (0, 'b')), (2, (1, 'A')), (5, (1, 'B')), (8, (0, 'e'))]
コード例 #15
0
ファイル: merge_test.py プロジェクト: yongkyunlee/IoTPy
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()
コード例 #16
0
ファイル: element_test.py プロジェクト: juxiong/IoTPy
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