def map_window_examples_simple(): #---------------------------------------------------------------- # Example with no state and no additional parameters #---------------------------------------------------------------- # Create streams x = Stream('x') s = Stream() # Create agents map_window(func=sum, in_stream=x, out_stream=s, window_size=3, step_size=10) # Explanation of agent # The function sum() returns the sum of the window. # out_stream[j] = \ # func(in_stream[j*step_size : j*step_size + window_size]) # for all j. # So if window_size is 3 and step_size is 10: # s[j] = x[10*j] + x[10*j+1] + x[10*j+2], for all j # For window_size of 3 and step_size of 10, the output # stream will contain: # [0 + 1 + 2, 10 + 11 + 12, 20 + 21 + 22, 30 + 31 + 32] # Put data into input streams and run. DATA = list(range(40)) x.extend(DATA) run() # Inspect output assert recent_values(s) == [3, 33, 63, 93]
def run_stream_from_config(config): r = redis.from_url(config.redis_url) listener = stream.RedisPublishListener(r, config.status_channel) # Blocks and processes stream stream.run(config.consumer_key, config.consumer_secret, config.access_token, config.access_token_secret, listener)
def test_echo_1(): spoken = Stream('spoken') heard = make_echo_1(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 map_window_example_simple_with_state_and_parameter(): #---------------------------------------------------------------- # Example with state and additional parameters #---------------------------------------------------------------- # Declare functions # This function returns an element of the output stream and then # next state. # The parameter is multiplicand. def maxes(in_stream_window, state, multiplicand): next_output_element = sum(in_stream_window) > multiplicand * state next_state = max(state, sum(in_stream_window)) return (next_output_element, next_state) # Create streams x = Stream('x') m = Stream('m') # Create agents map_window(func=maxes, in_stream=x, out_stream=m, state=0, window_size=2, step_size=3, multiplicand=1.5) # Explanation of agent # in_stream_window on step j is: # x[step_size*j : step_size*j + window_size] # For j > 0: state[j] = \ # max(state[j-1], sum(x[step_size*j : step_size*j + window_size])) # m[j] = sum(x[step_size*j : step_size*j + window_size]) > \ # multiplicand * state[j] # With a window_size of 2 and step_size of 3 and multiplicand of 1.5 # state[j] is max(state[j-1], x[3*j, 3*j+1]) # m[j] is (x[3*j, 3*j+1]) > 1.5*state[j] # Put data into input streams and run. DATA = [1, 1, 0, -1, 0, 3, 1, 5, 11, 1] # With window_size=3, step_size=2, and this DATA the output steps are: # Initially state = 0, because of the specification in max_window # m[0] is (DATA[0]+DATA[1]) > 1.5*0, which is (1 + 1) > 0 or True. # state[1] is max(0, DATA[0]+DATA[1]) which is 2. # m[1] is (DATA[3]+DATA[4]) > 1.5*2 which is (-1 + 0) > 2 or False. # state[2] is max(2, DATA[3]+DATA[4]) which is 2. # m[2] is (DATA[6]+DATA[7]) > 1.5*2 which is (1 + 5) > 2 or True. # state[3] is max(2, DATA[6]+DATA[7]) which is 6. # So the output stream is: # [True, False, True, ....] x.extend(DATA) run() # Inspect output assert recent_values(m) == [True, False, True]
def echo_output(input_sound, D, A): """ Same as make_echo except that input_sound is a list or array and is not a Stream. """ spoken = Stream('spoken') heard = make_echo(spoken, D, A) spoken.extend(input_sound) run() return recent_values(heard)
def map_window_example_simple_with_state(): #---------------------------------------------------------------- # Example with state and no additional parameters #---------------------------------------------------------------- # Declare functions def comp(this_list, state): # this_list is a list # state is a number # return next element of output stream, next state next_state = sum(this_list) next_output_element = sum(this_list) > state return next_output_element, next_state # Create streams x = Stream('x') c = Stream('c') # Create agents map_window(func=comp, in_stream=x, out_stream=c, state=0, window_size=2, step_size=4) # Explanation of agent # The initial state is 0 # The initial value of this_list is x[:window_size] # The zeroth element of the output stream is: # func(x[:window_size], state) # c[0] = sum(x[:window_size]) > 0 because 0 is the initial state # c[j] = sum(x[step_size*j:step_size*j+window_size]) > # sum(x[step_size*(j-1):step_size*(j-1)+window_size]) # Put data into input streams and run. DATA = [1, 10, 0, -1, 2, 3, -10, -20, 11, 1] # With window_size=2, step_size=4, and this DATA the output is: # [(1 + 10 > 0), (2 + 3 > 1 + 10), (11 + 1 > 2 + 3)] which is # [True, False, True] x.extend(DATA) run() # Inspect output assert recent_values(c) == [True, False, True]
def map_window_example_simple_with_parameter(): #---------------------------------------------------------------- # Example with no state and additional parameters #---------------------------------------------------------------- # The additional parameter is threshold # Since this agent has no state, this function returns a single # value which is appended to the output stream. # Declare functions def thresh(in_stream_window, threshold): return sum(in_stream_window) > threshold # Create streams x = Stream('x') t = Stream('t') # Create agents map_window(func=thresh, in_stream=x, out_stream=t, window_size=3, step_size=2, threshold=0) # Explanation of agent # For all j, : t[j] is True if and only if # sum(x[window_size*j : step_size*j+window_size]) > threshold. # With window_size of 3 and step_size of 2, and threshold of 5 # the output is # [sum(x[0:3]) > 0, sum(x[2:5] > 0, sum(x[4:7] > 0, ...] # # Note: You can use any names as arguments in the # definition of func, as in: # def thresh(v, s): return sum(v) > w # Put data into input streams and run. DATA = [1, 1, 0, -1, 0, 3, -1, -20, 11, 1] # With window_size=3, step_size=2, and this DATA the output is: # [(1 + 1 + 0 > 0), (0 + (-1) + 0 > 0), (0 + 3 + (-1) > 0), ..] which is # [True, False, True] x.extend(DATA) run() # Inspect output assert recent_values(t) == [True, False, True, False]
def examples(): #----------------------------------------------- # Example with @fmap_w and @map_w @fmap_w def sum_window(window): return sum(window) @map_w def total_window(window): return sum(window) r = Stream() s = Stream() t = sum_window(r, window_size=2, step_size=2) total_window(in_stream=r, out_stream=s, window_size=2, step_size=2) r.extend(list(range(10))) run() assert recent_values(t) == [1, 5, 9, 13, 17] assert recent_values(t) == recent_values(s) #----------------------------------------------- # Example with keyword argument @fmap_w def sum_add(v, addend): return sum(v) + addend s = Stream() t = sum_add(s, window_size=2, step_size=2, addend=10) s.extend(list(range(10))) Stream.scheduler.step() assert recent_values(t) == [11, 15, 19, 23, 27] # Example with keyword argument using map_w @map_w def sum_add_relation(v, addend): return sum(v) + addend s = Stream() t = Stream() sum_add_relation(s, t, window_size=2, step_size=2, addend=10) s.extend(list(range(10))) Stream.scheduler.step() assert recent_values(t) == [11, 15, 19, 23, 27] #----------------------------------------------- # Example with state and keyword argument @fmap_w def h(v, state, addend): next_state = state + 1 return sum(v) + state + addend, next_state s = Stream() t = h(s, window_size=2, step_size=2, state=0, addend=10) s.extend(list(range(10))) run() assert recent_values(t) == [11, 16, 21, 26, 31] #----------------------------------------------- # Output stream is the average of the max of # successive windows @fmap_w def h(window, state): count, total = state next_total = total + max(window) next_count = count + 1 next_output = next_total / next_count next_state = next_count, next_total return next_output, next_state s = Stream() t = h(s, window_size=4, step_size=4, state=(0, 0.0)) s.extend(list(range(20))) run() assert recent_values(t) == [3.0, 5.0, 7.0, 9.0, 11.0] return
def examples_filter_element(): x = Stream('x') #---------------------------------------------------------------- # Filter to only have even numbers #---------------------------------------------------------------- even = Stream() filter_element(func=lambda v: not v % 2, in_stream=x, out_stream=even) # Example: If x = [0, 1, 2, 3, ... ] then y is [0, 2, 4, ...] #---------------------------------------------------------------- # Filter to only have odd numbers #---------------------------------------------------------------- odd = Stream() filter_element(func=lambda v: v % 2, in_stream=x, out_stream=odd) #---------------------------------------------------------------- # Filter to only have negative numbers #---------------------------------------------------------------- neg = Stream('negative') filter_element(func=lambda v: v < 0, in_stream=x, out_stream=neg) #---------------------------------------------------------------- # Filter to only have non_negativenumbers #---------------------------------------------------------------- non_neg = Stream('non_negative') filter_element(func=lambda v: v >= 0, in_stream=x, out_stream=non_neg) #---------------------------------------------------------------- # filter_element with state and no additional arguments #---------------------------------------------------------------- def less_than_n(v, state): next_output_element = (v <= state) next_state = state + 1 return next_output_element, next_state y = Stream('y') less = Stream() filter_element(func=less_than_n, in_stream=y, out_stream=less, state=0) # State on j-th step is j. # less_than_n(v, state) returns (v < j) on the j-th step. # less filters out all elements v for which v > j # So if y is [1, 5, 0, 2, 6, 3] then since states are [ 0, 1, 2, 3, 4,..] # then since not(y[0] <= 0), not(y[1] <= 1), # y[2] <= 2, y[3] <=3, .... the sequence of outputs of the function # less_than_v are [(False, 0), (False, 1), (True, 2), (True, 3), ...]. So # the output stream contains y[2], y[3], ... or [0, 2, ...] #---------------------------------------------------------------- # filter_element with state and with additional keyword arguments #---------------------------------------------------------------- # The keyword argument is addend. def less_than_n_plus_addend(v, state, addend): # return pair: boolean filter, next state return v <= state + addend, state + 1 z = Stream('z') less_addend = Stream() filter_element(func=less_than_n_plus_addend, in_stream=z, out_stream=less_addend, state=0, addend=3) # State on j-th step is j. # Stream less contains z[j] if and only if z[j] <= j+3 # For example, if z = [2, 3, 3, 4, 10, 15, 7, .....] then the # output stream is [2, 3, 3, 4, 7, ...] #---------------------------------------------------------------- # filter out numbers above the threshold #---------------------------------------------------------------- def threshold(v, threshold): return v > threshold above_threshold = Stream('above threshold') filter_element(func=threshold, in_stream=x, out_stream=above_threshold, threshold=0) # Put data into input streams and run. DATA_x = list(range(-5, 5, 1)) x.extend(DATA_x) DATA_y = [1, 5, 0, 2, 6, 3] y.extend(DATA_y) DATA_z = [2, 3, 3, 4, 10, 15, 7] z.extend(DATA_z) run() # Inspect output assert recent_values(even) == [-4, -2, 0, 2, 4] assert recent_values(odd) == [-5, -3, -1, 1, 3] assert recent_values(non_neg) == [0, 1, 2, 3, 4] assert recent_values(neg) == [-5, -4, -3, -2, -1] assert recent_values(less) == [0, 2, 3] assert recent_values(less_addend) == [2, 3, 3, 4, 7] assert recent_values(above_threshold) == [1, 2, 3, 4]
import stream stream.run()
def examples(): #---------------------------------------------- # EXAMPLE: SIMPLE SPLIT # Split a stream into a list of streams. In this # example, a stream (s) is split into two streams: # u and v. # Decorate a conventional function to get a # stream function. This function returns a list # of two values corresponding to the two output # streams. @split_e def h(x): return [2 * x, x + 1000] # Create streams. s = Stream() u = Stream() v = Stream() # Create agents by calling the decorated function. h(s, [u, v]) # Put data into input streams. DATA = list(range(5)) s.extend(DATA) # Run the agents. run() # Check values of output streams. assert recent_values(u) == [2 * x for x in DATA] assert recent_values(v) == [x + 1000 for x in DATA] #---------------------------------------------- # EXAMPLE: SPLIT WITH KEYWORD ARGUMENT # Split a stream into a list of streams. Use # a keyword argument in the splitting function. # Decorate a conventional function to get a # stream function. This function returns a list # of two values corresponding to the two output # streams. addend is a keyword argument in the # function that creates agents. @split_e def h(x, addend): return [x + addend, x + 1000 + addend] # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function. ADDEND = 10 h(s, [u, v], addend=ADDEND) # Put data into input streams. s.extend(DATA) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [x + ADDEND for x in DATA] assert recent_values(v) == [x + 1000 + ADDEND for x in DATA] #---------------------------------------------- # EXAMPLE: SPLIT WITH KEYWORD ARGUMENT AND STATE # Split a stream into a list of streams, with # a keyword argument and state. # Decorate a conventional function to get a # stream function. addend and multiplicand are # keyword arguments used in the call to create # agents. The function h returns 2 values: # (1) a list of two numbers corresponding to the # two output streams and # (2) the next state. @split_e def h(v, state, addend, multiplicand): next_state = state + 2 return ([v + addend + state, v * multiplicand + state], next_state) # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function to create agents. The initial state # is 0. Include keyword arguments in the call. ADDEND = 10 MULTIPLICAND = 2 h(s, [u, v], state=0, addend=ADDEND, multiplicand=MULTIPLICAND) # Put data into input streams. s.extend(DATA) # Run the agent. run() # Check values of output streams. assert recent_values(u) == [10, 13, 16, 19, 22] assert recent_values(v) == [0, 4, 8, 12, 16] #---------------------------------------------- # EXAMPLE: SPLIT WITH STATE AND NO KEYWORD ARGUMENTS # Split a stream into a list of streams, with # a state. # Decorate a conventional function to get a # stream function. This function returns two values # a list and the next state, where the list has two # values with one value for each output streams. @split_e def h(v, state): next_state = state + 1 return [v + state, v + 1000 + state], next_state # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function to create agents. h(in_stream=s, out_streams=[u, v], state=0) # Put data into input streams. s.extend(DATA) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [0, 2, 4, 6, 8] assert recent_values(v) == [1000, 1002, 1004, 1006, 1008] #---------------------------------------------- # EXAMPLE: SPLIT USING FUNCTIONAL FORM FOR # SPLITTING A STREAM INTO 2 STREAMS. # Split a stream into exactly two streams. # This is in functional form, i.e. it creates # and returns two streams. # Decorate a conventional function to get a # stream function. @fsplit_2e def h(v): return [v, v + 1000] # Create streams. s = Stream() # Call decorated function to create agents # Note that h creates streams u, v. It creates # 2 streams because the decorator is fsplit_2e u, v = h(s) # Put data into input streams. s.extend(DATA) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == DATA assert recent_values(v) == [1000 + x for x in DATA] #---------------------------------------------- # EXAMPLE: SPLIT USING FUNCTIONAL FORM FOR # SPLITTING A STREAM INTO 2 STREAMS. # Split a stream into exactly two streams, with a # keyword argument. This is in functional # form, i.e. it creates and returns two streams. # Decorate a conventional function to get a # stream function. @fsplit_2e def h(v, addend): return [v + addend, v + 1000 + addend] # Create streams. s = Stream() # Call decorated function to create agents. Note # functional form. u, v = h(s, addend=10) # Put data into input streams. s.extend(DATA) # Run the agents. run() # Check values of output streams. assert recent_values(u) == [10, 11, 12, 13, 14] assert recent_values(v) == [1010, 1011, 1012, 1013, 1014] #---------------------------------------------- # EXAMPLE: FUNCTIONAL FORM # Split a stream into exactly two streams, with a # state and keyword argument. This is in functional # form, i.e. it creates and returns two streams. # Decorate a conventional function to get a # stream function. @fsplit_2e def h(v, state, addend): next_state = state + 1 return ([v + addend + state, v + 1000 + addend + state], next_state) # Create streams. s = Stream() # Call decorated function. u, v = h(s, state=0, addend=10) # Put data into input streams. s.extend(list(range(5))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [10, 12, 14, 16, 18] assert recent_values(v) == [1010, 1012, 1014, 1016, 1018] #---------------------------------------------- # Split a stream into exactly two streams, with a # state. This is in functional form, # i.e. it creates and returns two streams. # Decorate a conventional function to get a # stream function. @fsplit_2e def hk(v, state): next_state = state + 1 return [v + state, v + 1000 + state], next_state # Create streams. s = Stream() # Call decorated function. u, v = h(s, state=0, addend=10) u, v = hk(s, state=0) # Put data into input streams. s.extend(list(range(5))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [0, 2, 4, 6, 8] assert recent_values(v) == [1000, 1002, 1004, 1006, 1008] #---------------------------------------------- # Split a stream into a list of streams. # Window operation # Decorate a conventional function to get a # stream function. @split_w def h(window): return [sum(window), max(window)] # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function. h(s, [u, v], window_size=3, step_size=2) # Put data into input streams. s.extend(list(range(12))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [3, 9, 15, 21, 27] assert recent_values(v) == [2, 4, 6, 8, 10] #---------------------------------------------- # Split a stream into a list of streams with # keyword argument. Window operation # Decorate a conventional function to get a # stream function. @split_w def h(window, addend): return [sum(window) + addend, max(window) + addend] # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function to create agents. h(s, [u, v], window_size=3, step_size=2, addend=1000) # Put data into input streams. s.extend(list(range(12))) # Run the agents. run() # Check values of output streams. assert recent_values(u) == [1003, 1009, 1015, 1021, 1027] assert recent_values(v) == [1002, 1004, 1006, 1008, 1010] #---------------------------------------------- # Split a stream into a list of streams with state and # keyword argument. Window operation # Decorate a conventional function to get a # stream function. @split_w def h(window, state, addend): next_state = state + 1 return ([sum(window) + addend + state, max(window) + addend + state], next_state) # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function. h(s, [u, v], window_size=3, step_size=2, state=0, addend=1000) # Put data into input streams. s.extend(list(range(12))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [1003, 1010, 1017, 1024, 1031] assert recent_values(v) == [1002, 1005, 1008, 1011, 1014] #---------------------------------------------- # SPLITTING WITH WINDOWS #---------------------------------------------- # EXAMPLE # Split a stream into a list of streams with state. # Window operation # Decorate a conventional function to get a # stream function. The first argument of the function # is a list, i.e., the window. The function returns # two values: a list and the next state where the list # has one item for eah output stream. @split_w def h(window, state): next_state = state + 1 return [sum(window) + state, max(window) + state], next_state # Create streams. s = Stream() u = Stream() v = Stream() # Call decorated function to create agents. h(s, [u, v], window_size=3, step_size=2, state=0) # Put data into input streams. s.extend(list(range(12))) # Run the agents. run() # Check values of output streams. assert recent_values(u) == [3, 10, 17, 24, 31] assert recent_values(v) == [2, 5, 8, 11, 14] #---------------------------------------------- # Split a stream into exactly TWO streams. # WINDOW operation # Decorate a conventional function to get a # stream function. This is in functional form, # i.e. it creates and returns a list of streams. @fsplit_2w def h(window): return sum(window), max(window) # Create streams. s = Stream() # Call decorated function. This function # creates and returns two streams. u, v = h(s, window_size=3, step_size=2) # Put data into input streams. s.extend(list(range(12))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [3, 9, 15, 21, 27] assert recent_values(v) == [2, 4, 6, 8, 10] #---------------------------------------------- # Split a stream into exactly two streams with # keyword argument. Window operation # Decorate a conventional function to get a # stream function. This is in functional form, # i.e. it creates and returns two streams. @fsplit_2w def h(window, addend): return sum(window) + addend, max(window) + addend * 2 # Create streams. s = Stream() # Call decorated function. This function # creates and returns two streams. u, v = h(s, window_size=3, step_size=2, addend=1000) # Put data into input streams. s.extend(list(range(12))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [1003, 1009, 1015, 1021, 1027] assert recent_values(v) == [2002, 2004, 2006, 2008, 2010] #---------------------------------------------- # Split a stream into exactly two streams with # state and keyword argument. Window operation # Decorate a conventional function to get a # stream function. This is in functional form, # i.e. it creates and returns two streams. @fsplit_2w def h(window, state, addend): next_state = state + 1 return ([ sum(window) + addend + state, max(window) + addend * 2 - state ], next_state) # Create streams. s = Stream() # Call decorated function. This function # creates and returns two streams. u, v = h(s, window_size=3, step_size=2, state=0, addend=1000) # Put data into input streams. s.extend(list(range(12))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [1003, 1010, 1017, 1024, 1031] assert recent_values(v) == [2002, 2003, 2004, 2005, 2006] #---------------------------------------------- # Split a stream into exactly two streams with # state. Window operation # Decorate a conventional function to get a # stream function. This is in functional form, # i.e. it creates and returns two streams. @fsplit_2w def h(window, state): next_state = state + 1 return [sum(window) + state, max(window) - state], next_state # Create streams. s = Stream() # Call decorated function. This function # creates and returns two streams. u, v = h(s, window_size=3, step_size=2, state=0) # Put data into input streams. s.extend(list(range(12))) # Run the decorated function. run() # Check values of output streams. assert recent_values(u) == [3, 10, 17, 24, 31] assert recent_values(v) == [2, 3, 4, 5, 6]
def examples(): # -------------------------------------------- # Example: simple example of @map_e and @fmap_e # Specify agent functions @fmap_e def twice(v): return 2*v @map_e def double(v): return 2*v # Create streams x = Stream() y = Stream() # Create agents # Relational form: g(in_stream=x, out_stream=y) double(in_stream=x, out_stream=y) # Functional form: Create stream z. z = twice(x) # Put data into streams and run DATA = list(range(5)) x.extend(DATA) run() # Check results. assert recent_values(z) == [2*v for v in DATA] assert recent_values(y) == [2*v for v in DATA] # -------------------------------------------- # -------------------------------------------- # Examples: @map_e and @fmap_e with keyword # arguments # Specify agent functions @map_e def g(v, addend): return v + addend @fmap_e def h(v, addend): return v + addend # Create streams x = Stream('x') y = Stream('y') # Create agents # keyword argument is addend ADDEND = 10 # Relational form: g(in_stream=x, out_stream=y, **kwargs) g(x, y, addend=ADDEND) # Functional form: Create stream z. h(in_stream=x, **kwargs) z = h(x, addend=ADDEND) # Put data into streams and run DATA = list(range(10)) x.extend(DATA) run() # Check results. assert recent_values(y) == [v+ADDEND for v in DATA] assert recent_values(y) == recent_values(z) # -------------------------------------------- # -------------------------------------------- # Examples @map_e and @fmap_e with keyword # arguments # Specify agent functions @map_e def multiply(v, multiplicand): return v * multiplicand @fmap_e def times(v, multiplicand): return v * multiplicand # Create streams x = Stream('x') y = Stream('y') # Create agents # keyword argument is multiplicand MULTIPLICAND=10 # Relational form: multiply(in_stream=x, out_stream=y, **kwargs) multiply(x, y, multiplicand=MULTIPLICAND) # Functional form: Create stream z. times(in_stream=x, **kwargs) z = times(x, multiplicand=MULTIPLICAND) # Put data into streams and run DATA = list(range(4)) x.extend(DATA) run() # Check results. assert recent_values(y) == [v*MULTIPLICAND for v in DATA] assert recent_values(y) == recent_values(z) # -------------------------------------------- # -------------------------------------------- # Examples @map_e and @fmap_e with state # Specify agent functions @map_e def deltas(input_value, state): difference = input_value - state next_state = input_value return difference, next_state @fmap_e def increments(u, v): return u - v, u # Create streams x = Stream('x') y = Stream('y') # Create agents # Initial state is 0. # Relational form deltas(in_stream=x, out_stream=y, state=0) # Functional form. Create stream z. increments(in_stream=x, state=0) z = increments(x, state=0) # Put data into streams and run x.extend([0, 1, 5, 21]) run() # Check results. assert recent_values(y) == [0, 1, 4, 16] assert recent_values(y) == recent_values(z) # -------------------------------------------- # -------------------------------------------- # Examples @map_e and @fmap_e with state # Specify agent functions. @map_e def g(v, state): next_state = state + 1 return v + state, next_state @fmap_e def h(v, state): next_state = state + 1 return v + state, next_state # Create streams x = Stream('x') y = Stream('y') # Create agents # Initial state is 0. # Relational form g(in_stream=x, out_stream=y, state=0) g(x, y, state=0) # Functional form. Create stream z. h(in_stream=x, state=0) z = h(x, state=0) # Put data into streams and run DATA = list(range(10)) x.extend(DATA) run() # Check results. assert recent_values(y) == [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18] assert recent_values(y) == recent_values(z) # -------------------------------------------- # -------------------------------------------- # Examples @map_e and @fmap_e with keyword # arguments and state # Specify agent functions. @map_e def g(v, state, addend): next_state = state + 1 return v + addend + state, next_state @fmap_e def h(v, state, addend): next_state = state + 1 return v + addend + state, next_state # Create streams. x = Stream('x') y = Stream('y') # Create agents # Initial state is 0, keyword argument is addend # Relational form g(in_stream=x, out_stream=y, state=0, **kwargs) ADDEND = 10 g(x, y, state=0, addend=ADDEND) z = h(x, state=0, addend=ADDEND) # Put data into streams and run DATA = list(range(10)) x.extend(DATA) run() # Check results. assert recent_values(y) == [ 10, 12, 14, 16, 18, 20, 22, 24, 26, 28] assert recent_values(y) == recent_values(z) # -------------------------------------------- # -------------------------------------------- # Example @map_e with keyword arguments and state # Specify agent functions. @map_e def anomalous_change(v, state, max_change): delta = v - state next_state = v next_output = True if delta > max_change else False return next_output, next_state @fmap_e def big_change(v, state, max_change): delta = v - state next_state = v next_output = True if delta > max_change else False return next_output, next_state # Create streams. x = Stream('x') y = Stream('y') # Create agents # Initial state is 0, keyword argument is max_change # Relational form # anomalous_change(in_stream=x, out_stream=y, state=0, **kwargs) anomalous_change(x, y, state=0, max_change=4) # Functional form # big_change(in_stream=x, state=0, max_change=4) z = big_change(x, state=0, max_change=4) # Put data into streams and run x.extend([0, 1, 6, 12, 11, 20, 22]) run() # Check results. assert recent_values(y) == [ False, False, True, True, False, True, False] assert recent_values(y) == recent_values(z) # -------------------------------------------- # Example @fmap_e with keyword arguments and state # Define a function that uses #fmap_e # Specify exponential_smoothing def exponential_smoothing(x, a): """ Parameters ---------- x: Stream a: number where 0 <= a <= 1. The smoothing factor Returns ------- y: Stream y[0] = x[0] y[n] = a*x[n] + (1-a)*y[n-1] for n > 0. """ # Specify agent functions. @fmap_e def f(v, state, a): next_state = (v if state is 'empty' else (1.0-a)*state + a*v) return next_state, next_state # return an agent specification return f(x, state='empty', a=a) # Finished definition of exponential_smoothing() # Create streams x, y and create the network of agents # specified by the function exponential_smoothing() x = Stream('x') y = exponential_smoothing(x, a=0.5) # Put data into input streams and run x.extend([16, 8, 4, 2, 1]) run() # Check results. assert recent_values(y) == [16, 12.0, 8.0, 5.0, 3.0] # -------------------------------------------- # -------------------------------------------- # Examples @fmap_e with _no_value # This example shows how to specify a filter by using # _no_value. h(v) returns _no_value for odd v, and # so stream z contains only even numbers. # Specify agent functions. @fmap_e def h(v): return _no_value if v%2 else v # Create streams. x = Stream('x') # Create agents and stream z. z = h(x) # Put data into input streams and run DATA = list(range(10)) x.extend(DATA) run() # Check results. assert recent_values(z) == [v for v in DATA if not v%2 ] assert recent_values(z) == [ 0, 2, 4, 6, 8] # -------------------------------------------- # -------------------------------------------- # Examples @fmap_e with _multi_value # Illustrates difference between returning # _multivalue(v) and v. # Specify agent functions. @fmap_e def h(v): return _multivalue(v) @fmap_e def g(v): return v # Create streams. x = Stream('x') # Create streams z, y and create the network of agents # specified by the functions h() and g(). z = h(x) y = g(x) # Put data into input streams and run x.extend([('hello', 'Hola'), ('bye', 'adios')]) run() # Check results. assert recent_values(z) == [ 'hello', 'Hola', 'bye', 'adios'] assert recent_values(y) == [ ('hello', 'Hola'), ('bye', 'adios')] # -------------------------------------------- # -------------------------------------------- # Examples @fmap_e with _multi_value and _no_value # Specify agent functions. @fmap_e def h(v): return _multivalue((v, v+10)) if v%2 else _no_value # Create streams. s = Stream() # Create stream t and create the network of agents # specified by the function h(). t = h(s) # Put data into input streams and run. DATA = list(range(10)) s.extend(DATA) run() # Check results. assert recent_values(t) == [ 1, 11, 3, 13, 5, 15, 7, 17, 9, 19] # -------------------------------------------- # -------------------------------------------- # Specify class and create an object of this class. class add(object): def __init__(self, addend): self.addend = addend def func(self, v): return _multivalue((v, v+self.addend)) if v%2 else _no_value add_object = add(10) # Specify agent functions. @fmap_e def h(v): return add_object.func(v) # Create streams. s = Stream() # Create stream t and create the network of agents # specified by the function h(). t = h(s) # Put data into input streams and run. DATA = list(range(10)) s.extend(DATA) run() # Check results. assert recent_values(t) == [1, 11, 3, 13, 5, 15, 7, 17, 9, 19] # -------------------------------------------- # -------------------------------------------- # Examples @fmap_e with a class # Specify class and create an object of this class. class average(object): def __init__(self, max_value): # Clip elements of the stream greater than max_value self.max_value = max_value self.count = 0 self.total = 0.0 def f(self, v): v = min(v, self.max_value) self.total += v self.count += 1 return self.total/float(self.count) c = average(max_value=10) # Specify agent functions. @fmap_e def avg(v): return c.f(v) # Create streams. x = Stream('x') # Create stream y and create the network of agents # specified by the function avg(). y = avg(x) # Put data into input streams and run. x.extend([1, 7, 20, 2]) run() # Check results. assert recent_values(y) == [ 1, 4, 6, 5]
return np.exp(-(np.square(x-dd)+np.square(y))/(dd/10.)) + np.exp(-(np.square(x+dd)+np.square(y))/(dd/10.)) def init(i, t): bas.omega.f = vortexInit # Use matplotlib to visualize the generated vorticity field def graph(i, t): print ("t=",t) Z = bas.omega.f(X,Y) images.append(Image.fromarray(np.uint8(cmap(Z)*255))) plt.cla() plt.imshow(Z) plt.pause(0.010) # Shift the origin using predefined function shiftOrigin(-0.5,-0.5,0) # Initialize grid bas.init_grid(N) #Register the initial condition and graph the function bas.event(init, t=0.) plt.ion() plt.figure() bas.event(graph, t=np.arange(0,30,0.1)) #Run the simulation bas.run() #Save the gif file: images[0].save('VortexMerger.gif',save_all=True, append_images=images[1:], optimize=False, duration=5, loop=0)
def examples_merge(): #---------------------------------------------- # Simple merge of list of streams. # Decorate a conventional function to get a # stream function. @fmerge_e def sum_stream(l): # l is a list. return sum(l) # Create streams. x = Stream('X') y = Stream('Y') # Call decorated function. t = sum_stream([x, y]) # Put data into input streams. x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Check values of output streams. assert recent_values(t) == [ 100, 102, 104, 106, 108, 110, 112, 114, 116, 118 ] #---------------------------------------------- # Merge list of streams with keyword argument. # Decorate a conventional function to get a # stream function. @fmerge_e def h(l, addend): # l is a list. return sum(l) + addend # Create streams. x = Stream('X') y = Stream('Y') # Call decorated function. t = h([x, y], addend=1000) # Put data into input streams. x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Check values of output streams. assert recent_values(t) == [ 1100, 1102, 1104, 1106, 1108, 1110, 1112, 1114, 1116, 1118 ] #---------------------------------------------- # Merge list of streams with keyword argument # and state. # Decorate a conventional function to get a # stream function. @fmerge_e def h(l, state, addend): # l is a list. next_state = state + 1 return sum(l) + addend + state, next_state # Create streams. x = Stream('X') y = Stream('Y') # Call decorated function. t = h([x, y], state=0, addend=1000) # Put data into input streams x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Check values of output streams. assert recent_values(t) == [ 1100, 1103, 1106, 1109, 1112, 1115, 1118, 1121, 1124, 1127 ] #---------------------------------------------- # Merge list of streams with state. # Decorate a conventional function to get a # stream function. @fmerge_e def h(l, state): # l is a list. next_state = state + 1 return sum(l) + state, next_state # Create streams. x = Stream('X') y = Stream('Y') # Call decorated function. t = h([x, y], state=0) # Put data into input streams x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Check values of output streams. assert recent_values(t) == [ 100, 103, 106, 109, 112, 115, 118, 121, 124, 127 ] #---------------------------------------------- # Asynchonous merge. # Decorate a conventional function to get a # stream function. @merge_asynch def h(v): # v is an argument of any input stream. return 2 * v # Create streams. x = Stream('X') y = Stream('Y') # Call decorated function. h([x, y], t) # Put data into input streams. x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Print contents of output streams. #print recent_values(t) #---------------------------------------------- # Asynchonous merge with state. # Decorate a conventional function to get a # stream function. @merge_asynch def h(v, state): next_state = state + 1 return 2 * v + state, next_state # Create streams. x = Stream('x') y = Stream('y') # Call decorated function. h([x, y], t, state=0) # Put data into input streams. x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() #print recent_values(t) #---------------------------------------------- # Asynchonous merge with keyword parameter. @merge_asynch def h(v, addend): return 2 * v + addend # Create streams. x = Stream('X') y = Stream('Y') # Call decorated function. h([x, y], t, addend=1000) # Put data into input streams. x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() #print recent_values(t) #---------------------------------------------- # Merge two streams. # Decorate a conventional function to get a # stream function. @fmerge_2e def h(x, y): # x,y are elements of the two input streams. return x + 2 * y # Create streams. x = Stream() y = Stream() # Call decorated function. t = h(x, y) # Put data into input streams. x.extend(list(range(10))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Check values of output streams. assert recent_values(t) == [ 200, 203, 206, 209, 212, 215, 218, 221, 224, 227 ] #---------------------------------------------- # Merge two streams with keyword parameters. # Decorate a conventional function to get a # stream function. @fmerge_2e def h(x, y, addend): # x,y are elements of the two input streams. return x + 2 * y + addend x = Stream() y = Stream() t = h(x, y, addend=1000) x.extend(list(range(10))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 1200, 1203, 1206, 1209, 1212, 1215, 1218, 1221, 1224, 1227 ] #---------------------------------------------- # Merge two streams with keyword parameters and # state. # Decorate a conventional function to get a # stream function. @fmerge_2e def h(x, y, state, addend): # x,y are elements of the two input streams. next_state = state + 1 return x + 2 * y + addend + state, next_state x = Stream() y = Stream() t = h(x, y, state=0, addend=1000) x.extend(list(range(10))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 1200, 1204, 1208, 1212, 1216, 1220, 1224, 1228, 1232, 1236 ] #---------------------------------------------- # Merge two streams with state. # Decorate a conventional function to get a # stream function. @fmerge_2e def h(x, y, state): # x,y are elements of the two input streams. next_state = state + 1 return x + 2 * y + state, next_state x = Stream() y = Stream() t = h(x, y, state=0) x.extend(list(range(10))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 200, 204, 208, 212, 216, 220, 224, 228, 232, 236 ] #---------------------------------------------- # Merge list of streams: window operation. # Decorate a conventional function to get a # stream function. @fmerge_w def h(list_of_windows): window_0, window_1 = list_of_windows return sum(window_0) + 2 * sum(window_1) # Create streams. x = Stream() y = Stream() # Call decorated function. in_streams = [x, y] t = h(in_streams, window_size=2, step_size=2) # Put data into input streams. x.extend(list(range(20))) y.extend(list(range(100, 120))) # Run the decorated function. run() # Check values of output streams. assert recent_values(t) == [ 403, 415, 427, 439, 451, 463, 475, 487, 499, 511 ] #---------------------------------------------- # Merge list of streams with keyword argument: # window operation. # Decorate a conventional function to get a # stream function. @fmerge_w def h(list_of_windows, addend): window_0, window_1 = list_of_windows return sum(window_0) + 2 * sum(window_1) + addend x = Stream() y = Stream() in_streams = [x, y] t = h(in_streams, window_size=2, step_size=2, addend=1000) x.extend(list(range(20))) y.extend(list(range(100, 120))) Stream.scheduler.step() assert recent_values(t) == [ 1403, 1415, 1427, 1439, 1451, 1463, 1475, 1487, 1499, 1511 ] #---------------------------------------------- # Merge list of streams with state and keyword argument: # window operation. # Decorate a conventional function to get a # stream function. @fmerge_w def h(list_of_windows, state, addend): next_state = state + 1 window_0, window_1 = list_of_windows return sum(window_0) + 2 * sum(window_1) + addend + state, next_state x = Stream() y = Stream() in_streams = [x, y] t = h(in_streams, window_size=2, step_size=2, state=0, addend=1000) x.extend(list(range(20))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 1403, 1416, 1429, 1442, 1455, 1468, 1481, 1494, 1507, 1520 ] #---------------------------------------------- # Merge list of streams with state: # window operation. # Decorate a conventional function to get a # stream function. @fmerge_w def h(list_of_windows, state): next_state = state + 1 window_0, window_1 = list_of_windows return sum(window_0) + 2 * sum(window_1) + state, next_state x = Stream() y = Stream() in_streams = [x, y] t = h(in_streams, window_size=2, step_size=2, state=0) x.extend(list(range(20))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 403, 416, 429, 442, 455, 468, 481, 494, 507, 520 ] #---------------------------------------------- # Merge two streams: window operation. # Decorate a conventional function to get a # stream function. @fmerge_2w def h(window_x, window_y): return sum(window_x) + 2 * sum(window_y) x = Stream() y = Stream() t = h(x, y, window_size=2, step_size=2) x.extend(list(range(20))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 403, 415, 427, 439, 451, 463, 475, 487, 499, 511 ] #---------------------------------------------- # Merge two streams with keyword argument: # window operation. # Decorate a conventional function to get a # stream function. @fmerge_2w def h(window_0, window_1, addend): return sum(window_0) + 2 * sum(window_1) + addend x = Stream() y = Stream() in_streams = [x, y] t = h(x, y, window_size=2, step_size=2, addend=1000) x.extend(list(range(20))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 1403, 1415, 1427, 1439, 1451, 1463, 1475, 1487, 1499, 1511 ] #---------------------------------------------- # Merge two streams with state and keyword argument: # window operation. # Decorate a conventional function to get a # stream function. @fmerge_2w def h(window_0, window_1, state, addend): next_state = state + 1 return ((sum(window_0) + 2 * sum(window_1) + addend + state), next_state) x = Stream() y = Stream() in_streams = [x, y] t = h(x, y, window_size=2, step_size=2, state=0, addend=1000) x.extend(list(range(20))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 1403, 1416, 1429, 1442, 1455, 1468, 1481, 1494, 1507, 1520 ] #---------------------------------------------- # Merge two streams with state: # window operation. # Decorate a conventional function to get a # stream function. @fmerge_2w def h(window_0, window_1, state): next_state = state + 1 return sum(window_0) + 2 * sum(window_1) + state, next_state x = Stream() y = Stream() in_streams = [x, y] t = h(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(20))) y.extend(list(range(100, 120))) run() assert recent_values(t) == [ 403, 416, 429, 442, 455, 468, 481, 494, 507, 520 ]