Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
"""
Text Module
============
Module for drawing live-updating text.
"""
from mesa.visualization.ModularVisualization import VisualizationElement
class TextElement(VisualizationElement):
package_includes = ["TextModule.js"]
js_code = "elements.push(new TextModule());"
from mesa.visualization.ModularVisualization import VisualizationElement
class SimpleCanvas(VisualizationElement):
package_includes = ["simple_continuous_canvas.js"]
portrayal_method = None
canvas_height = 500
canvas_width = 500
def __init__(self, portrayal_method, canvas_height=500, canvas_width=500):
'''
Instantiate a new SimpleCanvas
'''
self.portrayal_method = portrayal_method
self.canvas_height = canvas_height
self.canvas_width = canvas_width
new_element = ("new Simple_Continuous_Module({}, {})".
format(self.canvas_width, self.canvas_height))
self.js_code = "elements.push(" + new_element + ");"
# -*- coding: utf-8 -*-
"""
Modular Canvas Rendering
========================
Module for visualizing model objects in grid cells.
"""
from collections import defaultdict
from mesa.visualization.ModularVisualization import VisualizationElement
class CanvasGrid(VisualizationElement):
""" A CanvasGrid object uses a user-provided portrayal method to generate a
portrayal for each object. A portrayal is a JSON-ready dictionary which
tells the relevant JavaScript code (GridDraw.js) where to draw what shape.
The render method returns a dictionary, keyed on layers, with values as
lists of portrayals to draw. Portrayals themselves are generated by the
user-provided portrayal_method, which accepts an object as an input and
produces a portrayal of it.
A portrayal as a dictionary with the following structure:
"x", "y": Coordinates for the cell in which the object is placed.
"Shape": Can be either "circle", "rect" or "arrowHead"
For Circles:
"r": The radius, defined as a fraction of cell size. r=1 will
fill the entire cell.
For Rectangles:
# -*- coding: utf-8 -*-
"""
Network Visualization Module
============
Module for rendering the network, using [sigma.js](http://sigmajs.org/) or [d3.js](https://d3js.org/) frameworks.
"""
from mesa.visualization.ModularVisualization import VisualizationElement
class NetworkModule(VisualizationElement):
package_includes = []
def __init__(self, portrayal_method, canvas_height=500, canvas_width=500, library='sigma'):
library_types = ['sigma', 'd3']
if library not in library_types:
raise ValueError("Invalid javascript library type. Expected one of: %s" % library_types)
NetworkModule.package_includes = ["NetworkModule_sigma.js", "sigma.min.js"] if library == 'sigma' else [
"NetworkModule_d3.js", "d3.min.js"]
self.portrayal_method = portrayal_method
self.canvas_height = canvas_height
self.canvas_width = canvas_width
new_element = ("new NetworkModule({}, {})".
format(self.canvas_width, self.canvas_height))
self.js_code = "elements.push(" + new_element + ");"
from mesa.visualization.ModularVisualization import VisualizationElement
class SimpleCanvas(VisualizationElement):
local_includes = ["boid_flockers/simple_continuous_canvas.js"]
portrayal_method = None
canvas_height = 500
canvas_width = 500
def __init__(self, portrayal_method, canvas_height=500, canvas_width=500):
'''
Instantiate a new SimpleCanvas
'''
self.portrayal_method = portrayal_method
self.canvas_height = canvas_height
self.canvas_width = canvas_width
new_element = ("new Simple_Continuous_Module({}, {})".
format(self.canvas_width, self.canvas_height))
self.js_code = "elements.push(" + new_element + ");"
# -*- coding: utf-8 -*-
"""
Modular Canvas Rendering
========================
Module for visualizing model objects in hexagonal grid cells.
"""
from collections import defaultdict
from mesa.visualization.ModularVisualization import VisualizationElement
class CanvasHexGrid(VisualizationElement):
""" A CanvasHexGrid object functions similarly to a CanvasGrid object. It takes a portrayal dictionary and talks to HexDraw.js to draw that shape.
A portrayal as a dictionary with the following structure:
"x", "y": Coordinates for the cell in which the object is placed.
"Shape": Can be either "hex" or "circle"
"r": The radius, defined as a fraction of cell size. r=1 will
fill the entire cell.
"Color": The color to draw the shape in; needs to be a valid HTML
color, e.g."Red" or "#AA08F8"
"Filled": either "true" or "false", and determines whether the shape is
filled or not.
"Layer": Layer number of 0 or above; higher-numbered layers are drawn
above lower-numbered layers.
"text": The text to be inscribed inside the Shape. Normally useful for
showing the unique_id of the agent.
"text_color": The color to draw the inscribed text. Should be given in
# -*- coding: utf-8 -*-
"""
Chart Module
============
Module for drawing live-updating line charts using Charts.js
"""
import json
from mesa.visualization.ModularVisualization import VisualizationElement
class ChartModule(VisualizationElement):
""" Each chart can visualize one or more model-level series as lines
with the data value on the Y axis and the step number as the X axis.
At the moment, each call to the render method returns a list of the most
recent values of each series.
Attributes:
series: A list of dictionaries containing information on series to
plot. Each dictionary must contain (at least) the "Label" and
"Color" keys. The "Label" value must correspond to a
model-level series collected by the model's DataCollector, and
"Color" must have a valid HTML color.
canvas_height, canvas_width: The width and height to draw the chart on
the page, in pixels. Default to 200 x 500
data_collector_name: Name of the DataCollector object in the model to
retrieve data from.