Skip to content

Commit f4f44ad

Browse files
rhttpike3
authored andcommittedMay 15, 2022
Implement simpler Mesa namespace
1 parent 041c593 commit f4f44ad

File tree

9 files changed

+65
-26
lines changed

9 files changed

+65
-26
lines changed
 

‎.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ branch = True
55
[report]
66
omit =
77
tests/*
8+
mesa/flat/*

‎examples/wolf_sheep/wolf_sheep/agents.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mesa import Agent
1+
import mesa
22
from wolf_sheep.random_walk import RandomWalker
33

44

@@ -92,7 +92,7 @@ def step(self):
9292
self.model.schedule.add(cub)
9393

9494

95-
class GrassPatch(Agent):
95+
class GrassPatch(mesa.Agent):
9696
"""
9797
A patch of grass that grows at a fixed rate and it is eaten by sheep
9898
"""

‎examples/wolf_sheep/wolf_sheep/model.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
Northwestern University, Evanston, IL.
1010
"""
1111

12-
from mesa import Model
13-
from mesa.space import MultiGrid
14-
from mesa.datacollection import DataCollector
12+
import mesa
1513

1614
from wolf_sheep.scheduler import RandomActivationByTypeFiltered
1715
from wolf_sheep.agents import Sheep, Wolf, GrassPatch
1816

1917

20-
class WolfSheep(Model):
18+
class WolfSheep(mesa.Model):
2119
"""
2220
Wolf-Sheep Predation Model
2321
"""
@@ -84,8 +82,8 @@ def __init__(
8482
self.sheep_gain_from_food = sheep_gain_from_food
8583

8684
self.schedule = RandomActivationByTypeFiltered(self)
87-
self.grid = MultiGrid(self.width, self.height, torus=True)
88-
self.datacollector = DataCollector(
85+
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
86+
self.datacollector = mesa.DataCollector(
8987
{
9088
"Wolves": lambda m: m.schedule.get_type_count(Wolf),
9189
"Sheep": lambda m: m.schedule.get_type_count(Sheep),

‎examples/wolf_sheep/wolf_sheep/server.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from mesa.visualization.ModularVisualization import ModularServer
2-
from mesa.visualization.modules import CanvasGrid, ChartModule
3-
from mesa.visualization.UserParam import UserSettableParameter
1+
import mesa
42

53
from wolf_sheep.agents import Wolf, Sheep, GrassPatch
64
from wolf_sheep.model import WolfSheep
@@ -40,8 +38,8 @@ def wolf_sheep_portrayal(agent):
4038
return portrayal
4139

4240

43-
canvas_element = CanvasGrid(wolf_sheep_portrayal, 20, 20, 500, 500)
44-
chart_element = ChartModule(
41+
canvas_element = mesa.visualization.CanvasGrid(wolf_sheep_portrayal, 20, 20, 500, 500)
42+
chart_element = mesa.visualization.ChartModule(
4543
[
4644
{"Label": "Wolves", "Color": "#AA0000"},
4745
{"Label": "Sheep", "Color": "#666666"},
@@ -50,20 +48,22 @@ def wolf_sheep_portrayal(agent):
5048
)
5149

5250
model_params = {
53-
"grass": UserSettableParameter("checkbox", "Grass Enabled", True),
54-
"grass_regrowth_time": UserSettableParameter(
51+
"grass": mesa.visualization.UserSettableParameter(
52+
"checkbox", "Grass Enabled", True
53+
),
54+
"grass_regrowth_time": mesa.visualization.UserSettableParameter(
5555
"slider", "Grass Regrowth Time", 20, 1, 50
5656
),
57-
"initial_sheep": UserSettableParameter(
57+
"initial_sheep": mesa.visualization.UserSettableParameter(
5858
"slider", "Initial Sheep Population", 100, 10, 300
5959
),
60-
"sheep_reproduce": UserSettableParameter(
60+
"sheep_reproduce": mesa.visualization.UserSettableParameter(
6161
"slider", "Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01
6262
),
63-
"initial_wolves": UserSettableParameter(
63+
"initial_wolves": mesa.visualization.UserSettableParameter(
6464
"slider", "Initial Wolf Population", 50, 10, 300
6565
),
66-
"wolf_reproduce": UserSettableParameter(
66+
"wolf_reproduce": mesa.visualization.UserSettableParameter(
6767
"slider",
6868
"Wolf Reproduction Rate",
6969
0.05,
@@ -72,15 +72,15 @@ def wolf_sheep_portrayal(agent):
7272
0.01,
7373
description="The rate at which wolf agents reproduce.",
7474
),
75-
"wolf_gain_from_food": UserSettableParameter(
75+
"wolf_gain_from_food": mesa.visualization.UserSettableParameter(
7676
"slider", "Wolf Gain From Food Rate", 20, 1, 50
7777
),
78-
"sheep_gain_from_food": UserSettableParameter(
78+
"sheep_gain_from_food": mesa.visualization.UserSettableParameter(
7979
"slider", "Sheep Gain From Food", 4, 1, 10
8080
),
8181
}
8282

83-
server = ModularServer(
83+
server = mesa.visualization.ModularServer(
8484
WolfSheep, [canvas_element, chart_element], "Wolf Sheep Predation", model_params
8585
)
8686
server.port = 8521

‎mesa/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
from mesa.model import Model
1010
from mesa.agent import Agent
1111

12+
import mesa.time as time
13+
import mesa.space as space
14+
import mesa.flat.visualization as visualization
15+
from mesa.datacollection import DataCollector
1216

13-
__all__ = ["Model", "Agent"]
17+
__all__ = ["Model", "Agent", "time", "space", "visualization", "DataCollector"]
1418

1519
__title__ = "mesa"
1620
__version__ = "0.9.0"

‎mesa/flat/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from mesa import * # noqa
2+
from mesa.time import * # noqa
3+
from mesa.space import * # noqa
4+
from mesa.datacollection import * # noqa
5+
from .visualization import * # noqa

‎mesa/flat/visualization.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This collects all of Mesa visualization components under a flat namespace.
2+
from mesa.visualization.ModularVisualization import * # noqa
3+
from mesa.visualization.modules import * # noqa
4+
from mesa.visualization.UserParam import * # noqa

‎tests/test_import_namespace.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def test_import():
2+
# This tests the new, simpler Mesa namespace. See
3+
# https://github.com/projectmesa/mesa/pull/1294.
4+
import mesa
5+
import mesa.flat as mf
6+
7+
from mesa.time import RandomActivation
8+
9+
mesa.time.RandomActivation
10+
RandomActivation
11+
mf.RandomActivation
12+
13+
from mesa.space import MultiGrid
14+
15+
mesa.space.MultiGrid
16+
MultiGrid
17+
mf.MultiGrid
18+
19+
from mesa.visualization.ModularVisualization import ModularServer
20+
21+
mesa.visualization.ModularServer
22+
ModularServer
23+
mf.ModularServer
24+
25+
from mesa.datacollection import DataCollector
26+
27+
DataCollector
28+
mesa.DataCollector
29+
mf.DataCollector

‎tests/test_main.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def tearDown(self):
2020
sys.path[:] = self.old_sys_path
2121

2222
def test_run(self):
23-
with patch(
24-
"mesa.visualization.ModularVisualization.ModularServer"
25-
) as ModularServer:
23+
with patch("mesa.visualization.ModularServer") as ModularServer:
2624
example_dir = os.path.abspath(
2725
os.path.join(os.path.dirname(__file__), "../examples/wolf_sheep")
2826
)

0 commit comments

Comments
 (0)
Please sign in to comment.