Skip to content

Commit c162ed2

Browse files
committed
add docstrings
1 parent ba777e9 commit c162ed2

6 files changed

Lines changed: 224 additions & 45 deletions

File tree

src/mesarcade/artist.py

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,26 @@ def update(self):
218218

219219

220220
class CellAgentArtists(Artist):
221-
"""
222-
A visual representation of CellAgents.
221+
"""Renders agents on a cell-based grid space.
222+
223+
Visualizes agents that live on cells.
224+
Each agent is drawn at its cell's coordinate position.
225+
226+
Args:
227+
color: Default color for all entities. Used when color_attribute is None.
228+
color_attribute: Agent attribute name or callable to determine color.
229+
color_map: Matplotlib colormap name or dict mapping values to colors.
230+
color_vmin: Minimum value for colormap normalization.
231+
color_vmax: Maximum value for colormap normalization.
232+
shape: Entity shape, either "rect" or "circle".
233+
size: Size multiplier relative to cell size. Defaults to 1.
234+
filter_entities: Callable to filter which agents are displayed.
235+
jitter: If True, adds random offset to prevent overlap.
236+
dynamic_color: If True, updates colors each frame.
237+
dynamic_position: If True, updates positions each frame.
238+
dynamic_population: If True, handles agents being added/removed.
239+
get_population: Callable returning the agent collection from model.
240+
get_xy_position: Callable returning (x, y) position for an agent.
223241
"""
224242

225243
def __init__(
@@ -266,8 +284,26 @@ def scale_y(self, y):
266284

267285

268286
class CellArtists(Artist):
269-
"""
270-
A visual representation of Cells.
287+
"""Renders cells of a grid space.
288+
289+
Visualizes the cells themselves (not agents on them). Useful for displaying
290+
cell properties like terrain type, resource levels, or other cell attributes.
291+
292+
Args:
293+
color: Default color for all cells. Used when color_attribute is None.
294+
color_attribute: Cell attribute name or callable to determine color.
295+
color_map: Matplotlib colormap name or dict mapping values to colors.
296+
color_vmin: Minimum value for colormap normalization.
297+
color_vmax: Maximum value for colormap normalization.
298+
shape: Cell shape, either "rect" or "circle".
299+
size: Size multiplier relative to cell size. Defaults to 1.
300+
filter_entities: Callable to filter which cells are displayed.
301+
jitter: If True, adds random offset to cell positions.
302+
dynamic_color: If True, updates colors each frame.
303+
dynamic_position: If True, updates positions each frame.
304+
dynamic_population: If True, handles cells being added/removed.
305+
get_population: Callable returning the cell collection from model.
306+
get_xy_position: Callable returning (x, y) position for a cell.
271307
"""
272308

273309
def __init__(
@@ -312,8 +348,26 @@ def scale_y(self, y):
312348

313349

314350
class ContinuousSpaceAgentArtists(Artist):
315-
"""
316-
A visual representation of ContinuousSpaceAgents.
351+
"""Renders agents in a continuous space.
352+
353+
Visualizes agents that move freely in continuous 2D space rather than
354+
on a discrete grid. Positions are scaled to fit the plot area.
355+
356+
Args:
357+
color: Default color for all agents. Used when color_attribute is None.
358+
color_attribute: Agent attribute name or callable to determine color.
359+
color_map: Matplotlib colormap name or dict mapping values to colors.
360+
color_vmin: Minimum value for colormap normalization.
361+
color_vmax: Maximum value for colormap normalization.
362+
shape: Agent shape, either "rect" or "circle".
363+
size: Size multiplier for agent sprites. Defaults to 2.
364+
filter_entities: Callable to filter which agents are displayed.
365+
jitter: If True, adds random offset to prevent overlap.
366+
dynamic_color: If True, updates colors each frame.
367+
dynamic_position: If True, updates positions each frame.
368+
dynamic_population: If True, handles agents being added/removed.
369+
get_population: Callable returning the agent collection from model.
370+
get_xy_position: Callable returning (x, y) position for an agent.
317371
"""
318372

319373
def __init__(
@@ -358,6 +412,28 @@ def scale_y(self, y):
358412

359413

360414
class NetworkCellArtists(Artist):
415+
"""Renders nodes and edges of a network graph.
416+
417+
Visualizes cells as nodes in a network layout.
418+
419+
Args:
420+
color: Default color for all nodes. Used when color_attribute is None.
421+
color_attribute: Cell attribute name or callable to determine color.
422+
color_map: Matplotlib colormap name or dict mapping values to colors.
423+
color_vmin: Minimum value for colormap normalization.
424+
color_vmax: Maximum value for colormap normalization.
425+
shape: Node shape, either "rect" or "circle".
426+
size: Size multiplier for node sprites. Defaults to 2.
427+
filter_entities: Callable to filter which nodes are displayed.
428+
jitter: If True, adds random offset to node positions.
429+
dynamic_color: If True, updates colors each frame.
430+
dynamic_position: If True, updates positions each frame.
431+
dynamic_population: If True, handles nodes being added/removed.
432+
networkx_layout: Layout function from networkx (e.g., spring_layout).
433+
get_population: Callable returning the cell collection from model.
434+
get_xy_position: Callable returning (x, y) position for a cell.
435+
"""
436+
361437
def __init__(
362438
self,
363439
color: Color = "black",
@@ -447,6 +523,28 @@ def draw(self):
447523

448524

449525
class NetworkAgentArtists(NetworkCellArtists):
526+
"""Renders agents on a network graph.
527+
528+
Visualizes agents that live on network nodes.
529+
530+
Args:
531+
color: Default color for all agents. Used when color_attribute is None.
532+
color_attribute: Agent attribute name or callable to determine color.
533+
color_map: Matplotlib colormap name or dict mapping values to colors.
534+
color_vmin: Minimum value for colormap normalization.
535+
color_vmax: Maximum value for colormap normalization.
536+
shape: Agent shape, either "rect" or "circle".
537+
size: Size multiplier for agent sprites. Defaults to 2.
538+
filter_entities: Callable to filter which agents are displayed.
539+
jitter: If True, adds random offset to prevent overlap.
540+
dynamic_color: If True, updates colors each frame.
541+
dynamic_position: If True, updates positions each frame.
542+
dynamic_population: If True, handles agents being added/removed.
543+
networkx_layout: Layout function from networkx (e.g., spring_layout).
544+
get_population: Callable returning the agent collection from model.
545+
get_xy_position: Callable returning (x, y) position for an agent.
546+
"""
547+
450548
def __init__(
451549
self,
452550
color: Color = "black",

src/mesarcade/canvas.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,25 @@
1414

1515

1616
class Canvas:
17-
"""The main GUI window."""
17+
"""The main GUI window for visualizing mesa agent-based models.
18+
19+
Creates an interactive window that displays plots, controls, and value
20+
displays for a mesa simulation. Call show() to start the visualization.
21+
22+
Args:
23+
model_class: The mesa model class to instantiate and visualize.
24+
plots: List of plots (e.g., GridSpacePlot, ModelHistoryPlot) to display.
25+
Maximum 4 plots supported.
26+
controllers: List of controllers (NumController, CatController) for
27+
adjusting model parameters interactively.
28+
value_displays: List of ValueDisplay instances showing model metrics.
29+
window_width: Window width in pixels. Height is calculated as 60% of
30+
width. Defaults to 1200.
31+
window_title: Title shown in the window title bar. Defaults to "mesarcade".
32+
target_fps: Target frames per second for animation. Defaults to 40.
33+
rendering_step: Number of simulation steps between visual updates.
34+
Defaults to 1.
35+
"""
1836

1937
def __init__(
2038
self,
@@ -28,33 +46,6 @@ def __init__(
2846
rendering_step: int = 1,
2947
_visible: bool = True,
3048
) -> None:
31-
""" "
32-
Creates a new canvas instance.
33-
34-
Args:
35-
model_class (type[mesa.Model]):
36-
The mesa model class.
37-
plots (list, optional):
38-
The list of controllers that should be placed in the canvas.
39-
Currently, the maximum number of plots is 4.
40-
Defaults to [].
41-
controllers (list, optional):
42-
The list of controllers that should be placed in the canvas.
43-
Defaults to [].
44-
window_width (int, optional):
45-
The width of the canvas window in pixels.
46-
Determines the height of the canvas.
47-
Defaults to 1200.
48-
window_title (str, optional):
49-
The title of the canvas.
50-
Defaults to "mesarcade".
51-
target_fps (int, optional):
52-
The number of frames per second (FPS) that the animations should show.
53-
Defaults to 40.
54-
rendering_step (int, optional):
55-
The number of simulation steps until the visualizations are rerendered.
56-
Defaults to 1.
57-
"""
5849
window_height = int(window_width * 0.6)
5950

6051
if not arcade.timings_enabled():

src/mesarcade/controller.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ def calculate_align_y(self, i):
5454

5555

5656
class CatController(_Controller):
57+
"""A dropdown controller for categorical model parameters.
58+
59+
Displays a dropdown menu allowing users to select from predefined options
60+
during the simulation.
61+
62+
Args:
63+
parameter_name: Name of the model parameter to control.
64+
parameter_value: Initial value of the parameter.
65+
parameter_options: List of allowed values for the dropdown.
66+
label: Optional display label. If None, parameter_name is used.
67+
"""
68+
5769
def __init__(
5870
self,
5971
parameter_name: str,
@@ -86,6 +98,20 @@ def setup(self, i):
8698

8799

88100
class NumController(_Controller):
101+
"""A slider controller for numeric model parameters.
102+
103+
Displays a slider with +/- buttons allowing users to adjust numeric
104+
parameters within a defined range during the simulation.
105+
106+
Args:
107+
parameter_name: Name of the model parameter to control.
108+
parameter_value: Initial value of the parameter.
109+
min_value: Minimum allowed value.
110+
max_value: Maximum allowed value.
111+
step: Increment/decrement step size.
112+
label: Optional display label. If None, parameter_name is used.
113+
"""
114+
89115
def __init__(
90116
self,
91117
parameter_name: str,

src/mesarcade/history_plot.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,37 @@ def draw(self):
282282

283283

284284
class ModelHistoryPlot(Figure):
285+
"""A line plot that displays the history of model attributes over time.
286+
287+
Tracks one or more model attributes and visualizes their values as lines
288+
over the course of the simulation. Supports up to 6 lines.
289+
290+
Args:
291+
model_attributes: List of model attributes to track. Each element can be
292+
a string (attribute name) or a callable that takes a mesa.Model and
293+
returns a numeric value.
294+
labels: Optional labels for the legend. If None, attribute names are
295+
used for string attributes, or "no label" for callables.
296+
colors: Optional colors for the lines. Accepts color names, RGB tuples,
297+
or RGBA tuples. Defaults to a predefined color palette.
298+
legend: Whether to display a legend. Defaults to True.
299+
title: Optional title for the plot.
300+
from_datacollector: If True, reads values from the model's datacollector
301+
instead of directly from model attributes. Only works with string
302+
attributes. Defaults to False.
303+
rendering_step: Simulation steps between plot updates. Defaults to 3.
304+
"""
305+
285306
def __init__(
286307
self,
287-
model_attributes: str | list[str],
308+
model_attributes: list[str],
288309
labels: list[str] | None = None,
289310
colors: list[Color] | None = None,
290311
legend: bool = True,
291312
title: str | None = None,
292313
from_datacollector: bool = False,
293314
rendering_step: int = 3,
294315
) -> None:
295-
if not isinstance(model_attributes, (list, tuple)):
296-
model_attributes = [model_attributes]
297-
298316
plot = _ModelHistoryPlot(
299317
model_attributes=model_attributes,
300318
labels=labels,

src/mesarcade/space_plot.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111

1212
class GridSpacePlot(Figure):
13+
"""A plot for visualizing cell-based grid spaces.
14+
15+
Displays agents and/or cells on a discrete grid. The grid dimensions
16+
are automatically determined from the space.
17+
18+
Args:
19+
artists: Artist(s) defining how to render entities (e.g., CellAgentArtists).
20+
background_color: Background color of the plot area.
21+
title: Optional title displayed above the plot.
22+
get_space: Callable returning the grid space from the model.
23+
"""
24+
1325
def __init__(
1426
self,
1527
artists: Artist | list[Artist] = [],
@@ -30,6 +42,18 @@ def __init__(
3042

3143

3244
class ContinuousSpacePlot(Figure):
45+
"""A plot for visualizing continuous 2D spaces.
46+
47+
Displays agents that move freely in continuous space. Agent positions
48+
are scaled to fit the plot area based on the space dimensions.
49+
50+
Args:
51+
artists: Artist(s) defining how to render entities (e.g., ContinuousSpaceAgentArtists).
52+
background_color: Background color of the plot area.
53+
title: Optional title displayed above the plot.
54+
get_space: Callable returning the continuous space from the model.
55+
"""
56+
3357
def __init__(
3458
self,
3559
artists: Artist | list[Artist] = [],
@@ -50,6 +74,18 @@ def __init__(
5074

5175

5276
class NetworkPlot(Figure):
77+
"""A plot for visualizing network/graph-based spaces.
78+
79+
Displays agents and/or nodes on a network graph. Node positions are
80+
computed using a networkx layout algorithm specified in the artist.
81+
82+
Args:
83+
artists: Artist(s) defining how to render entities (e.g., NetworkCellArtists).
84+
background_color: Background color of the plot area.
85+
title: Optional title displayed above the plot.
86+
get_space: Callable returning the network grid from the model.
87+
"""
88+
5389
def __init__(
5490
self,
5591
artists: Artist | list[Artist] = [],

src/mesarcade/value_display.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,30 @@
1010

1111

1212
class ValueDisplay:
13+
"""Displays the current value of a model attribute in the GUI.
14+
15+
Shows a labeled value that updates during the simulation. Useful for
16+
displaying key metrics like population counts, resource levels, or
17+
any other scalar value derived from the model.
18+
19+
Args:
20+
model_attribute: The attribute to display. Can be a string (attribute
21+
name) or a callable that takes a mesa.Model and returns a value.
22+
label: Optional label text. If None, the attribute name is used for
23+
string attributes, or "no label" for callables.
24+
update_step: Simulation steps between value updates. Defaults to 10.
25+
from_datacollector: If True, reads values from the model's datacollector
26+
instead of directly from the model. Only works with string
27+
attributes. Defaults to False.
28+
"""
29+
1330
def __init__(
1431
self,
1532
model_attribute: str | Callable[[mesa.Model], Any] | None,
1633
label: str | None = None,
1734
update_step: int = 10,
1835
from_datacollector: bool = False,
1936
) -> None:
20-
"""Displays the value of a given model attribute.
21-
22-
Args:
23-
model_attribute (str | None, optional): The model attribute. Defaults to None.
24-
label (str | None, optional): An optional label for the model attribute. Defaults to None.
25-
update_step (int, optional): The number of steps after which the displayed value gets updated. Defaults to 10.
26-
"""
2737
self.model_attribute = model_attribute
2838
self.label = label
2939
self.update_step = update_step

0 commit comments

Comments
 (0)