def example_1():
    print "example_1"

    # This examples uses the function total()
    # from list and state to a list and state.
    # The state is cumulative --- the cumulative
    # sum up to this point in the stream.
    # The result_list is the list of cumulative
    # values of the input list.
    
    def total(lst, cumulative):
        result_list = []
        for v in lst:
            cumulative += v
            result_list.append(cumulative)
        return (result_list, cumulative)

    x = Stream('x')
    # The initial state is 0.
    state = 0
    y = op(total, x, state)
    # Since call_streams is not specified, values will be
    # appended to y when values are appended to x.
    print "initial state = ", state
    print 'function on stream is total()'
    print 'This function computes the cumulative sum of the stream'
    print 'x is the input stream and y is the output stream.'
    print 'The state is the cumulative so far.'

    y.set_name('y')

    x.extend([3, 7])
    # Now the value of x is [3, 7], and so the
    # value of y is [3, 10], and the state is 10
    print_streams_recent([x, y])
    print ""

    x.extend([0, 11, 5])
    # Now the value of x is [3, 7, 0, 11, 5]
    # so the value of y is [3, 10, 10, 21, 26] and
    # the value of state is 26.
    print_streams_recent([x, y])
Exemple #2
0
def example_1():
    print "example_1"

    # This examples uses the function total()
    # from list and state to a list and state.
    # The state is cumulative --- the cumulative
    # sum up to this point in the stream.
    # The result_list is the list of cumulative
    # values of the input list.

    def total(lst, cumulative):
        result_list = []
        for v in lst:
            cumulative += v
            result_list.append(cumulative)
        return (result_list, cumulative)

    x = Stream('x')
    # The initial state is 0.
    state = 0
    y = op(total, x, state)
    # Since call_streams is not specified, values will be
    # appended to y when values are appended to x.
    print "initial state = ", state
    print 'function on stream is total()'
    print 'This function computes the cumulative sum of the stream'
    print 'x is the input stream and y is the output stream.'
    print 'The state is the cumulative so far.'

    y.set_name('y')

    x.extend([3, 7])
    # Now the value of x is [3, 7], and so the
    # value of y is [3, 10], and the state is 10
    print_streams_recent([x, y])
    print ""

    x.extend([0, 11, 5])
    # Now the value of x is [3, 7, 0, 11, 5]
    # so the value of y is [3, 10, 10, 21, 26] and
    # the value of state is 26.
    print_streams_recent([x, y])
Exemple #3
0
def example_2():
    print ""
    print "example_2"

    # This example uses the function:
    # avg_since_last_drop()
    # from a list and state to a list and state.
    # The output list is the list of averages of
    # the input list from the point that the value
    # in the input list drops by more than
    # drop_threshold.
    # The state is a tuple:
    # (time_since_last_drop, sum_since_last_drop,
    #  last_value, drop_threshold)
    # where time_since_last_drop is the number of
    # values since the last drop over the threshold,
    # sum_since_last_drop is the sum of the values
    # since the last drop over the threshold,
    # last_value is the most recent value read from
    # the input stream, and
    # drop_threshold is the specified threshold.

    def avg_since_last_drop(lst, state):
        time_since_last_drop, sum_since_last_drop,\
          last_value, drop_threshold = state
        result_list = []
        if lst == []:
            return (result_list, state)
        if last_value is None:
            last_value = lst[0]
        for v in lst:
            if last_value - v < drop_threshold:
                time_since_last_drop += 1
                sum_since_last_drop += v
            else:
                time_since_last_drop = 1
                sum_since_last_drop = v
            last_value = float(v)
            avg = sum_since_last_drop / float(time_since_last_drop)
            result_list.append(avg)
        return (result_list, state)

    x = Stream('x')
    time_since_last_drop = 0
    sum_since_last_drop = 0
    last_value = None
    drop_threshold = 100
    print "INITIAL VALUES"
    print 'time_since_last_drop = ', time_since_last_drop
    print 'sum_since_last_drop = ', sum_since_last_drop
    print 'last_value = ', last_value
    print 'drop_threshold = ', drop_threshold
    print ''
    state = (time_since_last_drop, sum_since_last_drop, last_value,
             drop_threshold)

    y = op(avg_since_last_drop, x, state)
    y.set_name('y')

    print 'first step'
    x.extend([16500, 16750, 16550, 16600, 16581])
    # The first drop of over drop_threshold is from 16750 to 16550
    # So, the output before that drop is the average for the window:
    # 16500.0, followed by average of (16500, 16750) = 16625.0
    # The output then starts computing averages afresh after the drop
    # starting with the value after the drop: 16550.0, 16575.0, 16577.0
    print_streams_recent([x, y])
    print ""

    print 'second step'
    x.extend([16400, 16500, 17002])
    # Another drop over the threshold of 100 occurs from 16581 to 16400
    # So, compute averages afresh from this point to get:
    # 16400.0, 16450.0, 16634.0
    print_streams_recent([x, y])
Exemple #4
0
"""This module contains examples of the op() function
where:
op(f,x) returns a stream where x is a stream, and f
is a function from a list and state to a list and state.
The state keeps track of the input stream up to the
current point.

Examples of the op() function, in its stateless form
are found in ExamplesOpNoState.py

The form of the call to op is:
op(f, in_stream, state, call_streams=None)
where f is the function from list, state to list, state,
and in_stream is the single input stream, and
call_streams is the list of streams that cause op()
to be invoked. If call_streams is None then the function
is invoked whenever a value is appended to in_stream.

"""
if __name__ == '__main__':
    if __package__ is None:
        import sys
        from os import path
        sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))

from Agent import *
from Stream import *
from ListOperators import op
from PrintFunctions import print_streams_recent
def example_1():
    # FUNCTIONS FROM LIST TO LIST

    # This example uses the following list operators:
    # functions from a list to a list.
    # f, g, h, r

    # Example A: function using list comprehension
    def f(lst):
        return [w*w for w in lst]

    # Example B: function using filter
    threshold = 2
    def predicate(w):
        return w > threshold
    def g(lst):
        return filter(predicate, lst)

    # Example C: function using map
    # Raise each element of the list to the n-th power.   
    n = 3
    def power(w):
        return w**n
    def h(lst):
        return map(power, lst)

    # Example D: function using another list comprehension
    # Discard any element of x that is not a
    # multiple of a parameter n, and divide the
    # elements that are multiples of n by n.
    n = 3
    def r(lst):
        result = []
        for w in lst:
            if w%n == 0: result.append(w/n)
        return result

    # CREATE A CLOCK STREAM
    trigger_stream = clock_stream(period=0.01,
                                  num_periods=12, name='trigger_stream')

    list_of_values = [ 3, 7, 0, 11, 5, 4, 6, 2, -1, 3, 2, 0, 4, 6, -3, -9, 8]
    x = stream_from_list(trigger_stream, list_of_values)
    x.set_name('x')
    y = stream_from_file(trigger_stream, file_name='ExampleFile.dat', step_size=1)
    y.set_name('y')

    # EXAMPLES OF OPERATIONS ON STREAMS
    
    # The output streams a, b, c, d obtained by
    # applying the list operators f, g, h, r to
    # stream x.
    state = None
    call_streams = [trigger_stream]
    a = op(f, x, state, call_streams)
    b = op(g, x, state, call_streams)
    c = op(h, y, state, call_streams)
    d = op(r, x, state, call_streams)

    # Name the output streams to label the output
    # so that reading the output is easier.
    a.set_name('a')
    b.set_name('b')
    c.set_name('c')
    d.set_name('d')
    
    print_stream_with_index(x)
    print_stream_with_index(trigger_stream)
    print_stream_with_index(a)
    print_stream_with_index(b)
    print_stream_with_index(c)
    print_stream_with_index(d)
    print_stream_with_index(y)
"""This module illustrates the use of call_stream.
A stream operation is carried out when a value is
appended to any call_stream.

This module illustrates call_streams using examples
of the op() function. For examples about op() see:
ExamplesOpNoState.py and
ExamplesOpWithState.py

These examples also show how streams are generated
from files and from lists. The times for opening
and closing files seem to slow down start up and
shut down.

The examples in this module are from:
ExamplesOpNoState.py.

For convenience, information from ExamplesOpNoState.py
is repeated here.

op(f,x) returns a stream where x is a stream, and f
is an operator on lists, i.e., f is a function from
a list to a list. These lists are of objects other than
streams and agents.

Function f must be stateless, i.e., for any lists u, v:
f(u.extend(v)) = f(u).extend(f(v))
(Stateful functions are given in OpStateful.py with
examples in OpStatefulExamples.py.)
def example_2():
    print ""
    print "example_2"

    # This example uses the function:
    # avg_since_last_drop()
    # from a list and state to a list and state.
    # The output list is the list of averages of
    # the input list from the point that the value
    # in the input list drops by more than
    # drop_threshold.
    # The state is a tuple:
    # (time_since_last_drop, sum_since_last_drop,
    #  last_value, drop_threshold)
    # where time_since_last_drop is the number of
    # values since the last drop over the threshold,
    # sum_since_last_drop is the sum of the values
    # since the last drop over the threshold,
    # last_value is the most recent value read from
    # the input stream, and
    # drop_threshold is the specified threshold.

    def avg_since_last_drop(lst, state):
        time_since_last_drop, sum_since_last_drop,\
          last_value, drop_threshold = state
        result_list = []
        if lst == []:
            return (result_list, state)
        if last_value is None:
            last_value = lst[0]
        for v in lst:
            if last_value - v < drop_threshold:
                time_since_last_drop += 1
                sum_since_last_drop += v
            else:
                time_since_last_drop = 1
                sum_since_last_drop = v
            last_value = float(v)
            avg = sum_since_last_drop/float(time_since_last_drop)
            result_list.append(avg)
        return (result_list, state)

    x = Stream('x')
    time_since_last_drop = 0
    sum_since_last_drop = 0
    last_value = None
    drop_threshold = 100
    print "INITIAL VALUES"
    print 'time_since_last_drop = ', time_since_last_drop
    print 'sum_since_last_drop = ', sum_since_last_drop
    print 'last_value = ', last_value
    print 'drop_threshold = ', drop_threshold
    print ''
    state = (time_since_last_drop, sum_since_last_drop, last_value, drop_threshold)

    y = op(avg_since_last_drop, x, state)
    y.set_name('y')

    print 'first step'
    x.extend([16500, 16750, 16550, 16600, 16581])
    # The first drop of over drop_threshold is from 16750 to 16550
    # So, the output before that drop is the average for the window:
    # 16500.0, followed by average of (16500, 16750) = 16625.0
    # The output then starts computing averages afresh after the drop
    # starting with the value after the drop: 16550.0, 16575.0, 16577.0
    print_streams_recent([x, y])
    print ""

    print 'second step'
    x.extend([16400, 16500, 17002])
    # Another drop over the threshold of 100 occurs from 16581 to 16400
    # So, compute averages afresh from this point to get:
    # 16400.0, 16450.0, 16634.0
    print_streams_recent([x, y])
"""This module contains examples of the op() function
where:
op(f,x) returns a stream where x is a stream, and f
is a function from a list and state to a list and state.
The state keeps track of the input stream up to the
current point.

Examples of the op() function, in its stateless form
are found in ExamplesOpNoState.py

The form of the call to op is:
op(f, in_stream, state, call_streams=None)
where f is the function from list, state to list, state,
and in_stream is the single input stream, and
call_streams is the list of streams that cause op()
to be invoked. If call_streams is None then the function
is invoked whenever a value is appended to in_stream.

"""
if __name__ == '__main__':
    if __package__ is None:
        import sys
        from os import path
        sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )

from Agent import *
from Stream import *
from ListOperators import op
from PrintFunctions import print_streams_recent
Exemple #9
0
def example_1():
    # FUNCTIONS FROM LIST TO LIST

    # This example uses the following list operators:
    # functions from a list to a list.
    # f, g, h, r

    # Example A: function using list comprehension
    def f(lst):
        return [w * w for w in lst]

    # Example B: function using filter
    threshold = 2

    def predicate(w):
        return w > threshold

    def g(lst):
        return filter(predicate, lst)

    # Example C: function using map
    # Raise each element of the list to the n-th power.
    n = 3

    def power(w):
        return w**n

    def h(lst):
        return map(power, lst)

    # Example D: function using another list comprehension
    # Discard any element of x that is not a
    # multiple of a parameter n, and divide the
    # elements that are multiples of n by n.
    n = 3

    def r(lst):
        result = []
        for w in lst:
            if w % n == 0: result.append(w / n)
        return result

    # CREATE A CLOCK STREAM
    trigger_stream = clock_stream(period=0.01,
                                  num_periods=12,
                                  name='trigger_stream')

    list_of_values = [3, 7, 0, 11, 5, 4, 6, 2, -1, 3, 2, 0, 4, 6, -3, -9, 8]
    x = stream_from_list(trigger_stream, list_of_values)
    x.set_name('x')
    y = stream_from_file(trigger_stream,
                         file_name='ExampleFile.dat',
                         step_size=1)
    y.set_name('y')

    # EXAMPLES OF OPERATIONS ON STREAMS

    # The output streams a, b, c, d obtained by
    # applying the list operators f, g, h, r to
    # stream x.
    state = None
    call_streams = [trigger_stream]
    a = op(f, x, state, call_streams)
    b = op(g, x, state, call_streams)
    c = op(h, y, state, call_streams)
    d = op(r, x, state, call_streams)

    # Name the output streams to label the output
    # so that reading the output is easier.
    a.set_name('a')
    b.set_name('b')
    c.set_name('c')
    d.set_name('d')

    print_stream_with_index(x)
    print_stream_with_index(trigger_stream)
    print_stream_with_index(a)
    print_stream_with_index(b)
    print_stream_with_index(c)
    print_stream_with_index(d)
    print_stream_with_index(y)
Exemple #10
0
"""This module illustrates the use of call_stream.
A stream operation is carried out when a value is
appended to any call_stream.

This module illustrates call_streams using examples
of the op() function. For examples about op() see:
ExamplesOpNoState.py and
ExamplesOpWithState.py

These examples also show how streams are generated
from files and from lists. The times for opening
and closing files seem to slow down start up and
shut down.

The examples in this module are from:
ExamplesOpNoState.py.

For convenience, information from ExamplesOpNoState.py
is repeated here.

op(f,x) returns a stream where x is a stream, and f
is an operator on lists, i.e., f is a function from
a list to a list. These lists are of objects other than
streams and agents.

Function f must be stateless, i.e., for any lists u, v:
f(u.extend(v)) = f(u).extend(f(v))
(Stateful functions are given in OpStateful.py with
examples in OpStatefulExamples.py.)