def stream_to_output(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)

    """
    py_audio = pyaudio.PyAudio()
    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)
    wf = wave.open("modified.wav", 'wb')

    wf.setsampwidth(sample_width)
    wf.setnchannels(num_channels)
    wf.setframerate(frame_rate)


    def write_samples_to_output(formatted_samples):
        print "Play"
        audio_stream.write(formatted_samples.encode("ISO-8859-1"))
        wf.writeframes(formatted_samples.encode("ISO-8859-1"))


    stream_agent(inputs=input_stream, outputs=[], f_type='element',
                       f=write_samples_to_output)
Ejemplo n.º 2
0
def make_output_manager(stream_dict, output_stream_names_dict):
    output_stream_names_list = output_stream_names_dict.keys()
    output_stream_list = [stream_dict[stream_name] for stream_name in output_stream_names_list]
    print " output_stream_names_list", output_stream_names_list
    print "output_stream_list", output_stream_list

    def send_message_to_queue(value_and_index_tuple):
        message_content, stream_index = value_and_index_tuple
        output_stream_name = output_stream_names_list[stream_index]
        receiver_queue = output_stream_names_dict[output_stream_name]
        message = (output_stream_name, message_content)
        receiver_queue.put(message)

    stream_agent(inputs=output_stream_list, outputs=None, f_type="asynch_element", f=send_message_to_queue, f_args=None)
def echo_list(in_stream, out_stream):
    def _copy(lst):
        print 'echo, lst = ', lst
        return lst
    return stream_agent(
        inputs=in_stream,
        outputs= out_stream,
        f_type='list',
        f=_copy)
Ejemplo n.º 4
0
def echo(in_stream, out_stream):
    def _copy(v):
        print 'echo, v = ', v
        return v
    return stream_agent(
        inputs=in_stream,
        outputs= out_stream,
        f_type='element',
        f=_copy)
def test():
    in_1 = Stream(name='in_1')
    out_1 = Stream(name='out_1')
    out_2 = Stream(name= 'out_2')

    check(out_1, [4, 8])
    check(out_2, [3, 5])

    stream_agent(
        inputs=in_1,
        outputs=[out_1, out_2],
        f_type='element',
        f=number_even_odd)
    in_1.extend([3, 4, 5, 8])
    out_1.print_recent()
    out_2.print_recent()

    check_empty()
def echo(in_streams, out_streams):
    def _copy(v_list):
        print 'echo, v_list = ', v_list
        return [v_list[1], v_list[0]]
    return stream_agent(
        inputs=in_streams,
        outputs= out_streams,
        f_type='element',
        f=_copy)
def add_count(in_stream, out_stream):
    def _add(v,count):
        print 'output is ', v+1
        return (v+1, count+1)
    return stream_agent(
        inputs=in_stream,
        outputs= out_stream,
        f_type='element',
        f=_add,
        state=0)
Ejemplo n.º 8
0
    def single_stream_of_random_numbers(trigger_stream, out_stream):
        def ran():
            return random.random()

        return stream_agent(
            inputs=None,
            f_type='element',
            f=ran,
            outputs=out_stream,
            call_streams=[trigger_stream])
Ejemplo n.º 9
0
def make_output_manager(stream_dict, output_stream_names_dict):
    output_stream_names_list = output_stream_names_dict.keys()
    output_stream_list = \
      [stream_dict[stream_name] for stream_name in output_stream_names_list]
    print ' output_stream_names_list',  output_stream_names_list
    print 'output_stream_list', output_stream_list
    
    def send_message_to_queue(value_and_index_tuple):
        message_content, stream_index = value_and_index_tuple
        output_stream_name = output_stream_names_list[stream_index]
        receiver_queue = output_stream_names_dict[output_stream_name]
        message = (output_stream_name, message_content)
        receiver_queue.put(message)

    stream_agent(
        inputs=output_stream_list,
        outputs=None,
        f_type='asynch_element',
        f=send_message_to_queue,
        f_args=None)
def keep_every_nth_value(input_stream, output_stream, n):
    def drop(lst):
        print "Drop"
        return lst[0]
    return stream_agent(
        inputs=input_stream, # The input is input_stream
        outputs=output_stream,
        f_type='window', # Identifies 'window' wrapper
        f=drop, # The function that is wrapped
        window_size=n,
        step_size=n)
def package_into_lists(input_stream, output_stream, window_size):
    def identity(lst):
        print "Package"
        return lst
    return stream_agent(
        inputs=input_stream, # The input is input_stream
        outputs=output_stream,
        f_type='window', # Identifies 'window' wrapper
        f=identity, # The function that is wrapped
        window_size=window_size,
        step_size=window_size)
Ejemplo n.º 12
0
    def print_agent(input_streams, output_streams):
        input_stream = input_streams[0]
        
        def p(v):
            if v != _close:
                print 'print_agent', input_stream.name, v

        return stream_agent(
            inputs=input_stream,
            outputs=[],
            f_type='element',
            f=p)
Ejemplo n.º 13
0
def print_agent(input_streams, output_streams):
    from Stream import Stream, _close, _no_value
    from Operators import stream_agent, stream_agent
    input_stream = input_streams[0]

    def p(v):
        if v != _close:
            print 'print_agent', input_stream.name, v

    return stream_agent(
        inputs=input_stream,
        outputs=[],
        f_type='element',
        f=p)
def insert_interpolated_values(input_stream, output_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_agent(
        inputs=input_stream, # The input is input_stream
        outputs=output_stream,
        f_type='window', # Identifies 'window' wrapper
        f=interpolate, # The function that is wrapped
        window_size=2,
        step_size=1)
def echo_n_times(in_stream, out_stream, n):

    def echo_value(v,count):
        if count < n:
            print 'echo_value. receive', v
            count += 1
            return(v+1, count)
        else:
            return(_close, count)

    return stream_agent(
        inputs=in_stream,
        outputs=out_stream,
        f_type='element',
        f=echo_value,
        state=0)
 def number_even_odd(m):
     if m%2:
         # since n is odd, the zero-th element is
         # _no_value, and the first element is n
         # in the returned list
         return [_no_value, m]
     else:
         # since n is even, the zero-th element is
         # n, and the first element is _no_value
         # in the returned list.            
         return [m, _no_value]
     return stream_agent(
     inputs=in_stream,
     outputs=[out_stream_0, out_stream_1],
     f_type='element',
     f=number_even_odd)
Ejemplo n.º 17
0
 def apply_func_agent(input_streams, output_streams):
     input_stream = input_streams[0]
     output_stream = output_streams[0]
     
     def apply_func(v):
         # When the input stream is closed, return
         # _close to cause the output stream to close.
         if v == _close:
             return _close
         else:
             return f(v)
     
     return stream_agent(
         inputs=input_stream,
         outputs=output_stream,
         f_type='element',
         f=apply_func)
def format_audio_output(input_stream, output_stream):
    """
    Parameters
    ----------
    stream: stream of lists of shorts that need to be converted to byte data for audio output
    """

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

    return stream_agent(
                       inputs=input_stream,
                       outputs=output_stream,
                       f_type='element',
                       f=format_data)
Ejemplo n.º 19
0
def apply_func_agent(input_streams, output_streams):
    from Stream import Stream, _close, _no_value
    from Operators import stream_agent, stream_agent

    def f(v): return 2*v

    input_stream = input_streams[0]
    output_stream = output_streams[0]

    def apply_func(v):
        # When the input stream is closed, return
        # _close to cause the output stream to close.
        if v == _close:
            return _close
        else:
            print "Apply func"
            return f(v)

    return stream_agent(
        inputs=input_stream,
        outputs=output_stream,
        f_type='element',
        f=apply_func)
Ejemplo n.º 20
0
    def make_output_manager(self):
        """ Creates an agent, called the output manager, that
        receives messages on streams and inserts these messages
        into queues. The output manager receives messages on all
        streams in the list output_streams and output_streams[j]
        is associated with the list of queues, output_queues_list[j].
        Note that output_queues_list[j] is a list of queues and not a
        singleton queue. A message that arrives on the stream
        output_streams[j] is copied to each of the queues in
        output_queues_list[j]. When a message is placed in a queue
        the message is a tuple (stream_name, message_content).
        Each queue is either Multiprocessing.Queue or
        StreamPy.RemoteQueue.

        Parameters
        ----------
        output_streams : list of Stream
                      list of output streams
        output_queues_list : list of list of Queue
                      list of list of output queues.
                      output_queues_list[j] is the list of queues to which
                      messages in output_streams[j] should be sent.
                      assert len(output_streams) == len(output_queues_list)

        Returns
        -------
        None

        """
        # The sequential program that implements state-transitions of the agent.
        def send_message_to_queue(msg_content_and_stream_index_tuple):
            """ The parameter msg_content_and_stream_index_tuple
            specifies the content of a message and an index, j, which
            specifies a stream, namely output_streams[j].
            Append the message content to the each of the queues
            in the list of queues, output_queues_list[j].
            The message placed on the queue is a tuple
                (output_stream_name, message_content).

            Parameter
            ---------
            msg_content_and_index_tuple: tuple
                       (message_content, stream_index)
                       message_content: value to be inserted into queues of
                                     processes that receive the specified stream.
                       stream_index: int
                                     The slot of the sending stream in the list
                                     output_stream_names_list.

            """
            message_content, stream_index = msg_content_and_stream_index_tuple
            # receiver_queue_list is the list of queues to
            # which this message is copied.
            receiver_process_list = self.output_process_list[stream_index]
            # output_streams[stream_index] is the output stream
            # on which this message arrived.
            # output_stream_name is the name of the stream on which
            # this message arrived.
            output_stream_name = self.output_streams[stream_index].name

            # The messages in the queue must be serializable. The
            # object _close is not serializable; so convert it into a
            # string '_close'. The receiving agent will convert this
            # string back into the object _close.
            if message_content is _close:
                message_content = '_close'
            # The message placed in each of the receiver queues is
            # a tuple (name of the stream, content of the message).
            message = json.dumps((output_stream_name, message_content))

            for process_id in receiver_process_list:
                if process_id not in self.process_conns:
                    self.process_conns[process_id] = self.node.create_process_conn(process_id)
                print "Process {0} sending message {1} to process {2}".format(self.id, message, process_id)
                self.process_conns[process_id].send(message)
                self.process_conns[process_id].close()
                del self.process_conns[process_id]
                print "Success!"

            return _no_value

        # Create the agent
        stream_agent(
            # The agent listens to output streams of func
            inputs=self.output_streams,
            # The agent does not generate its own output streams.
            outputs=[Stream('empty_stream')],
            # The agent processes messages from all its input
            # streams as the messages arrive. The agent does not
            # synchronize messages across different input streams.
            # So, f_type is 'asynch_element' rather than 'element'.
            f_type='asynch_element',
            f=send_message_to_queue)
Ejemplo n.º 21
0
                print 'put message', message
                receiver_queue.put(message)
                #time.sleep(0.1)
            except Exception, err:
                print 'Error', err
                return

        return _no_value

    # Create the agent
    stream_agent(
        # The agent listens to output streams of func
        inputs=output_streams,
        # The agent does not generate its own output streams.
        outputs=[Stream('empty_stream')],
        # The agent processes messages from all its input
        # streams as the messages arrive. The agent does not
        # synchronize messages across different input streams.
        # So, f_type is 'asynch_element' rather than 'element'.
        f_type='asynch_element',
        f=send_message_to_queue)


def make_process(
        input_stream_names, output_stream_names, func,
        input_queue, output_queues_list):
    """ Makes a process that gets messages on its single
    input queue, processes the messages and puts messages
    on its output queues. An output queue of this process
    is an input queue of another process.
    
Ejemplo n.º 22
0
def make_output_manager(output_streams, output_conn_list):
    """ Creates an agent, called the output manager, that
    receives messages on streams and inserts these messages
    into queues. The output manager receives messages on all
    streams in the list output_streams and output_streams[j]
    is associated with the list of queues, output_queues_list[j].
    Note that output_queues_list[j] is a list of queues and not a
    singleton queue. A message that arrives on the stream
    output_streams[j] is copied to each of the queues in
    output_queues_list[j]. When a message is placed in a queue
    the message is a tuple (stream_name, message_content).
    Each queue is either Multiprocessing.Queue or
    StreamPy.RemoteQueue.

    Parameters
    ----------
    output_streams : list of Stream
                  list of output streams
    output_queues_list : list of list of Queue
                  list of list of output queues.
                  output_queues_list[j] is the list of queues to which
                  messages in output_streams[j] should be sent.
                  assert len(output_streams) == len(output_queues_list)

    Returns
    -------
    None

    """
    # The sequential program that implements state-transitions of the agent.
    def send_message_to_queue(msg_content_and_stream_index_tuple):
        """ The parameter msg_content_and_stream_index_tuple
        specifies the content of a message and an index, j, which
        specifies a stream, namely output_streams[j].
        Append the message content to the each of the queues
        in the list of queues, output_queues_list[j].
        The message placed on the queue is a tuple
            (output_stream_name, message_content).

        Parameter
        ---------
        msg_content_and_index_tuple: tuple
                   (message_content, stream_index)
                   message_content: value to be inserted into queues of
                                 processes that receive the specified stream.
                   stream_index: int
                                 The slot of the sending stream in the list
                                 output_stream_names_list.

        """
        message_content, stream_index = msg_content_and_stream_index_tuple
        # receiver_queue_list is the list of queues to
        # which this message is copied.
        receiver_conn_list = output_conn_list[stream_index]
        # output_streams[stream_index] is the output stream
        # on which this message arrived.
        # output_stream_name is the name of the stream on which
        # this message arrived.
        output_stream_name = output_streams[stream_index].name

        # The messages in the queue must be serializable. The
        # object _close is not serializable; so convert it into a
        # string '_close'. The receiving agent will convert this
        # string back into the object _close.
        if message_content is _close:
            message_content = '_close'
        # The message placed in each of the receiver queues is
        # a tuple (name of the stream, content of the message).
        message = json.dumps((output_stream_name, message_content))

        for receiver_conn in receiver_conn_list:
            host, port = receiver_conn
            try:
                logging.info('make_output_manager. send_message_to_queue')
                logging.info('put message' + str(message))
                logging.info("Connecting to {0}:{1}".format(host, port))
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                s.connect((host, port))
                s.send(message)
                s.close()

            except socket.error as error_msg:
                logging.error(error_msg)
                continue

        return _no_value

    # Create the agent
    stream_agent(
        # The agent listens to output streams of func
        inputs=output_streams,
        # The agent does not generate its own output streams.
        outputs=[Stream('empty_stream')],
        # The agent processes messages from all its input
        # streams as the messages arrive. The agent does not
        # synchronize messages across different input streams.
        # So, f_type is 'asynch_element' rather than 'element'.
        f_type='asynch_element',
        f=send_message_to_queue)
Ejemplo n.º 23
0
def make_output_manager(output_streams, output_conn_list):
    """ Creates an agent, called the output manager, that
    receives messages on streams and inserts these messages
    into queues. The output manager receives messages on all
    streams in the list output_streams and output_streams[j]
    is associated with the list of queues, output_queues_list[j].
    Note that output_queues_list[j] is a list of queues and not a
    singleton queue. A message that arrives on the stream
    output_streams[j] is copied to each of the queues in
    output_queues_list[j]. When a message is placed in a queue
    the message is a tuple (stream_name, message_content).
    Each queue is either Multiprocessing.Queue or
    StreamPy.RemoteQueue.

    Parameters
    ----------
    output_streams : list of Stream
                  list of output streams
    output_queues_list : list of list of Queue
                  list of list of output queues.
                  output_queues_list[j] is the list of queues to which
                  messages in output_streams[j] should be sent.
                  assert len(output_streams) == len(output_queues_list)

    Returns
    -------
    None

    """
    # The sequential program that implements state-transitions of the agent.
    def send_message_to_queue(msg_content_and_stream_index_tuple):
        """ The parameter msg_content_and_stream_index_tuple
        specifies the content of a message and an index, j, which
        specifies a stream, namely output_streams[j].
        Append the message content to the each of the queues
        in the list of queues, output_queues_list[j].
        The message placed on the queue is a tuple
            (output_stream_name, message_content).

        Parameter
        ---------
        msg_content_and_index_tuple: tuple
                   (message_content, stream_index)
                   message_content: value to be inserted into queues of
                                 processes that receive the specified stream.
                   stream_index: int
                                 The slot of the sending stream in the list
                                 output_stream_names_list.

        """
        message_content, stream_index = msg_content_and_stream_index_tuple
        # receiver_queue_list is the list of queues to
        # which this message is copied.
        receiver_conn_list = output_conn_list[stream_index]
        # output_streams[stream_index] is the output stream
        # on which this message arrived.
        # output_stream_name is the name of the stream on which
        # this message arrived.
        output_stream_name = output_streams[stream_index].name

        # The messages in the queue must be serializable. The
        # object _close is not serializable; so convert it into a
        # string '_close'. The receiving agent will convert this
        # string back into the object _close.
        if message_content is _close:
            message_content = '_close'
        # The message placed in each of the receiver queues is
        # a tuple (name of the stream, content of the message).
        message = json.dumps((output_stream_name, message_content))

        for receiver_conn in receiver_conn_list:
            host, port = receiver_conn
            try:
                logging.info('make_output_manager. send_message_to_queue')
                logging.info('put message' + str(message))
                logging.info("Connecting to {0}:{1}".format(host, port))
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                s.connect((host, port))
                s.send(message)
                s.close()

            except socket.error as error_msg:
                logging.error(error_msg)
                continue

        return _no_value

    # Create the agent
    stream_agent(
        # The agent listens to output streams of func
        inputs=output_streams,
        # The agent does not generate its own output streams.
        outputs=[Stream('empty_stream')],
        # The agent processes messages from all its input
        # streams as the messages arrive. The agent does not
        # synchronize messages across different input streams.
        # So, f_type is 'asynch_element' rather than 'element'.
        f_type='asynch_element',
        f=send_message_to_queue)
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))
Ejemplo n.º 25
0
def make_network(stream_names_tuple, agent_descriptor_dict):
    """ This function makes a network of agents given the names
    of the streams in the network and a description of the
    agents in the network.

    Parameters
    ----------
    stream_names_tuple: tuple of lists
        A tuple consisting of names of streams in the network.
        Each stream in the network must have a unique name.
    agent_descriptor_dict: dict of tuples
        The key is an agent name
        The value is a tuple:
           in_list, out_list, f, f_type, f_args, state
           where:
             in_list: list of input stream names
             out_list: list of output stream names
             f: function associated with the agent
             f_type: 'element', 'list', 'window', etc
             f_args: tuple of arguments for functions f
             state: the state associated with this agent.

    Local Variables
    ---------------
    stream_dict: dict
          key: stream name
          value: Stream
    agent_dict: dict
          key: agent name
          value: agent with the specified description:
                 in_list, out_list, f, f_type, f_args, state,
                 call_streams=[timer_stream]
                 where one timer stream is associated with
                 each agent.
    agent_timer_dict: dict
          key: agent_name
          value: Stream
          The value is the timer stream associated with the
          agent. When the timer stream has a message, the
          agent is made to execute a step.

    """
    # Create streams and insert streams into stream_dict.
    stream_dict = dict()
    for stream_name in stream_names_tuple:
        stream_dict[stream_name] = Stream(stream_name)

    agent_dict = dict()
    agent_timer_dict = dict()

    # Create agents with the specified description
    # and put the agents into agent_dict.
    for agent_name in agent_descriptor_dict.keys():
        in_list, out_list, f, f_type, f_args, state, call_streams = \
          agent_descriptor_dict[agent_name]

        # Replace a list consisting of a single input stream
        # by the stream itself.
        if len(in_list) == 1:
            single_input_stream_name = in_list[0]
            inputs = stream_dict[single_input_stream_name]
        else:
            inputs = list()
            for input_stream_name in in_list:
                inputs.append(stream_dict[input_stream_name])

        # Replace a list consisting of a single output stream
        # by the stream itself.
        if len(out_list) == 1:
            single_output_stream_name = out_list[0]
            outputs = stream_dict[single_output_stream_name]
        else:
            outputs = list()
            for output_stream_name in out_list:
                outputs.append(stream_dict[output_stream_name])

        # Create timer streams and insert them into agent_timer_dict
        agent_timer_dict[agent_name] = Stream(agent_name + ':timer')

        # Create agents and insert them into agent_dict
        agent_dict[agent_name] = stream_agent(
            inputs,
            outputs,
            f_type,
            f,
            f_args,
            state,
            call_streams=[agent_timer_dict[agent_name]])

        # Set the name for this agent.
        agent_dict[agent_name].name = agent_name

    return (stream_dict, agent_dict, agent_timer_dict)
Ejemplo n.º 26
0
from examples_element_wrapper import print_stream
import numpy as np
from random import random
""" Simple example of a windows-wrapper.

"""

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

print_stream(x)
print_stream(y)
print_stream(z)

stream_agent(inputs=x, outputs=y, f_type='window', f=np.mean,
             window_size=5, step_size=5)
# Alternative form
wf(x, z, np.mean, 5, 5)

x.extend([99.5+random() for _ in range(21)])

# Second example
# Illustrates that step_size may be larger than window_size.
# Stream b samples every n-th element of stream a.
a = Stream('a')
b = Stream('b')
print_stream(a)
print_stream(b)
n = 6
wf(a, b, lambda l: l[0], 1, n)
a.extend(range(18))
Ejemplo n.º 27
0
def make_network(stream_names_tuple, agent_descriptor_dict):
    """ This function makes a network of agents given the names
    of the streams in the network and a description of the
    agents in the network.

    Parameters
    ----------
    stream_names_tuple: tuple of lists
        A tuple consisting of names of streams in the network.
        Each stream in the network must have a unique name.
    agent_descriptor_dict: dict of tuples
        The key is an agent name
        The value is a tuple:
           in_list, out_list, f, f_type, f_args, state
           where:
             in_list: list of input stream names
             out_list: list of output stream names
             f: function associated with the agent
             f_type: 'element', 'list', 'window', etc
             f_args: tuple of arguments for functions f
             state: the state associated with this agent.

    Local Variables
    ---------------
    stream_dict: dict
          key: stream name
          value: Stream
    agent_dict: dict
          key: agent name
          value: agent with the specified description:
                 in_list, out_list, f, f_type, f_args, state,
                 call_streams=[timer_stream]
                 where one timer stream is associated with
                 each agent.
    agent_timer_dict: dict
          key: agent_name
          value: Stream
          The value is the timer stream associated with the
          agent. When the timer stream has a message, the
          agent is made to execute a step.

    """
    # Create streams and insert streams into stream_dict.
    stream_dict = dict()
    for stream_name in stream_names_tuple:
        stream_dict[stream_name] = Stream(stream_name)

    agent_dict = dict()
    agent_timer_dict = dict()

    # Create agents with the specified description
    # and put the agents into agent_dict.
    for agent_name in agent_descriptor_dict.keys():
        in_list, out_list, f, f_type, f_args, state, call_streams = \
          agent_descriptor_dict[agent_name]

        # Replace a list consisting of a single input stream
        # by the stream itself.
        if len(in_list) == 1:
            single_input_stream_name = in_list[0]
            inputs = stream_dict[single_input_stream_name]
        else:
            inputs = list()
            for input_stream_name in in_list:
                inputs.append(stream_dict[input_stream_name])

        # Replace a list consisting of a single output stream
        # by the stream itself.
        if len(out_list) == 1:
            single_output_stream_name = out_list[0]
            outputs = stream_dict[single_output_stream_name]
        else:
            outputs = list()
            for output_stream_name in out_list:
                outputs.append(stream_dict[output_stream_name])
        
        # Create timer streams and insert them into agent_timer_dict 
        agent_timer_dict[agent_name] = Stream(
            agent_name + ':timer')

        # Create agents and insert them into agent_dict
        agent_dict[agent_name] = stream_agent(
            inputs, outputs, f_type, f, f_args, state,
            call_streams=[agent_timer_dict[agent_name]])

        # Set the name for this agent.
        agent_dict[agent_name].name = agent_name

    return (stream_dict, agent_dict, agent_timer_dict)
Ejemplo n.º 28
0
#_____________________________________________________
#_____________________________________________________
# 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,
    outputs=mean_of_x_a,
    f_type='window',
    f=np.mean,
    window_size=window_size,
    step_size=step_size)
print_stream(mean_of_x_a)

 
# Drive the example.
# Add values to stream x.
x.extend([random.random() for _ in range(N)])

#_____________________________________________________
# EXAMPLE 2. STANDARD DEVIATION OF SLIDING WINDOW
#_____________________________________________________

# SPECIFICATION:
Ejemplo n.º 29
0
#_____________________________________________________
#_____________________________________________________
# 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,
    outputs=mean_of_x_a,
    f_type='window',
    f=np.mean,
    window_size=window_size,
    step_size=step_size)
print_stream(mean_of_x_a)

 
# Drive the example.
# Add values to stream x.
x.extend([random.random() for _ in range(N)])

#_____________________________________________________
# EXAMPLE 2. STANDARD DEVIATION OF SLIDING WINDOW
#_____________________________________________________

# SPECIFICATION: