def test_(self): def copy(list_of_in_lists, state): return ([ in_list.list[in_list.start:in_list.stop] for in_list in list_of_in_lists ], state, [in_list.stop for in_list in list_of_in_lists]) input_stream_0 = Stream('input_stream_0', num_in_memory=32) input_stream_1 = Stream('input_stream_1', num_in_memory=32) output_stream_0 = Stream('output_stream_0', num_in_memory=32) output_stream_1 = Stream('output_stream_1', num_in_memory=32) A = Agent(in_streams=[input_stream_0, input_stream_1], out_streams=[output_stream_0, output_stream_1], transition=copy, name='A') input_stream_0.extend(list(range(10))) run() assert (output_stream_0.stop == 10) assert (output_stream_1.stop == 0) assert (output_stream_0.recent[:10] == list(range(10))) assert (input_stream_0.start == {A: 10}) assert (input_stream_1.start == {A: 0}) input_stream_1.extend(list(range(10, 25, 1))) run() assert (output_stream_0.stop == 10) assert (output_stream_1.stop == 15) assert (output_stream_0.recent[:10] == list(range(10))) assert (output_stream_1.recent[:15] == list(range(10, 25, 1))) assert (input_stream_0.start == {A: 10}) assert (input_stream_1.start == {A: 15})
def test_class(self): class C(object): def __init__(self): return def f(self, value): if value > 0: return self.pos(value) else: return self.neg(value) def pos(self, value): return value * value def neg(self, value): return value + value s = Stream('s') t = Stream('t') c = C() @map_e def g(v): return c.f(v) g(in_stream=s, out_stream=t) s.extend(list(range(-4, 4))) run() assert (recent_values(t) == [-8, -6, -4, -2, 0, 1, 4, 9])
def test_thread_2(self): @merge_sink_e def f(list_of_elements, q_out): q_out.put(sum(list_of_elements)) x = Stream('x') y = Stream('y') q_in = queue.Queue() q_out = queue.Queue() f([x, y], q_out=q_out) streams = [x, y] finished = _close thr = threading.Thread(target=self.thread_target, args=(q_in, q_out, streams, finished)) thr.start() # Put data into input queue q_in.put(('x', 1)) q_in.put(('y', 100)) q_in.put(finished) output = [] while True: w = q_out.get() if w == finished: break else: output.append(w) thr.join() assert output == [101]
def test_r_sub(self): x = Stream() y = Stream() r_sub(x, y, 2) x.extend(list(range(5))) run() assert recent_values(y) == [-2, -1, 0, 1, 2]
def test_r_mul(self): x = Stream() y = Stream() a = r_mul(x, y, 2) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]
def test_source_file(self, filename='tests/test_source_file_name.txt'): s = Stream('s') with open(filename, 'r') as input_file: for line in input_file: s.append(int(line)) run() assert recent_values(s) == [1, 2, 3]
def test_sieve(self): x = Stream() primes = [] sieve(x, primes) x.extend(list(range(2, 30))) Stream.scheduler.step() assert primes == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
def count_pos_and_non_pos(self, count, lst): """ Parameters ---------- count : list list with at least 2 elements lst : list The input list Return ------ count[0]: number of non-positive values in lst count[1]: number of positive values in lst. """ @sink_e def f(v, count): if v > 0: count[1] += 1 else: count[0] += 1 x = Stream('x') f(x, count=count) x.extend(lst) run()
def test_f_add(self): x = Stream() K = 5 y = f_add(x, K) x.extend(list(range(3))) run() assert recent_values(y) == [5, 6, 7]
def test_echo(self): spoken = Stream('spoken') heard = make_echo(spoken, D=1, A=0.5) spoken.extend([64, 32, 16, 8, 4, 2, 1, 0, 0, 0, 0]) run() assert recent_values(heard) == [ 64.0, 64.0, 48.0, 32.0, 20.0, 12.0, 7.0, 3.5, 1.75, 0.875, 0.4375 ]
def test_r_add(self): x = Stream() y = Stream() z = 5 r_add(x, y, z) x.extend(list(range(3))) run() assert recent_values(y) == [5, 6, 7]
def f_echo(in_streams, out_streams, delay): sound_made, attenuated = in_streams echo = Stream('echo') echo.extend([0] * delay) map_element(lambda v: v, attenuated, echo) # The zip_map output is the sound heard which is # the sound heard plus the echo. zip_map(sum, [sound_made, echo], out_streams[0])
def test_initial_value(self): def double(v): return v * 2 x = Stream('x') y = Stream(name='y', initial_value=[0] * 5) a = map_element(func=double, in_stream=x, out_stream=y) x.extend(list(range(5))) run() assert recent_values(y) == [0] * 5 + [0, 2, 4, 6, 8]
def test_example_1(self): # Specify streams x = Stream('x') y = Stream('y') # Specify encapsulated functions (if any) def f(v): return 2 * v # Specify agents. map_element(func=f, in_stream=x, out_stream=y) # Execute a step # Put test values in the input streams. x.extend(list(range(3))) # Execute a step run() # Look at recent values of output streams. assert recent_values(y) == [0, 2, 4] # Execute a step # Put test values in the input streams. x.extend([10, 20, 30]) # Execute a step run() # Look at recent values of output streams. assert recent_values(y) == [0, 2, 4, 20, 40, 60] # Execute a step # Put test values in the input streams. x.extend([0, -10]) # Execute a step run() # Look at recent values of output streams. assert recent_values(y) == [0, 2, 4, 20, 40, 60, 0, -20]
def test_timed_window(self): x = Stream('x') y = Stream('y') def f(v): return v timed_window(func=f, in_stream=x, out_stream=y, window_duration=10, step_time=10) x.extend([(1, 'a'), (8, 'b'), (12, 'c')]) run() assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')])]) x.extend([(14, 'd'), (36, 'e'), (43, 'g'), (75, 'h')]) run() assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')]), (20, [(12, 'c'), (14, 'd')]), (40, [(36, 'e')]), (50, [(43, 'g')])]) x.extend([(79, 'i'), (101, 'j')]) run() assert (recent_values(y) == [(10, [(1, 'a'), (8, 'b')]), (20, [(12, 'c'), (14, 'd')]), (40, [(36, 'e')]), (50, [(43, 'g')]), (80, [(75, 'h'), (79, 'i')])]) return
def test_multiple_relations_2(self): @map_e def double(v): return v * 2 x = Stream('x', [10, 11]) y = Stream('y') double(x, y) double(x, y) x.extend(list(range(5))) run()
def test_sink_2(self): x = Stream() y = Stream() @sink_e def f(v, out_stream): out_stream.append(v + 100) f(x, out_stream=y) x.extend(list(range(5))) run()
def test_sink_1(self): x = Stream() y = Stream() @sink_w def f(v, out_stream): out_stream.append(sum(v) + 10) f(x, window_size=2, step_size=1, out_stream=y) x.extend(list(range(5))) run()
def test(): publisher = PikaPublisher( routing_key='temperature', exchange='publications', host='localhost') x = Stream('x') y = Stream('y') map_element(lambda v: 2*v, x, y) publisher.publish(y) for i in range(3): x.extend(list(range(i*4, (i+1)*4))) run() time.sleep(0.001)
def test_filter_number_1(self): input_list = list(range(-5, 5)) in_stream = Stream('input') out_stream = Stream('output') def f(v): return v > 0 filter_element(f, in_stream, out_stream) in_stream.extend(input_list) run() assert recent_values(out_stream) == list(filter(f, input_list))
def test_map_with_state(self): x = Stream() y = Stream() @map_e def f(v, state): return v + state, state + 1 f(x, y, state=0) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]
def test_map_window_with_state(self): x = Stream() y = Stream() @map_w def f(window, state): return sum(window) + state, state + 1 f(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(10))) run() assert recent_values(y) == [1, 6, 11, 16, 21]
def test_map_window_list_2(self): x = Stream() y = Stream() @map_wl def f(window, state): return [v + 10 * state for v in window], state + 1 f(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(10))) run() assert recent_values(y) == [0, 1, 12, 13, 24, 25, 36, 37, 48, 49]
def test_map_window_list_1(self): x = Stream() y = Stream() @map_wl def f(window): return [2 * v for v in window] f(x, y, window_size=2, step_size=2) x.extend(list(range(10))) run() assert recent_values(y) == list(range(0, 20, 2))
def test_map_with_keyword_arg(self): x = Stream() y = Stream() @map_e def f(v, k): return v + k f(x, y, k=10) x.extend(list(range(5))) run() assert recent_values(y) == [10, 11, 12, 13, 14]
def square_and_count_pos_and_non_pos(self, count, input_list): @map_e def f(v, count): if v > 0: count[1] += 1 else: count[0] += 1 return v * v x = Stream('x') y = Stream('y') f(in_stream=x, out_stream=y, count=count) x.extend(input_list) run() return recent_values(y)
def test_map_window_list_3(self): x = Stream() y = Stream() @map_wl def f(window, state, K): return [v + 10 * state + K for v in window], state + 1 f(x, y, window_size=2, step_size=2, state=0, K=100) x.extend(list(range(10))) run() assert recent_values(y) == [ 100, 101, 112, 113, 124, 125, 136, 137, 148, 149 ]
def test_1(self): # From map_element_examples x = Stream('x') y = Stream('y') def f(in_stream_element): out_stream_element = 2 * in_stream_element return out_stream_element map_element(func=f, in_stream=x, out_stream=y) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]
def test_delay(self): y = Stream(initial_value=[0] * 5) x = Stream() @map_e def f(v): return 2 * v f(x, y) x.extend(list(range(10))) run() assert recent_values(y) == [ 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ]
def test_pass_parameter(self): result = [] def f(v, state, result): state += v result.append(state) return v, state x = Stream('x') y = Stream('y') map_element(func=f, in_stream=x, out_stream=y, state=0, result=result) x.extend(list(range(5))) run() assert result == [0, 1, 3, 6, 10]