Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importable examples #2381

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added examples/__init__.py
Empty file.
Empty file added examples/advanced/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions examples/basic/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
4 changes: 2 additions & 2 deletions examples/basic/boid_flockers/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"""

import numpy as np
from agents import Boid

import mesa

from .agents import Boid
EwoutH marked this conversation as resolved.
Show resolved Hide resolved


class BoidFlockers(mesa.Model):
"""Flocker model class. Handles agent creation, placement and scheduling."""
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/boltzmann_wealth_model/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/boltzmann_wealth_model/model.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/conways_game_of_life/model.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down
6 changes: 3 additions & 3 deletions examples/basic/conways_game_of_life/server.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
3 changes: 2 additions & 1 deletion examples/basic/schelling/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import solara
from model import Schelling

from mesa.visualization import (
Slider,
Expand All @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/schelling/model.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/virus_on_network/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/virus_on_network/model.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions mesa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,7 @@
"DataCollector",
"batch_run",
"experimental",
"examples",
]

__title__ = "mesa"
Expand Down
3 changes: 3 additions & 0 deletions mesa/examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""This module is a collection of example models built using the Mesa framework."""

__path__ = ["examples"]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("_"))

Expand Down
Loading