Skip to content

Commit

Permalink
converted docstring to double quote
Browse files Browse the repository at this point in the history
  • Loading branch information
lowcloudnine committed Jun 4, 2016
1 parent d6d7107 commit 0eedc82
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 104 deletions.
2 changes: 1 addition & 1 deletion mesa/visualization/ModularVisualization.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Modular Visualization - An In-Depth Look
=================================================================
========================================

Modular visualization is (well, will be) one of Mesa's core features. Mesa does (will) include a (wide -- some day) range of predefined visualization modules, which can be easily subclassed for your needs, and mixed-and-matched to visualize your particular model. It should also be fairly straightforward to create new visualization modules. In this document, I will attempt to provide an overview of how to do both.

Expand Down
64 changes: 27 additions & 37 deletions mesa/visualization/ModularVisualization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''
# -*- coding: utf-8 -*-
"""
ModularServer
============================================================================
=============
A visualization server which renders a model via one or more elements.
Expand Down Expand Up @@ -75,7 +76,7 @@
"step:" index of the step to get.
}
'''
"""
import os
import datetime as dt

Expand All @@ -92,7 +93,7 @@


class VisualizationElement(object):
'''
"""
Defines an element of the visualization.
Attributes:
Expand All @@ -105,7 +106,8 @@ class VisualizationElement(object):
Methods:
render: Takes a model object, and produces JSON data which can be sent
to the client.
'''
"""

package_includes = []
local_includes = []
Expand All @@ -116,25 +118,23 @@ def __init__(self):
pass

def render(self, model):
'''
Build visualization data from a model object.
""" Build visualization data from a model object.
Args:
model: A model object
Returns:
A JSON-ready object.
'''
"""
return "<b>VisualizationElement goes here</b>."

# =============================================================================
# Actual Tornado code starts here:


class PageHandler(tornado.web.RequestHandler):
'''
Handler for the HTML template which holds the visualization.
'''
""" Handler for the HTML template which holds the visualization. """

def get(self):
elements = self.application.visualization_elements
Expand All @@ -148,10 +148,7 @@ def get(self):


class SocketHandler(tornado.websocket.WebSocketHandler):
'''
Handler for websocket.
'''

""" Handler for websocket. """
def open(self):
if self.application.verbose:
print("Socket opened!")
Expand All @@ -160,9 +157,9 @@ def check_origin(self, origin):
return True

def on_message(self, message):
'''
Receiving a message from the websocket, parse, and act accordingly.
'''
""" Receiving a message from the websocket, parse, and act accordingly.
"""
if self.application.verbose:
print(message)
msg = tornado.escape.json_decode(message)
Expand Down Expand Up @@ -190,10 +187,7 @@ def on_message(self, message):


class ModularServer(tornado.web.Application):
'''
Main visualization application.
'''

""" Main visualization application. """
verbose = True

model_name = "Mesa Model"
Expand Down Expand Up @@ -226,9 +220,7 @@ class ModularServer(tornado.web.Application):

def __init__(self, model_cls, visualization_elements, name="Mesa Model",
*args, **kwargs):
'''
Create a new visualization server with the given elements.
'''
""" Create a new visualization server with the given elements. """
# Prep visualization elements:
self.visualization_elements = visualization_elements
self.package_includes = set()
Expand All @@ -253,16 +245,15 @@ def __init__(self, model_cls, visualization_elements, name="Mesa Model",
super().__init__(self.handlers, **self.settings)

def reset_model(self):
'''
Reinstantiate the model object, using the current parameters.
'''
""" Reinstantiate the model object, using the current parameters. """
self.model = self.model_cls(*self.model_args, **self.model_kwargs)
self.viz_states = [self.render_model()]

def render_model(self):
'''
Turn the current state of the model into a dictionary of visualizations
'''
""" Turn the current state of the model into a dictionary of
visualizations
"""
visualization_state = []
for element in self.visualization_elements:
element_state = element.render(self.model)
Expand All @@ -271,10 +262,11 @@ def render_model(self):

@tornado.gen.coroutine
def run_model(self):
'''
Run the model forward and store each viz state.
""" Run the model forward and store each viz state.
#TODO: Have this run concurrently (I think) inside the event loop?
'''
"""
while self.model.schedule.steps < self.max_steps and self.model.running:
self.model.step()
self.viz_states.append(self.render_model())
Expand All @@ -283,9 +275,7 @@ def run_model(self):
dt.timedelta(milliseconds=5))

def launch(self, port=None):
'''
Run the app.
'''
""" Run the app. """
if port is not None:
self.port = port
print('Interface starting at http://127.0.0.1:{PORT}'.format(PORT=self.port))
Expand Down
61 changes: 21 additions & 40 deletions mesa/visualization/TextVisualization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''
# -*- coding: utf-8 -*-
"""
Text Visualization
=======================
==================
Base classes for ASCII-only visualizations of a model.
These are useful for quick debugging, and can readily be rendered in an IPython
Expand All @@ -25,74 +26,60 @@
potentially render a cell based on several values (e.g. an Agent grid and a
Patch value grid).
'''

"""
# Pylint instructions: allow single-character variable names.
# pylint: disable=invalid-name


class TextVisualization(object):
'''
ASCII-Only visualization of a model.
""" ASCII-Only visualization of a model.
Properties:
model: The underlying model object to be visualized.
elements: List of visualization elements, which will be rendered
in the order they are added.
'''
"""
def __init__(self, model):
'''
Create a new Text Visualization object.
'''
""" Create a new Text Visualization object. """
self.model = model
self.elements = []

def render(self):
'''
Render all the text elements, in order.
'''
""" Render all the text elements, in order. """
for element in self.elements:
print(element)

def step(self):
'''
Advance the model by a step and print the results.
'''
""" Advance the model by a step and print the results. """
self.model.step()
self.render()


class TextElement(object):
'''
Base class for all TextElements to render.
""" Base class for all TextElements to render.
Methods:
render: 'Renders' some data into ASCII and returns.
__str__: Displays render() by default.
'''
"""

def __init__(self):
pass

def render(self):
'''
Render the element as text.
'''
""" Render the element as text. """
return "Placeholder!"

def __str__(self):
return self.render()


class TextData(TextElement):
'''
Prints the value of one particular variable from the base model.
'''
""" Prints the value of one particular variable from the base model. """
def __init__(self, model, var_name):
'''
Create a new data renderer.
'''
""" Create a new data renderer. """
self.model = model
self.var_name = var_name

Expand All @@ -101,41 +88,35 @@ def render(self):


class TextGrid(TextElement):
'''
Class for creating an ASCII visualization of a basic grid object.
""" Class for creating an ASCII visualization of a basic grid object.
By default, assume that each cell is represented by one character, and
that empty cells are rendered as ' ' characters. When printed, the TextGrid
results in a width x height grid of ascii characters.
Properties:
grid: The underlying grid object.
'''
"""
grid = None

def __init__(self, grid, converter):
'''
Create a new ASCII grid visualization.
""" Create a new ASCII grid visualization.
Args:
grid: The underlying Grid object.
converter: function for converting the content of each cell
to ascii
'''
"""
self.grid = grid

@staticmethod
def converter(x):
"""
Text content of cells.
"""
""" Text content of cells. """
return 'X'

def render(self):
'''
What to show when printed.
'''
""" What to show when printed. """
viz = ""
for y in range(self.grid.height):
for x in range(self.grid.width):
Expand Down
7 changes: 5 additions & 2 deletions mesa/visualization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'''
# -*- coding: utf-8 -*-
"""
Mesa Visualization Module
-------------------------
TextVisualization: Base class for writing ASCII visualizations of model state.
TextServer: Class which takes a TextVisualization child class as an input, and
renders it in-browser, along with an interface.
'''
"""
24 changes: 13 additions & 11 deletions mesa/visualization/modules/CanvasGridVisualization.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
'''
Modular Canvas rendering
'''
# -*- 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):
'''
Module for visualizing model objects in grid cells.
A CanvasGrid object uses a user-provided portrayal method to generate a
""" 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.
Expand Down Expand Up @@ -41,26 +43,25 @@ class CanvasGrid(VisualizationElement):
canvas_height, canvas_width: Size, in pixels, of the grid visualization
to draw on the client.
template: "canvas_module.html" stores the module's HTML template.
'''
"""
package_includes = ["GridDraw.js", "CanvasModule.js"]
portrayal_method = None # Portrayal function
canvas_height = 500
canvas_width = 500

def __init__(self, portrayal_method, grid_height, grid_width,
canvas_height=500, canvas_width=500):
'''
Instantiate a new CanvasGrid.
""" Instantiate a new CanvasGrid.
Args:
portrayal_method: function to convert each object on the grid to
a portrayal, as described above.
grid_height, grid_width: Size of the grid, in cells.
canvas_height, canvas_width: Size of the canvas to draw in the
client, in pixels. (default: 500x500)
'''
"""
self.portrayal_method = portrayal_method
self.grid_height = grid_height
self.grid_width = grid_width
Expand All @@ -84,4 +85,5 @@ def render(self, model):
portrayal["x"] = x
portrayal["y"] = y
grid_state[portrayal["Layer"]].append(portrayal)

return grid_state
Loading

0 comments on commit 0eedc82

Please sign in to comment.