def test_map_list_with_arrays(): from 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 compute_func(in_streams, out_streams): def f(lst): return [v * 2 if v % 2 else v / 2 for v in lst] check_list = f(source_list) t = Stream() map_list(func=f, in_stream=in_streams[0], out_stream=t) check_correctness_of_output(in_stream=t, check_list=check_list) stream_to_file( in_stream=t, filename='single_process_single_source_map_list_example_2.dat')
def compute_func(in_streams, out_streams): def f(lst): return lst + lst check_list = [ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 ] t = Stream() map_list(func=f, in_stream=in_streams[0], out_stream=t) check_correctness_of_output(in_stream=t, check_list=check_list) stream_to_file( in_stream=t, filename='single_process_single_source_map_list_example_4.dat')
def compute_func(in_streams, out_streams): def h(v): return v < 5 def f(lst): return filter(h, lst) check_list = f(source_list) t = Stream() map_list(func=f, in_stream=in_streams[0], out_stream=t) check_correctness_of_output(in_stream=t, check_list=check_list) stream_to_file( in_stream=t, filename='single_process_single_source_map_list_example_3.dat')
def compute_func(in_streams, out_streams): ## F1 - Amplitude Adjustment def delay_and_attenuate(in_streams): tmp = deepcopy(in_streams) for i in range(len(tmp)): tmp[i] *= alpha #print("Length of tmp==tmp1 --",len(tmp)==len(tmp1)) return tmp t = Stream(initial_value=[0] * delay) w = Stream() map_list(func=delay_and_attenuate, in_stream=in_streams[0], out_stream=t) zip_map(sum, [t, in_streams[0]], w) stream_to_file(in_stream=w, filename='Reverb.dat')
def g(in_stream, out_stream, **kwargs): map_list(func, in_stream, out_stream, **kwargs) return out_stream
def test_source(): import random import multiprocessing import time scheduler = Stream.scheduler from op import map_element, map_list from merge import zip_stream, zip_stream_f # Function created by the source agent test_list = [] def random_integer(test_list, a, b): return_value = random.randint(a, b) test_list.append(return_value) return return_value def read_list(state, in_list): return in_list[state], state + 1 a = Stream('a') q = Stream('q') r = Stream('r') s = Stream('s') t = Stream('t') u = Stream('u') sb = Stream('sb') sc = Stream('sc') sd = Stream('sd') filename = 'test.dat' with open(filename, 'w') as output_file: for i in range(10): output_file.write(str(i) + '\n') scheduler.name_to_stream = {'s': s, 'r': r, 'a': a} vv = source_function(func=random_integer, stream_name='s', num_steps=5, name='random', window_size=2, test_list=test_list, a=10, b=20) ww = source_list(in_list=range(10), stream_name='r', num_steps=5, name='read list', window_size=2) xx = source_file(func=lambda x: 2 * int(x), stream_name='a', filename='test.dat', time_interval=0.5, num_steps=None) zz = source_to_stream(func=read_list, out_stream=sd, num_steps=5, name='read list', window_size=2, state=0, in_list=range(10)) map_element(lambda x: 2 * x, s, t) def f(lst): return [10 * v for v in lst] map_list(f, t, u) map_element(lambda x: x * x, r, q) p = zip_stream_f([u, q]) map_element(lambda x: x * x, sb, sc) import Queue que = Queue.Queue() test_list_source_to_queue = [] def sqf(): return random_integer(test_list_source_to_queue, 1, 10) sqq = func_to_q(func=sqf, q=que, state=None, sleep_time=0.1, num_steps=5, name='source_to_q') q_to_streams_queue = Queue.Queue() q_to_streams_test_list = range(5) q_to_streams_test_list_0 = [] q_to_streams_test_list_1 = [] for v in q_to_streams_test_list: if v % 2: q_to_streams_queue.put(('q2s_0', v)) q_to_streams_test_list_0.append(v) else: q_to_streams_queue.put(('q2s_1', v)) q_to_streams_test_list_1.append(v) q_to_streams_queue.put('_close') q2s_0 = Stream('q2s_0') q2s_1 = Stream('q2s_1') q2sss = q_to_streams(q=q_to_streams_queue, streams=[q2s_0, q2s_1]) q2s_general_queue = Queue.Queue() q2s_test_list = range(5) random_thread, random_thread_ready = vv list_thread, list_thread_ready = ww file_thread, file_thread_ready = xx stream_thread, stream_thread_ready = zz q_thread, q_thread_ready = sqq q2s_thread, q2s_thread_ready = q2sss random_thread.start() list_thread.start() file_thread.start() stream_thread.start() q_thread.start() q2s_thread.start() random_thread_ready.wait() list_thread_ready.wait() file_thread_ready.wait() stream_thread_ready.wait() q_thread_ready.wait() q2s_thread_ready.wait() sb.extend(range(5)) #random_thread.join() list_thread.join() file_thread.join() stream_thread.join() q_thread.join() q2s_thread.join() scheduler.start() random_thread.join() scheduler.join() assert recent_values(s) == test_list assert recent_values(r) == range(10) assert recent_values(a) == [v * 2 for v in range(10)] assert recent_values(t) == [v * 2 for v in recent_values(s)] assert recent_values(u) == [v * 10 for v in recent_values(t)] assert recent_values(q) == [v * v for v in recent_values(r)] assert recent_values(p) == zip(*[recent_values(u), recent_values(q)]) assert recent_values(sc) == [x * x for x in range(5)] assert recent_values(sd) == range(10) que_contents = [] while not que.empty(): que_contents.append(que.get()) assert test_list_source_to_queue == que_contents assert recent_values(q2s_0) == q_to_streams_test_list_0 assert recent_values(q2s_1) == q_to_streams_test_list_1 print 'SOURCE TEST IS SUCCESSFUL!'