예제 #1
0
def example_1():
    print "example_1"
    print "Calling signature:"
    print "split(f, in_stream, num_out_streams, state=None, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "List function: f(lst) where lst is a list. "
    print "f() returns a list of num_out_streams lists."
    print "in_stream is a single stream. \n"

    print "In this example, split() returns two streams: multiples and nonmultiples of factor."
    print "x is the input stream."

    factor = 2
    print "factor = ", factor
    def f(lst):
        return [filter(lambda n: n%factor == 0, lst), \
                filter(lambda n: n%factor != 0, lst)]

    x = Stream('x')

    list_of_two_streams =split(f, x, 2)
    multiples, nonmultiples = list_of_two_streams

    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 11])
    print ""
    print "FIRST STEP"
    print_streams_recent([x, multiples, nonmultiples])
    print""

    x.extend([9, 15, 19, 8, 20])
    print "SECOND STEP"
    print_streams_recent([x, multiples, nonmultiples])
def example_2():
    def f_stream(list_of_streams):

        def f_list(list_of_lists, state):
            sum_list = map(sum, zip(*list_of_lists))
            if not sum_list:
                return ([[], []], state)
            cumulative_list = [0]*len(sum_list)
            cumulative_list[0] = sum_list[0] + state
            for i in range(1, len(sum_list)):
                cumulative_list[i] = cumulative_list[i-1] + sum_list[i]
            state = cumulative_list[-1] if cumulative_list else 0
            first_list = filter(lambda v: v % 2 == 0, cumulative_list)
            second_list = filter(lambda v: v % 2 != 0, cumulative_list)
            return ([first_list, second_list], state)

        return many_to_many_stateful(f_list, list_of_streams, num_outputs=2, state=0)

    print 'example_2'
    x = Stream('x')
    y = Stream('y')

    multiples, nonmultiples = f_stream([x,y])
    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 1])
    y.extend([2, 4, 5])
    print_streams_recent([x, y, multiples, nonmultiples])
    print""

    x.extend([6, 5, -5, 8, 2])
    y.extend([0, -1, 5])
    print_streams_recent([x, y, multiples, nonmultiples])
    return
예제 #3
0
def example_1():
    print "example_1"
    print "This example shows how multiple input streams are merged into a single output stream."
    print "op(f, in_streams): f is a function from a list of lists to a list"
    print "in_streams is a list of streams \n"
    print "In this example, the input streams are x and y."
    print "In this example, the function f() computes element by element sums of the input streams."
    print ""

    def f(in_streams):
        return map(sum, zip(*in_streams))

    x = Stream('x')
    y = Stream('y')

    a = merge(f, [x, y])
    a.set_name('a')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    print "FIRST STEP"
    print_streams_recent([x, y, a])
    print ""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    print "SECOND STEP"
    print_streams_recent([x, y, a])
예제 #4
0
def example_1():
    print "example_1"
    print "This example shows how multiple input streams are merged into a single output stream."
    print "op(f, in_streams): f is a function from a list of lists to a list"
    print "in_streams is a list of streams \n"
    print "In this example, the input streams are x and y."
    print "In this example, the function f() computes element by element sums of the input streams."
    print ""

    def f(in_streams):
        return map(sum, zip(*in_streams))

    x = Stream('x')
    y = Stream('y')

    a = merge_stateless(f, [x,y])
    a.set_name('a')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    print "FIRST STEP"
    print_streams_recent([x, y, a])
    print""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    print "SECOND STEP"
    print_streams_recent([x, y, a])
예제 #5
0
def example_1():
    x = StreamArray('x')
    y = StreamArray('y')

    list_of_two_stream_arrays = many_to_many(f, [x,y], 2)
    hypotenuses, tangents = list_of_two_stream_arrays
    hypotenuses.set_name('hypotenuse_stream')
    tangents.set_name('tangent_stream')

    x.extend([3.0, 4.0])
    y.extend([4.0, 3.0])
    print_streams_recent([x, y,hypotenuses, tangents])
예제 #6
0
def example_1():
    print "example_1"
    print "Calling signature:"
    print "split(f, in_stream, num_out_streams, state, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "List function: f(lst, state) where lst is a list. "
    print "f() returns a list of num_out_streams lists and state"
    print "in_stream is a single stream. \n"

    print "THIS EXAMPLE"
    print "The input stream is x and the output streams are a and b."
    print "The input stream is fed to one of the output streams until"
    print "the next even number (multiple of factor=2); at that point"
    print "the stream is fed to the other output stream."
    print "This function switches x between a and b where the switch is triggered"
    print "by an event: namely the appearance of an even value."
    print ""

    factor = 2

    def f(lst, state):
        a = []
        b = []
        for j in range(len(lst)):
            if (lst[j]+state) % factor == 0:
                state = 1
                a.append(lst[j])
            else:
                state = 0
                b.append(lst[j])
        return ([a,b], state)


    x = Stream('x')

    a, b =split(f, in_stream=x, num_out_streams=2, state=0)
    a.set_name('a')
    b.set_name('b')

    x.extend([4, 5, 10, 11, 13, 16, 9])
    print "FIRST STEP"
    print_streams_recent([x, a, b])
    print""

    x.extend([15, 19, 8, 7, 20, 21])
    print "SECOND STEP"
    print_streams_recent([x, a, b])
def example_1():
    print "example_1"
    print "Calling signature:"
    print "many_to_many_stateful(f, in_streams, num_out_streams, state, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "in_streams is a list of streams.\n"
    print "List function: f(lists, state) where lists is a list of lists, and "
    print "state is a tuple."
    print "f() returns a list of num_out_streams lists and the next state. \n"

    print "THIS EXAMPLE, many_to_many_stateful() returns two streams: multiples and nonmultiples."
    print "If the cumulative of both input streams is a multiple of factor then"
    print "the cumulative appears in multiples and otherwise it appears in nonmultiples."
    print "The input streams are x and y."

    factor = 2
    def f(two_lists, state):
        x = two_lists[0]
        y = two_lists[1]
        a = []
        b = []
        for j in range(min(len(x), len(y))):
            state = x[j] + y[j] + state
            if state % factor == 0:
                a.append(state)
            else:
                b.append(state)
        return ([a, b], state)

    x = Stream('x')
    y = Stream('y')

    multiples, nonmultiples = many_to_many_stateful(f, [x,y], 2, state=0)
    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 1])
    y.extend([2, 4, 5])
    print_streams_recent([x, y, multiples, nonmultiples])
    print""

    x.extend([6, 5, -5, 8, 2])
    y.extend([0, -1, 5])
    print_streams_recent([x, y, multiples, nonmultiples])
    return
def example_1():
    print "example_1"
    print "Calling signature:"
    print "many_to_many_stateless(f, in_streams, num_out_streams, state=None, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "in_streams is a list of streams."
    print "List function: f(lists) where lists is a list of lists. "
    print "f() returns a list of num_out_streams lists. \n"

    print "In this example, many_to_many_stateless() returns two streams: multiples and nonmultiples."
    print "The input streams are summed element by element;"
    print "if this sum is a multiple  of factor(=2), then the sum appears in multiples"
    print "and otherwise it appears in nonmultiples."
    print "The input streams are x and y."

    factor = 2

    def f(lists):
        r = map(sum, zip(*lists))
        return [filter(lambda v: v % factor == 0, r), filter(lambda v: v % factor != 0, r)]

    x = Stream("x")
    y = Stream("y")

    list_of_two_streams = many_to_many_stateless(f, [x, y])
    multiples, nonmultiples = list_of_two_streams

    multiples.set_name("multiples")
    nonmultiples.set_name("nonmultiples")

    x.extend([5, 11])
    y.extend([2, 4, 5])
    print ""
    print "FIRST STEP"
    # Element by element sum of x and y is [5+2, 11+4].
    # Since these values are odd, they appear in nonmultiples,
    # and multiples is empty.
    print_streams_recent([x, y, multiples, nonmultiples])
    print ""

    print "SECOND STEP"
    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    print_streams_recent([x, y, multiples, nonmultiples])
    return
예제 #9
0
def example_1():
    print "example_1"
    print "Calling signature:"
    print "many_to_many(f, in_streams, num_out_streams, state=None, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "in_streams is a list of streams."
    print "List function: f(lists) where lists is a list of lists. "
    print "f() returns a list of num_out_streams lists. \n"

    print "In this example, many_to_many() returns two streams: multiples and nonmultiples."
    print "The input streams are summed element by element;"
    print "if this sum is a multiple  of factor(=2), then the sum appears in multiples"
    print "and otherwise it appears in nonmultiples."
    print "The input streams are x and y."

    factor = 2

    def f(lists):
        r = map(sum, zip(*lists))
        return [filter(lambda v: v % factor == 0, r),\
                filter(lambda v: v % factor != 0, r)]

    x = Stream('x')
    y = Stream('y')

    list_of_two_streams = many_to_many(f, [x, y], 2)
    multiples, nonmultiples = list_of_two_streams

    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    print ""
    print "FIRST STEP"
    # Element by element sum of x and y is [5+2, 11+4].
    # Since these values are odd, they appear in nonmultiples,
    # and multiples is empty.
    print_streams_recent([x, y, multiples, nonmultiples])
    print ""

    print "SECOND STEP"
    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    print_streams_recent([x, y, multiples, nonmultiples])
def example_1():
    print "example_1"
    print "Calling signature:"
    print "many_to_many(f, in_streams, num_out_streams, state, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "in_streams is a list of streams.\n"
    print "List function: f(lists, state) where lists is a list of lists, and "
    print "state is a tuple."
    print "f() returns a list of num_out_streams lists and the next state. \n"

    print "THIS EXAMPLE, many_to_many() returns two streams: multiples and nonmultiples."
    print "If the cumulative of both input streams is a multiple of factor then"
    print "the cumulative appears in multiples and otherwise it appears in nonmultiples."
    print "The input streams are x and y."

    factor = 2
    def f(two_lists, state):
        x = two_lists[0]
        y = two_lists[1]
        a = []
        b = []
        for j in range(len(x)):
            state = x[j] + y[j] + state
            if state % factor == 0:
                a.append(state)
            else:
                b.append(state)
        return ([a, b], state)

    x = Stream('x')
    y = Stream('y')

    multiples, nonmultiples = many_to_many(f, [x,y], 2, state=0)
    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 1])
    y.extend([2, 4, 5])
    print_streams_recent([x, y, multiples, nonmultiples])
    print""

    x.extend([6, 5, -5, 8, 2])
    y.extend([0, -1, 5])
    print_streams_recent([x, y, multiples, nonmultiples])
예제 #11
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 = stream_operator_stateful(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])
예제 #12
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])
예제 #13
0
def example_1():
    print "example_1"
    print "example functions from list to value: sum(), mean(), mean_and_sigma() "
    window_size = 2
    step_size = 2
    print "window_size = ", window_size
    print "step_size = ", step_size
    print ""

    x = Stream('x')
    # x is the in_stream.
    # sum() is the function on the window
    window_sum = window_op(sum, x, window_size, step_size)
    window_sum.set_name('sum')

    # mean() is the function on the window
    window_mean = window_op(mean, x, window_size, step_size)
    window_mean.set_name('mean')

    window_mean_and_sigma = \
       window_op(mean_and_sigma, x, window_size, step_size)
    window_mean_and_sigma.set_name('mean_and_sigma')

    x.extend([5, 11])
    print_streams_recent([x, window_sum, window_mean, window_mean_and_sigma])
    print ""

    x.extend([9, 15, 19, 8, 20])
    print_streams_recent([x, window_sum, window_mean, window_mean_and_sigma])
    print ""

    x.extend([19, 10, 11, 28, 30])
    print_streams_recent([x, window_sum, window_mean, window_mean_and_sigma])
    print ""
def example_1():
    print "example_1"
    print "example functions from list to value: sum(), mean(), mean_and_sigma() "
    window_size = 2
    step_size = 2
    print "window_size = ", window_size
    print "step_size = ", step_size
    print ""

    x = Stream('x')
    # x is the in_stream.
    # sum() is the function on the window
    window_sum = window_op(sum, x, window_size, step_size)
    window_sum.set_name('sum')

    # mean() is the function on the window
    window_mean = window_op(mean, x, window_size, step_size)
    window_mean.set_name('mean')

    window_mean_and_sigma = \
       window_op(mean_and_sigma, x, window_size, step_size)
    window_mean_and_sigma.set_name('mean_and_sigma')


    x.extend([5, 11])
    print_streams_recent([x, window_sum, window_mean, window_mean_and_sigma])
    print""

    x.extend([9, 15, 19, 8, 20])
    print_streams_recent([x, window_sum, window_mean, window_mean_and_sigma])
    print""
    
    x.extend([19, 10, 11, 28, 30])
    print_streams_recent([x, window_sum, window_mean, window_mean_and_sigma])
    print""
def example_1():
    print "example_1"
    factor = 2

    # f takes windows as input, where windows is a list
    # in which each element is a window (a list).
    # The output of f is a list with an element for each
    # output stream.
    # This example is for a list with two output streams.
    # Sum corresponding elements of all input windows.
    # If the sum of the corresponding windows is divisible by
    # factor, then that sum is added to the zeroth output
    # stream, and otherwise it is added to the first output
    # stream.
    def f(windows):
        r = map(sum, zip(*windows))
        multiples = filter(lambda v: v % factor == 0, r)
        nonmultiples = filter(lambda v: v % factor != 0, r)
        return [sum(multiples), sum(nonmultiples)]

    x = Stream('x')
    y = Stream('y')

    list_of_two_streams = window_many_to_many(f,
                                              in_streams=[x,y],
                                              num_out_streams=2,
                                              window_size=2,
                                              step_size=2)
    multiples, nonmultiples = list_of_two_streams

    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    # The windows for the two input streams x, y are:
    # [5, 11] and [2, 4] respectively.
    # The sums of the input streams, element by element are:
    # [5+2, 11+4] or [7, 15]. Since these elements are not
    # multiples of factor, the multiples stream outputs [0] 
    # and the nonmultiples stream outputs [7+15] or [22]
    print_streams_recent([x, y, multiples, nonmultiples])
    print""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    # The new windows for x are [9, 15], [19, 8] and
    # for y are [5, 1], [3, 7].
    # The sums of the input streams, element by element are:
    # [14, 16], [22, 15].
    # The multiples stream outputs [14+16], [22], and the
    # nonmultiples stream outputs [0], [15].
    print_streams_recent([x, y, multiples, nonmultiples])
    print""

    x.extend([19, 10, 11, 28, 30])
    y.extend([1, 3, 7, 13, 17, 19, 20, 40, 80])
    print_streams_recent([x, y, multiples, nonmultiples])
    print""
def example_1():
    print "example_1"
    print "This example shows how multiple streams are merged into a single stream"
    print ""
    print "merge_stateful(f, in_streams, state, call_streams=None): f is a function"
    print "from a list of lists and state a to a list and state"
    print "in_streams is a list of streams \n"
    print "In this example, state is the cumulative of the zeroth input stream"
    print "The output stream is the sum of the state and the value in the first input stream."
    print "The input streams are x and y. The output stream is a."
    print ""

    def f(two_lists, state):
        result_list = []
        for j in range(len(two_lists[0])):
            result_list.append(state+two_lists[0][j]+two_lists[1][j])
            state += two_lists[0][j]
        return (result_list, state)

    x = Stream('x')
    y = Stream('y')

    a = merge_stateful(f, inputs=[x,y], state=0)
    a.set_name('a')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    print "FIRST STEP"
    # The cumulative for x is [5, 16]. So the output is [5+2, 16+4]
    print_streams_recent([x, y, a])
    print""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    print "SECOND STEP"
    # The cumulative for x is [5, 16, 25, 40, 59, 67, 87]
    # So, the output is [5+2, 16+4, 25+5, 40+1, 59+3, 67+7]
    print_streams_recent([x, y, a])
def example_1():
    print "example_1"
    factor = 2
    # f is a function from a window (a list) to
    # a list, where the result list has one element
    # for each output stream.
    # This example has two output streams.
    # The zeroth output stream gets the sum of the window
    # if the sum is divisible by factor, and 0 otherwise.
    # The first output stream is symmetric.
    def f(window,state):
        s = sum(window) + state
        state += 1
        if s % factor == 0:
            return ([s, 0], state)
        else:
            return ([0, s], state)

    x = Stream('x')
    # f is the function that operates on a window and produces a list.
    # x is the input stream.
    # window_split returns a list of num_out_streams of streams.
    [y, z] = window_split(f, in_stream=x,
                          num_out_streams=2,
                          window_size=2, step_size=2, state=0)
    y.set_name('y')
    z.set_name('z')
    
    x.extend([5, 11, 3, 8, 5, 5, 2, 3, 9, 21, 7])
    # The windows are [5, 11], [3, 8], [5, 5], [2, 3], [9, 21]
    # with sums 16, 11, 10, 5, 30 and the even numbers are 16, 10, 30
    # and so the y stream is [16, 0, 10, 0, 30] and
    # the x stream is [0, 11, 0, 5, 0]
    print_streams_recent([x, y, z])
    print""

    x.extend([8, 15, 18, 8, 20])
    print_streams_recent([x, y, z])
예제 #18
0
def example_3():
    
    def G(in_lists):
        def f(in_lists):
            return map(sum, zip(*in_lists))
        return merge_stateless(f, in_lists)

    x = Stream('x')
    y = Stream('y')

    a = G([x,y])
    a.set_name('a')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    print "FIRST STEP"
    print_streams_recent([x, y, a])
    print""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    print "SECOND STEP"
    print_streams_recent([x, y, a])
예제 #19
0
def example_3():
    def total(x_list, cumulative):
        result_list = []
        for v in x_list:
            cumulative += v
            result_list.append(cumulative)
        return (result_list, cumulative)

    def cumulative_stream(x_stream): return stream_operator_stateful(total, x_stream, state=0)

    x_stream = Stream('x_stream')
    y_stream = cumulative_stream(x_stream)
    y_stream.set_name('y_stream')
    # Appending messages to x_stream, we get:
    x_stream.extend([2, 4, -3])
    # Value of x_stream is [2, 4, -3]
    # y_stream becomes [2, 6, 3]
    print_streams_recent([x_stream, y_stream])
    # Appending more messages to x_stream, we get:
    x_stream.extend([-3, 5])
    # Value of x_stream is [2, 4, -3, -3, 5]
    # y_stream becomes [2, 6, 3, 0, 5]
    print_streams_recent([x_stream, y_stream])
def example_1():
    print "example_1"
    factor = 2
    # f is a function from a window (a list) to
    # a list, where the result list has one element
    # for each output stream.
    # This example has two output streams.
    # The zeroth output stream gets the sum of the window
    # if the sum is divisible by factor, and 0 otherwise.
    # The first output stream is symmetric.
    def f(window):
        s = sum(window)
        if s % factor == 0:
            return [s, 0]
        else:
            return [0, s]

    x = Stream('x')
    # f is the function that operates on a window and produces a list.
    # x is the input stream.
    # window_split returns a list of num_out_streams of streams.
    [y, z] = window_split(f, in_stream=x,
                          num_out_streams=2,
                          window_size=2, step_size=2)
    y.set_name('y')
    z.set_name('z')
    
    x.extend([5, 11, 3, 8, 5, 5, 2, 3, 9, 21, 7])
    # The windows are [5, 11], [3, 8], [5, 5], [2, 3], [9, 21]
    # with sums 16, 11, 10, 5, 30 and the even numbers are 16, 10, 30
    # and so the y stream is [16, 0, 10, 0, 30] and
    # the x stream is [0, 11, 0, 5, 0]
    print_streams_recent([x, y, z])
    print""

    x.extend([8, 15, 18, 8, 20])
    print_streams_recent([x, y, z])
예제 #21
0
def example_1():
    print "example_1"
    print "Calling signature:"
    print "split(f, in_stream, num_out_streams, state=None, call_streams=None)"
    print "Returns a list of num_out_streams streams."
    print "List function: f(lst) where lst is a list. "
    print "f() returns a list of num_out_streams lists."
    print "in_stream is a single stream. \n"

    print "In this example, split() returns two streams: multiples and nonmultiples of factor."
    print "x is the input stream."

    factor = 2
    print "factor = ", factor

    def f(lst):
        return [filter(lambda n: n%factor == 0, lst), \
                filter(lambda n: n%factor != 0, lst)]

    x = Stream('x')

    #list_of_two_streams =split(f, x, 2)
    list_of_two_streams = split(f, x)
    multiples, nonmultiples = list_of_two_streams

    multiples.set_name('multiples')
    nonmultiples.set_name('nonmultiples')

    x.extend([5, 11])
    print ""
    print "FIRST STEP"
    print_streams_recent([x, multiples, nonmultiples])
    print ""

    x.extend([9, 15, 19, 8, 20])
    print "SECOND STEP"
    print_streams_recent([x, multiples, nonmultiples])
예제 #22
0
def example_1():
    print "example_1"
    factor = 2

    def f(windows):
        r = map(sum, zip(*windows))
        multiples = filter(lambda v: v % factor == 0, r)
        return sum(multiples)

    x = Stream('x')
    y = Stream('y')

    # In window_merge: f is a function that operates on a lis
    # of windows. [x, y] are the streams that are merged.
    # window_size and step_size specify the size of the sliding
    # window and how the window is moved at each step.
    # Sum x, y element by element. If the sum is a multiple
    # of factor (factor=2), then return the sum for the window.
    z = window_merge(f, [x, y], window_size=2, step_size=2)
    z.set_name('z')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    # Since window_size=2 and step_size=2, the first windows are:
    # for x: [5, 11], and for y: [2, 4]. So, the sum is [7, 15].
    # Since 7, 15 are not divisible by factor (2), set z to [0]
    print_streams_recent([x, y, z])
    print ""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    # The next windows are: for x: [9, 15], [19, 8], and
    # for y: [5, 1], and [3, 7]. So, the sums are [14, 16]
    # and [22, 15]. Since 14, 16 are divisible by factor return
    # their sum: 30. Since 22 is divisible by factor and 15 is not
    # return 22. So the new value of z is [0, 30, 22].
    print_streams_recent([x, y, z])
    print ""

    x.extend([19, 10, 11, 28, 30])
    y.extend([1, 3, 7, 13, 17, 19, 20, 40, 80])
    print_streams_recent([x, y, z])
    print ""
def example_1():
    print "example_1"
    factor = 2
    def f(windows, state):
        r = map(sum, zip(*windows))
        multiples = filter(lambda v: v % factor == 0, [v+state for v in r])
        state += 1
        return (sum(multiples), state)

    x = Stream('x')
    y = Stream('y')

    # In window_merge: f is a function that operates on a lis
    # of windows. [x, y] are the streams that are merged.
    # window_size and step_size specify the size of the sliding
    # window and how the window is moved at each step.
    # Sum x, y element by element. If the sum is a multiple
    # of factor (factor=2), then return the sum for the window.
    z = window_merge(f, [x,y], window_size=2, step_size=2, state=0)
    z.set_name('z')

    x.extend([5, 11])
    y.extend([2, 4, 5])
    # Since window_size=2 and step_size=2, the first windows are:
    # for x: [5, 11], and for y: [2, 4]. So, the sum is [7, 15].
    # Since 7, 15 are not divisible by factor (2), set z to [0]
    print_streams_recent([x, y, z])
    print""

    x.extend([9, 15, 19, 8, 20])
    y.extend([1, 3, 7])
    # The next windows are: for x: [9, 15], [19, 8], and
    # for y: [5, 1], and [3, 7]. So, the sums are [14, 16]
    # and [22, 15]. Since 14, 16 are divisible by factor return
    # their sum: 30. Since 22 is divisible by factor and 15 is not
    # return 22. So the new value of z is [0, 30, 22].
    print_streams_recent([x, y, z])
    print""

    x.extend([19, 10, 11, 28, 30])
    y.extend([1, 3, 7, 13, 17, 19, 20, 40, 80])
    print_streams_recent([x, y, z])
    print""
예제 #24
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 = stream_operator_stateful(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])
예제 #25
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])
예제 #26
0
def example_1():
    print "example_1"
    print "op(f, x): f is a function from a list to a list"
    print "x is a stream \n"

    # 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 = 6

    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

    # EXAMPLES OF OPERATIONS ON STREAMS

    # The input stream for these examples
    x = Stream('x')

    print 'x is the input stream.'
    print 'a is a stream consisting of the squares of the input'
    print 'b is the stream consisting of values that exceed 6'
    print 'c is the stream consisting of the third powers of the input'
    print 'd is the stream consisting of values that are multiples of 3 divided by 3'
    print 'newa is the same as a. It is defined in a more succinct fashion.'
    print 'newb has squares that exceed 6.'
    print ''

    # The output streams a, b, c, d obtained by
    # applying the list operators f, g, h, r to
    # stream x.
    a = op(f, x)
    b = op(g, x)
    c = op(h, x)
    d = op(r, x)

    # You can also define a function only on streams.
    # You can do this using functools in Python or
    # by simple encapsulation as shown below.
    def F(x):
        return op(f, x)

    def G(x):
        return op(g, x)

    newa = F(x)
    newb = G(F(x))
    # The advantage is that F is a function only
    # of streams. So, function composition looks cleaner
    # as in G(F(x))

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

    # At this point x is the empty stream:
    # its value is []
    x.extend([3, 7])
    # Now the value of x is [3, 7]
    print "FIRST STEP"
    print_streams_recent([x, a, b, c, d, newa, newb])
    print ""

    x.extend([0, 11, 15])
    # Now the value of x is [3, 7, 0, 11, 15]
    print "SECOND STEP"
    print_streams_recent([x, a, b, c, d, newa, newb])
예제 #27
0
def example_1():
    print "example_1"
    print "op(f, x): f is a function from a list to a list"
    print "x is a stream \n"

    # 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 = 6
    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

    
    


    # EXAMPLES OF OPERATIONS ON STREAMS
    
    # The input stream for these examples
    x = Stream('x')

    print 'x is the input stream.'
    print 'a is a stream consisting of the squares of the input'
    print 'b is the stream consisting of values that exceed 6'
    print 'c is the stream consisting of the third powers of the input'
    print 'd is the stream consisting of values that are multiples of 3 divided by 3'
    print 'newa is the same as a. It is defined in a more succinct fashion.'
    print 'newb has squares that exceed 6.'
    print ''

    # The output streams a, b, c, d obtained by
    # applying the list operators f, g, h, r to
    # stream x.
    a = op(f, x)
    b = op(g, x)
    c = op(h, x)
    d = op(r, x)

    # You can also define a function only on streams.
    # You can do this using functools in Python or
    # by simple encapsulation as shown below.
    def F(x): return op(f,x)
    def G(x): return op(g,x)
    newa = F(x)
    newb = G(F(x))
    # The advantage is that F is a function only
    # of streams. So, function composition looks cleaner
    # as in G(F(x))

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

    # At this point x is the empty stream:
    # its value is []
    x.extend([3, 7])
    # Now the value of x is [3, 7]
    print "FIRST STEP"
    print_streams_recent([x, a, b, c, d, newa, newb])
    print ""

    x.extend([0, 11, 15])
    # Now the value of x is [3, 7, 0, 11, 15]
    print "SECOND STEP"
    print_streams_recent([x, a, b, c, d, newa, newb])