# Create streams. Call the streams of x, 'a', 'b' and 'c'. x = [Stream('a'), Stream('b'), Stream('c')] y = Stream('sum') z = Stream('mean') w = Stream('dif') # Agents for printing print_stream(x[0]) print_stream(x[1]) print_stream(x[2]) print_stream(y) print_stream(z) print_stream(w) # Agents to make y the sum and z the mean of x ef(x, y, sum) ef(x, z, np.mean) def dif(lst): return lst[1] - lst[0] # Agent to make w x[1] - x[0] ef([x[0], x[1]], w, dif) # Create agents that populate the x streams with random numbers. source_stream(output_stream=x[0], number_of_values=3, time_period=0.1, func=random.randint, a=0, b=100) source_stream(output_stream=x[1], number_of_values=4, time_period=0.2, func=random.randint, a=0, b=10) source_stream(output_stream=x[2], number_of_values=3, time_period=0.1, func=random.randint, a=0, b=20)
from Stream import Stream, _multivalue from Operators import ef from examples_element_wrapper import print_stream import numpy as np """Example of element wrapper with state and keyword arguments for tutorial. """ def update_smooth(lst, state, h, alpha): state = state*(1-alpha) + h(lst)*alpha return (state, state) # Create streams x = [Stream('a'), Stream('b'), Stream('c')] y = Stream('y') # Create printing agents print_stream(x[0]) print_stream(x[1]) print_stream(x[2]) print_stream(y) init = 100 ef(x, y, update_smooth, init, h=np.mean, alpha=0.5) x[0].extend(range(10)) x[1].extend(range(100, 110)) x[2].extend(range(200, 210))
# puts values into z. trigger_1 = Stream('trigger_1') trigger_2 = Stream('trigger_2') # Create printing agents print_stream(x) print_stream(y) print_stream(z) print_stream(trigger_1) print_stream(trigger_2) # Create an agent that puts the sine of x into y. # when elements appear on stream trigger_1. This # agent remains asleep until woken up by new elements # in trigger_1. ef(x, y, np.sin, call_streams=[trigger_1]) # Create an agent that puts the clipped elements of x # into z where values below a_min are set to a_min and # values above a_max are set to a_max. This agent # executes when elements appear in stream trigger_2. # This agent remains asleep until woken up by new elements # in trigger_2. ef(x, z, np.clip, call_streams=[trigger_2], a_min=2, a_max=8) # Put elements 0, 1, 2, 3, 4 into stream, x. # The agents only process elements in x when their trigger # streams have new values. x.extend(range(5)) # Wake up the agent that puts values into y. # Now y has 5 values: sine(0),... , sine(4) trigger_1.append(1)
from examples_element_wrapper import print_stream import numpy as np """ Example of an element-wrapper to create an agent with a single input stream and a single output stream. The function that is wrapped is np.sin. Illustrates: ef(x, y, np.sin) """ # CREATE STREAMS AND PRINTING AGENTS # Create a stream x and call it 'input' x = Stream('input') # Create a stream y and call it 'sine of input' y = Stream('sine of input') # Create an agent that prints stream x print_stream(x) # Create an agent that prints stream y print_stream(y) # Create an agent that puts the sine of x into y. ef(x, y, np.sin) # ALTERNATIVE WAY OF SPECIFYING THE AGENT #stream_agent(inputs=x, outputs=y, f_type='element', f=np.sin) # Put elements into the input stream, x. # As elements enter x, an agent will put elements into y, and # the printer agents will print elements from x and y. for _ in range(2): x.extend(np.linspace(0, np.pi, 8))
def print_stream_tv(stream): def print_element(v, count): print '{0}[{1}] time = {2}, value = {3}'.format(stream.name, count, v.time, v.value) return (count+1) ef(inputs=stream, outputs=None, func=print_element, state=0)
from Stream import Stream, _no_value from Operators import ef from examples_element_wrapper import print_stream """ Example that illustrates the element wrapper with _no_value. """ # Create streams x = Stream('x') y = Stream('y') # Create printing agents print_stream(x) print_stream(y) def ft(v, threshold): return v if v > threshold else _no_value # Create agent ef(x, y, ft, threshold=5) # Put values into x x.extend(range(10))
from Stream import Stream, _multivalue from Operators import ef from examples_element_wrapper import print_stream """Weaves a list of streams into a single stream. Described in the tutorial. """ # Create streams x = [Stream("a"), Stream("b"), Stream("c")] y = Stream("y") # Create printing agents print_stream(x[0]) print_stream(x[1]) print_stream(x[2]) print_stream(y) ef(x, y, _multivalue) x[0].extend(range(4)) x[1].extend(range(10, 16)) x[2].extend(range(20, 25))
from Stream import Stream from Operators import ef from examples_element_wrapper import print_stream from source_stream import source_stream import math """ Example of an element-wrapper to create an agent with a single streams and multiple output streams. """ def execute(v, g, h): return (g(v), h(v)) # Create streams x = Stream('x') y = Stream('y') z = Stream('z') # Create printing agents print_stream(x) print_stream(y) print_stream(z) # Create agent # input=x, output=[y,z], f=execute, kwargs for g,h ef(x, [y, z], execute, g=math.sqrt, h=math.exp) # Put elements into x x.extend(range(5))
from Stream import Stream from Operators import ef from examples_element_wrapper import print_stream from tutorial_split import execute import random """Example illustrating multiple input stream, multiple output streams for the tutorial. """ # Create streams x = [Stream('a'), Stream('b')] y = Stream('y') z = Stream('z') # Create printing agents print_stream(x[0]) print_stream(x[1]) print_stream(y) print_stream(z) # Create agent # input=x, output=[y,z], f=execute, kwargs for g,h ef(x, [y, z], execute, g=max, h=min) # Put elements into x x[0].extend([random.randint(0,100) for _ in range(5)]) x[1].extend([random.randint(0,100) for _ in range(5)])
from Stream import Stream, _multivalue from Operators import ef from examples_element_wrapper import print_stream def update_avg(v, state): state[0] += 1 state[1] += v return (state[1] / float(state[0]), state) # Create streams x = Stream("x") y = Stream("y") # Create printing agents print_stream(x) print_stream(y) # Create the agent that puts averages of x into y. ef(x, y, update_avg, [0, 0.0]) # Put elements into x x.extend(range(10))