Source code for visuals.visual

"""
Backend for AGE-ABM Visual Interface
@Author Max Hall
@Co-Authors Meghan Ireland and Matthew Fleischman

"""

### Imports
from utils.helper_methods import adding_name_checker, getting_name_checker, valid_plot_data_type
from utils.helper_methods import valid_plot_type
import visuals.visualutils as utils
from visuals.visual_classes import VisualiseAgent

[docs] class VisualEnv: """ This is a base class which will store all the data relating to an instance of creating new visualisations using the GUI this includes storing a dictionary storing visulisation classes """ def __init__(self, model): # This creates an object self.file_store = utils.WriteVisualFiles(model, self) # This stores the model which is being visualised self.model = model self.visuals = {}
[docs] def get_list_visuals(self): """Gets a list of all visual models added to the model. Parameters ---------- Returns ------- Attribute : list[str] | Returns a list of visual model names """ if self.visuals: return list(self.visuals) return []
[docs] def add_agent_visualisation(self, vis_name : str, agent_list : list[str], plot_data_type : str , plot_type : str, comp_of_interest : str = None , attr_of_interest : str = None): """Adds agent to visualise to the visuals dictionary. Parameters ---------- vis_name : str The name of visualise agent object. agent_list : list[str] List of agent names that the user would like to compare, the agent name in position 0 is the agent of focus. comp_of_interest : str This is the component of interest for this visualisation attr_of_interest : str This is the attribute of interest for this visualisation plot_data_type : str Specifies the type of data which the visual will show. plot_type : str Specifies the type of plot which will be generated. Returns ------- error term : int | Returns 0 on success | Returns 1 if plot_id already exist in the VisualEnv | Returns 2 if invalid characters used | Returns 3 if no agent names provided | Returns 4 if the plot data type is not a valid option | Returns 5 if the plot data type is not valid for provided agents | Returns 6 is the plot type is not valid """ # Tries to format the name to correct format formatted_vis_name = adding_name_checker(vis_name, list(self.visuals)) # Checks for error messages if formatted_vis_name in [1,2]: return formatted_vis_name # Name already in use or invalid chars # Ensure not an empty list if len(agent_list) == 0: return 3 # No agent names provided # Ensures that the plot_data_type is valid formatted_plot_data_type = valid_plot_data_type(plot_data_type) # Checks for error messages if formatted_plot_data_type == 1: return 4 # Invalid plot type if formatted_plot_data_type not in self.model.get_list_valid_plot_data_types(agent_list): return 5 # Agents not agents_of_interest or plot_data_type isn't valid for these agents # Tries to format the name to correct format if comp_of_interest is not None: formatted_comp_of_interest = getting_name_checker(comp_of_interest , self.model.get_agent(agent_list[0]).get_given_components()) # Checks for error messages if formatted_comp_of_interest in [1,2]: return formatted_comp_of_interest # Name not already in use or invalid chars else: formatted_comp_of_interest = comp_of_interest if attr_of_interest is not None: # Tries to format the name to correct format formatted_attr_of_interest = getting_name_checker(attr_of_interest , self.model.get_list_attributes(formatted_comp_of_interest) , True) # Checks for error messages if formatted_attr_of_interest in [1,2]: return formatted_attr_of_interest # Name not already in use or invalid chars else: formatted_attr_of_interest = attr_of_interest # Ensures the plot_type is valid formatted_plot_type = valid_plot_type(plot_type) # Checks for error messages if formatted_plot_type == 1: return 6 # Invalid plot type # Initialising the compare_agent_list if len(agent_list) > 1: compare_agent_list = agent_list[1:] else: compare_agent_list = [] # Initialising the key agent of interest visual_agent = agent_list[0] # Adding the plot self.visuals[formatted_vis_name] = VisualiseAgent(visual_agent, compare_agent_list , formatted_plot_data_type, formatted_plot_type , formatted_comp_of_interest, formatted_attr_of_interest) return 0
[docs] def get_agent_visualisation(self, vis_name : str): """Gets the VisualiseAgent object of the specified visual agent from the visuals dictionary. Parameters ---------- vis_name : str The name of the visual agent object that must be returned. Returns ------- error term : int | Returns 1 if plot_id doesn't exist in the VisualEnv yet | Returns 2 if invalid characters used visuals[vis_name] : VisualiseAgent | Returns the VisualiseAgent object on success """ # Tries to format the name to correct format formatted_name = getting_name_checker(vis_name, list(self.visuals)) # Checks for error messages if formatted_name == 1: return None # Name already in use return self.visuals[formatted_name]
[docs] def remove_agent_visualisation(self, vis_name : str): """Removes the provided visual agent from the visuals dictionary. Parameters ----------- vis_name : str The name of the VisualiseAgent object to be removed. Returns ------- error term : int | Returns 0 on success | Returns 1 if plot_id doesn't exist in the VisualEnv yet | Returns 2 if invalid characters used """ # Tries to format the name to correct format formatted_name = getting_name_checker(vis_name, list(self.visuals)) # Checks for error messages if formatted_name == 1: return 1 # Name not in use del self.visuals[formatted_name] return 0
[docs] def visualise_model(self, visual_plot_id : str): """ Generates a python file(s) necessary for the model to be visualised. Parameters ---------- visual_plot_id : str The visual_plot_id of the plot to be written. Returns ------- error term : int | Returns 0 on success | Returns 1 if plot_id doesn't exist in the VisualEnv yet """ if ".py" in visual_plot_id: # Makes sure there is no .py at the end of the filename visual_plot_id = visual_plot_id.replace(".py", "") if getting_name_checker(visual_plot_id, list(self.get_list_visuals())) in [1, 2]: return 1 self.file_store.create_visualisation_file_section(visual_plot_id) self.file_store.create_visualisation_file(visual_plot_id) return 0