Beispiel #1
0
def check(x, n):
    def check_value(x_element):
        print x_element
        assert len(n) > 0
        assert_equals(x_element,n.pop(0))
    output_test.append(n)
    stream_func(inputs = x, f = check_value, f_type = 'element', num_outputs = 0)
def window_example_of_novalue(stream):
    return stream_func(inputs=stream,
                       f_type='window',
                       f=example_of_no_value,
                       num_outputs=1,
                       window_size=2,
                       step_size=2)
def exponential_smoothed_timed_windows(input_stream, func, alpha, window_size,
                                       step_size, initial_state):
    """
    Parameters

    ----------
    input_stream: Stream
           A previously defined stream
           This is the only input stream of the agent.
    func: function
          func operates on a list of TimeAndValue objects
          and returns an object that can be smoothed
          exponentially.
    alpha: number
          The exponential smoothing parameter.
    window_size, step_size, initial_state:
          Already defined.

    """
    def exponential_smoothed_list(timed_list, state):
        next_state = ((1 - alpha) * func(timed_list) + alpha * state)
        message = next_state
        return (message, next_state)

    return stream_func(
        inputs=input_stream,  # single input timed stream
        f_type='timed',  # identifies 'timed' wrapper
        f=exponential_smoothed_list,  # function that is wrapped
        num_outputs=1,  # single output stream
        state=initial_state,
        window_size=window_size,
        step_size=step_size)
Beispiel #4
0
def inrange_and_outlier_streams(
        list_of_two_streams, window_size, step_size,
        threshold):

    # The function that is wrapped.
    def inrange_and_outlier_lists(list_of_two_windows):
        # Call the two windows, x and y, respectively.
        x, y = list_of_two_windows
        # Convert to numpy arrays to call numpy functions.
        x = np.array(x)
        y = np.array(y)
        if abs(y.mean() - x.mean()) <= threshold*x.std():
            # (x,y) is in range.
            # Return _no_value for the outlier stream.
            return ([y.mean(), _no_value])
        else:
            # (x,y) is an outlier
            # Return _no_value for the in-range stream.
            return ([_no_value, y.mean()])

    # The wrapper
    return stream_func(
        inputs=list_of_two_streams, # A list of streams
        f_type='window', # Indicates the 'window' wrapper
        f=inrange_and_outlier_lists, # Function that is wrapped.
        num_outputs=2, # Returns a list of two streams.
        window_size=window_size,
        step_size=step_size)
Beispiel #5
0
def quench_stream(stream):
    """
    Input Streams
    -------------
      Single input stream with elements of the form:
      (timestamp, value) where value is a number.

    Output Streams
    --------------
      Single output stream with elements of the form:
      (timestamp, value).
      A (time, value) element in the input stream is
      put in the output stream if the time is outside
      a quenching interval or if the value is greater
      than the maximum value observed in this quenching
      interval.

    state
    -----
    The state is a 2-tuple:
    (t_quench_started, max_acc_in_quench_period)
    see function quench.

    Initially state is (-(MIN_GAP+1), 0.0) because the
    next pick must start a new quenching period even
    if the pick is at time 0.
  
"""
    return stream_func(inputs=stream,
                       f_type='element',
                       f=quench,
                       num_outputs=1,
                       state=(MIN_GAP, 0.0))
Beispiel #6
0
def single_stream_of_random_numbers(timer_stream):
    return stream_func(
        inputs=None,
        f_type='element',
        f=random.random,
        num_outputs=1,
        call_streams=[timer_stream])
def main():
    def max_of_std(lst, state):
        a = np.array(lst)
        state = max(a.std(), state)
        return (state, state)
    print "example_1"
    print "example function from list to value: mean_and_sigma() "
    window_size = 10
    step_size = 10
    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

    z = stream_func(x, f_type='window', f=max_of_std, num_outputs=1, state=0.0,
                    window_size=window_size, step_size=step_size)

    z.set_name('z')
 

    x.extend([random.random() for i in range(30)])
    x.print_recent()
    z.print_recent()
def main():
    def sum_diff_of_means(list_of_two_lists, cumulative):
        a, b = list_of_two_lists
        cumulative += np.mean(a) - np.mean(b)
        return (cumulative, cumulative)
    x = Stream('x')
    y = Stream('y')
    z = stream_func([x,y], f_type='window', f=sum_diff_of_means,
                    num_outputs=1, state = 0,
                    window_size=2, step_size=2)
    z.set_name('z')
    
    x.extend([3,5])
    y.extend([2])
    x.print_recent()
    y.print_recent()
    z.print_recent()
    print

    x.extend([11,15])
    y.extend([4, -10, -12])
    x.print_recent()
    y.print_recent()
    z.print_recent()
    print
Beispiel #9
0
def stream_to_output(py_audio, input_stream, num_channels=1, sample_width=2, frame_rate=44100):
    """
    Parameters
    ----------
    py_audio: instance of PyAudio
    input_stream: Stream of audio samples
    num_channels: number of audio channels (1 or 2 for mono/stereo)
    sample_width: number of bytes per sample
    frame_rate: rate at which samples are played back (samples/second)
    
    """

    logging.info("Preparing output audio stream")

    audio_stream = py_audio.open(
        format=py_audio.get_format_from_width(sample_width),
        channels=num_channels,
        rate=frame_rate,
        output=True,
        input=False,
    )

    def write_samples_to_output(formatted_samples):
        audio_stream.write(formatted_samples)

    return stream_func(inputs=input_stream, f_type="element", f=write_samples_to_output, num_outputs=0)
def inrange_and_outlier_streams(
        x_and_y_streams,
        window_size, step_size,
        threshold):

    def inrange_and_outlier(x_and_y_lists):
        x, y = x_and_y_lists
        x = np.array(x)
        y = np.array(y)
        if abs(y.mean() - x.mean()) <= threshold*x.std():
            # (x,y) is in range.
            # Return _no_value for the outlier stream.
            return ([y.mean(), _no_value])
        else:
            # (x,y) is an outlier
            # Return _no_value for the in-range stream.
            return ([_no_value, y.mean()])

    return stream_func(
        inputs=x_and_y_streams,
        f_type='window',
        f=inrange_and_outlier,
        num_outputs=2,
        window_size=window_size,
        step_size=step_size)
def inrange_and_outlier_streams(
        list_of_two_streams, window_size, step_size,
        threshold):

    # The function that is wrapped.
    def inrange_and_outlier_lists(list_of_two_windows):
        # Call the two windows, x and y, respectively.
        x, y = list_of_two_windows
        # Convert to numpy arrays to call numpy functions.
        x = np.array(x)
        y = np.array(y)
        if abs(y.mean() - x.mean()) <= threshold*x.std():
            # (x,y) is in range.
            # Return _no_value for the outlier stream.
            return ([y.mean(), _no_value])
        else:
            # (x,y) is an outlier
            # Return _no_value for the in-range stream.
            return ([_no_value, y.mean()])

    # The wrapper
    return stream_func(
        inputs=list_of_two_streams, # A list of streams
        f_type='window', # Indicates the 'window' wrapper
        f=inrange_and_outlier_lists, # Function that is wrapped.
        num_outputs=2, # Returns a list of two streams.
        window_size=window_size,
        step_size=step_size)
Beispiel #12
0
def metrics_stream(input_stream):
    return stream_func(inputs=input_stream,
                       f_type='window',
                       f=metrics,
                       num_outputs=1,
                       state=None,
                       window_size=METRICS_WINDOW_SIZE,
                       step_size=METRICS_STEP_SIZE)
Beispiel #13
0
 def average_stream(stream):
     return stream_func(
         inputs=stream,
         f_type='element',
         f=average,
         num_outputs=0,
         state=(0,0.0)
         )
def multiply_elements_in_stream(stream, multiplier):

    # function on an element that returns an element
    def mult(v):
        return multiplier*v

    # Use the stream_func wrapper to return a function on streams.
    return stream_func(inputs=stream, f_type='element', f=mult, num_outputs=1)
Beispiel #15
0
def cumulative_stream(stream):
    return stream_func(
        inputs=stream,
        f_type='element',
        f=cumulative_sum,
        num_outputs=1,
        state=0 # The initial state
        )
Beispiel #16
0
def average_stream(stream):
    return stream_func(
        inputs=stream,
        f_type='element',
        f=average,
        num_outputs=1,
        state=(0, 0.0) # The initial state
        # Initially n = 0, cumulative = 0.0
        )
Beispiel #17
0
    def print_stream(stream):

        def print_list(lst):
            for v in lst:
                print 'from print_list {0} : {1}'.format(stream.name, v)

        return stream_func(
            inputs=stream, f_type='list',
            f=print_list, num_outputs=0)
Beispiel #18
0
def package_into_lists(input_stream, window_size):
    def identity(lst):
        return lst
    return stream_func(
        inputs=input_stream, # The input is input_stream
        f_type='window', # Identifies 'window' wrapper
        f=identity, # The function that is wrapped
        num_outputs=1, # Returns a single stream
        window_size=window_size,
        step_size=window_size)
Beispiel #19
0
def mean_of_window(stream):
    return stream_func(
        inputs=stream, # A single stream
        f_type='window', # Identifies the 'window' wrapper
        f=mean_inc, # The wrapped function
        num_outputs=1, # The wrapper returns a single stream
        state=(None, None), # Initial state
        window_size=100,
        step_size=1 # step_size is 1 for incremental computation.
        )
Beispiel #20
0
def keep_every_nth_value(input_stream, n):
    def drop(lst):
        return lst[0]
    return stream_func(
        inputs=input_stream, # The input is input_stream
        f_type='window', # Identifies 'window' wrapper
        f=drop, # The function that is wrapped
        num_outputs=1, # Returns a single stream
        window_size=n,
        step_size=n)
def mean_of_window(stream):
    return stream_func(
        inputs=stream, # A single stream
        f_type='window', # Identifies the 'window' wrapper
        f=mean_inc, # The wrapped function
        num_outputs=1, # The wrapper returns a single stream
        state=(None, None), # Initial state
        window_size=100,
        step_size=1 # step_size is 1 for incremental computation.
        )
Beispiel #22
0
    def single_stream_of_random_numbers(trigger_stream):
        def ran():
            return random.random()

        return stream_func(
            inputs=None,
            f_type='element',
            f=ran,
            num_outputs=1,
            call_streams=[trigger_stream])
Beispiel #23
0
def print_stream(stream):

    def print_element(v, count):
        print '{0}[{1}] = {2}'.format(stream.name, count, v)
        return (count+1)

    return stream_func(
        inputs=stream, f_type='element',
        f=print_element, num_outputs=0,
        state=0)
Beispiel #24
0
    def print_stream_elements(stream):

        def print_element(v):
            print 'from print_element {0} : {1}'.format(stream.name, v)

        return stream_func(
            inputs=stream,
            f_type='element',
            f=print_element,
            num_outputs=0)
def print_stream(stream):
    stream_name = stream.name

    def print_stream_value(v):
        print stream_name, ':', 'next value=', v

    return stream_func(
        inputs=stream,
        f_type='element',
        f=print_stream_value,
        num_outputs=1)
def boolean_of_values_greater_than_threshold(stream, threshold):

    # function on an element that returns an element
    def value_greater_than_threshold(value):
        return value > threshold

    # Use the stream_func wrapper to return a function on streams.
    return stream_func(
        inputs=stream,
        f_type='element',
        f=value_greater_than_threshold,
        num_outputs=1)
Beispiel #27
0
def split_into_even_odd_stream(input_stream):
 
    def number_even_odd(m):
            if not m%2:
                return [_no_value, m]
            else:
                return [m, _no_value]
 
    return stream_func(inputs=input_stream,
                       f_type='element',
                       f=number_even_odd,
                       num_outputs=2)
def inrange_and_outlier_streams(x_and_y_streams, a, b, delta):

    # Functions: list -> list
    def inrange_and_outlier(x_and_y_lists):
        z_list = zip(*x_and_y_lists)
        outliers_list = [v for v in z_list if abs(a*v[0] + b -v[1]) > delta]
        inrange_list= [v for v in z_list if abs(a*v[0] + b -v[1]) <= delta]
        return ([inrange_list, outliers_list])

    return stream_func(
        inputs=x_and_y_streams, f_type='list',
        f=inrange_and_outlier, num_outputs=2)
    def stream_of_normal_and_pareto(clock_stream, b):
        from scipy.stats import norm, pareto

        def normal_and_pareto():
            return [norm.rvs(size=1)[0], pareto.rvs(b, size=1)[0]]

        return stream_func(
            inputs=None,
            f_type='element',
            f=normal_and_pareto,
            num_outputs=2,
            call_streams=[clock_stream]
            )
Beispiel #30
0
def format_audio_output(stream):
    """
    Parameters
    ----------
    stream: stream of lists of shorts that need to be converted to byte data for audio output
    """

    def format_data(shorts):
        format = "h" * len(shorts)
        packed = struct.pack(format, *shorts)
        return packed

    return stream_func(inputs=stream, f_type="element", f=format_data, num_outputs=1)
Beispiel #31
0
def print_stream(stream):
        name = stream.name
         
        def print_stream_value(v, index):
            print name + '[' , index , '] = ', v
            return (_no_value, index+1)
 
        return stream_func(
            inputs=stream,
            f_type='element',
            f=print_stream_value,
            num_outputs=1,
            state=0
            )
Beispiel #32
0
def weighted_sum_of_past_values(input_stream, weights):
    
    def dot_product_window_with_weights(window_list):
        assert len(window_list) == len(weights)
        return np.dot(window_list, weights)
    
    return stream_func(
        inputs=input_stream, # A single stream
        f_type='window', # Identifies 'window' wrapper
        f=dot_product_window_with_weights, # wrapped this function
        num_outputs=1, # Returns a single stream
        window_size=len(weights),
        step_size=1 # The window always moves by 1
        )
def combine_windows(input_stream, weights):
    
    def dot_product_window_with_weights(window_list):
        assert len(window_list) == len(weights)
        return np.dot(window_list, weights)
    
    return stream_func(
        inputs=input_stream, # A single stream
        f_type='window', # Identifies 'window' wrapper
        f=dot_product_window_with_weights,
        num_outputs=1, # Returns a single stream
        window_size=len(weights),
        step_size=1 # The window always moves by 1
        )
Beispiel #34
0
def combine_windows(input_stream, weights):
    
    def dot_product_window_with_weights(window_list):
        assert len(window_list) == len(weights)
        return np.dot(window_list, weights)
    
    return stream_func(
        inputs=input_stream, # A single stream
        f_type='window', # Identifies 'window' wrapper
        f=dot_product_window_with_weights,
        num_outputs=1, # Returns a single stream
        window_size=len(weights),
        step_size=1 # The window always moves by 1
        )
Beispiel #35
0
def insert_interpolated_values(input_stream, n):

    def interpolate(lst):
        if len(lst) < 2: return lst
        increment = (lst[1] - lst[0])/float(n)
        return_list = [lst[0]+k*increment for k in range(n)]
        return _multivalue(return_list)
    
    return stream_func(
        inputs=input_stream, # The input is input_stream
        f_type='window', # Identifies 'window' wrapper
        f=interpolate, # The function that is wrapped
        num_outputs=1, # Returns a single stream
        window_size=2,
        step_size=1)
Beispiel #36
0
def inrange_and_outlier_streams(
        list_of_two_streams, window_size, step_size,
        alpha, # The exponential smoothing parameter
        threshold):

    # The function that is wrapped.
    def inrange_and_outlier_windows(
            list_of_two_windows, # one window for each input stream
            state): # The state
        threshold, count = state
        # Call the two windows x and y
        x, y = list_of_two_windows
        x = np.array(x)
        y = np.array(y)
        num_standard_deviations = \
          abs(y.mean() - x.mean())/x.std()
        if num_standard_deviations <= threshold:
            # (x,y) is in range.
            inrange_value = (x.mean(), y.mean())
            outlier_value = _no_value
            # The output message
            message = [(count, inrange_value), outlier_value]
        else:
            # (x,y) is an outlier
            inrange_value = _no_value
            outlier_value = (x.mean(), y.mean())
            # The output message
            message = [inrange_value, (count, outlier_value)]

        # Exponentially smooth the current threshold to get
        # the new threshold.
        threshold = (num_standard_deviations * alpha
                     + threshold * (1 - alpha))
        count += 1
        # The next state
        state = (threshold, count)
        # Return the output message and the next state.
        return (message, state)

    # The wrapper
    return stream_func(
        inputs=list_of_two_streams,
        f_type='window', # Indicates the 'window' wrapper.
        f=inrange_and_outlier_windows, # The function that is wrapped.
        num_outputs=2, # Returns a list of two streams
        state=(0.0, 0), # Initial state, i.e., initial threshold, count
        window_size=window_size,
        step_size=step_size)
def inrange_and_outlier_streams(
        list_of_two_streams, window_size, step_size,
        alpha, # The exponential smoothing parameter
        threshold):

    # The function that is wrapped.
    def inrange_and_outlier_windows(
            list_of_two_windows, # one window for each input stream
            state): # The state
        threshold, count = state
        # Call the two windows x and y
        x, y = list_of_two_windows
        x = np.array(x)
        y = np.array(y)
        num_standard_deviations = \
          abs(y.mean() - x.mean())/x.std()
        if num_standard_deviations <= threshold:
            # (x,y) is in range.
            inrange_value = (x.mean(), y.mean())
            outlier_value = _no_value
            # The output message
            message = [(count, inrange_value), outlier_value]
        else:
            # (x,y) is an outlier
            inrange_value = _no_value
            outlier_value = (x.mean(), y.mean())
            # The output message
            message = [inrange_value, (count, outlier_value)]

        # Exponentially smooth the current threshold to get
        # the new threshold.
        threshold = (num_standard_deviations * alpha
                     + threshold * (1 - alpha))
        count += 1
        # The next state
        state = (threshold, count)
        # Return the output message and the next state.
        return (message, state)

    # The wrapper
    return stream_func(
        inputs=list_of_two_streams,
        f_type='window', # Indicates the 'window' wrapper.
        f=inrange_and_outlier_windows, # The function that is wrapped.
        num_outputs=2, # Returns a list of two streams
        state=(0.0, 0), # Initial state, i.e., initial threshold, count
        window_size=window_size,
        step_size=step_size)
Beispiel #38
0
def write_stream(stream):
    name = stream.name
    def write_stream_value(v, index):
        name = stream.name
        output_file_name = 'stream_vals.csv'
        f = open(output_file_name, 'a')
        f.write(name + '[' + str(index) + '] = ' + str(v) +',\n')
        f.close()
        return (_no_value, index+1)

    return stream_func(
        inputs=stream,
        f_type='element',
        f=write_stream_value,
        num_outputs=1,
        state=0
        )

        
Beispiel #39
0
def exp_smoothing_mean_and_std_of_stream(
        stream, alpha, window_size, step_size):
    def exp_smoothing_mean_and_std_of_list(lst, state):
        alpha = 0.8
        a = np.array(lst)
        #m is mean. s is standard deviation.
        m, s = state
        m = (1-alpha)*m + alpha*a.mean()
        s = (1-alpha)*s + alpha*a.std()
        state = (m,s)
        tuple_of_messages = (m,s)
        return (tuple_of_messages, state)
    return stream_func(
        inputs=stream, # A single Stream
        f_type='window', # Identifies the 'window' wrapper
        f=exp_smoothing_mean_and_std_of_list, # The wrapped function
        num_outputs=2, # The wrapper returns a list of two output streams.
        state=(10.5, 0.29), # Initial estimates of mean, std
        window_size=window_size,
        step_size=step_size)
# 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)

# Drive the input streams.
t = 0.0
for _ in range(20):
    t += random.random()
    v = random.randint(0, 9)
    a.append(TimeAndValue(t, v))

############################################################
Beispiel #41
0
# Second step:
# Wrap the function using the wrapper, stream_func,
# in the same way as was done for the element_wrapper.
# This function returns a stream; we call that
# stream mean_of_x.

# The input stream for this example is x
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
#_____________________________________________________
#_____________________________________________________
Beispiel #42
0
def multiply_elements_in_stream(stream, multiplier):
    def mult(v):
        return multiplier*v
    return stream_func(stream, f_type = 'element', f=mult, num_outputs = 1)
                                                alpha=0.001)

    x = Stream('x')

    linear_regression.init_plot()
    model = Stream_Learn(data_train=x,
                         data_out=x,
                         train_func=m.train,
                         predict_func=m.predict,
                         min_window_size=min_window_size,
                         max_window_size=max_window_size,
                         step_size=step_size,
                         num_features=num_features,
                         all_func=all_func)
    y = model.run()
    stream_func(inputs=y, f=print_stream, f_type='element', num_outputs=0)

    while i < num_points:
        w[1] += 0.01
        x_value = np.ones((1, num_features)) * i
        x_b = np.hstack((np.ones((1, 1)), x_value)).transpose()
        y_value = w.transpose().dot(x_b)[0][0]
        values = x_value.tolist()[0]
        values.append(y_value)
        x.extend([tuple(values)])

        if i % 100 == 0 and i != 0:
            model.reset()

        print i
        i += 1
Beispiel #44
0
def multiply_elements_stream(stream, multiplier):
    return stream_func(inputs = stream, f_type = 'element', f=multiply_elements, num_outputs = 1)
Beispiel #45
0
def split_into_even_odd_stream(input_stream):
    return stream_func(inputs=input_stream,
                       f_type='element',
                       f=split_into_even_odd,
                       num_outputs=2)