Skip to content

Commit a41914e

Browse files
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>
1 parent c63ed4c commit a41914e

File tree

10 files changed

+68
-35
lines changed

10 files changed

+68
-35
lines changed

mesa/examples/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from mesa.examples.advanced.epstein_civil_violence.model import EpsteinCivilViolence
22
from mesa.examples.advanced.pd_grid.model import PdGrid
3+
from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt
4+
from mesa.examples.advanced.wolf_sheep.model import WolfSheep
35
from mesa.examples.basic.boid_flockers.model import BoidFlockers
46
from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealthModel
57
from mesa.examples.basic.conways_game_of_life.model import ConwaysGameOfLife
@@ -14,4 +16,6 @@
1416
"VirusOnNetwork",
1517
"EpsteinCivilViolence",
1618
"PdGrid",
19+
"SugarscapeG1mt",
20+
"WolfSheep",
1721
]

mesa/examples/advanced/pd_grid/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ def pd_agent_portrayal(agent):
4747
model_params=model_params,
4848
name="Spatial Prisoner's Dilemma",
4949
)
50-
51-
page
50+
page # noqa B018

mesa/examples/advanced/sugarscape_g1mt/agents.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import math
22

3-
from mesa.experimental.cell_space import CellAgent
4-
from mesa.experimental.cell_space import FixedAgent
5-
3+
from mesa.experimental.cell_space import CellAgent, FixedAgent
64

75

86
# Helper function
@@ -18,6 +16,8 @@ def get_distance(cell_1, cell_2):
1816
dx = x1 - x2
1917
dy = y1 - y2
2018
return math.sqrt(dx**2 + dy**2)
19+
20+
2121
class Resource(FixedAgent):
2222
"""
2323
Resource:
@@ -43,9 +43,6 @@ def step(self):
4343
self.spice_amount = min([self.max_spice, self.spice_amount + 1])
4444

4545

46-
47-
48-
4946
class Trader(CellAgent):
5047
"""
5148
Trader:

mesa/examples/advanced/sugarscape_g1mt/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import sys
21
import os.path
3-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../../')))
2+
import sys
3+
4+
sys.path.insert(
5+
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../"))
6+
)
47

58

69
import numpy as np
710
import solara
811
from matplotlib.figure import Figure
9-
from mesa.visualization import SolaraViz, make_plot_measure
10-
1112
from sugarscape_g1mt.model import SugarscapeG1mt
1213
from sugarscape_g1mt.trader_agents import Trader
1314

15+
from mesa.visualization import SolaraViz, make_plot_measure
16+
1417

1518
def SpaceDrawer(model):
1619
def portray(g):

mesa/examples/advanced/sugarscape_g1mt/model.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from pathlib import Path
22

3-
import mesa
43
import numpy as np
5-
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid
64

5+
import mesa
76
from mesa.examples.advanced.sugarscape_g1mt.agents import Resource, Trader
8-
7+
from mesa.experimental.cell_space import OrthogonalVonNeumannGrid
98

109

1110
# Helper Functions
@@ -53,7 +52,7 @@ def __init__(
5352
vision_min=1,
5453
vision_max=5,
5554
enable_trade=True,
56-
seed=None
55+
seed=None,
5756
):
5857
super().__init__(seed=seed)
5958
# Initiate width and height of sugarscape
@@ -177,5 +176,5 @@ def step(self):
177176
self.datacollector._agent_records[self.steps] = agent_trades
178177

179178
def run_model(self, step_count=1000):
180-
for i in range(step_count):
179+
for _ in range(step_count):
181180
self.step()

mesa/examples/advanced/sugarscape_g1mt/tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import numpy as np
22
from scipy import stats
3-
from .model import SugarscapeG1mt, flatten
3+
44
from .agents import Trader
5+
from .model import SugarscapeG1mt, flatten
56

67

78
def check_slope(y, increasing):

mesa/examples/advanced/wolf_sheep/app.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mesa.examples.advanced.wolf_sheep.agents import Wolf, Sheep, GrassPatch
1+
from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf
22
from mesa.examples.advanced.wolf_sheep.model import WolfSheep
33
from mesa.visualization import (
44
Slider,
@@ -37,21 +37,18 @@ def wolf_sheep_portrayal(agent):
3737

3838
return portrayal
3939

40+
4041
model_params = {
4142
# The following line is an example to showcase StaticText.
4243
"grass": {
4344
"type": "Select",
4445
"value": True,
4546
"values": [True, False],
46-
"label": "grass regrowth enabled?"
47+
"label": "grass regrowth enabled?",
4748
},
4849
"grass_regrowth_time": Slider("Grass Regrowth Time", 20, 1, 50),
49-
"initial_sheep": Slider(
50-
"Initial Sheep Population", 100, 10, 300
51-
),
52-
"sheep_reproduce": Slider(
53-
"Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01
54-
),
50+
"initial_sheep": Slider("Initial Sheep Population", 100, 10, 300),
51+
"sheep_reproduce": Slider("Sheep Reproduction Rate", 0.04, 0.01, 1.0, 0.01),
5552
"initial_wolves": Slider("Initial Wolf Population", 10, 5, 100),
5653
"wolf_reproduce": Slider(
5754
"Wolf Reproduction Rate",
@@ -60,9 +57,7 @@ def wolf_sheep_portrayal(agent):
6057
1.0,
6158
0.01,
6259
),
63-
"wolf_gain_from_food": Slider(
64-
"Wolf Gain From Food Rate", 20, 1, 50
65-
),
60+
"wolf_gain_from_food": Slider("Wolf Gain From Food Rate", 20, 1, 50),
6661
"sheep_gain_from_food": Slider("Sheep Gain From Food", 4, 1, 10),
6762
}
6863

mesa/examples/advanced/wolf_sheep/model.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"""
1111

1212
import mesa
13-
from mesa.experimental.cell_space import OrthogonalMooreGrid
14-
1513
from mesa.examples.advanced.wolf_sheep.agents import GrassPatch, Sheep, Wolf
14+
from mesa.experimental.cell_space import OrthogonalMooreGrid
1615

1716

1817
class WolfSheep(mesa.Model):
@@ -81,13 +80,17 @@ def __init__(
8180
collectors = {
8281
"Wolves": lambda m: len(m.agents_by_type[Wolf]),
8382
"Sheep": lambda m: len(m.agents_by_type[Sheep]),
84-
"Grass": lambda m: len(m.agents_by_type[GrassPatch].select(lambda a:a.fully_grown)) if m.grass else -1,
83+
"Grass": lambda m: len(
84+
m.agents_by_type[GrassPatch].select(lambda a: a.fully_grown)
85+
)
86+
if m.grass
87+
else -1,
8588
}
8689

8790
self.datacollector = mesa.DataCollector(collectors)
8891

8992
# Create sheep:
90-
for i in range(self.initial_sheep):
93+
for _ in range(self.initial_sheep):
9194
x = self.random.randrange(self.width)
9295
y = self.random.randrange(self.height)
9396
energy = self.random.randrange(2 * self.sheep_gain_from_food)
@@ -130,5 +133,5 @@ def step(self):
130133
self.datacollector.collect(self)
131134

132135
def run_model(self, step_count=200):
133-
for i in range(step_count):
136+
for _ in range(step_count):
134137
self.step()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ path = "mesa/__init__.py"
9797
# Hardcode to Python 3.10.
9898
# Reminder to update mesa-examples if the value below is changed.
9999
target-version = "py310"
100-
extend-exclude = ["docs", "build", "mesa/examples/advanced"] # TODO: Remove examples/advanced
100+
extend-exclude = ["docs", "build"]
101101

102102
[tool.ruff.lint]
103103
select = [

tests/test_examples.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
BoidFlockers,
44
BoltzmannWealthModel,
55
ConwaysGameOfLife,
6+
EpsteinCivilViolence,
7+
PdGrid,
68
Schelling,
9+
SugarscapeG1mt,
710
VirusOnNetwork,
11+
WolfSheep,
812
)
913

1014

@@ -38,3 +42,31 @@ def test_boid_flockers(): # noqa: D103
3842

3943
for _i in range(10):
4044
model.step()
45+
46+
47+
def test_epstein(): # noqa: D103
48+
model = EpsteinCivilViolence(seed=42)
49+
50+
for _i in range(10):
51+
model.step()
52+
53+
54+
def test_pd_grid(): # noqa: D103
55+
model = PdGrid(seed=42)
56+
57+
for _i in range(10):
58+
model.step()
59+
60+
61+
def test_sugarscape_g1mt(): # noqa: D103
62+
model = SugarscapeG1mt(seed=42)
63+
64+
for _i in range(10):
65+
model.step()
66+
67+
68+
def test_wolf_sheep(): # noqa: D103
69+
model = WolfSheep(seed=42)
70+
71+
for _i in range(10):
72+
model.step()

0 commit comments

Comments
 (0)