def operator_overload(self, another_stream, func): from merge import merge_list assert another_stream.dimension == self.dimension, \ 'Both stream arrays must have the same dimension.' \ 'The dimensions are {0} and {1}'.format( self.dimension, another_stream.dimension) assert another_stream.dtype == self.dtype, \ 'Both stream arrays must have the same dtype.' \ 'The dtypes are {0} and {1}'.format( self.dtype, another_stream.dtype) output_stream = StreamArray(dimension=self.dimension, dtype=self.dtype) merge_list(func=func, in_streams=[self, another_stream], out_stream=output_stream) return output_stream
def test_merge_list(self): """ Tests the merge_list(list) array """ # [1,3,5] & [2,4,6] => [1,2,3,4,5,6] l1 = [1, 3, 5] l2 = [2, 4, 6] expected = [1, 2, 3, 4, 5, 6] self.assertEqual(merge_list(l1, l2), expected) # [1, 32, 49] & [2, 41, 83, 123] => [1, 2, 32, 41, 49, 83, 123] l1 = [1, 32, 49] l2 = [2, 41, 83, 123] expected = [1, 2, 32, 41, 49, 83, 123] self.assertEqual(merge_list(l1, l2), expected) # Tests when numbers equal each other # [1, 2, 3] & [1, 2, 3] => [1, 1, 2, 2, 3, 3] l1 = [1, 2, 3] l2 = [1, 2, 3] expected = [1, 1, 2, 2, 3, 3] self.assertEqual(merge_list(l1, l2), expected) # Test empty lists l1 = [] l2 = [] expected = [] self.assertEqual(merge_list(l1, l2), expected) # Test l1 empty l1 = [1, 2, 3] l2 = [] expected = [1, 2, 3] self.assertEqual(merge_list(l1, l2), expected) # Test l2 empty l1 = [] l2 = [1, 2, 3] expected = [1, 2, 3] self.assertEqual(merge_list(l1, l2), expected)
def test_list(): scheduler = Stream.scheduler n = Stream('n') o = Stream('o') p = Stream('p') q = Stream('q') r = Stream('r') s = Stream('s') t = Stream('t') u = Stream('u') v = Stream('v') w = Stream('w') x = Stream('x') y = Stream('y') z = Stream('z') #------------------------------------------------------------------- # Test simple map def simple(lst): return [2 * v for v in lst] a = map_list(func=simple, in_stream=x, out_stream=y, name='a') yy = map_list_f(simple, x) #------------------------------------------------------------------- #------------------------------------------------------------------- # Test map with state # Function that operates on an element and state and returns an # element and state. def f(input_list, state): output_list = [[]] * len(input_list) for i in range(len(input_list)): output_list[i] = input_list[i] + state state += 2 return output_list, state b = map_list(func=f, in_stream=x, out_stream=z, state=0, name='b') zz = map_list_f(f, x, 0) #------------------------------------------------------------------- #------------------------------------------------------------------- # Test map with call streams c = map_list(func=f, in_stream=x, out_stream=v, state=10, call_streams=[w], name='c') #------------------------------------------------------------------- #------------------------------------------------------------------- # Test sink with state def sink_with_state(input_list, output_list): # sink has no output stream. # This function only returns the next state. return output_list.extend(input_list) out_list = [] # In this simple example, out_list will be the same as the input # stream. sink_agent = sink_list(func=sink_with_state, in_stream=x, name='sink_agent', state=out_list) out_list_stream = [] # Function version of the previous agent example sink_list_f(sink_with_state, x, out_list_stream) #------------------------------------------------------------------- #------------------------------------------------------------------- # Test merge # Function that operates on a list of lists def g(list_of_lists): return [sum(snapshot) for snapshot in zip(*list_of_lists)] d = merge_list(func=g, in_streams=[x, u], out_stream=s, name='d') ss = merge_list_f(g, [x, u]) #------------------------------------------------------------------- #------------------------------------------------------------------- # Test split def h(input_list): return [[element + 1 for element in input_list], [element * 2 for element in input_list]] e = split_list(func=h, in_stream=x, out_streams=[r, t], name='e') rr, tt = split_list_f(h, x, num_out_streams=2) #------------------------------------------------------------------- #------------------------------------------------------------------- # Test split with state def h_state(input_list, state): length = len(input_list) output_list_0 = [[]] * length output_list_1 = [[]] * length for i in range(length): output_list_0[i] = input_list[i] + state output_list_1[i] = input_list[i] * state state += 1 return ([output_list_0, output_list_1], state) split_list(func=h_state, in_stream=x, out_streams=[p, q], state=0) pp, qq = split_list_f(h_state, x, num_out_streams=2, state=0) #------------------------------------------------------------------- #------------------------------------------------------------------- # Test many def f_many(list_of_lists): snapshots = zip(*list_of_lists) return [[max(snapshot) for snapshot in snapshots], [min(snapshot) for snapshot in snapshots]] multi_agent = multi_list(func=f_many, in_streams=[x, u], out_streams=[n, o], name='multi_agent') nn, oo = multi_list_f(func=f_many, in_streams=[x, u], num_out_streams=2) #------------------------------------------------------------------- #------------------------------------------------------------------- #------------------------------------------------------------------- x.extend(range(5)) scheduler.step() assert recent_values(x) == range(5) assert recent_values(y) == [0, 2, 4, 6, 8] assert recent_values(z) == [0, 3, 6, 9, 12] assert recent_values(v) == [] assert out_list == range(5) assert out_list == out_list_stream assert recent_values(s) == [] assert recent_values(r) == [1, 2, 3, 4, 5] assert recent_values(t) == [0, 2, 4, 6, 8] assert recent_values(p) == [0, 2, 4, 6, 8] assert recent_values(q) == [0, 1, 4, 9, 16] assert recent_values(n) == [] assert recent_values(o) == [] assert recent_values(y) == recent_values(yy) assert recent_values(z) == recent_values(zz) assert recent_values(s) == recent_values(ss) assert recent_values(r) == recent_values(rr) assert recent_values(t) == recent_values(tt) assert recent_values(p) == recent_values(pp) assert recent_values(q) == recent_values(qq) assert recent_values(n) == recent_values(nn) assert recent_values(o) == recent_values(oo) #------------------------------------------------------------------- #------------------------------------------------------------------- w.append(0) scheduler.step() assert recent_values(x) == range(5) assert recent_values(y) == [0, 2, 4, 6, 8] assert recent_values(z) == [0, 3, 6, 9, 12] assert recent_values(v) == [10, 13, 16, 19, 22] assert out_list == range(5) assert recent_values(s) == [] assert recent_values(r) == [1, 2, 3, 4, 5] assert recent_values(t) == [0, 2, 4, 6, 8] assert recent_values(p) == [0, 2, 4, 6, 8] assert recent_values(q) == [0, 1, 4, 9, 16] assert recent_values(n) == [] assert recent_values(o) == [] assert recent_values(y) == recent_values(yy) assert recent_values(z) == recent_values(zz) assert recent_values(s) == recent_values(ss) assert recent_values(r) == recent_values(rr) assert recent_values(t) == recent_values(tt) assert recent_values(p) == recent_values(pp) assert recent_values(q) == recent_values(qq) assert recent_values(n) == recent_values(nn) assert recent_values(o) == recent_values(oo) #------------------------------------------------------------------- #------------------------------------------------------------------- u.extend([10, 15, 18]) scheduler.step() assert recent_values(s) == [10, 16, 20] assert recent_values(n) == [10, 15, 18] assert recent_values(o) == [0, 1, 2] u.append(37) scheduler.step() assert recent_values(s) == [10, 16, 20, 40] assert recent_values(n) == [10, 15, 18, 37] assert recent_values(o) == [0, 1, 2, 3] u.extend([96, 95]) scheduler.step() assert recent_values(x) == range(5) assert recent_values(y) == [0, 2, 4, 6, 8] assert recent_values(z) == [0, 3, 6, 9, 12] assert recent_values(v) == [10, 13, 16, 19, 22] assert out_list == range(5) assert recent_values(s) == [10, 16, 20, 40, 100] assert recent_values(r) == [1, 2, 3, 4, 5] assert recent_values(t) == [0, 2, 4, 6, 8] assert recent_values(p) == [0, 2, 4, 6, 8] assert recent_values(q) == [0, 1, 4, 9, 16] assert recent_values(n) == [10, 15, 18, 37, 96] assert recent_values(o) == [0, 1, 2, 3, 4] assert recent_values(y) == recent_values(yy) assert recent_values(z) == recent_values(zz) assert recent_values(s) == recent_values(ss) assert recent_values(r) == recent_values(rr) assert recent_values(t) == recent_values(tt) assert recent_values(p) == recent_values(pp) assert recent_values(q) == recent_values(qq) assert recent_values(n) == recent_values(nn) assert recent_values(o) == recent_values(oo) #------------------------------------------------------------------ #------------------------------------------------------------------ # Test NumPy arrays: StreamArray #------------------------------------------------------------------ #------------------------------------------------------------------ # Test list map on StreamArray (dimension is 0). a_stream_array = StreamArray(name='a_stream_array') b_stream_array = StreamArray(name='b_stream_array') def f_np(input_array): return input_array + 1 a_np_agent = map_list(func=f_np, in_stream=a_stream_array, out_stream=b_stream_array, name='a_np_agent') bb_stream_array = map_array_f(f_np, a_stream_array) scheduler.step() assert np.array_equal(recent_values(b_stream_array), np.array([], dtype=np.float64)) assert np.array_equal(recent_values(b_stream_array), recent_values(bb_stream_array)) a_stream_array.extend(np.arange(5.0)) scheduler.step() assert np.array_equal(recent_values(b_stream_array), np.arange(5.0) + 1) assert np.array_equal(recent_values(b_stream_array), recent_values(bb_stream_array)) a_stream_array.extend(np.arange(5.0, 10.0, 1.0)) scheduler.step() assert np.array_equal(recent_values(b_stream_array), np.arange(10.0) + 1) assert np.array_equal(recent_values(b_stream_array), recent_values(bb_stream_array)) # Test list map with state on StreamArray (dimension is 0) c_stream_array = StreamArray(name='c_stream_array') d_stream_array = StreamArray(name='d_stream_array') def f_np_state(input_array, state): return np.cumsum(input_array) + state, np.sum(input_array) b_np_agent = map_list(func=f_np_state, in_stream=c_stream_array, out_stream=d_stream_array, state=0.0, name='b_np_agent') dd_stream_array = map_array_f(f_np_state, c_stream_array, state=0.0) scheduler.step() assert np.array_equal(recent_values(d_stream_array), np.array([], dtype=np.float64)) assert np.array_equal(recent_values(d_stream_array), recent_values(dd_stream_array)) c_stream_array.extend(np.arange(5.0)) scheduler.step() assert np.array_equal(d_stream_array.recent[:d_stream_array.stop], np.cumsum(np.arange(5.0))) assert np.array_equal(recent_values(d_stream_array), recent_values(dd_stream_array)) c_stream_array.extend(np.arange(5.0, 10.0, 1.0)) scheduler.step() assert np.array_equal(d_stream_array.recent[:d_stream_array.stop], np.cumsum(np.arange(10.0))) assert np.array_equal(recent_values(d_stream_array), recent_values(dd_stream_array)) # Test list map with positive integer dimension on StreamArray e_stream_array = StreamArray(name='e_stream_array', dimension=3) f_stream_array = StreamArray(name='f_stream_array', dimension=2) def f_np_dimension(input_array): output_array = np.zeros([len(input_array), 2]) output_array[:, 0] = input_array[:, 0] + input_array[:, 1] output_array[:, 1] = input_array[:, 2] return output_array c_np_agent = map_list(func=f_np_dimension, in_stream=e_stream_array, out_stream=f_stream_array, name='c_np_agent') e_stream_array.extend(np.array([[1.0, 2.0, 3.0]])) scheduler.step() assert np.array_equal(f_stream_array.recent[:f_stream_array.stop], np.array([[3.0, 3.0]])) e_stream_array.extend(np.array([[4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])) scheduler.step() assert np.array_equal(f_stream_array.recent[:f_stream_array.stop], np.array([[3.0, 3.0], [9.0, 6.0], [15.0, 9.0]])) # Test list map with a dimension which is a tuple of integers. g_stream_array = StreamArray(name='g_stream_array', dimension=(2, 2)) h_stream_array = StreamArray(name='h_stream_array', dimension=(2, 2)) def f_np_tuple_dimension(input_array): return input_array * 2 d_np_agent = map_list(func=f_np_tuple_dimension, in_stream=g_stream_array, out_stream=h_stream_array, name='d_np_agent') a_array = np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) g_stream_array.extend(a_array) scheduler.step() assert np.array_equal(h_stream_array.recent[:h_stream_array.stop], a_array * 2) b_array = np.array([[[9.0, 10.0], [11.0, 12.0]]]) g_stream_array.extend(b_array) scheduler.step() assert np.array_equal(h_stream_array.recent[:h_stream_array.stop], np.vstack((a_array, b_array)) * 2) # Test list map with a datatype and dimension of 0. dt_0 = np.dtype([('time', int), ('value', (float, 3))]) dt_1 = np.dtype([('time', int), ('value', float)]) i_stream_array = StreamArray(name='i_stream_array', dtype=dt_0) j_stream_array = StreamArray(name='j_stream_array', dtype=dt_1) def f_datatype(input_array): output_array = np.zeros(len(input_array), dtype=dt_1) output_array['time'] = input_array['time'] output_array['value'] = np.sum(input_array['value'], axis=1) return output_array e_np_agent = map_list(func=f_datatype, in_stream=i_stream_array, out_stream=j_stream_array, name='e_np_agent') c_array = np.array([(1, [2.0, 3.0, 4.0])], dtype=dt_0) assert j_stream_array.stop == 0 i_stream_array.extend(c_array) scheduler.step() assert np.array_equal(j_stream_array.recent[:j_stream_array.stop], f_datatype(c_array)) d_array = np.array([(10, [6.0, 7.0, 8.0]), (20, [10.0, 11.0, 12.0])], dtype=dt_0) i_stream_array.extend(d_array) scheduler.step() assert np.array_equal(j_stream_array.recent[:j_stream_array.stop], f_datatype(np.hstack((c_array, d_array)))) # Test list map with a datatype and positive integer dimension. k_stream_array = StreamArray(name='k_stream_array', dtype=dt_0, dimension=2) l_stream_array = StreamArray(name='l_stream_array', dtype=dt_1) def f_datatype_int_dimension(input_array): m = len(input_array) output_array = np.zeros(m, dtype=dt_1) for i in range(m): output_array[i]['time'] = np.max(input_array[i]['time']) output_array[i]['value'] = np.sum(input_array[i]['value']) return output_array f_np_agent = map_list(func=f_datatype_int_dimension, in_stream=k_stream_array, out_stream=l_stream_array, name='f_np_agent') e_array = np.array([[(1, [2.0, 3.0, 4.0]), (2, [5.0, 6.0, 7.0])]], dtype=dt_0) assert l_stream_array.stop == 0 k_stream_array.extend(e_array) scheduler.step() assert np.array_equal(l_stream_array.recent[:l_stream_array.stop], f_datatype_int_dimension(e_array)) f_array = np.array([[(3, [8.0, 9.0, 10.0]), (4, [11.0, 12.0, 13.0])], [(5, [-1.0, 0.0, 1.0]), (6, [-2.0, 2.0, -2.0])]], dtype=dt_0) k_stream_array.extend(f_array) scheduler.step() assert np.array_equal( l_stream_array.recent[:l_stream_array.stop], f_datatype_int_dimension(np.vstack((e_array, f_array)))) # Test list map with a datatype and a dimension which is a tuple m_stream_array = StreamArray(name='m_stream_array', dtype=dt_0, dimension=(2, 2)) n_stream_array = StreamArray(name='n_stream_array', dtype=dt_1) g_np_agent = map_list(func=f_datatype_int_dimension, in_stream=m_stream_array, out_stream=n_stream_array, name='g_np_agent') assert n_stream_array.stop == 0 g_array = np.array( [ # zeroth 2x2 array [[(1, [2.0, 3.0, 4.0]), (2, [5.0, 6.0, 7.0])], [(3, [8.0, 9.0, 10.0]), (4, [11.0, 12.0, 13.0])]], # first 2x2 array [[(5, [12.0, 13.0, 14.0]), (6, [15.0, 16.0, 17.0])], [(7, [18.0, 19.0, 20.0]), (8, [21.0, 22.0, 23.0])]] ], dtype=dt_0) m_stream_array.extend(g_array) scheduler.step() assert np.array_equal(n_stream_array.recent[:n_stream_array.stop], f_datatype_int_dimension(g_array)) h_array = np.array([[[(9, [0.0, 1.0, -1.0]), (10, [2.0, 2.0, -4.0])], [(11, [80.0, -71.0, -9.0]), (15, [0.0, 0.0, 0.0])]]], dtype=dt_0) m_stream_array.extend(h_array) scheduler.step() assert np.array_equal( n_stream_array.recent[:n_stream_array.stop], f_datatype_int_dimension(np.vstack((g_array, h_array)))) # Test list merge with StreamArray and no dimension and no data type a_in_0 = StreamArray(name='a_in_0') a_in_1 = StreamArray(name='a_in_1') a_out = StreamArray(name='a_out') def a_merge(list_of_lists): array_0, array_1 = list_of_lists return array_0 + array_1 a_s_agent = merge_list(func=a_merge, in_streams=[a_in_0, a_in_1], out_stream=a_out, name='a_s_agent') assert a_out.stop == 0 #a_in_0.extend(np.array([1.0, 2.0, 3.0])) a_in_0.extend(np.array([1.0, 2.0, 3.0])) scheduler.step() assert a_out.stop == 0 a_in_0.extend(np.array([4.0, 5.0, 6.0])) scheduler.step() assert a_out.stop == 0 a_in_1.extend(np.array([10.0, 20.0])) scheduler.step() assert np.array_equal(a_out.recent[:a_out.stop], np.array([11.0, 22.0])) a_in_1.extend(np.array([30.0, 40.0])) scheduler.step() assert np.array_equal(a_out.recent[:a_out.stop], np.array([11.0, 22.0, 33.0, 44.0])) # Test list merge with StreamArray and no dimension and data type a_in_dt_0 = StreamArray(name='a_in_dt_0', dtype=dt_0) a_in_dt_1 = StreamArray(name='a_in_dt_1', dtype=dt_0) a_out_dt = StreamArray(name='out', dtype=dt_0) def a_merge_dtype(list_of_arrays): input_array_0, input_array_1 = list_of_arrays output_array = np.zeros(len(input_array_0), dtype=dt_0) output_array['time'] = \ np.max((input_array_0['time'], input_array_1['time']), axis=0) output_array['value'] = input_array_0['value'] + input_array_1['value'] return output_array a_s_dt_agent = merge_list(func=a_merge_dtype, in_streams=[a_in_dt_0, a_in_dt_1], out_stream=a_out_dt, name='a_s_dt_agent') a_in_dt_0.extend(np.array([(1, [1.0, 2.0, 3.0])], dtype=dt_0)) scheduler.step() assert a_out_dt.stop == 0 a_in_dt_1.extend(np.array([(2, [10.0, 20.0, 30.0])], dtype=dt_0)) scheduler.step() assert np.array_equal(a_out_dt.recent[:a_out_dt.stop], np.array([(2, [11.0, 22.0, 33.0])], dtype=dt_0)) a_in_dt_0.extend( np.array([(5, [21.0, 23.0, 32.0]), (9, [27.0, 29.0, 31.0])], dtype=dt_0)) scheduler.step() assert np.array_equal(a_out_dt.recent[:a_out_dt.stop], np.array([(2, [11.0, 22.0, 33.0])], dtype=dt_0)) a_in_dt_1.extend( np.array([(6, [19.0, 17.0, 8.0]), (8, [13.0, 11.0, 9.0]), (10, [3.0, 1.0, 5.0])], dtype=dt_0)) scheduler.step() assert np.array_equal( a_out_dt.recent[:a_out_dt.stop], np.array([(2, [11.0, 22.0, 33.0]), (6, [40.0, 40.0, 40.0]), (9, [40.0, 40.0, 40.0])], dtype=dt_0)) # Test list split with StreamArray and positive integer dimension and no data type dim = 2 b_in = StreamArray(name='b_in', dimension=dim) b_out_0 = StreamArray(name='b_out_0', dimension=dim) b_out_1 = StreamArray(name='b_out_1') def b_split(array_of_arrays): length = len(array_of_arrays) output_array_0 = np.zeros(( length, dim, )) output_array_1 = np.zeros(length) for i in range(length): input_array = array_of_arrays[i] output_array_0[i] = np.array( [[np.max(input_array), np.min(input_array)]]) output_array_1[i] = np.array([np.sum(input_array)]) return output_array_0, output_array_1 b_split_agent = split_list(func=b_split, in_stream=b_in, out_streams=[b_out_0, b_out_1], name='b_split_agent') b_array_0 = np.array([[1.0, 9.0]]) b_in.extend(b_array_0) scheduler.step() assert np.array_equal(b_out_0.recent[:b_out_0.stop], np.array([[9.0, 1.0]])) assert np.array_equal(b_out_1.recent[:b_out_1.stop], np.array([10.0])) b_array_1 = np.array([[98.0, 2.0]]) b_in.extend(b_array_1) scheduler.step() assert np.array_equal(b_out_0.recent[:b_out_0.stop], np.array([[9.0, 1.0], [98.0, 2.0]])) assert np.array_equal(b_out_1.recent[:b_out_1.stop], np.array([10.0, 100.0])) b_array_3 = np.array([[10.0, 20.0], [3.0, 37.0], [55.0, 5.0]]) b_in.extend(b_array_3) scheduler.step() assert np.array_equal( b_out_0.recent[:b_out_0.stop], np.array([[9.0, 1.0], [98.0, 2.0], [20.0, 10.0], [37.0, 3.0], [55.0, 5.0]])) assert np.array_equal(b_out_1.recent[:b_out_1.stop], np.array([10.0, 100.0, 30.0, 40.0, 60.0])) # Test list many with StreamArray and no dimension and no data type c_in_0 = StreamArray(name='c_in_0') c_in_1 = StreamArray(name='c_in_1') c_out_0 = StreamArray(name='c_out_0') c_out_1 = StreamArray(name='c_out_1') def c_many(list_of_arrays): length = len(list_of_arrays) input_array_0, input_array_1 = list_of_arrays output_array_0 = np.zeros(length) output_array_1 = np.zeros(length) output_array_0 = input_array_0 + input_array_1 output_array_1 = input_array_0 - input_array_1 return [output_array_0, output_array_1] c_multi_agent = multi_list(func=c_many, in_streams=[c_in_0, c_in_1], out_streams=[c_out_0, c_out_1], name='c_multi_agent') c_array_0_0 = np.arange(3.0) * 3 c_array_1_0 = np.arange(3.0) c_in_0.extend(c_array_0_0) scheduler.step() c_in_1.extend(c_array_1_0) scheduler.step() assert np.array_equal(c_out_0.recent[:c_out_0.stop], np.array([0.0, 4.0, 8.0])) assert np.array_equal(c_out_1.recent[:c_out_1.stop], np.array([0.0, 2.0, 4.0])) c_array_0_1 = np.array([100.0]) c_array_1_1 = np.array([4.0, 5.0, 6.0]) c_in_0.extend(c_array_0_1) c_in_1.extend(c_array_1_1) scheduler.step() assert np.array_equal(c_out_0.recent[:c_out_0.stop], np.array([0.0, 4.0, 8.0, 104.0])) assert np.array_equal(c_out_1.recent[:c_out_1.stop], np.array([0.0, 2.0, 4.0, 96.0])) ## # Test list many with StreamArray and no dimension and no data type ## z_in_0 = StreamArray(name='z_in_0') ## z_in_1 = StreamArray(name='z_in_1') ## z_out_0 = StreamArray(name='z_out_0') ## z_out_1 = StreamArray(name='z_out_1') ## def execute_list_of_np_func(v, list_of_np_func): ## length = len(list_of_arrays) ## input_array_0, input_array_1 = list_of_arrays ## output_array_0 = np.zeros(length) ## output_array_1 = np.zeros(length) ## output_array_0 = input_array_0 + input_array_1 ## output_array_1 = input_array_0 - input_array_1 ## return [output_array_0, output_array_1] # Test list many with StreamArray and positive integer dimension and no data type dim = 2 d_in_0 = StreamArray(name='d_in_0', dimension=dim) d_in_1 = StreamArray(name='d_in_1', dimension=dim) d_out_0 = StreamArray(name='d_out_0', dimension=dim) d_out_1 = StreamArray(name='d_out_1') def d_many(list_of_arrays): length = len(list_of_arrays) input_array_0, input_array_1 = list_of_arrays output_array_0 = input_array_0 + input_array_1 output_array_1 = np.array([np.sum(input_array_0 + input_array_1)]) return output_array_0, output_array_1 d_multi_agent = multi_list(func=d_many, in_streams=[d_in_0, d_in_1], out_streams=[d_out_0, d_out_1], name='d_multi_agent') d_array_0_0 = np.array([[1.0, 2.0]]) d_array_1_0 = np.array([[0.0, 10.0]]) d_in_0.extend(d_array_0_0) scheduler.step() d_in_1.extend(d_array_1_0) scheduler.step() assert np.array_equal(d_out_0.recent[:d_out_0.stop], np.array([[1.0, 12.0]])) assert np.array_equal(d_out_1.recent[:d_out_1.stop], np.array([13.0])) d_array_0_1 = np.array([[4.0, 8.0]]) d_array_1_1 = np.array([[2.0, 4.0]]) d_in_0.extend(d_array_0_1) d_in_1.extend(d_array_1_1) scheduler.step() assert np.array_equal(d_out_0.recent[:d_out_0.stop], np.array([[1.0, 12.0], [6.0, 12.0]])) assert np.array_equal(d_out_1.recent[:d_out_1.stop], np.array([13.0, 18.0])) d_array_0_2 = np.array([[20.0, 30.0], [40.0, 50.0]]) d_array_1_2 = np.array([[-10.0, -20.0]]) d_in_0.extend(d_array_0_2) d_in_1.extend(d_array_1_2) scheduler.step() assert np.array_equal(d_out_0.recent[:d_out_0.stop], np.array([[1.0, 12.0], [6.0, 12.0], [10.0, 10.0]])) assert np.array_equal(d_out_1.recent[:d_out_1.stop], np.array([13.0, 18.0, 20.0])) # Test list many with StreamArray and tuple dimension and no data type dim = (2, 2) e_in_0 = StreamArray(name='e_in_0', dimension=dim) e_in_1 = StreamArray(name='e_in_1', dimension=dim) e_out_0 = StreamArray(name='e_out_0', dimension=dim) e_out_1 = StreamArray(name='e_out_1') def e_many(list_of_arrays): input_array_0, input_array_1 = list_of_arrays output_array_0 = input_array_0 + input_array_1 output_array_1 = \ np.array([np.sum(input_array_0[i]+ input_array_1[i]) for i in range(len(input_array_0))]) return output_array_0, output_array_1 e_multi_agent = multi_list(func=e_many, in_streams=[e_in_0, e_in_1], out_streams=[e_out_0, e_out_1], name='e_multi_agent') e_array_0_0 = np.array([[[10.0, 20.0], [30.0, 40.0]]]) e_in_0.extend(e_array_0_0) e_array_1_0 = np.array([[[1.0, 2.0], [3.0, 4.0]]]) e_in_1.extend(e_array_1_0) scheduler.step() assert np.array_equal(e_out_0.recent[:e_out_0.stop], np.array([[[11.0, 22.0], [33.0, 44.0]]])) assert np.array_equal(e_out_1.recent[:e_out_1.stop], np.array([110.0])) e_array_0_1 = np.array([[[11.0, 13.0], [17.0, 19.0]], [[2.0, 4.0], [6.0, 8.0]]]) e_in_0.extend(e_array_0_1) scheduler.step() assert np.array_equal(e_out_0.recent[:e_out_0.stop], np.array([[[11.0, 22.0], [33.0, 44.0]]])) assert np.array_equal(e_out_1.recent[:e_out_1.stop], np.array([110.0])) e_array_1_1 = np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) e_in_1.extend(e_array_1_1) scheduler.step() assert np.array_equal( e_out_0.recent[:e_out_0.stop], np.array([[[11.0, 22.0], [33.0, 44.0]], [[12.0, 15.0], [20.0, 23.0]], [[7.0, 10.0], [13.0, 16.0]]])) assert np.array_equal(e_out_1.recent[:e_out_1.stop], np.array([110.0, 70.0, 46.0])) e_array_1_2 = np.array([[[11.0, 12.0], [13.0, 14.0]], [[15.0, 16.0], [17.0, 18.0]]]) e_in_1.extend(e_array_1_2) scheduler.step() e_array_0_2 = np.array([[[-10.0, -11.0], [12.0, 16.0]], [[-14.0, -15.0], [-16.0, -17.0]]]) e_in_0.extend(e_array_0_2) scheduler.step() assert np.array_equal( e_out_0.recent[:e_out_0.stop], np.array([[[11.0, 22.0], [33.0, 44.0]], [[12.0, 15.0], [20.0, 23.0]], [[7.0, 10.0], [13.0, 16.0]], [[1.0, 1.0], [25.0, 30.0]], [[1.0, 1.0], [1.0, 1.0]]])) assert np.array_equal(e_out_1.recent[:e_out_1.stop], np.array([110.0, 70.0, 46.0, 57.0, 4.0])) #------------------------------------------------------------------ #------------------------------------------------------------------ # Test args and kwargs #------------------------------------------------------------------ #------------------------------------------------------------------ # Test map def map_args(lst, multiplicand): return [multiplicand * element for element in lst] in_stream_map_args_stream = Stream('in_stream_map_args_stream') out_stream_map_args_stream = Stream('out_stream_map_args_stream') out_stream_map_kwargs_stream = Stream('out_stream_map_kwargs_stream') map_args_agent = map_list(map_args, in_stream_map_args_stream, out_stream_map_args_stream, None, None, 'map_args_agent', 2) map_kwargs_agent = map_list(func=map_args, in_stream=in_stream_map_args_stream, out_stream=out_stream_map_kwargs_stream, name='map_args_agent', multiplicand=2) scheduler.step() assert out_stream_map_args_stream.recent[:out_stream_map_args_stream.stop] == \ [] assert out_stream_map_kwargs_stream.recent[:out_stream_map_kwargs_stream.stop] == \ [] in_stream_map_args_stream.extend(range(5)) scheduler.step() assert out_stream_map_args_stream.recent[:out_stream_map_args_stream.stop] == \ [0, 2, 4, 6, 8] assert out_stream_map_kwargs_stream.recent[:out_stream_map_kwargs_stream.stop] == \ [0, 2, 4, 6, 8] in_stream_map_args_stream.append(5) scheduler.step() assert out_stream_map_args_stream.recent[:out_stream_map_args_stream.stop] == \ [0, 2, 4, 6, 8, 10] assert out_stream_map_kwargs_stream.recent[:out_stream_map_kwargs_stream.stop] == \ [0, 2, 4, 6, 8, 10] # Test list map on StreamArray (dimension is 0). a_stream_array_args = StreamArray(name='a_stream_array_args') b_stream_array_args = StreamArray(name='b_stream_array_args') c_stream_array_args_kwargs = StreamArray(name='c_stream_array_args_kwargs') def f_np_args(input_array_args, addend): return input_array_args + addend def f_np_args_kwargs(input_array_args_kwargs, multiplicand, addend): return input_array_args_kwargs * multiplicand + addend a_np_agent_args = map_list(f_np_args, a_stream_array_args, b_stream_array_args, None, None, 'a_np_agent_args', 1) a_np_agent_args_kwargs = map_list(f_np_args_kwargs, a_stream_array_args, c_stream_array_args_kwargs, None, None, 'a_np_agent_args_kwargs', 2, addend=10) scheduler.step() assert np.array_equal( b_stream_array_args.recent[:b_stream_array_args.stop], np.array([])) assert np.array_equal( c_stream_array_args_kwargs.recent[:c_stream_array_args_kwargs.stop], np.array([])) a_stream_array_args.extend(np.arange(5.0)) scheduler.step() assert np.array_equal( b_stream_array_args.recent[:b_stream_array_args.stop], np.arange(5.0) + 1) assert np.array_equal( c_stream_array_args_kwargs.recent[:c_stream_array_args_kwargs.stop], np.arange(5.0) * 2 + 10) a_stream_array_args.extend(np.arange(5.0, 10.0, 1.0)) scheduler.step() assert np.array_equal( b_stream_array_args.recent[:b_stream_array_args.stop], np.arange(10.0) + 1) assert np.array_equal( c_stream_array_args_kwargs.recent[:c_stream_array_args_kwargs.stop], np.arange(10.0) * 2 + 10) print 'TEST OF OP (LISTS) IS SUCCESSFUL'
dict_csv = "../../Datasets/processed/word_count_dictionary" fw_csv = "../../Datasets/processed/filtered_words" aw_csv = "../../Datasets/processed/all_words" # Input data, no need to have output file name if test data input_file_name = "../../Datasets/train_input.csv" output_file_name = "../../Datasets/train_output.csv" """ Code, you shouldn't really need to touch this unless there's something very specific required """ merged =[] inputs = merge.read_csv(input_file_name) outputs = merge.read_csv(output_file_name) merged = merge.merge_list(inputs, outputs, 0, 0) # create the preprocessor pp = prp.PreProcessor(merged, accepts, rejects, tfms, n, stem=stemming, labelled = True) # save to csv if needed if save2csv: categories = pp.get_categories() worddict = pp.word_dict() ft_words = pp.ft_data allwords = pp.top_words labels = pp.labels ssuff = "" ssuff = ssuff + "_stemmed" if stemming else ssuff ssuff = ssuff + fsuffix
def test_merge(input, expected): assert merge_list(input[0], input[1]) == expected
def test_some_merge_agents(): import numpy as np scheduler = Stream.scheduler #---------------------------------------------------- # Declare streams s = Stream('s') t = Stream('t') u = Stream('u') v_stream = Stream('v') x = Stream('x') #---------------------------------------------------- # Define functions def g(lst): return sum(lst) def g_args(lst, multiplier): return sum(lst) * multiplier def general_f(lst, f): return f(lst) def fff(lst, f, addend): return f(lst, addend) def hhh(lst, addend): return sum(lst) + addend #---------------------------------------------------- # Define agents d = zip_map(func=sum, in_streams=[x, u], out_stream=s, name='d') def magnitude(vector): return math.sqrt(sum([w * w for w in vector])) ssssss = Stream() ddd = zip_map(func=magnitude, in_streams=[x, u], out_stream=ssssss) zipxu = zip_stream_f([x, u]) zip_map_xu = zip_map_f(sum, [x, u]) zip_map_xu_merge = Stream('zip map xu merge') zip_map(sum, [x, u], zip_map_xu_merge) zip_map_g_args = zip_map_f(g_args, [x, u], multiplier=2) dd = zip_map(func=general_f, in_streams=[x, u], out_stream=t, name='dd', f=np.mean) zip_map_ss = zip_map_f(np.mean, [x, u]) dddd = zip_map(func=fff, in_streams=[x, u], out_stream=v_stream, name='dddd', f=hhh, addend=10) #---------------------------------------------------- #---------------------------------------------------- # Append values to stream x.extend(list(range(3))) u.extend([10, 15, 18]) scheduler.step() assert recent_values(s) == [10, 16, 20] assert recent_values(zip_map_g_args) == [2 * v for v in recent_values(s)] assert recent_values(zipxu) == [(0, 10), (1, 15), (2, 18)] assert recent_values(t) == [5, 8, 10] assert recent_values(zip_map_ss) == [5.0, 8.0, 10.0] assert recent_values(v_stream) == [20, 26, 30] assert recent_values(zip_map_xu) == s.recent[:s.stop] assert recent_values(zip_map_xu) == recent_values(zip_map_xu_merge) #---------------------------------------------------- u.append(37) x.extend(list(range(3, 5, 1))) scheduler.step() assert recent_values(s) == [10, 16, 20, 40] assert recent_values(zip_map_g_args) == [2 * v for v in recent_values(s)] assert recent_values(zipxu) == [(0, 10), (1, 15), (2, 18), (3, 37)] assert recent_values(t) == [5, 8, 10, 20] assert recent_values(v_stream) == [20, 26, 30, 50] assert recent_values(zip_map_xu) == recent_values(zip_map_xu_merge) assert recent_values(ssssss) == [ 10.0, 15.033296378372908, 18.110770276274835, 37.12142238654117 ] #---------------------------------------------------- u.extend([96, 95]) scheduler.step() assert recent_values(s) == [10, 16, 20, 40, 100] assert recent_values(zipxu) == [(0, 10), (1, 15), (2, 18), (3, 37), (4, 96)] assert recent_values(t) == [5, 8, 10, 20, 50] assert recent_values(v_stream) == [20, 26, 30, 50, 110] assert recent_values(zip_map_xu) == recent_values(zip_map_xu_merge) #---------------------------------------------------- # TEST MERGE_ASYNCH AND MIX #---------------------------------------------------- x = Stream('x') y = Stream('y') z = Stream('z') w = Stream('w') def g_asynch(pair): index, value = pair if index == 0: return value * 10 elif index == 1: return value * 2 else: raise Exception() merge_asynch(func=lambda v: v, in_streams=[x, y], out_stream=z) merge_asynch(func=g_asynch, in_streams=[x, y], out_stream=w) mix_z = mix_f([x, y]) scheduler.step() assert recent_values(z) == [] assert recent_values(mix_z) == [] assert recent_values(w) == [] x.append(10) scheduler.step() assert recent_values(z) == [(0, 10)] assert recent_values(mix_z) == recent_values(z) assert recent_values(w) == [100] y.append('A') scheduler.step() assert recent_values(z) == [(0, 10), (1, 'A')] assert recent_values(mix_z) == recent_values(z) assert recent_values(w) == [100, 'AA'] y.append('B') scheduler.step() assert recent_values(z) == [(0, 10), (1, 'A'), (1, 'B')] assert recent_values(mix_z) == recent_values(z) assert recent_values(w) == [100, 'AA', 'BB'] x.append(20) scheduler.step() assert recent_values(z) == [(0, 10), (1, 'A'), (1, 'B'), (0, 20)] assert recent_values(z) == recent_values(mix_z) assert recent_values(w) == [100, 'AA', 'BB', 200] fahrenheit = Stream('fahrenheit') celsius = Stream('celsius') def fahrenheit_and_celsius(pair): index, value = pair if index == 0: return (value - 32.0) / 1.8 elif index == 1: return value else: raise Exception() fahrenheit_stream = Stream('fahrenheit temperatures') celsius_stream = Stream('celsius temperatures') centigrade_stream = Stream('centigrade temperatures') merge_asynch(func=fahrenheit_and_celsius, in_streams=[fahrenheit_stream, celsius_stream], out_stream=centigrade_stream) fahrenheit_stream.append(32) scheduler.step() assert recent_values(centigrade_stream) == [0.0] fahrenheit_stream.append(50) scheduler.step() assert recent_values(centigrade_stream) == [0.0, 10.0] fahrenheit_stream.append(68) scheduler.step() assert recent_values(centigrade_stream) == [0.0, 10.0, 20.0] celsius_stream.append(-10.0) scheduler.step() assert recent_values(centigrade_stream) == [0.0, 10.0, 20.0, -10.0] #---------------------------------------------------- # TEST BLEND #---------------------------------------------------- x = Stream('x') y = Stream('y') z = Stream('z') z_addend = Stream('z_addend') def double(v): return 2 * v def double_add(v, addend): return 2 * v + addend blend(func=double, in_streams=[x, y], out_stream=z) blend(func=double, in_streams=[x, y], out_stream=z_addend) blend_z = blend_f(double, [x, y]) blend_add_z = blend_f(double_add, [x, y], addend=10) x.append(1) scheduler.step() assert recent_values(z) == [2] assert recent_values(blend_z) == recent_values(z) assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)] x.extend(list(range(2, 4))) scheduler.step() assert recent_values(z) == [2, 4, 6] assert recent_values(blend_z) == recent_values(z) assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)] y.extend(list(range(100, 102))) scheduler.step() assert recent_values(z) == [2, 4, 6, 200, 202] assert recent_values(blend_z) == recent_values(z) assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)] x.extend([10, 20]) scheduler.step() assert recent_values(z) == [2, 4, 6, 200, 202, 20, 40] assert recent_values(blend_z) == recent_values(z) assert recent_values(blend_add_z) == [v + 10 for v in recent_values(z)] #---------------------------------------------------- # TEST MANY #---------------------------------------------------- # func operates on a list with one element for each input stream. # func returns a list with one element for each output stream. def f_many(lst): return [sum(lst), sum(lst) + 1] u_stream = Stream(name='u_stream') v_stream = Stream(name='v_stream') w_stream = Stream(name='w_stream') x_stream = Stream(name='x_stream') multi_agent = multi_element(func=f_many, in_streams=[u_stream, v_stream], out_streams=[w_stream, x_stream], name='multi_agent') ww_stream, xx_stream = multi_element_f(func=f_many, in_streams=[u_stream, v_stream], num_out_streams=2) u_stream.extend(list(range(5))) v_stream.extend(list(range(0, 40, 4))) scheduler.step() assert recent_values(w_stream) == [0, 5, 10, 15, 20] assert recent_values(x_stream) == [1, 6, 11, 16, 21] assert recent_values(ww_stream) == recent_values(w_stream) assert recent_values(xx_stream) == recent_values(x_stream) # ------------------------------------ # Test many with args and kwargs # func operates on a list with one element for each input stream. # func returns a list with one element for each output stream. def f_multi_args_kwargs(lst, multiplicand, addend): return sum(lst) * multiplicand, sum(lst) + addend u_args_kwargs_stream = Stream(name='u_args_kwargs_stream') v_args_kwargs_stream = Stream(name='v_args_kwargs_stream') w_args_kwargs_stream = Stream(name='w_args_kwargs_stream') x_args_kwargs_stream = Stream(name='x_args_kwargs_stream') multi_args_kwargs_agent = multi_element( func=f_multi_args_kwargs, in_streams=[u_args_kwargs_stream, v_args_kwargs_stream], out_streams=[w_args_kwargs_stream, x_args_kwargs_stream], name='multi_args_kwargs_agent', multiplicand=2, addend=10) ww_args_kwargs_stream, xx_args_kwargs_stream = multi_element_f( func=f_multi_args_kwargs, in_streams=[u_args_kwargs_stream, v_args_kwargs_stream], num_out_streams=2, multiplicand=2, addend=10) assert (recent_values(ww_args_kwargs_stream) == recent_values( w_args_kwargs_stream)) assert (recent_values(xx_args_kwargs_stream) == recent_values( x_args_kwargs_stream)) u_args_kwargs_stream.extend(list(range(5))) v_args_kwargs_stream.extend(list(range(0, 40, 4))) scheduler.step() assert recent_values(w_args_kwargs_stream) == [0, 10, 20, 30, 40] assert recent_values(x_args_kwargs_stream) == [10, 15, 20, 25, 30] assert (recent_values(ww_args_kwargs_stream) == recent_values( w_args_kwargs_stream)) assert (recent_values(xx_args_kwargs_stream) == recent_values( x_args_kwargs_stream)) u_args_kwargs_stream.append(100) v_args_kwargs_stream.extend(list(range(40, 80, 4))) scheduler.step() assert recent_values(w_args_kwargs_stream) == \ [0, 10, 20, 30, 40, 240] assert recent_values(x_args_kwargs_stream) == \ [10, 15, 20, 25, 30, 130] assert (recent_values(ww_args_kwargs_stream) == recent_values( w_args_kwargs_stream)) assert (recent_values(xx_args_kwargs_stream) == recent_values( x_args_kwargs_stream)) u_args_kwargs_stream.extend([200, 300]) scheduler.step() v_args_kwargs_stream.append(100) scheduler.step() assert recent_values(w_args_kwargs_stream) == \ [0, 10, 20, 30, 40, 240, 448, 656] assert recent_values(x_args_kwargs_stream) == \ [10, 15, 20, 25, 30, 130, 234, 338] assert (recent_values(ww_args_kwargs_stream) == recent_values( w_args_kwargs_stream)) assert (recent_values(xx_args_kwargs_stream) == recent_values( x_args_kwargs_stream)) #---------------------------------------------------- #---------------------------------------------------- # TEST STREAM ARRAY #---------------------------------------------------- #---------------------------------------------------- #---------------------------------------------------- # Test zip_map with StreamArray #---------------------------------------------------- x = StreamArray('x') y = StreamArray('y') z = StreamArray('z') a = StreamArray('a') def sum_array_axis_0(a_list_of_arrays): return np.sum(a_list_of_arrays, axis=0) merge_list(func=sum_array_axis_0, in_streams=[x, y], out_stream=z) def mean_array_axis_0(a_list_of_arrays): return np.mean(a_list_of_arrays, axis=0) zip_map_list(func=mean_array_axis_0, in_streams=[x, y], out_stream=a) x.extend(np.linspace(0.0, 9.0, 10)) scheduler.step() y.extend(np.linspace(0.0, 4.0, 5)) scheduler.step() expected_array = np.sum( [np.linspace(0.0, 4.0, 5), np.linspace(0.0, 4.0, 5)], axis=0) assert isinstance(z, StreamArray) assert np.array_equal(recent_values(z), expected_array) expected_means = np.linspace(0.0, 4.0, 5) assert np.array_equal(recent_values(a), expected_means) #---------------------------------------------------- # Test blend with StreamArray #---------------------------------------------------- x = StreamArray('x') y = StreamArray('y') z = StreamArray('z') a = StreamArray('a') def double(v): return 2 * v def double_add(v, addend): return 2 * v + addend ## blend(func=double, in_streams=[x, y], out_stream=z) ## blend(func=double_add, in_streams=[x, y], out_stream=a, addend=10.0) ## x.append(np.array(1.0)) ## scheduler.step() ## assert np.array_equal(recent_values(z), np.array([2.0])) ## assert np.array_equal(recent_values(a), recent_values(z)+10.0) ## x.extend(np.linspace(2.0, 3.0, 2)) ## scheduler.step() ## assert np.array_equal(recent_values(z), np.array([2., 4., 6.])) ## assert np.array_equal(recent_values(a), recent_values(z)+10.0) ## y.extend(np.linspace(100.0, 101.0, 2)) ## scheduler.step() ## assert np.array_equal(recent_values(z), [2., 4., 6., 200., 202.]) ## assert np.array_equal(recent_values(a), recent_values(z)+10.0) ## x.extend([10., 20.]) ## scheduler.step() ## assert np.array_equal(recent_values(z), [2., 4., 6., 200., 202., 20., 40.]) ## assert np.array_equal(recent_values(a), recent_values(z)+10.0) #---------------------------------------------------- # Test merge_asynch with StreamArray #---------------------------------------------------- x = StreamArray('x') y = StreamArray('y') dt_0 = np.dtype([('time', int), ('value', float)]) z = StreamArray('z', dimension=2) merge_asynch(func=lambda v: v, in_streams=[x, y], out_stream=z) scheduler.step() assert np.array_equal(recent_values(z), np.empty(shape=(0, 2))) x.append(np.array(10.0)) scheduler.step() assert np.array_equal(recent_values(z), np.array([(0, 10.0)])) y.append(np.array(1.0)) scheduler.step() assert np.array_equal(recent_values(z), [(0, 10.), (1, 1.0)]) y.append(np.array(2.0)) scheduler.step() assert np.array_equal(recent_values(z), [(0, 10.), (1, 1.0), (1, 2.0)]) x.append(np.array(20.0)) scheduler.step() assert np.array_equal(recent_values(z), [(0, 10.), (1, 1.), (1, 2.), (0, 20.)]) #---------------------------------------------------------------- # Test window merge agent with no state r = Stream('r') w = Stream('w') x = Stream('x') a = Stream('a') def h(list_of_windows): return sum([sum(window) for window in list_of_windows]) merge_window(func=h, in_streams=[r, w], out_stream=x, window_size=3, step_size=3) merge_stream = merge_window_f(func=h, in_streams=[r, w], window_size=3, step_size=3) #---------------------------------------------------------------- #---------------------------------------------------------------- # Test window merge agent with state def h_with_state(list_of_windows, state): return (sum([sum(window) for window in list_of_windows]) + state, state + 1) merge_window(func=h_with_state, in_streams=[r, w], out_stream=a, window_size=3, step_size=3, state=0) #---------------------------------------------------------------- #---------------------------------------------------------------- r.extend(list(range(16))) scheduler.step() assert recent_values(r) == list(range(16)) assert recent_values(x) == [] assert recent_values(merge_stream) == recent_values(x) assert recent_values(a) == [] w.extend([10, 12, 14, 16, 18]) scheduler.step() assert recent_values(r) == list(range(16)) assert recent_values(w) == [10, 12, 14, 16, 18] assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14)] assert recent_values(a) == [39] #---------------------------------------------------------------- r.extend([10, -10, 21, -20]) scheduler.step() assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14)] assert recent_values(a) == [39] #---------------------------------------------------------------- w.append(20) scheduler.step() assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14), (3 + 4 + 5) + (16 + 18 + 20)] assert recent_values(a) == [39, 67] #---------------------------------------------------------------- r.extend([-1, 1, 0]) scheduler.step() assert recent_values(x) == [(0 + 1 + 2) + (10 + 12 + 14), (3 + 4 + 5) + (16 + 18 + 20)] assert recent_values(a) == [39, 67] #---------------------------------------------------------------- # TEST MERGE_WINDOW WITH STREAM ARRAY #---------------------------------------------------------------- x = StreamArray('x', dimension=2) b = StreamArray('b', dimension=2) a = StreamArray('a', dimension=2) #---------------------------------------------------------------- # Test window merge agent with state def h_array(list_of_windows, state): return (sum([sum(window) for window in list_of_windows]) + state, state + 1) merge_window(func=h_array, in_streams=[x, a], out_stream=b, window_size=2, step_size=2, state=0) #---------------------------------------------------------------- x.extend(np.array([[1., 5.], [7., 11.]])) a.extend(np.array([[0., 1.], [2., 3.]])) scheduler.step() np.array_equal(recent_values(b), np.empty(shape=(0, 2))) a.extend(np.array([[0., 1.], [1., 0.]])) scheduler.step() np.array_equal(recent_values(b), np.empty(shape=(0, 2))) x.extend(np.array([[14., 18.], [18., 30.], [30., 38.], [34., 42.]])) scheduler.step() #------------------------------------------------------------------- # TEST MERGE_LIST #------------------------------------------------------------------- # Function g operates on a list of lists, one list for each input # stream, to return a single list for the output stream. x = Stream('x list merge') u = Stream('u list merge') s = Stream('s list merge') def g(list_of_lists): return [sum(snapshot) for snapshot in list(zip(*list_of_lists))] d = merge_list(func=g, in_streams=[x, u], out_stream=s, name='d') ss = merge_list_f(g, [x, u]) x.extend(list(range(4))) u.extend(list(range(10, 20, 2))) scheduler.step() assert recent_values(x) == [0, 1, 2, 3] assert recent_values(u) == [10, 12, 14, 16, 18] assert recent_values(s) == [10, 13, 16, 19] x = StreamArray() y = StreamArray() z = StreamArray(dtype='bool') def f(two_lists): return np.array(two_lists[0]) > np.array(two_lists[1]) merge_list(f, [x, y], z) x.extend(np.array([3.0, 5.0, 7.0])) y.extend(np.array([4.0, 3.0, 10.0])) run()