agentMET4FOF agents¶
Base agents¶
-
class
agentMET4FOF.agents.base_agents.
AgentMET4FOF
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ Base class for all agents with specific functions to be overridden/supplied by user.
Behavioral functions for users to provide are init_parameters, agent_loop and on_received_message. Communicative functions are bind_output, unbind_output and send_output.
-
_bind_output
(output_agent, channel='default')[source]¶ Internal method which implements the logic for connecting this agent, to the output_agent.
-
_convert_matplotlib_fig
(fig: matplotlib.figure.Figure, mode: str = 'image')[source]¶ Convert matplotlib figure to be rendered by the dashboard
-
_convert_to_plotly
(matplotlib_fig: matplotlib.figure.Figure)[source]¶ Internal method to convert matplotlib figure to plotly figure
Parameters: matplotlib_fig (plt.Figure) – Matplotlib figure to be converted
-
_fig_to_uri
(matplotlib_fig: matplotlib.figure.Figure)[source]¶ Internal method to convert matplotlib figure to base64 uri image for display
Parameters: matplotlib_fig (plt.Figure) – Matplotlib figure to be converted
-
_get_metadata
(data)[source]¶ Internal helper function for getting the data type & dimensions of data. This is for update_output_channels_info()
-
_is_type_message
(data)[source]¶ Internal method to check if the data carries signature of an agent message type
Parameters: data – Data to be checked for type Returns: result Return type: boolean
-
_remove_methods
(cls)[source]¶ Remove methods from the other backends base class from the current agent
-
_update_output_channels_info
(data, channel)[source]¶ Internal method to update the dict of output_channels_info. This is used in conjunction with send_output().
Checks and records data type & dimension and channel name If the data is nested within dict, then it will search deeper and subsequently record the info of each inner hierarchy
Parameters: - data – data to be checked for type & dimension
- channel (str) – name of channel to be recorded
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via init_agent_loop( loop_wait)
To start a new loop, call init_agent_loop(loop_wait) on the agent. Example of usage is to check the current_state of the agent and send data periodically.
-
bind_output
(output_agent, channel='default')[source]¶ Forms Output connection with another agent
Any call on send_output will reach this newly binded agent. Adds the agent to its list of Outputs.
Parameters: - output_agent (AgentMET4FOF or list) – Agent(s) to be binded to this agent’s output channel
- channel (str or list of str) – Specific name of the channel(s) to be subscribed to. (Default = “data”)
-
buffer_clear
(agent_name: Optional[str] = None)[source]¶ Empties buffer which is a dict indexed by the agent_name.
Parameters: agent_name (str, optional) – Key of the memory dict, which can be the name of input agent, or self.name. If not supplied (default), we assume to clear the entire memory.
-
buffer_filled
(agent_name=None)[source]¶ Checks whether the internal buffer has been filled to the maximum allowed specified by self.buffer_size
Parameters: agent_name (str) – Index of the buffer which is the name of input agent. Returns: status of buffer filled Return type: boolean
-
buffer_store
(agent_from: str, data=None, concat_axis=0)[source]¶ Updates data stored in self.buffer with the received message
Checks if sender agent has sent any message before If it did,then append, otherwise create new entry for it
Parameters: - agent_from (str) – Name of agent sender
- data – Any supported data which can be stored in dict as buffer. See AgentBuffer for more information.
-
get_attr
(attr)[source]¶ Return the specified attribute of the agent.
Parameters: name – Name of the attribute to be retrieved.
-
handle_process_data
(message)[source]¶ Internal method to handle incoming message before calling on_received_message
If current_state is either Stop or Reset, it will terminate early before entering on_received_message.
-
init_agent
(buffer_size=1000, log_mode=True)[source]¶ Internal initialization to setup the agent: mainly on setting the dictionary of Inputs, Outputs, PubAddr. Calls user-defined init_parameters() upon finishing.
-
Inputs
¶ Dictionary of Agents connected to its input channels. Messages will arrive from agents in this dictionary. Automatically updated when bind_output() function is called
Type: dict
-
Outputs
¶ Dictionary of Agents connected to its output channels. Messages will be sent to agents in this dictionary. Automatically updated when bind_output() function is called
Type: dict
-
current_state
¶ Current state of agent. Can be used to define different states of operation such as “Running”, “Idle, “Stop”, etc.. Users will need to define their own flow of handling each type of self.current_state in the agent_loop
Type: str
-
-
init_agent_loop
(loop_wait: Optional[int] = None)[source]¶ Initiates the agent loop, which iterates every loop_wait seconds
Stops every timers and initiate a new loop.
Parameters: loop_wait (int, optional) – The wait between each iteration of the loop
-
init_buffer
(buffer_size)[source]¶ A method to initialise the buffer. By overriding this method, user can provide a custom buffer, instead of the regular AgentBuffer. This can be used, for example, to provide a MetrologicalAgentBuffer in the metrological agents.
-
log_info
(message)[source]¶ Prints logs to be saved into logfile with Logger Agent
Parameters: message (str) – Message to be logged to the internal Logger Agent
-
on_connect_output
(output_agent)[source]¶ This method is called whenever an agent is connected to its output
This can be for example, to send metadata or ping to the output agent.
-
on_received_message
(message)[source]¶ User-defined method and is triggered to handle the message passed by Input.
Parameters: message (Dictionary) – The message received is in form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}. agent_name is the name of the Input agent which sent the message data is the actual content of the message.
-
pack_data
(data, channel='default')[source]¶ Internal method to pack the data content into a dictionary before sending out.
Special case : if the data is already a message, then the from and senderType will be altered to this agent, without altering the data and channel within the message this is used for more succinct data processing and passing.
Parameters: - data (argument) – Data content to be packed before sending out to agents.
- channel (str) – Key of dictionary which stores data
Returns: - Packed message data (dict of the form {‘from’:agent_name, ‘data’: data,)
- ’senderType’ (agent_class, ‘channel’:channel_name}.)
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
respond_request_attr_
(attribute: str)[source]¶ Response to a request of attribute from input agents.
This agent reply with the requested attribute if it has it.
-
respond_request_method_
(message_data: dict)[source]¶ Response to a request of executing method from input agents.
This agent will execute the method with the provided parameters of the method.
-
send_output
(data, channel='default')[source]¶ Sends message data to all connected agents in self.Outputs.
Output connection can first be formed by calling bind_output. By default calls pack_data(data) before sending out. Can specify specific channel as opposed to ‘default’ channel.
Parameters: - data (argument) – Data content to be sent out
- channel (str) – Key of message dictionary which stores data
Returns: message – {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}.
Return type:
-
send_plot
(fig: Union[matplotlib.figure.Figure, Dict[str, matplotlib.figure.Figure]], mode: str = 'image')[source]¶ Sends plot to agents connected to this agent’s Output channel.
This method is different from send_output which will be sent to through the ‘plot’ channel to be handled.
Tradeoffs between “image” and “plotly” modes are that “image” are more stable and “plotly” are interactive. Note not all (complicated) matplotlib figures can be converted into a plotly figure.
Parameters: - fig (matplotlib.figure.Figure or dict of matplotlib.figure.Figure) – Alternatively, multiple figures can be nested in a dict (with any preferred keys) e.g {“Temperature”:matplotlib.Figure, “Acceleration”:matplotlib.Figure}
- mode (str) – “image” - converts into image via encoding at base64 string. “plotly” - converts into plotly figure using mpl_to_plotly Default: “image”
Returns: graph
Return type: str or plotly figure or dict of one of those converted figure(s)
-
send_request_attribute
(attribute: str)[source]¶ Send a request of attribute to output agents.
Output agents will reply with the requested attribute if they have.
-
send_request_method
(method: str, **method_params)[source]¶ Send a request of executing methods to output agents.
Output agents will respond by calling the method.
-
send_set_attr
(attr: str, value)[source]¶ Sends a message to set the attr of another agent to that of value.
Parameters: - attr (str) – The variable name of the output agent to be set.
- value – The value of the variable to be set
-
set_attr
(**kwargs)[source]¶ Set object attributes.
Parameters: kwargs ([name, value]) – Keyword arguments will be used to set the object attributes.
-
shutdown
()[source]¶ Cleanly stop and shut down the agent assuming the agent is running.
Will let the main thread do the tear down.
-
stop_agent_loop
()[source]¶ Stops agent_loop from running
Note that the agent will still be responding to messages.
-
unbind_output
(output_agent)[source]¶ Remove existing output connection with another agent
This reverses the bind_output method.
Parameters: output_agent (AgentMET4FOF) – Agent binded to this agent’s output channel
-
-
class
agentMET4FOF.agents.base_agents.
DataStreamAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ Able to simulate generation of datastream by loading a given DataStreamMET4FOF
Can be used in incremental training or batch training mode. To simulate batch training mode, set pretrain_size=-1 , otherwise, set pretrain_size and batch_size for the respective. See DataStreamMET4FOF on loading your own data set as a data stream.
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via init_agent_loop( loop_wait)
To start a new loop, call init_agent_loop(loop_wait) on the agent. Example of usage is to check the current_state of the agent and send data periodically.
-
init_parameters
(stream=<agentMET4FOF.streams.base_streams.DataStreamMET4FOF object>, pretrain_size=None, batch_size=1, loop_wait=1, randomize=False)[source]¶ Parameters: - stream (DataStreamMET4FOF) – A DataStreamMET4FOF object which provides the sample data
- pretrain_size (int) – The number of sample data to send through in the first loop cycle, and subsequently, the batch_size will be used
- batch_size (int) – The number of sample data to send in every loop cycle
- loop_wait (int) – The duration to wait (seconds) at the end of each loop cycle before going into the next cycle
- randomize (bool) – Determines if the dataset should be shuffled before streaming
-
-
class
agentMET4FOF.agents.base_agents.
MonitorAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ Unique Agent for storing plots and data from messages received from input agents.
The dashboard searches for Monitor Agents’ buffer and plots to draw the graphs “plot” channel is used to receive base64 images from agents to plot on dashboard
-
plot_filter
¶ List of keys to filter the ‘data’ upon receiving message to be saved into memory Used to specifically select only a few keys to be plotted
Type: list of str
-
custom_plot_function
¶ a custom plot function that can be provided to handle the data in the monitor agents buffer (see
AgentMET4FOF
for details). The function gets provided with the content (value) of the buffer and with the string of the sender agent’s name as stored in the buffer’s keys. Additionally any other parameters can be provided as a dict in custom_plot_parameters.Type: callable
-
custom_plot_parameters
¶ a custom dictionary of parameters that shall be provided to each call of the custom_plot_function
Type: dict
-
init_parameters
(plot_filter: Optional[List[str]] = None, custom_plot_function: Optional[Callable[[...], plotly.graph_objs._scatter.Scatter]] = None, **kwargs)[source]¶ Initialize the monitor agent’s parameters
Parameters: - plot_filter (list of str, optional) – List of keys to filter the ‘data’ upon receiving message to be saved into memory. Used to specifically select only a few keys to be plotted
- custom_plot_function (callable, optional) – a custom plot function that can be provided to handle the data in the
monitor agents buffer (see
AgentMET4FOF
for details). The function gets provided with the content (value) of the buffer and with the string of the sender agent’s name as stored in the buffer’s keys. Additionally any other parameters can be provided as a dict in custom_plot_parameters. By default the data gets plotted as shown in the various tutorials. - kwargs (Any) – custom key word parameters that shall be provided to each call of
the
custom_plot_function
-
on_received_message
(message)[source]¶ Handles incoming data from ‘default’ and ‘plot’ channels.
Stores ‘default’ data into
buffer
and ‘plot’ data intoplots
Parameters: message (dict) – Acceptable channel values are ‘default’ or ‘plot’
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
update_plot_memory
(message: Dict[str, Any])[source]¶ Updates plot figures stored in self.plots with the received message
Parameters: message (dict) – Standard message format specified by AgentMET4FOF class Message[‘data’] needs to be base64 image string and can be nested in dictionary for multiple plots. Only the latest plot will be shown kept and does not keep a history of the plots.
-
Signal agents¶
-
class
agentMET4FOF.agents.signal_agents.
SineGeneratorAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ An agent streaming a sine signal
Takes samples from the
SineGenerator
and pushes them sample by sample to connected agents via its output channel.-
agent_loop
()[source]¶ 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
AgentMET4FOF.send_output()
.
-
Metrologically enabled base agents¶
-
class
agentMET4FOF.agents.metrological_base_agents.
MetrologicalAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ -
_input_data
= None¶ Input dictionary of all incoming data including metadata:
dict like { <from>: { "buffer": TimeSeriesBuffer(maxlen=buffer_size), "metadata": MetaData(**kwargs).metadata, }
-
_output_data
= None¶ Output dictionary of all outgoing data including metadata:
dict like { <from>: { "buffer": TimeSeriesBuffer(maxlen=buffer_size), "metadata": MetaData(**kwargs).metadata, }
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via init_agent_loop( loop_wait)
To start a new loop, call init_agent_loop(loop_wait) on the agent. Example of usage is to check the current_state of the agent and send data periodically.
-
init_parameters
(input_data_maxlen=25, output_data_maxlen=25)[source]¶ User provided function to initialize parameters of choice.
-
on_received_message
(message)[source]¶ User-defined method and is triggered to handle the message passed by Input.
Parameters: message (Dictionary) – The message received is in form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}. agent_name is the name of the Input agent which sent the message data is the actual content of the message.
-
pack_data
(data, channel='default')[source]¶ Internal method to pack the data content into a dictionary before sending out.
Special case : if the data is already a message, then the from and senderType will be altered to this agent, without altering the data and channel within the message this is used for more succinct data processing and passing.
Parameters: - data (argument) – Data content to be packed before sending out to agents.
- channel (str) – Key of dictionary which stores data
Returns: - Packed message data (dict of the form {‘from’:agent_name, ‘data’: data,)
- ’senderType’ (agent_class, ‘channel’:channel_name}.)
-
-
class
agentMET4FOF.agents.metrological_base_agents.
MetrologicalMonitorAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ -
init_parameters
(*args, **kwargs)[source]¶ User provided function to initialize parameters of choice.
-
on_received_message
(message)[source]¶ Handles incoming data from ‘default’ and ‘plot’ channels.
Stores ‘default’ data into self.memory and ‘plot’ data into self.plots
Parameters: message (dict) – Acceptable channel values are ‘default’ or ‘plot’
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
update_plot_memory
(message)[source]¶ Updates plot figures stored in self.plots with the received message
Parameters: message (dict) – Standard message format specified by AgentMET4FOF class Message[‘data’] needs to be base64 image string and can be nested in dictionary for multiple plots Only the latest plot will be shown kept and does not keep a history of the plots.
-
Metrologically enabled signal agents¶
-
class
agentMET4FOF.agents.metrological_signal_agents.
MetrologicalGeneratorAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ An agent streaming a specified signal
Takes samples from an instance of
MetrologicalDataStreamMET4FOF
with sampling frequencysfreq
and signal frequencysine_freq
and pushes them sample by sample to connected agents via its output channel.-
agent_loop
()[source]¶ Model the agent’s behaviour
On state Running the agent will extract sample by sample the input datastream’s content and push it into its output buffer.
-
init_parameters
(signal: agentMET4FOF.streams.metrological_base_streams.MetrologicalDataStreamMET4FOF = <agentMET4FOF.streams.metrological_signal_streams.MetrologicalSineGenerator object>, **kwargs)[source]¶ Initialize the input data stream
Parameters: signal (MetrologicalDataStreamMET4FOF) – the underlying signal for the generator (defaults to MetrologicalSineGenerator
)
-