From a41914e46d89cf63cca465fd1cbcafd8d3c3fbfd Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Thu, 24 Oct 2024 22:29:42 +0200 Subject: [PATCH] reactivate ruff for advanced examples and include them in tests (#2414) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- mesa/examples/__init__.py | 4 +++ mesa/examples/advanced/pd_grid/app.py | 3 +- .../advanced/sugarscape_g1mt/agents.py | 9 ++---- mesa/examples/advanced/sugarscape_g1mt/app.py | 11 ++++--- .../advanced/sugarscape_g1mt/model.py | 9 +++--- .../advanced/sugarscape_g1mt/tests.py | 3 +- mesa/examples/advanced/wolf_sheep/app.py | 17 ++++------ mesa/examples/advanced/wolf_sheep/model.py | 13 +++++--- pyproject.toml | 2 +- tests/test_examples.py | 32 +++++++++++++++++++ 10 files changed, 68 insertions(+), 35 deletions(-) diff --git a/mesa/examples/__init__.py b/mesa/examples/__init__.py index 0258243c9c3..048138b7b00 100644 --- a/mesa/examples/__init__.py +++ b/mesa/examples/__init__.py @@ -1,5 +1,7 @@ from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence from mesa.examples.advanced.pd_grid.model import PdGrid +from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt +from mesa.examples.advanced.wolf_sheep.model import WolfSheep from mesa.examples.basic.boid_flockers.model import BoidFlockers from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealthModel from mesa.examples.basic.conways_game_of_life.model import ConwaysGameOfLife @@ -14,4 +16,6 @@ "VirusOnNetwork", "EpsteinCivilViolence", "PdGrid", + "SugarscapeG1mt", + "WolfSheep", ] diff --git a/mesa/examples/advanced/pd_grid/app.py b/mesa/examples/advanced/pd_grid/app.py index 4f97f9ff491..c8ceec9fe16 100644 --- a/mesa/examples/advanced/pd_grid/app.py +++ b/mesa/examples/advanced/pd_grid/app.py @@ -47,5 +47,4 @@ def pd_agent_portrayal(agent): model_params=model_params, name="Spatial Prisoner's Dilemma", ) - -page +page # noqa B018 diff --git a/mesa/examples/advanced/sugarscape_g1mt/agents.py b/mesa/examples/advanced/sugarscape_g1mt/agents.py index 74b33e91c85..aa9e9adc2ee 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/agents.py +++ b/mesa/examples/advanced/sugarscape_g1mt/agents.py @@ -1,8 +1,6 @@ import math -from mesa.experimental.cell_space import CellAgent -from mesa.experimental.cell_space import FixedAgent - +from mesa.experimental.cell_space import CellAgent, FixedAgent # Helper function @@ -18,6 +16,8 @@ def get_distance(cell_1, cell_2): dx = x1 - x2 dy = y1 - y2 return math.sqrt(dx**2 + dy**2) + + class Resource(FixedAgent): """ Resource: @@ -43,9 +43,6 @@ def step(self): self.spice_amount = min([self.max_spice, self.spice_amount + 1]) - - - class Trader(CellAgent): """ Trader: diff --git a/mesa/examples/advanced/sugarscape_g1mt/app.py b/mesa/examples/advanced/sugarscape_g1mt/app.py index 78327721650..7c8cc2cfead 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/app.py +++ b/mesa/examples/advanced/sugarscape_g1mt/app.py @@ -1,16 +1,19 @@ -import sys import os.path -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../../'))) +import sys + +sys.path.insert( + 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../")) +) import numpy as np import solara from matplotlib.figure import Figure -from mesa.visualization import SolaraViz, make_plot_measure - from sugarscape_g1mt.model import SugarscapeG1mt from sugarscape_g1mt.trader_agents import Trader +from mesa.visualization import SolaraViz, make_plot_measure + def SpaceDrawer(model): def portray(g): diff --git a/mesa/examples/advanced/sugarscape_g1mt/model.py b/mesa/examples/advanced/sugarscape_g1mt/model.py index f90cd3334cb..c6c5601cac3 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/model.py +++ b/mesa/examples/advanced/sugarscape_g1mt/model.py @@ -1,11 +1,10 @@ from pathlib import Path -import mesa import numpy as np -from mesa.experimental.cell_space import OrthogonalVonNeumannGrid +import mesa from mesa.examples.advanced.sugarscape_g1mt.agents import Resource, Trader - +from mesa.experimental.cell_space import OrthogonalVonNeumannGrid # Helper Functions @@ -53,7 +52,7 @@ def __init__( vision_min=1, vision_max=5, enable_trade=True, - seed=None + seed=None, ): super().__init__(seed=seed) # Initiate width and height of sugarscape @@ -177,5 +176,5 @@ def step(self): self.datacollector._agent_records[self.steps] = agent_trades def run_model(self, step_count=1000): - for i in range(step_count): + for _ in range(step_count): self.step() diff --git a/mesa/examples/advanced/sugarscape_g1mt/tests.py b/mesa/examples/advanced/sugarscape_g1mt/tests.py index d570ce42161..a25715ea0d5 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/tests.py +++ b/mesa/examples/advanced/sugarscape_g1mt/tests.py @@ -1,7 +1,8 @@ import numpy as np from scipy import stats -from .model import SugarscapeG1mt, flatten + from .agents import Trader +from .model import SugarscapeG1mt, flatten def check_slope(y, increasing): diff --git a/mesa/examples/advanced/wolf_sheep/app.py b/mesa/examples/advanced/wolf_sheep/app.py index 426610d6899..b5ac6e8bf47 100644 --- a/mesa/examples/advanced/wolf_sheep/app.py +++ b/mesa/examples/advanced/wolf_sheep/app.py @@ -1,4 +1,4 @@ -from mesa.examples.advanced.wolf_sheep.agents import Wolf, Sheep, GrassPatch +from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf from mesa.examples.advanced.wolf_sheep.model import WolfSheep from mesa.visualization import ( Slider, @@ -37,21 +37,18 @@ def wolf_sheep_portrayal(agent): return portrayal + model_params = { # The following line is an example to showcase StaticText. "grass": { "type": "Select", "value": True, "values": [True, False], - "label": "grass regrowth enabled?" + "label": "grass regrowth enabled?", }, "grass_regrowth_time": Slider("Grass Regrowth Time", 20, 1, 50), - "initial_sheep": Slider( - "Initial Sheep Population", 100, 10, 300 - ), - "sheep_reproduce": Slider( - "Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01 - ), + "initial_sheep": Slider("Initial Sheep Population", 100, 10, 300), + "sheep_reproduce": Slider("Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01), "initial_wolves": Slider("Initial Wolf Population", 10, 5, 100), "wolf_reproduce": Slider( "Wolf Reproduction Rate", @@ -60,9 +57,7 @@ def wolf_sheep_portrayal(agent): 1.0, 0.01, ), - "wolf_gain_from_food": Slider( - "Wolf Gain From Food Rate", 20, 1, 50 - ), + "wolf_gain_from_food": Slider("Wolf Gain From Food Rate", 20, 1, 50), "sheep_gain_from_food": Slider("Sheep Gain From Food", 4, 1, 10), } diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index 37ea8e4ac8a..2ee09d3f732 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -10,9 +10,8 @@ """ import mesa -from mesa.experimental.cell_space import OrthogonalMooreGrid - from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf +from mesa.experimental.cell_space import OrthogonalMooreGrid class WolfSheep(mesa.Model): @@ -81,13 +80,17 @@ def __init__( collectors = { "Wolves": lambda m: len(m.agents_by_type[Wolf]), "Sheep": lambda m: len(m.agents_by_type[Sheep]), - "Grass": lambda m: len(m.agents_by_type[GrassPatch].select(lambda a:a.fully_grown)) if m.grass else -1, + "Grass": lambda m: len( + m.agents_by_type[GrassPatch].select(lambda a: a.fully_grown) + ) + if m.grass + else -1, } self.datacollector = mesa.DataCollector(collectors) # Create sheep: - for i in range(self.initial_sheep): + for _ in range(self.initial_sheep): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.sheep_gain_from_food) @@ -130,5 +133,5 @@ def step(self): self.datacollector.collect(self) def run_model(self, step_count=200): - for i in range(step_count): + for _ in range(step_count): self.step() diff --git a/pyproject.toml b/pyproject.toml index d9aaffded44..600616a3ad9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,7 @@ path = "mesa/__init__.py" # Hardcode to Python 3.10. # Reminder to update mesa-examples if the value below is changed. target-version = "py310" -extend-exclude = ["docs", "build", "mesa/examples/advanced"] # TODO: Remove examples/advanced +extend-exclude = ["docs", "build"] [tool.ruff.lint] select = [ diff --git a/tests/test_examples.py b/tests/test_examples.py index 09f4f09a9b7..ff5cd478e06 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -3,8 +3,12 @@ BoidFlockers, BoltzmannWealthModel, ConwaysGameOfLife, + EpsteinCivilViolence, + PdGrid, Schelling, + SugarscapeG1mt, VirusOnNetwork, + WolfSheep, ) @@ -38,3 +42,31 @@ def test_boid_flockers(): # noqa: D103 for _i in range(10): model.step() + + +def test_epstein(): # noqa: D103 + model = EpsteinCivilViolence(seed=42) + + for _i in range(10): + model.step() + + +def test_pd_grid(): # noqa: D103 + model = PdGrid(seed=42) + + for _i in range(10): + model.step() + + +def test_sugarscape_g1mt(): # noqa: D103 + model = SugarscapeG1mt(seed=42) + + for _i in range(10): + model.step() + + +def test_wolf_sheep(): # noqa: D103 + model = WolfSheep(seed=42) + + for _i in range(10): + model.step()