def test_local_source(): # Example with a local source thread and a single process. def f(in_streams, out_streams): x = Stream('x') y = Stream('y') double(in_streams[0], x) increment(x, y) print_stream(y, name=y.name) # Specify processes and connections. processes = \ { 'process': {'in_stream_names_types': [('in', 'x')], 'out_stream_names_types': [], 'compute_func': f, 'sources': {'acceleration': {'type': 'x', 'func': source_thread_target }, }, 'actuators': {} } } connections = \ { 'process' : { 'acceleration' : [('process', 'in')] } } multicore(processes, connections)
def test_1(): # Functions wrapped by agents def f(in_streams, out_streams): double(in_streams[0], out_streams[0]) def g(in_streams, out_streams): s = Stream(name='s') increment(in_streams[0], s) print_stream(s, name=s.name) # Specify processes and connections. processes = \ { 'source_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': f, 'sources': {'acceleration': {'type': 'i', 'func': source_thread_target }, }, 'actuators': {} }, 'aggregate_and_output_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [], 'compute_func': g, 'sources': {}, 'actuators': {} } } connections = \ { 'source_process' : { 'out' : [('aggregate_and_output_process', 'in')], 'acceleration' : [('source_process', 'in')] }, 'aggregate_and_output_process': {} } multicore(processes, connections)
def test_ntp_1(): ntp_obj = ntp_mgr("0.us.pool.ntp.org") v = ntp_obj.get_offset() def source_thread_target(source): num_steps = 3 for i in range(num_steps): v = ntp_obj.get_offset() copy_data_to_source([v], source) time.sleep(0.01) source_finished(source) def compute_func(in_streams, out_streams): print_stream(in_streams[0]) # Specify processes and connections. processes = \ { 'process': {'in_stream_names_types': [('in', 'f')], 'out_stream_names_types': [], 'compute_func': compute_func, 'sources': {'ntp_times': {'type': 'f', 'func': source_thread_target }, }, 'actuators': {} } } connections = \ { 'process' : { 'ntp_times' : [('process', 'in')] } } multicore(processes, connections)
def test_parameter(ADDEND_VALUE): # Functions wrapped by agents # Function f is used in get_source_data_and_compute_process # ADDEND is a keyword arg of f. # Note: ADDEND must be passed in the specification of # the process. See the line: # 'keyword_args' : {'ADDEND' :ADDEND_VALUE}, def f(in_streams, out_streams, ADDEND): gg(in_streams[0], out_streams[0], ADD_VALUE=ADDEND) # Function g is used in aggregate_and_output_process # Function g has no arguments other than in_streams and out_streams. # So we do not have to add 'keyword_args' : {} # to the specification of the process. def g(in_streams, out_streams): s = Stream(name='s') increment(in_stream=in_streams[0], out_stream=s) print_stream(s, name=s.name) # Target of source thread. def source_thread_target(source): num_steps = 2 step_size = 4 for i in range(num_steps): data = list(range(i * step_size, (i + 1) * step_size)) copy_data_to_source(data, source) time.sleep(0.001) source_finished(source) return #--------------------------------------------------------------------- # Specify processes and connections. # This example has two processes: # (1) get_source_data_and_compute_process and # (2) aggregate_and_output_process. # Specification of get_source_data_and_compute_process: # (1) Inputs: It has a single input stream called 'in' which # is of type int ('i'). # (2) Outputs: It has a single output stream called 'out' # which is of type int ('i'). # (3) Computation: It creates a network of agents that carries # out computation in the main thread by calling function f. # (4) Keyword arguments: Function f has a keyword argument # called ADDEND. This argument must be a constant. # (5) sources: This process has a single source called # 'acceleration'. The source thread target is specified by # the function source_thread_target. This function generates # int ('i'). # (6) actuators: This process has no actuators. # Specification of aggregate_and_output_process: # (1) Inputs: It has a single input stream called 'in' which # is of type int ('i'). # (2) Outputs: It has no outputs. # (3) Computation: It creates a network of agents that carries # out computation in the main thread by calling function g. # (4) Keyword arguments: Function g has no keyword argument # (5) sources: This process has no sources # (6) actuators: This process has no actuators. # Connections between processes. # (1) Output 'out' of 'get_source_data_and_compute_process' is # connected to input 'in' of aggregate_and_output_process. # (2) The source, 'acceleration', of 'get_source_data_and_compute_process' # is connected to input 'in' of 'get_source_data_and_compute_process'. processes = \ { 'get_source_data_and_compute_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': f, 'keyword_args' : {'ADDEND' :ADDEND_VALUE}, 'sources': {'acceleration': {'type': 'i', 'func': source_thread_target }, }, 'actuators': {} }, 'aggregate_and_output_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [], 'compute_func': g, 'keyword_args' : {}, 'sources': {}, 'actuators': {} } } connections = \ { 'get_source_data_and_compute_process' : { 'out' : [('aggregate_and_output_process', 'in')], 'acceleration' : [('get_source_data_and_compute_process', 'in')] }, 'aggregate_and_output_process': {} } multicore(processes, connections)
def test_4(): # Functions wrapped by agents def f(in_streams, out_streams): identical(in_streams[0], out_streams[0]) def g(in_streams, out_streams): multiply(in_streams[0], out_streams[0], multiplicand=2) def h(in_streams, out_streams): square(in_streams[0], out_streams[0]) def m(in_streams, out_streams): s = Stream('s') sum_numbers(in_streams, s) print_stream(s, name='s') processes = \ { 'source_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': f, 'sources': {'acceleration': {'type': 'i', 'func': source_thread_target }, } }, 'multiply_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': g, 'sources': {} }, 'square_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': h, 'sources': {} }, 'merge_process': {'in_stream_names_types': [('in_multiply', 'i'), ('in_square', 'i')], 'out_stream_names_types': [], 'compute_func': m, 'sources': {} } } connections = \ { 'source_process' : { 'out' : [('multiply_process', 'in'), ('square_process', 'in')], 'acceleration' : [('source_process', 'in')] }, 'multiply_process': { 'out' : [('merge_process', 'in_multiply')] }, 'square_process': { 'out' : [('merge_process', 'in_square')] }, 'merge_process': { } } multicore(processes, connections)
def test_3(): # Functions wrapped by agents def f(in_streams, out_streams): multiply_and_add(in_streams[0], out_streams[0], multiplicand=2, addend=1) def g(in_streams, out_streams): t = Stream('t') filter_then_square(in_streams[0], t, filter_threshold=20) print_stream(t, name='p1') def sums(in_streams, out_streams): s = Stream('s') sum_window(in_streams[0], s, window_size=3, step_size=3) print_stream(s, name=' p2') processes = \ { 'source_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': f, 'sources': {'acceleration': {'type': 'i', 'func': source_thread_target }, } }, 'process_1': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [], 'compute_func': g, 'sources': {} }, 'process_2': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [], 'compute_func': sums, 'sources': {} } } connections = \ { 'source_process' : { 'out' : [('process_1', 'in'), ('process_2', 'in')], 'acceleration' : [('source_process', 'in')] }, 'process_1': { }, 'process_2': { } } multicore(processes, connections)
def test_2(): # Functions wrapped by agents def f(in_streams, out_streams): multiply_and_add(in_streams[0], out_streams[0], multiplicand=2, addend=1) def g(in_streams, out_streams): filter_then_square(in_streams[0], out_streams[0], filter_threshold=20) def h(in_streams, out_streams): s = Stream('s') sum_window(in_streams[0], s, window_size=3, step_size=3) print_stream(s, name=s.name) # Specify processes and connections. processes = \ { 'source_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('out', 'i')], 'compute_func': f, 'sources': {'acceleration': {'type': 'i', 'func': source_thread_target }, }, 'actuators': {} }, 'filter_and_square_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [('filtered', 'i')], 'compute_func': g, 'sources': {}, 'actuators': {} }, 'aggregate_and_output_process': {'in_stream_names_types': [('in', 'i')], 'out_stream_names_types': [], 'compute_func': h, 'sources': {}, 'actuators': {} } } connections = \ { 'source_process' : { 'out' : [('filter_and_square_process', 'in')], 'acceleration' : [('source_process', 'in')] }, 'filter_and_square_process' : { 'filtered' : [('aggregate_and_output_process', 'in')], }, 'aggregate_and_output_process': {} } multicore(processes, connections)