Skip to content

Commit

Permalink
refactor(agents): refactor agents.py into a package agents with the m…
Browse files Browse the repository at this point in the history
…odules base_agents and signal_agents
  • Loading branch information
BjoernLudwigPTB committed Jun 29, 2021
1 parent 67fc99e commit c7bdfae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
1 change: 1 addition & 0 deletions agentMET4FOF/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from base_agents import AgentMET4FOF, AgentNetwork, MonitorAgent
44 changes: 5 additions & 39 deletions agentMET4FOF/agents.py → agentMET4FOF/agents/base_agents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Agent dependencies
import base64
import copy
import csv
Expand All @@ -22,11 +21,11 @@
from osbrain import Agent as osBrainAgent, NSProxy, run_agent, run_nameserver
from pandas import DataFrame
from plotly import tools as tls
from .dashboard.default_network_stylesheet import default_agent_network_stylesheet
from plotly.graph_objs import Scatter

from .dashboard.Dashboard_agt_net import Dashboard_agt_net
from .streams import DataStreamMET4FOF, SineGenerator
from ..dashboard.default_network_stylesheet import \
default_agent_network_stylesheet
from ..streams import DataStreamMET4FOF


class AgentMET4FOF(MesaAgent, osBrainAgent):
Expand Down Expand Up @@ -1444,11 +1443,11 @@ def __init__(self, ip_addr="127.0.0.1", port=3333, connect=False, log_filename="

# Initialize dashboard process/thread.
if self.backend == "osbrain":
from .dashboard.Dashboard import AgentDashboardThread
from ..dashboard.Dashboard import AgentDashboardThread

self.dashboard_proc = AgentDashboardThread(**dashboard_params)
elif self.backend == "mesa":
from .dashboard.Dashboard import AgentDashboardThread
from ..dashboard.Dashboard import AgentDashboardThread

self.dashboard_proc = AgentDashboardThread(**dashboard_params)
self.dashboard_proc.start()
Expand Down Expand Up @@ -2095,36 +2094,3 @@ def save_log_info(self, log_msg):
self.save_cycles += 1
except:
raise Exception


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.
"""

def init_parameters(self, sfreq=500, sine_freq=5):
"""Initialize the input data
Initialize the input data stream as an instance of the :class:`SineGenerator`
class.
Parameters
----------
sfreq : int
sampling frequency for the underlying signal
sine_freq : float
frequency of the generated sine wave
"""
self._sine_stream = SineGenerator(sfreq=sfreq, sine_freq=sine_freq)

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 :meth:`AgentMET4FOF.send_output`.
"""
if self.current_state == "Running":
sine_data = self._sine_stream.next_sample() # dictionary
self.send_output(sine_data["quantities"])
35 changes: 35 additions & 0 deletions agentMET4FOF/agents/signal_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from agentMET4FOF.agents.base_agents import AgentMET4FOF
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.
"""

def init_parameters(self, sfreq=500, sine_freq=5):
"""Initialize the input data
Initialize the input data stream as an instance of the :class:`SineGenerator`
class.
Parameters
----------
sfreq : int
sampling frequency for the underlying signal
sine_freq : float
frequency of the generated sine wave
"""
self._sine_stream = SineGenerator(sfreq=sfreq, sine_freq=sine_freq)

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 :meth:`AgentMET4FOF.send_output`.
"""
if self.current_state == "Running":
sine_data = self._sine_stream.next_sample() # dictionary
self.send_output(sine_data["quantities"])

0 comments on commit c7bdfae

Please sign in to comment.