def pitchshift_stream(sound_array, n, window_size=2**13, h=2**11): """ Changes the pitch of a sound by n semitones. Notes ----- This application has 2 sink_window agents and 3 streams x, y, z. Stretch agent: The first agent gets input x and outputs y which stretches the data in stream x. The stretching code is from Zulko, pianoputer. Speed up agent: The next agent gets input y and outputs z which speeds up y by the specified factor. This agent interpolates the data in y to the number of points determined by factor. """ factor = 2**(1.0 * n / 12.0) f = 1.0 / factor # Declare streams x = StreamArray('x', dtype=np.int16) y = StreamArray('y', dtype=np.int16) z = StreamArray('z', dtype=np.int16) # Define the stretch agent stretch_object = Stretch(in_stream=x, out_stream=y, factor=factor, window_size=window_size, h=h) sink_window(func=stretch_object.stretch, in_stream=x, window_size=window_size + h, step_size=int(h * f)) # Define the speedup agent. def f(window, out_stream): indices = np.arange(0, window_size, factor) out_stream.extend( np.int16(np.interp(indices, np.arange(window_size), window))) sink_window(func=f, in_stream=y, window_size=window_size, step_size=window_size, out_stream=z) # Partition sound_array into sound bites. Extend the # input with a sequence of sound bites and run each # sound bite until the sound_array data is finished. sound_bite_size = 2**14 for i in range(0, sound_array.size, sound_bite_size): # sound_bite = sound_array[i:i+sound_bite_size] x.extend(sound_array[i:i + sound_bite_size]) run() # Process any data in sound_array that wasn't processed # in the for loop. x.extend(sound_array[i:]) # Return the result. return z.recent[:z.stop]
def test_minus_operator_with_arrays(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) z = y - x x.extend(np.arange(3)) y.extend(np.arange(100, 105, 2)) run() assert np.array_equal(recent_values(z), np.array([100, 101, 102]))
def test_multiply_operator_with_arrays(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) z = y * x x.extend(np.arange(3)) y.extend(np.arange(100, 105, 2)) run() assert np.array_equal(recent_values(z), np.array([0, 102, 208]))
def test_minus_operator_with_arrays_and_dimension(self): x = StreamArray(dimension=3, dtype=int) y = StreamArray(dimension=3, dtype=int) z = y - x A = np.array([[10, 20, 30], [40, 50, 60]]) B = np.array([[100, 100, 100], [200, 200, 200], [300, 300, 00]]) x.extend(A) y.extend(B) run() assert np.array_equal(recent_values(z), np.array([[90, 80, 70], [160, 150, 140]]))
def test_iot(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) z = StreamArray(dtype=int) u = StreamArray(dtype=int) v = StreamArray(dtype=int) def f(A, y, z): """ Function wrapped by an iot agent. The first parameter 'A' is an array obtained from the input stream of the agent. The other parameters, y and z, are positional arguments (*args) of the function. The function returns a pointer into the input stream. In this example, each call to the function processes the entire input array 'A' and so the function returns len(A). """ y.extend(2*A) z.extend(3*A) # Return a pointer into the input array. return len(A) def g(A, u, v): """ Parameters are similar to f. """ u.extend(A+A) v.extend(A**2) return len(A) # Create agents that wrap functions f and g. iot(f, x, y, z) iot(g, x, u, v) # Extend stream x with an array x.extend(np.arange(5, dtype=int)) run() assert np.array_equal(recent_values(y), 2*np.arange(5, dtype=int)) assert np.array_equal(recent_values(z), 3*np.arange(5, dtype=int)) assert np.array_equal(recent_values(u), 2*np.arange(5, dtype=int)) assert np.array_equal(recent_values(v), np.arange(5, dtype=int)**2) # Extend stream x with another array x.extend(np.arange(5, 10, dtype=int)) run() assert np.array_equal(recent_values(y), 2*np.arange(10, dtype=int)) assert np.array_equal(recent_values(z), 3*np.arange(10, dtype=int)) assert np.array_equal(recent_values(u), 2*np.arange(10, dtype=int)) assert np.array_equal(recent_values(v), np.arange(10, dtype=int)**2)
def test_map_list_with_arrays(self): from IoTPy.agent_types.op import map_list x = StreamArray(dtype=int) y = StreamArray(dtype=int) def f(A): return 2 * A map_list(f, x, y) x.extend(np.arange(5)) run() assert np.array_equal(recent_values(y), 2 * np.arange(5))
def test_sink_3(self): x = StreamArray(dtype='int') y = StreamArray() @sink_w def f(window, y): y.append(window[-1] - np.mean(window)) f(x, window_size=2, step_size=1, y=y) x.extend(np.arange(5)) run() assert (np.array_equal(recent_values(y), np.array([0.5, 0.5, 0.5, 0.5])))
def test_iot_class(self): x = StreamArray(name='x', dtype=int) y = StreamArray(name='y', dtype=int) # Create an agent that wraps np.sum sw = self.sliding_window_test( func=np.sum, in_stream=x, out_stream=y, window_size=5, step_size=2) x.extend(np.arange(10, dtype=int)) run() assert np.array_equal(recent_values(y), np.array([10, 20, 30])) x.extend(np.arange(10, 20, dtype=int)) run() assert np.array_equal(recent_values(y), np.array([10, 20., 30, 40, 50, 60, 70, 80]))
def test_kmeans_sliding_windows(): print('-----------------------------------------') print(' ') print('testing kmeans sliding windows') num_dimensions = 2 window_size = 12 step_size = 2 num_clusters = 4 in_stream = StreamArray(name='in', dimension=num_dimensions) out_stream = StreamArray(name='out', dimension=window_size, dtype=int) kmeans_sliding_windows(in_stream, out_stream, window_size, step_size, num_clusters) points = np.array([ [+1.0, +1.0], [+1.1, +1.1], [+0.9, +0.9], [+1.0, -1.0], [+1.1, -0.9], [+0.9, -1.1], [-1.0, +1.0], [-1.1, +0.9], [-0.9, +1.1], [-1.0, -1.0], [-1.1, -1.1], [-0.9, -0.9], # NEXT STEP [+1.0, +1.0], [+1.1, +1.1], # NEXT STEP [+0.9, +0.9], [+1.0, -1.0], # NEXT STEP [-1.2, -1.2], [-0.8, -0.8] ]) in_stream.extend(points) run() print(' ') print('num_dimensions = ', num_dimensions) print('window_size = ', window_size) print('step_size = ', step_size) print('num_clusters = ', num_clusters) print('points: ') print(points) print('output_stream: ') print(recent_values(out_stream))
def g_numpy(in_streams, out_streams): t = StreamArray('t') map_window(max, dtype_float(in_streams[0]), t, window_size=2, step_size=2) print_stream(t, 't')
def f_echo(in_streams, out_streams, delay, attenuation, q): echo = StreamArray('echo', initial_value=np.array([0.0] * delay, dtype='float'), dtype='float') #Note: sound_made = in_streams[0] sound_heard = in_streams[0] + echo map_element(lambda v: v * attenuation, sound_heard, echo) stream_to_queue(sound_heard, q)
def test_plus_operator_with_arrays_1(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) z = x + y x.extend(np.arange(3)) y.extend(np.arange(100, 105)) run() assert isinstance(recent_values(z), np.ndarray) assert np.array_equal(recent_values(z), np.array([100, 102, 104])) x.extend(np.arange(3, 7)) run() assert np.array_equal(recent_values(z), np.array([100, 102, 104, 106, 108])) run() assert np.array_equal(recent_values(z), np.array([100, 102, 104, 106, 108]))
def test_iot_merge(self): x = StreamArray(dtype=float) y = StreamArray(dtype=float) z = StreamArray(dimension=2, dtype=float) def f(A_list, z): """ f is the function wrapped by an iot_merge agent. A_list is a list of arrays. A_list[j] is the input array obtained from the j-th input stream of the agent that wraps f. z is the positional argument of f. z is an output stream that is extended by f. The agent completes reading n_rows elements of each array in A_list where n_rows is the number of elements in the smallest array. So, the function returns n_rows. """ n_rows = min([len(A) for A in A_list]) n_cols = len(A_list) out = np.column_stack((A_list[0][:n_rows], A_list[1][:n_rows])) z.extend(out) return [n_rows for A in A_list] # Create the agent by wrapping function f. # A_list has two arrays from streams x and y. # z is a keyword argument for f. iot_merge(f, [x, y], z=z) # Extend stream x with [0, 1, 2, 3, 4] x.extend(np.arange(5, dtype=float)) run() assert np.array_equal(recent_values(x), np.array(np.arange(5, dtype=float))) assert np.array_equal(recent_values(x), np.array([0., 1., 2., 3., 4.])) assert np.array_equal(recent_values(y), np.zeros(shape=(0,), dtype=float)) assert np.array_equal(recent_values(z), np.zeros(shape=(0, 2), dtype=float)) y.extend(np.arange(100, 107, dtype=float)) run() assert np.array_equal(recent_values(x), np.array([0., 1., 2., 3., 4.])) assert np.array_equal(recent_values(y), np.array([100., 101., 102., 103., 104., 105., 106.])) assert np.array_equal( recent_values(z), np.array( [[ 0., 100.], [ 1., 101.], [ 2., 102.], [ 3., 103.], [ 4., 104.]]))
def test_simple_array(self): """ Same as test_simple except that StreamArray is used in place of Stream. Create an agent with a single input stream array, x, and a single output stream array, y. The elements of y are twice the corresponding elements of x. """ # Create the streams arrays. x = StreamArray(name='x', dtype=int) y = StreamArray(name='y', dtype=int) def f(array_of_int, s): """ Parameters ---------- f: func the function that is wrapped by iot to create an agent. array_of_int: NumPy array of int s: StreamArray the output stream array of the agent. Returns ------- The function returns len(array_of_int) because the agent has finished processing the entire array. """ s.extend(array_of_int * 2) return len(array_of_int) # Create the agent by wrapping function f. iot(f, x, y) # Extend input stream x of the agent with an array x.extend(np.arange(5, dtype=int)) run() assert np.array_equal( recent_values(y), np.array([0, 2, 4, 6, 8]))
def test_plus_operator_with_arrays(self): x = StreamArray(dimension=2, dtype=int) y = StreamArray(dimension=2, dtype=int) z = x + y A = np.arange(6).reshape((3, 2)) B = np.arange(100, 110).reshape((5, 2)) x.extend(A) y.extend(B) run() assert isinstance(z, StreamArray) assert np.array_equal(recent_values(z), np.array([[100, 102], [104, 106], [108, 110]])) C = np.arange(6, 12).reshape((3, 2)) x.extend(C) run() assert np.array_equal( recent_values(z), np.array([[100, 102], [104, 106], [108, 110], [112, 114], [116, 118]]))
def test_sliding_window_with_startup(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) sw = sliding_window_with_startup( func=np.sum, in_stream=x, out_stream=y, window_size=10, step_size=3) # tests x.extend(np.arange(6, dtype=int)) run() assert np.array_equal(recent_values(y), np.array([3, 15])) x.extend(np.arange(6, 9, dtype=int)) run() assert np.array_equal(recent_values(y), np.array([3, 15, 36])) x.extend(np.arange(9, 15, dtype=int)) run() assert np.array_equal( recent_values(y), np.array([3, 15, 36, 65, 95])) x.extend(np.arange(15, 30, dtype=int)) run() assert np.array_equal( recent_values(y), np.array([3, 15, 36, 65, 95, 125, 155, 185, 215, 245]))
def test_stream_arrays_2(self): """ Example where the input stream of an agent is a stream array and its output stream is not a stream array. """ x = StreamArray(name='x', dimension=3, dtype=float) y = Stream() map_element(func=np.median, in_stream=x, out_stream=y) x.append(np.array([1., 2., 3.])) run() assert y.recent[:y.stop] == [2.0] x.extend(np.array([[4., 5., 6.], [7., 8., 9.]])) run() assert y.recent[:y.stop] == [2.0, 5.0, 8.0]
def test_multiply_function_with_multidimensional_array(self): x = StreamArray(dimension=2, dtype=int) # Create a stream array y. y = f_mul(x, 2) A = np.array([[1, 10], [2, 20], [3, 30]]) x.extend(A) run() assert np.array_equal(recent_values(y), [[2, 20], [4, 40], [6, 60]]) x.append(np.array([4, 40])) run() assert np.array_equal(recent_values(y), [[2, 20], [4, 40], [6, 60], [8, 80]])
def pitch_shift(in_streams, out_streams): """ Notes ----- For this particular function, there is only one input stream, that is the output of the shimmer effect. The output of this function is the pitchshifted version of the heard stream. """ print('pitch_shift') print('type(in_streams[0]) is ', type(in_streams[0])) print('type(out_streams[0]) is ', type(out_streams[0])) window_size = 2**13 h = 2**11 n = 12 factor = 2**(1.0 * n / 12.0) f = 1.0 / factor # Define the stretch agent y = StreamArray('y', dtype=np.int16) # The below is the Pitch Shift Agent stretch_object = Stretch(in_stream=in_streams[0], out_stream=y, factor=factor, window_size=window_size, h=h) sink_window(func=stretch_object.stretch, in_stream=in_streams[0], window_size=window_size + h, step_size=int(h * f)) # Define the speedup agent. def f(window, out_stream): indices = np.arange(0, window_size, factor) out_stream.extend( np.int16(np.interp(indices, np.arange(window_size), window)) + 0.0) sink_window(func=f, in_stream=y, window_size=window_size, step_size=window_size, out_stream=out_streams[0])
def test_fmap_with_stream_array(self): x = StreamArray(dimension=2, dtype=int) @fmap_e def g(v): return 2 * v y = g(x) A = np.array([[1, 10], [2, 20], [3, 30]]) x.extend(A) run() assert np.array_equal(recent_values(y), [[2, 20], [4, 40], [6, 60]]) x.append(np.array([4, 40])) run() assert np.array_equal(recent_values(y), [[2, 20], [4, 40], [6, 60], [8, 80]])
def test_sink(self): import numpy as np scheduler = Stream.scheduler ## ---------------------------------------------- ## # Examples from AssembleSoftware website: KEEP!! ## ---------------------------------------------- ## def print_index(v, state, delimiter): ## print str(state) + delimiter + str(v) ## return state+1 # next state ## s = Stream() ## sink(print_index, s, 0, delimiter=':') ## s.extend(list(range(100,105))) ## s = Stream() ## def print_index(v, state, delimiter): ## print str(state) + delimiter + str(v) ## return state+1 # next state ## sink(print_index, s, 0, delimiter=':') ## s.extend(list(range(100,105))) # Set up parameters for call to stream_to_list ## ---------------------------------------------- ## # Finished examples from AssembleSoftware website ## ---------------------------------------------- #----------------------------------------------- # Set up parameters for call to sink print_list = [] print_list_for_array = [] def print_index(v, state, print_list): print_list.append(str(state) + ':' + str(v)) return state + 1 # next state s = Stream('s') s_array = StreamArray('s_array', dtype=int) #----------------------------------------------- # Call sink with initial state of 0 sink(func=print_index, in_stream=s, state=0, print_list=print_list) sink(func=print_index, in_stream=s_array, state=0, print_list=print_list_for_array) s.extend(list(range(100, 103))) s_array.extend(np.arange(100, 103)) scheduler.step() assert print_list == ['0:100', '1:101', '2:102'] assert print_list_for_array == print_list s.extend(list(range(200, 203))) scheduler.step() assert print_list == [ '0:100', '1:101', '2:102', '3:200', '4:201', '5:202' ] #----------------------------------------------- input_stream = Stream('input stream') input_stream_array = StreamArray('input stream array', dtype=int) output_list = [] output_list_array = [] # Call stream_to_list with no function stream_to_list(input_stream, output_list) stream_to_list(input_stream_array, output_list_array) # A test a_test_list = list(range(100, 105)) a_test_array = np.arange(100, 105) input_stream.extend(a_test_list) input_stream_array.extend(a_test_array) scheduler.step() assert output_list == a_test_list assert output_list_array == a_test_list #----------------------------------------------- # test stream to list with a function def h(v, multiplier, addend): return v * multiplier + addend ss = Stream('ss') ss_array = StreamArray('ss_array', dtype=int) l = [] l_array = [] stream_to_list(ss, l, h, multiplier=2, addend=100) stream_to_list(in_stream=ss_array, target_list=l_array, element_function=h, multiplier=2, addend=100) test_list = [3, 23, 14] ss.extend(test_list) ss_array.extend(np.array(test_list)) scheduler.step() assert l == [v * 2 + 100 for v in test_list] assert l_array == l #----------------------------------------------- # test stream to list with a function and state def h(v, state, multiplier, addend): return v * multiplier + addend + state, v + state ss = Stream('ss') ss_array = StreamArray('ss_array', dtype=int) l = [] l_array = [] stream_to_list(ss, l, h, 0, multiplier=2, addend=100) stream_to_list(in_stream=ss_array, target_list=l_array, element_function=h, state=0, multiplier=2, addend=100) test_list = [3, 23, 14] ss.extend(test_list) ss_array.extend(np.array(test_list)) scheduler.step() assert l == [106, 149, 154] assert l_array == l ss = Stream('ss') ss_array = StreamArray('ss_array', dtype=int) l = [] l_array = [] stream_to_list(ss, l, h, 0, multiplier=2, addend=100) stream_to_list(in_stream=ss_array, target_list=l_array, element_function=h, state=0, multiplier=2, addend=100) test_list = list(range(5)) ss.extend(test_list) ss_array.extend(np.array(test_list)) scheduler.step() assert l == [100, 102, 105, 109, 114] assert l_array == l # Test sink # func operates on a single element of the single input stream and does # not return any value. def p(v, lst): lst.append(v) in_stream_sink = Stream('in_stream_sink') a_list = [] b_list = [] sink_agent = sink_element(func=p, in_stream=in_stream_sink, name='sink_agent', lst=a_list) sink(func=p, in_stream=in_stream_sink, lst=b_list) test_list = [1, 13, 29] in_stream_sink.extend(test_list) scheduler.step() assert a_list == test_list assert b_list == test_list # ------------------------------------ # Test sink with state # func operates on a single element of the single input stream and state. # func does not return any value. def p_s(element, state, lst, stream_name): lst.append([stream_name, element]) return state + 1 in_stream_sink_with_state = Stream('s') c_list = [] sink_with_state_agent = sink_element( func=p_s, in_stream=in_stream_sink_with_state, state=0, name='sink_with_state_agent', lst=c_list, stream_name='s') #------------------------------------------------------------------------------ # Test sink as a function with state d_list = [] sink(p_s, in_stream_sink_with_state, state=0, lst=d_list, stream_name='s') in_stream_sink_with_state.extend(list(range(2))) scheduler.step() assert c_list == [['s', 0], ['s', 1]] assert d_list == c_list # ------------------------------------ # Test sink with side effect # func operates on a single element of the single input stream and state. # func does not return any value. def sink_with_side_effect_func(element, side_effect_list, f): side_effect_list.append(f(element)) return None side_effect_list_0 = [] side_effect_list_1 = [] side_effect_list_2 = [] def ff(element): return element * 2 def fff(element): return element + 10 stm = Stream('stm') sink_with_side_effect_agent_0 = sink_element( func=sink_with_side_effect_func, in_stream=stm, name='sink_with_side_effect_agent_0', side_effect_list=side_effect_list_0, f=ff) sink_with_side_effect_agent_1 = sink_element( func=sink_with_side_effect_func, in_stream=stm, name='sink_with_side_effect_agent_1', side_effect_list=side_effect_list_1, f=fff) def f_stateful(element, state): return element + state, element + state def f_stateful_2(element, state): return element * state, element + state target_stream_to_list_simple = [] stream_to_list(stm, target_stream_to_list_simple) stream_to_list(in_stream=stm, target_list=side_effect_list_2, element_function=lambda v: 2 * v) target_stream_to_list_stateful = [] stream_to_list(in_stream=stm, target_list=target_stream_to_list_stateful, element_function=f_stateful, state=0) target_stream_to_list_stateful_2 = [] stream_to_list(in_stream=stm, target_list=target_stream_to_list_stateful_2, element_function=f_stateful_2, state=0) stream_to_file(stm, 'test1.txt') stream_to_file(stm, 'test2.txt', lambda v: 2 * v) stream_to_file(stm, 'test3.txt', f_stateful, state=0) is_py2 = sys.version[0] == '2' if is_py2: import Queue as queue else: import queue as queue queue_1 = queue.Queue() queue_2 = queue.Queue() queue_3 = queue.Queue() stream_to_queue(stm, queue_1) stream_to_queue(stm, queue_2, lambda v: 2 * v) stream_to_queue(stm, queue_3, f_stateful, 0) stm.extend(list(range(5))) scheduler.step() assert target_stream_to_list_stateful == [0, 1, 3, 6, 10] assert target_stream_to_list_stateful_2 == [0, 0, 2, 9, 24] assert side_effect_list_0 == [0, 2, 4, 6, 8] assert side_effect_list_1 == [10, 11, 12, 13, 14] assert side_effect_list_0 == side_effect_list_2 assert target_stream_to_list_simple == list(range(5)) with open('test1.txt') as the_file: file_contents_integers = [int(v) for v in (the_file.readlines())] assert file_contents_integers == recent_values(stm) with open('test2.txt') as the_file: file_contents_integers = [int(v) for v in (the_file.readlines())] assert file_contents_integers == [2 * v for v in recent_values(stm)] with open('test3.txt') as the_file: file_contents_integers = [int(v) for v in (the_file.readlines())] assert file_contents_integers == [0, 1, 3, 6, 10] os.remove('test1.txt') os.remove('test2.txt') os.remove('test3.txt') def h(v, multiplier, addend): return v * multiplier + addend ss = Stream() stream_to_file(ss, 'test4.txt', h, multiplier=2, addend=100) test_list = [3, 23, 14] ss.extend(test_list) scheduler.step() with open('test4.txt') as the_file: file_contents_integers = [int(v) for v in (the_file.readlines())] assert file_contents_integers == [v * 2 + 100 for v in test_list] os.remove('test4.txt') def h(v, state, multiplier, addend): return v * multiplier + addend + state, v + state ss = Stream() stream_to_file(ss, 'test5.txt', h, 0, multiplier=2, addend=100) test_list = [3, 23, 14] ss.extend(test_list) scheduler.step() with open('test5.txt') as the_file: file_contents_integers = [int(v) for v in (the_file.readlines())] scheduler.step() assert file_contents_integers == [106, 149, 154] os.remove('test5.txt') # ------------------------------------ # Testing stream_to_queue def h(v, state, multiplier, addend): return v * multiplier + addend + state, v + state ss = Stream() queue_4 = queue.Queue() stream_to_queue(ss, queue_4, h, 0, multiplier=2, addend=100) test_list = [3, 23, 14] ss.extend(test_list) scheduler.step() queue_contents = [] while not queue_4.empty(): queue_contents.append(queue_4.get()) assert queue_contents == [106, 149, 154] # Test with state and keyword arguments def h(v, state, multiplier, addend): return v * multiplier + addend + state, v + state ss = Stream() stream_to_queue(ss, queue_4, h, 0, multiplier=2, addend=100) test_list = [3, 23, 14] ss.extend(test_list) queue_contents = [] scheduler.step() while not queue_4.empty(): queue_contents.append(queue_4.get()) assert queue_contents == [106, 149, 154] # Another test with state and keyword arguments ss = Stream() queue_5 = queue.Queue() stream_to_queue(ss, queue_5, h, 0, multiplier=2, addend=100) test_list = list(range(5)) ss.extend(test_list) scheduler.step() queue_contents = [] while not queue_5.empty(): queue_contents.append(queue_5.get()) assert queue_contents == [100, 102, 105, 109, 114] # Test stream_to_buffer s = Stream() buf = Buffer(max_size=10) stream_to_buffer(s, buf) test_list = list(range(5)) s.extend(test_list) scheduler.step() assert buf.get_all() == test_list next_test = list(range(5, 10, 1)) s.extend(next_test) scheduler.step() assert buf.read_all() == next_test assert buf.get_all() == next_test s = Stream('s') print_list = [] def f(lst): print_list.extend(lst) sink_window(func=f, in_stream=s, window_size=4, step_size=2) s.extend(list(range(10))) scheduler.step() assert print_list == [0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9] s = Stream('s') print_list = [] def f(lst): print_list.extend(lst) sink_list(func=f, in_stream=s) s.extend(list(range(10))) Stream.scheduler.step() assert print_list == list(range(10)) import numpy as np t = StreamArray('t', dtype='int') print_list = [] def f(lst): print_list.extend(lst) sink_list(func=f, in_stream=t) t.extend(np.arange(10)) Stream.scheduler.step() assert print_list == list(range(10)) print('TEST OF SINK IS SUCCESSFUL')
def shimmer(original_sound_list, fs): """ Paramters --------- original_sound_list: Input Sound fs: Sampling Frequency """ delay = int(fs / 3) attenuation_vector = [0.6] input_stream = StreamArray('Input') heard = StreamArray('Output') pitch_out = StreamArray('PitchShift Output') echo = StreamArray(name='echo', initial_value=np.zeros(delay)) # This below zip_map agent is the part that merges the output from Echo agent above and # The input stream zip_map(func=sum, in_streams=[input_stream, echo], out_stream=heard) # This below agent takes the output from the Pitch Shifter and then # Creates the Echo out of that sound that is fed as input to the zip_map agent above window_dot_product(in_stream=pitch_out, out_stream=echo, multiplicand_vector=attenuation_vector) window_size = 2**13 h = 2**11 n = 12 factor = 2**(1.0 * n / 12.0) f = 1.0 / factor # Define the stretch agent y = StreamArray('y', dtype=np.int16) # The below is the Pitch Shift Agent stretch_object = Stretch(in_stream=heard, out_stream=y, factor=factor, window_size=window_size, h=h) sink_window(func=stretch_object.stretch, in_stream=heard, window_size=window_size + h, step_size=int(h * f)) # Define the speedup agent. def f(window, out_stream): indices = np.arange(0, window_size, factor) out_stream.extend( np.int16(np.interp(indices, np.arange(window_size), window)) + 0.0) sink_window(func=f, in_stream=y, window_size=window_size, step_size=window_size, out_stream=pitch_out) input_stream.extend(original_sound_list) run() return recent_values(heard)
def test_list(self): 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 = list(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(list(range(5))) run() assert recent_values(x) == list(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 == 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) run() assert recent_values(x) == list(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 == 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]) run() assert recent_values(s) == [10, 16, 20] assert recent_values(n) == [10, 15, 18] assert recent_values(o) == [0, 1, 2] u.append(37) run() 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]) run() assert recent_values(x) == list(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 == 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) run() 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)) run() 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)) run() 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) run() 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)) run() 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)) run() 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]])) run() 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]])) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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])) run() assert a_out.stop == 0 a_in_0.extend(np.array([4.0, 5.0, 6.0])) run() assert a_out.stop == 0 a_in_1.extend(np.array([10.0, 20.0])) run() 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])) run() 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)) run() assert a_out_dt.stop == 0 a_in_dt_1.extend(np.array([(2, [10.0, 20.0, 30.0])], dtype=dt_0)) run() 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)) run() 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)) run() 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) run() 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) run() 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) run() 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) run() c_in_1.extend(c_array_1_0) run() 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) run() 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) run() d_in_1.extend(d_array_1_0) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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) run() 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(list(range(5))) run() 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) run() 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) run() 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)) run() 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)) run() 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')
def test_window(self): 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
def test_element_simple(self): # SPECIFY STREAMS 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. # SPECIFY ENCAPSULATED FUNCTIONS (IF ANY) def double(v): return 2 * v # SPECIFY AGENTS 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 less than or equal to 2. # The elements of stream x that satisfy the boolean, filtering(), are # passed through. 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 # Pass through 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 TEST VALUES INTO INPUT STREAMS #---------------------------------------------------------------- # Put test values into streams x, x0 and n. x.extend(list(range(3))) x0.extend([0, 1, 3, 3, 6, 8]) n.append(0) # STEP 6: EXECUTE A STEP OF THE SCHEDULER run() # STEP 7: LOOK AT OUTPUT STREAMS assert recent_values(x) == [0, 1, 2] assert recent_values(y) == [0, 2, 4] assert recent_values(q0) == [0, 1, 3] assert recent_values(ymap) == recent_values(y) assert recent_values(yfilter) == [0, 1, 2] 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) == [0, 2] assert recent_values(q) == recent_values(p) assert recent_values(n) == [0, 2, 4] assert recent_values(o) == [2, 4, 6] #---------------------------------------------------------------- #---------------------------------------------------------------- x.extend(list(range(3, 5, 1))) run() 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) == [0, 1, 2] 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) == [0, 2, 4] assert recent_values(q) == recent_values(p) #---------------------------------------------------------------- #---------------------------------------------------------------- w.append(0) run() 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) == [0, 1, 2] 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) == [0, 2, 4] 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) run() 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
def test_multi(self): scheduler = Stream.scheduler import numpy as np u = Stream('u') v = Stream('v') x = Stream('x') y = Stream('y') def f(a_list): return max(a_list), sum(a_list) multi_element(f, [u, v], [x, y]) u.extend(list(range(8, 18, 2))) v.extend(list(range(10, 14, 1))) scheduler.step() assert recent_values(x) == [10, 11, 12, 14] assert recent_values(y) == [18, 21, 24, 27] u = Stream('u') v = Stream('v') def f(a_list): return max(a_list), sum(a_list) x, y = multi_element_f(f, [u, v], num_out_streams=2) u.extend(list(range(8, 18, 2))) v.extend(list(range(10, 14, 1))) scheduler.step() assert recent_values(x) == [10, 11, 12, 14] assert recent_values(y) == [18, 21, 24, 27] # test of single input and single output u = Stream('u') x = Stream('x') def f(lst): return [lst[0] * 2] multi_element(f, [u], [x]) u.extend(list(range(5))) scheduler.step() assert recent_values(x) == [0, 2, 4, 6, 8] # Test StreamArray u = StreamArray('u') v = StreamArray('v') x = StreamArray('x') y = StreamArray('y') z = StreamArray('z') def f(a_list): return max(a_list), np.mean(a_list), min(a_list) multi_element(f, [u, v], [x, y, z]) u.extend(np.arange(5.0)) v.extend(np.arange(10., 16., 1.)) scheduler.step() assert np.array_equal(recent_values(u), np.array([0.0, 1.0, 2.0, 3.0, 4.0])) assert np.array_equal(recent_values(v), np.array([10.0, 11.0, 12.0, 13.0, 14.0, 15.0])) assert np.array_equal(recent_values(x), np.array([10.0, 11.0, 12.0, 13.0, 14.0])) assert np.array_equal(recent_values(y), np.array([5.0, 6.0, 7.0, 8.0, 9.0])) assert np.array_equal(recent_values(z), np.array([0.0, 1.0, 2.0, 3.0, 4.0])) # Test StreamArray, dtype=int u = StreamArray('u', dtype=int) v = StreamArray('v', dtype=int) x = StreamArray('x', dtype=int) y = StreamArray('y', dtype=int) def f(a_list): return max(a_list), sum(a_list) + min(a_list) multi_element(f, [u, v], [x, y]) u.extend(np.arange(5)) v.extend(np.arange(10, 17, 1)) scheduler.step() assert np.array_equal(recent_values(u), np.array([0, 1, 2, 3, 4])) assert np.array_equal(recent_values(v), np.array([10, 11, 12, 13, 14, 15, 16])) assert np.array_equal(recent_values(x), np.array([10, 11, 12, 13, 14])) assert np.array_equal(recent_values(y), np.array([10, 13, 16, 19, 22])) def f_multi_window(pair_of_windows): window_0, window_1 = pair_of_windows output_0 = sum(window_0) + sum(window_1) output_1 = max(window_0) + max(window_1) return (output_0, output_1) w = Stream('w') x = Stream('x') y = Stream('y') z = Stream('z') multi_window(func=f_multi_window, in_streams=[w, x], out_streams=[y, z], window_size=2, step_size=2, state=None, name='multi_window_agent') w.extend(list(range(10))) x.extend(list(range(100, 120, 2))) scheduler.step() assert recent_values(y) == [203, 215, 227, 239, 251] assert recent_values(z) == [103, 109, 115, 121, 127] print("TEST OF MULTI IS SUCCESSFUL")
def test_some_merge_agents(self): 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()
def test_split_agents(self): 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(list(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(list(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(list(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(list(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(list(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(list(range(7))) scheduler.step() assert recent_values(y) == [1, 3, 5] assert recent_values(z) == [0, 2, 4] 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=3, step_size=3) x.extend(list(range(12))) scheduler.step() assert recent_values(y) == [2, 5, 8, 11] assert recent_values(z) == [0, 3, 6, 9] # ------------------------------------------------------ # 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() assert recent_values(y) == [0, 1, 2] assert recent_values(z) == ['A', 'B', 'C'] 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=3, step_size=3) x.extend(list(range(12))) scheduler.step() assert recent_values(y) == [2, 5, 8, 11] assert recent_values(z) == [0, 3, 6, 9] print('TEST OF SPLIT IS SUCCESSFUL')
def test_multiply_function_with_arrays(self): x = StreamArray(dtype=int) y = f_mul(x, 100) x.extend(np.arange(3)) run() assert np.array_equal(recent_values(y), np.array([0, 100, 200]))