Tutorial 1 - A simple pipeline to plot a signal¶

First we define a simple pipeline of two agents, of which one will generate a signal (in our case a SineGeneratorAgent) and the other one plots the signal on the dashboard (this is always a MonitorAgent).

We define a SineGeneratorAgent for which we have to override the functions init_parameters() & agent_loop() to define the new agent’s behaviour.

  • init_parameters() is used to setup the input data stream and potentially other necessary parameters.
  • agent_loop() will be endlessly repeated until further notice. It will sample by sample extract the input data stream’s content and push it to all agents connected to SineGeneratorAgent’s output channel by invoking send_output().

The MonitorAgent is connected to the SineGeneratorAgent’s output channel and per default automatically plots the output.

Each agent has an internal current_state which can be used as a switch to change the behaviour of the agent. The available states are listed here.

As soon as all agents are initialized and the connections are set up, the agent network is started by accordingly changing all agents’ state simultaneously.

[1]:
# %load tutorial_1_generator_agent.py
from agentMET4FOF.agents import AgentMET4FOF, AgentNetwork, MonitorAgent
from agentMET4FOF.streams import SineGenerator


class SineGeneratorAgent(AgentMET4FOF):
    """An agent streaming a sine signal

    Takes samples from the :py:mod:`SineGenerator` and pushes them sample by sample
    to connected agents via its output channel.
    """

    # The datatype of the stream will be SineGenerator.
    _sine_stream: SineGenerator

    def init_parameters(self):
        """Initialize the input data

        Initialize the input data stream as an instance of the
        :py:mod:`SineGenerator` class
        """
        self._sine_stream = SineGenerator()

    def agent_loop(self):
        """Model the agent's behaviour

        On state *Running* the agent will extract sample by sample the input data
        streams content and push it via invoking :py:method:`AgentMET4FOF.send_output`.
        """
        if self.current_state == "Running":
            sine_data = self._sine_stream.next_sample()  # dictionary
            self.send_output(sine_data["quantities"])


def demonstrate_generator_agent_use():
    # Start agent network server.
    agent_network = AgentNetwork()

    # Initialize agents by adding them to the agent network.
    gen_agent = agent_network.add_agent(agentType=SineGeneratorAgent)
    monitor_agent = agent_network.add_agent(agentType=MonitorAgent)

    # Interconnect agents by either way:
    # 1) by agent network.bind_agents(source, target).
    agent_network.bind_agents(gen_agent, monitor_agent)

    # 2) by the agent.bind_output().
    gen_agent.bind_output(monitor_agent)

    # Set all agents' states to "Running".
    agent_network.set_running_state()

    # Allow for shutting down the network after execution
    return agent_network


if __name__ == "__main__":
    demonstrate_generator_agent_use()

Starting NameServer...
Broadcast server running on 0.0.0.0:9091
NS running on 127.0.0.1:3333 (127.0.0.1)
URI = PYRO:Pyro.NameServer@127.0.0.1:3333

--------------------------------------------------------------
|                                                            |
| Your agent network is starting up. Open your browser and   |
| visit the agentMET4FOF dashboard on http://127.0.0.1:8050/ |
|                                                            |
--------------------------------------------------------------

INFO [2021-02-05 19:13:45.925758] (SineGeneratorAgent_1): INITIALIZED
INFO [2021-02-05 19:13:45.960173] (MonitorAgent_1): INITIALIZED
[2021-02-05 19:13:45.975418] (SineGeneratorAgent_1): Connected output module: MonitorAgent_1
SET STATE:   Running
[2021-02-05 19:13:46.932922] (SineGeneratorAgent_1): Pack time: 0.000957
[2021-02-05 19:13:46.935938] (SineGeneratorAgent_1): Sending: [0.]
[2021-02-05 19:13:46.939035] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:46.941337] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.])}
[2021-02-05 19:13:46.942394] (MonitorAgent_1): Tproc: 0.002278
[2021-02-05 19:13:47.932181] (SineGeneratorAgent_1): Pack time: 0.000614
[2021-02-05 19:13:47.935312] (SineGeneratorAgent_1): Sending: [0.06279052]
[2021-02-05 19:13:47.936553] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.06279052]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:47.941367] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052])}
[2021-02-05 19:13:47.942190] (MonitorAgent_1): Tproc: 0.004357
[2021-02-05 19:13:48.931650] (SineGeneratorAgent_1): Pack time: 0.00047
[2021-02-05 19:13:48.933397] (SineGeneratorAgent_1): Sending: [0.12533323]
[2021-02-05 19:13:48.934297] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.12533323]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:48.936482] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323])}
[2021-02-05 19:13:48.936997] (MonitorAgent_1): Tproc: 0.002201
[2021-02-05 19:13:49.932143] (SineGeneratorAgent_1): Pack time: 0.000937
[2021-02-05 19:13:49.940442] (SineGeneratorAgent_1): Sending: [0.18738131]
[2021-02-05 19:13:49.934471] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.18738131]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:49.938767] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131])}
[2021-02-05 19:13:49.939977] (MonitorAgent_1): Tproc: 0.004969
[2021-02-05 19:13:50.930904] (SineGeneratorAgent_1): Pack time: 0.000255
[2021-02-05 19:13:50.931636] (SineGeneratorAgent_1): Sending: [0.24868989]
[2021-02-05 19:13:50.932383] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.24868989]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:50.933944] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131, 0.24868989])}
[2021-02-05 19:13:50.934185] (MonitorAgent_1): Tproc: 0.001522
NS shut down.
[2021-02-05 19:13:51.932632] (SineGeneratorAgent_1): Pack time: 0.001127
[2021-02-05 19:13:51.935690] (SineGeneratorAgent_1): Sending: [0.30901699]
[2021-02-05 19:13:51.935544] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.30901699]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:51.944128] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131, 0.24868989,
       0.30901699])}
[2021-02-05 19:13:51.944378] (MonitorAgent_1): Tproc: 0.008396
[2021-02-05 19:13:52.930087] (SineGeneratorAgent_1): Pack time: 0.000108
[2021-02-05 19:13:52.930343] (SineGeneratorAgent_1): Sending: [0.36812455]
[2021-02-05 19:13:52.930548] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.36812455]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:52.930905] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131, 0.24868989,
       0.30901699, 0.36812455])}
[2021-02-05 19:13:52.931004] (MonitorAgent_1): Tproc: 0.000381
[2021-02-05 19:13:53.930135] (SineGeneratorAgent_1): Pack time: 0.000144
[2021-02-05 19:13:53.930526] (SineGeneratorAgent_1): Sending: [0.42577929]
[2021-02-05 19:13:53.930537] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.42577929]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:53.930940] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131, 0.24868989,
       0.30901699, 0.36812455, 0.42577929])}
[2021-02-05 19:13:53.930993] (MonitorAgent_1): Tproc: 0.00038
[2021-02-05 19:13:54.932072] (SineGeneratorAgent_1): Pack time: 0.0009
[2021-02-05 19:13:54.934391] (SineGeneratorAgent_1): Sending: [0.48175367]
[2021-02-05 19:13:54.934275] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.48175367]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:54.936874] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131, 0.24868989,
       0.30901699, 0.36812455, 0.42577929, 0.48175367])}
[2021-02-05 19:13:54.937315] (MonitorAgent_1): Tproc: 0.002635
[2021-02-05 19:13:55.931558] (SineGeneratorAgent_1): Pack time: 0.000454
[2021-02-05 19:13:55.933193] (SineGeneratorAgent_1): Sending: [0.53582679]
[2021-02-05 19:13:55.933792] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.53582679]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:13:55.936039] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052, 0.12533323, 0.18738131, 0.24868989,
       0.30901699, 0.36812455, 0.42577929, 0.48175367, 0.53582679])}
[2021-02-05 19:13:55.936606] (MonitorAgent_1): Tproc: 0.002345