Example #1
0
def main():
    in_stream=StreamArray('in_stream')
    out_stream = demean_stream(
        in_stream=in_stream,
        window_size=4,
        step_size=4,
        num_windows=3)
        
    out_stream.name = 'demeaned stream'
    print_stream(in_stream)
    print_stream(out_stream)

    in_stream.extend(np.arange(24))
    return
Example #2
0
def main():
    """For input:
          data=[2, 5, 8, 11, 14, 17, ...],
          initial_value=2,
       the expected output is:
          [ 2  4  6  8 10 12 ....]

    """
    print lossy_integration(
        data=np.array([2, 5, 8, 11, 14, 17]),
        initial_value=0,
        FACTOR=0.5)

    in_stream = StreamArray('in_stream')
    out_stream = lossy_integrate_stream(
        in_stream, window_size=4, initial_value=0.0, FACTOR=0.5)
    out_stream.name = "integrated"
    print_stream(in_stream)
    print_stream(out_stream)
    in_stream.extend([2+3*i for i in range(12)])
def main():
    input_stream = Stream('input')
    output_stream = Stream('output')
    time_stream = Stream('time_stream')
    delay = 10

    print_stream(input_stream)
    print_stream(output_stream)
    print_stream(time_stream)

    add_heartbeat_to_stream(
        input_stream, output_stream, time_stream, delay)

    input_stream.extend([(5, 'A'), (10, 'B'), (30, 'C')])
    time_stream.extend([50, 55])

    input_stream.extend([(50, 'A'), (58, 'B'), (61, 'C')])
    time_stream.extend([70, 80, 100])

    input_stream.extend([(80, 'A'), (85, 'B'), (93, 'C')])
    time_stream.extend([120])
from Stream import Stream
from Operators import adjustable_window_agent, awf
from examples_element_wrapper import print_stream
import numpy as np
from random import randint
""".

"""

w = Stream('w')
x = Stream('x')
y = Stream('y')
z = Stream('z')
print_stream(x)
print_stream(y)
print_stream(z)

def h(window, window_size, step_size, threshold, min_window_size):
    mx = max(window)
    mn =  min(window)
    dif = mx - mn
    if dif < threshold:
        window_size += 1
        step_size += 1
    elif window_size > min_window_size:
        window_size -= 1
        step_size -= 1

    # print to help understand the program
    print 'dif = ', dif, 'window_size = ', window_size
from Stream import Stream
from Operators import stream_agent, wf
from examples_element_wrapper import print_stream
import numpy as np
from random import randint
"""Simple example of the window function wf with
window size and step size of 3. Output is the mean
of the clipped window with a_min=9, a_max=11. These
values are used in this example so that you can
check the results easily.

"""

u = Stream('u')
v = Stream('v')
y = Stream('y')
print_stream(u)
print_stream(v)
print_stream(y)

def h(list_of_lists):
    return max([np.std(lst) for lst in list_of_lists])

wf([u, v], y, h, 5, 5)

u.extend([randint(0, 20) for _ in range(21)])
v.extend([randint(0, 20) for _ in range(25)])
output[j] = func(x[j], y[j], z[j])
where func is the reduction operation.

"""

# Create streams
x = Stream('x')
y = Stream('y')
z = Stream('z')
a = Stream('sum')
b = Stream('mean')
c = Stream('variance')
d = Stream('all')

# Create printing agents
print_stream(x)
print_stream(y)
print_stream(z)
print_stream(a)
print_stream(b)
print_stream(c)
print_stream(d)

def merge_func(list_of_input_streams, output_stream, func):
    def f(list_of_lists):
        return func(np.array(list_of_lists), axis=0)
    ## stream_agent(
    ##     inputs=list_of_input_streams, outputs=output_stream,
    ##     f_type ='list', f=f)
    lf(list_of_input_streams, output_stream, f)
       
Example #7
0
Illustrates:
      ef(x, y, sum),
      ef(x, z, np.mean), and
      ef([x[0], x[1]], w, dif).

"""

# 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.
from Stream import Stream
from Operators import lf, stream_agent
from examples_element_wrapper import print_stream

def h(x, weights, threshold):
    lst = [sum([weights[i]*v[i]
                for i in range(min(len(weights), len(x)))])
           for v in zip(*x)]
    above = [v for v in lst if v > threshold]
    below = [v for v in lst if v <= threshold]
    return (above, below)

u = Stream('u')
v = Stream('v')
w = Stream('w')
a = Stream('a')
b = Stream('b')

print_stream(u)
print_stream(v)
print_stream(w)
print_stream(a)
print_stream(b)

lf([u,v,w], [a,b], h, weights=(2,5,1), threshold=40)

u.extend(range(10,15))
v.extend(range(4))
w.extend(range(20, 25))
Example #9
0
def main():
    # Examples of source_stream

    # Create a stream s and give it the name 'random integers'.
    s = Stream('random integers')
    # Create a stream r and give it the name 'random Gaussian'.
    r = Stream('random Gaussian')
    # Create an agent to print stream s. This agent is discussed later. 
    print_stream(s)
    # Create an agent to print stream r.
    print_stream(r)
    
    # Periodically append random integers to stream s.
    # Note: the parameters of random.randint are a, b where the value
    # returned by randint(a, b) is a random integer uniformly
    # distributed between a and b.
    source_stream(
        output_stream=s,
        number_of_values=3,
        time_period=1,
        func=random.randint,
        a=0,
        b=100)

    # Periodically append Gaussian random numbers to stream r.
    # Note: the parameters of random.gauss are mu, sigma where the value
    # returned by gauss(mu, sigma) is a random float with a normal
    # distribution with mean, mu, and standard deviation, sigma.
    source_stream(
        output_stream=r,
        number_of_values=5,
        time_period=0,
        func=random.gauss,
        mu=0,
        sigma=1)

    # This example shows that output_stream could also be a
    # list. (Note: Appending values to a list forever will cause memory
    # overflow.) 
    lst = list()
    source_stream(
        output_stream=lst,
        number_of_values=3,
        time_period=2,
        func=random.randint,
        a=0,
        b=100)
    print 'list of random integers = ', lst

    # This example, adds values to a stream forever. Use commands from
    # your terminal to stop this program.
    # Create a stream s and give it the name 'random integers'.
    u = Stream('random integers for ever')
    # Create an agent to print stream u.
    print_stream(u)
    
    # Periodically append random integers to stream s.
    # Append values forever (since number_of_values is negative).
    source_stream(
        output_stream=u,
        number_of_values=-1,
        time_period=3,
        func=random.randint,
        a=0,
        b=100)
"""
# CREATE STREAMS AND PRINTING AGENTS
x = Stream('input')
y = Stream('sine of input')
z = Stream('clipped input')
# The two call_streams
# trigger_1 triggers execution of the sine agent and
# puts values into y.
# trigger_2 triggers execution of the clip agent and
# 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
from Stream import Stream
from Operators import stream_agent
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.
"""

# Create a stream x and call it 'input'
input_stream = Stream('input')
# Create a stream y and call it 'sine of input'
output_stream = Stream('sine of input')
# Create an agent that prints stream input_stream.
# This agent will print elements that enter input_stream.
print_stream(input_stream)
# Create an agent that prints stream output_stream.
print_stream(output_stream)
# Create an agent that puts the sine of elements of
# input_stream into output_stream. This agent is created by
# wrapping np.sin using the element wrapper.
# As elements are added to input_stream, this agent will put
# sine of these elements in output_stream.
stream_agent(inputs=input_stream, outputs=output_stream,
             f_type='element', f=np.sin)

# Put numbers into the input stream to drive all
# the other agents.
for _ in range(2):
    input_stream.extend(np.linspace(0, np.pi, 8))
x = Stream('x: stream of random numbers')

# The wrapper operates on the single input
# stream, x, to return a stream mean_of_x.
mean_of_x = stream_func(
    inputs=x, # A single stream
    f_type='window', # Identifies the 'window' wrapper
    f=np.mean, # The function that is wrapped
    num_outputs=1, # Returns a single stream
    window_size=window_size,
    step_size=step_size)

# Give the stream a name
mean_of_x.set_name('Mean of x')
# Create an agent to print the stream.
print_stream(mean_of_x)


#_____________________________________________________
#_____________________________________________________
# EXAMPLE 1A. SAME EXAMPLE USING AGENTS
#_____________________________________________________
#_____________________________________________________
#          MEAN OF SLIDING WINDOW
# USES STREAM_AGENT RATHER THAN STREAM_FUNC
#_____________________________________________________


mean_of_x_a = Stream('Mean of x for agent')
stream_agent(
    inputs=x,
Example #13
0
x = Stream('x: stream of random numbers')

# The wrapper operates on the single input
# stream, x, to return a stream mean_of_x.
mean_of_x = stream_func(
    inputs=x, # A single stream
    f_type='window', # Identifies the 'window' wrapper
    f=np.mean, # The function that is wrapped
    num_outputs=1, # Returns a single stream
    window_size=window_size,
    step_size=step_size)

# Give the stream a name
mean_of_x.set_name('Mean of x')
# Create an agent to print the stream.
print_stream(mean_of_x)


#_____________________________________________________
#_____________________________________________________
# EXAMPLE 1A. SAME EXAMPLE USING AGENTS
#_____________________________________________________
#_____________________________________________________
#          MEAN OF SLIDING WINDOW
# USES STREAM_AGENT RATHER THAN STREAM_FUNC
#_____________________________________________________


mean_of_x_a = Stream('Mean of x for agent')
stream_agent(
    inputs=x,
NOTE: step size must be non_zero and must eventually
be positive. A negative step size implies that the
agent reads elements of the stream that it had earlier
determined it would no longer need. If the step size
remains zero for ever then the agent will always need
to access the entire stream, even as the stream length
gets arbitrarily large.

"""
# Create streams and printing agents
u = Stream('u')
v = Stream('v')
y = Stream('y')
#print_stream(u)
#print_stream(v)
print_stream(y)

# Function used by awf
def h(list_of_windows, window_size, step_size, last_output,
      percent_increase, min_window_size):
    next_output =  max([np.std(window) for window in list_of_windows])
    if next_output > (1 + percent_increase/100.0) * last_output:
        window_size += 1
        step_size += 1
    elif window_size > min_window_size:
        window_size -= 1
        step_size -= 1
    print 'window_size', window_size
    # return output, next window size, next step size, next state
    return (next_output, window_size, step_size, next_output)
# If window_size=4.0 and step_size=2.0 then the output stream
# will consist of the sum of the values with timestamps in the
# intervals [0:4], [2:6], [4:8], ...

# HOW TO DEVELOP THE STREAMING PROGRAM.


# FIRST STEP:
# Write a function on a timed list.
def sum_values_in_timed_list(timed_list):
    return sum(v.value for v in timed_list)


# a is the input stream for this example
a = Stream('a timed stream')
print_stream(a)

# SECOND STEP.
# Wrap the function with the 'timed' wrapper.
# z is the output stream for this example.
z = stream_func(
    inputs=a,  # The input is a single stream
    f_type='timed',  # Identifes 'timed' wrapper
    f=sum_values_in_timed_list,  # Function that is wrapped.
    num_outputs=1,  # Single output stream
    window_size=4.0,
    step_size=2.0)

z.set_name('sum of a')
print_stream(z)
Example #16
0
def main():
    x = Stream('x')
    y = Stream('y')
    z = Stream('z')
    print_stream(x)
    print_stream(y)
    print_stream(z)
    
    timed_merge_agent([x,y], z)

    x.extend([(1, 'a'), (3, 'b'), (3, 'c'), (10, 'd'), (15, 'e'), (17, 'f')])
    y.extend([(2, 'A'), (3, 'B'), (3, 'C'), (9, 'D'), (20, 'E')])

    t = Stream('t')
    u = Stream('u')
    v = Stream('v')
    w = Stream('w')
    print_stream(t)
    print_stream(u)
    print_stream(v)
    print_stream(w)
    
    timed_merge_agent([u,v], w, call_streams=[t])

    u.extend([(1, 'a'), (3, 'b'), (3, 'c'), (10, 'd'), (15, 'e'), (17, 'f')])
    v.extend([(2, 'A'), (3, 'B'), (3, 'C'), (9, 'D'), (20, 'E')])
    t.append(0)
    u.extend([(21, 'g'), (23, 'h'), (33, 'i'), (40, 'j'), (50, 'K')])
    v.extend([(22, 'F'), (23, 'G'), (43, 'H'), (51, 'I'), (59, 'J')])
    t.append(0)