From 83fb7e515dafe6de55775289487d53dc54558ee9 Mon Sep 17 00:00:00 2001 From: corvince <13568919+Corvince@users.noreply.github.com> Date: Wed, 16 Oct 2024 23:58:47 +0200 Subject: [PATCH 1/4] make examples part of mesa wheel and importable --- examples/advanced/__init__.py | 0 examples/basic/__init__.py | 0 mesa/examples/__init__.py | 1 + pyproject.toml | 2 +- 4 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 examples/advanced/__init__.py create mode 100644 examples/basic/__init__.py create mode 100644 mesa/examples/__init__.py diff --git a/examples/advanced/__init__.py b/examples/advanced/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/examples/basic/__init__.py b/examples/basic/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/mesa/examples/__init__.py b/mesa/examples/__init__.py new file mode 100644 index 00000000000..8f1f4690933 --- /dev/null +++ b/mesa/examples/__init__.py @@ -0,0 +1 @@ +__path__ = ["examples"] # noqa: D104 diff --git a/pyproject.toml b/pyproject.toml index f7a3b464488..b046b797f66 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,7 +87,7 @@ repository = "https://github.com/projectmesa/mesa" mesa = "mesa.main:cli" [tool.hatch.build.targets.wheel] -packages = ["mesa"] +packages = ["mesa", "examples"] [tool.hatch.version] path = "mesa/__init__.py" From dfe356ca697287ba31a8521dccc899b0706a118e Mon Sep 17 00:00:00 2001 From: corvince <13568919+Corvince@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:44:26 +0200 Subject: [PATCH 2/4] make all basic examples importable directly by name --- examples/__init__.py | 0 examples/basic/__init__.py | 13 +++++++++++++ examples/basic/boid_flockers/app.py | 4 ++-- examples/basic/boid_flockers/model.py | 3 ++- examples/basic/boltzmann_wealth_model/app.py | 4 ++-- examples/basic/boltzmann_wealth_model/model.py | 4 ++-- examples/basic/conways_game_of_life/model.py | 4 ++-- examples/basic/conways_game_of_life/server.py | 6 +++--- examples/basic/schelling/app.py | 3 ++- examples/basic/schelling/model.py | 4 ++-- examples/basic/virus_on_network/app.py | 3 ++- examples/basic/virus_on_network/model.py | 3 ++- mesa/__init__.py | 2 ++ mesa/examples.py | 3 +++ mesa/examples/__init__.py | 1 - 15 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 examples/__init__.py create mode 100644 mesa/examples.py delete mode 100644 mesa/examples/__init__.py diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/examples/basic/__init__.py b/examples/basic/__init__.py index e69de29bb2d..faa81be131d 100644 --- a/examples/basic/__init__.py +++ b/examples/basic/__init__.py @@ -0,0 +1,13 @@ +from .boid_flockers.model import BoidFlockers +from .boltzmann_wealth_model.model import BoltzmannWealthModel +from .conways_game_of_life.model import ConwaysGameOfLife +from .schelling.model import Schelling +from .virus_on_network.model import VirusOnNetwork + +__all__ = [ + "BoidFlockers", + "BoltzmannWealthModel", + "ConwaysGameOfLife", + "Schelling", + "VirusOnNetwork", +] diff --git a/examples/basic/boid_flockers/app.py b/examples/basic/boid_flockers/app.py index cfc36d004f3..02fde323285 100644 --- a/examples/basic/boid_flockers/app.py +++ b/examples/basic/boid_flockers/app.py @@ -1,7 +1,7 @@ -from model import BoidFlockers - from mesa.visualization import Slider, SolaraViz, make_space_matplotlib +from .model import BoidFlockers + def boid_draw(agent): if not agent.neighbors: # Only for the first Frame diff --git a/examples/basic/boid_flockers/model.py b/examples/basic/boid_flockers/model.py index cb80ca0ad2f..2bc59216d67 100644 --- a/examples/basic/boid_flockers/model.py +++ b/examples/basic/boid_flockers/model.py @@ -5,10 +5,11 @@ """ import numpy as np -from agents import Boid import mesa +from .agents import Boid + class BoidFlockers(mesa.Model): """Flocker model class. Handles agent creation, placement and scheduling.""" diff --git a/examples/basic/boltzmann_wealth_model/app.py b/examples/basic/boltzmann_wealth_model/app.py index 80e2b50b215..ebbb1ab3f1a 100644 --- a/examples/basic/boltzmann_wealth_model/app.py +++ b/examples/basic/boltzmann_wealth_model/app.py @@ -1,11 +1,11 @@ -from model import BoltzmannWealthModel - from mesa.visualization import ( SolaraViz, make_plot_measure, make_space_matplotlib, ) +from .model import BoltzmannWealthModel + def agent_portrayal(agent): size = 10 diff --git a/examples/basic/boltzmann_wealth_model/model.py b/examples/basic/boltzmann_wealth_model/model.py index e308a554954..249778cd6a1 100644 --- a/examples/basic/boltzmann_wealth_model/model.py +++ b/examples/basic/boltzmann_wealth_model/model.py @@ -1,7 +1,7 @@ -from agents import MoneyAgent - import mesa +from .agents import MoneyAgent + class BoltzmannWealthModel(mesa.Model): """A simple model of an economy where agents exchange currency at random. diff --git a/examples/basic/conways_game_of_life/model.py b/examples/basic/conways_game_of_life/model.py index d0c2992af56..183cfd92988 100644 --- a/examples/basic/conways_game_of_life/model.py +++ b/examples/basic/conways_game_of_life/model.py @@ -1,8 +1,8 @@ -from agents import Cell - from mesa import Model from mesa.space import SingleGrid +from .agents import Cell + class ConwaysGameOfLife(Model): """Represents the 2-dimensional array of cells in Conway's Game of Life.""" diff --git a/examples/basic/conways_game_of_life/server.py b/examples/basic/conways_game_of_life/server.py index c1a7afbd14b..6da932f3ee6 100644 --- a/examples/basic/conways_game_of_life/server.py +++ b/examples/basic/conways_game_of_life/server.py @@ -1,8 +1,8 @@ -from model import ConwaysGameOfLife -from portrayal import portrayCell - import mesa +from .model import ConwaysGameOfLife +from .portrayal import portrayCell + # Make a world that is 50x50, on a 250x250 display. canvas_element = mesa.visualization.CanvasGrid(portrayCell, 50, 50, 250, 250) diff --git a/examples/basic/schelling/app.py b/examples/basic/schelling/app.py index da4d6765f69..51989203593 100644 --- a/examples/basic/schelling/app.py +++ b/examples/basic/schelling/app.py @@ -1,5 +1,4 @@ import solara -from model import Schelling from mesa.visualization import ( Slider, @@ -8,6 +7,8 @@ make_space_matplotlib, ) +from .model import Schelling + def get_happy_agents(model): """Display a text count of how many happy agents there are.""" diff --git a/examples/basic/schelling/model.py b/examples/basic/schelling/model.py index d138ba0d6e5..fba0de7d4ac 100644 --- a/examples/basic/schelling/model.py +++ b/examples/basic/schelling/model.py @@ -1,8 +1,8 @@ -from agents import SchellingAgent - import mesa from mesa import Model +from .agents import SchellingAgent + class Schelling(Model): """Model class for the Schelling segregation model.""" diff --git a/examples/basic/virus_on_network/app.py b/examples/basic/virus_on_network/app.py index 8a5b0e6f6e8..0e85e8ac4fc 100644 --- a/examples/basic/virus_on_network/app.py +++ b/examples/basic/virus_on_network/app.py @@ -3,10 +3,11 @@ import solara from matplotlib.figure import Figure from matplotlib.ticker import MaxNLocator -from model import State, VirusOnNetwork, number_infected from mesa.visualization import Slider, SolaraViz, make_space_matplotlib +from .model import State, VirusOnNetwork, number_infected + def agent_portrayal(graph): def get_agent(node): diff --git a/examples/basic/virus_on_network/model.py b/examples/basic/virus_on_network/model.py index 8ead9ff9e98..73abf75771a 100644 --- a/examples/basic/virus_on_network/model.py +++ b/examples/basic/virus_on_network/model.py @@ -1,11 +1,12 @@ import math import networkx as nx -from agents import State, VirusAgent import mesa from mesa import Model +from .agents import State, VirusAgent + def number_state(model, state): return sum(1 for a in model.grid.get_all_cell_contents() if a.state is state) diff --git a/mesa/__init__.py b/mesa/__init__.py index c5ff65a2301..514275f7cb3 100644 --- a/mesa/__init__.py +++ b/mesa/__init__.py @@ -5,6 +5,7 @@ import datetime +import mesa.examples as examples import mesa.experimental as experimental import mesa.space as space import mesa.time as time @@ -21,6 +22,7 @@ "DataCollector", "batch_run", "experimental", + "examples", ] __title__ = "mesa" diff --git a/mesa/examples.py b/mesa/examples.py new file mode 100644 index 00000000000..e7d6613bce8 --- /dev/null +++ b/mesa/examples.py @@ -0,0 +1,3 @@ +"""This module is a collection of example models built using the Mesa framework.""" + +__path__ = ["examples"] diff --git a/mesa/examples/__init__.py b/mesa/examples/__init__.py deleted file mode 100644 index 8f1f4690933..00000000000 --- a/mesa/examples/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__path__ = ["examples"] # noqa: D104 From 706f03d73ff775d2d3ce7ca7faa2887c08a41ed0 Mon Sep 17 00:00:00 2001 From: corvince <13568919+Corvince@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:48:35 +0200 Subject: [PATCH 3/4] add tests for basic examples import --- tests/test_examples.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_examples.py b/tests/test_examples.py index 7318665dcf8..a3444a88bf6 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -6,6 +6,23 @@ import unittest +def test_examples_imports(): + """Test examples imports.""" + from mesa.examples.basic import ( + BoidFlockers, + BoltzmannWealthModel, + ConwaysGameOfLife, + Schelling, + VirusOnNetwork, + ) + + BoltzmannWealthModel() + Schelling() + BoidFlockers() + ConwaysGameOfLife() + VirusOnNetwork() + + def classcase(name): # noqa: D103 return "".join(x.capitalize() for x in name.replace("-", "_").split("_")) From 6cfada28a916fc9e66529fc1c5427d42d4c1c2b8 Mon Sep 17 00:00:00 2001 From: corvince <13568919+Corvince@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:11:51 +0200 Subject: [PATCH 4/4] Fix vis tutorial --- docs/tutorials/visualization_tutorial.ipynb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index 03c594bc4d3..c4bfda6eb74 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -46,10 +46,9 @@ "import mesa\n", "print(f\"Mesa version: {mesa.__version__}\")\n", "\n", - "# You can either define the BoltzmannWealthModel (aka MoneyModel) or install mesa-models:\n", - "%pip install --quiet -U git+https://github.com/projectmesa/mesa-examples#egg=mesa-models\n", + "# You can either define the BoltzmannWealthModel (aka MoneyModel) or install it\n", "\n", - "from mesa_models.boltzmann_wealth_model.model import BoltzmannWealthModel" + "from mesa.examples.basic import BoltzmannWealthModel" ] }, {