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

reactivate ruff for advanced examples and include them in tests #2414

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
reactive ruff for advanced examples and include them in tests
  • Loading branch information
quaquel committed Oct 24, 2024
commit 87df41905f7fb1b1179c8ce9367248b9a5ab9bd7
5 changes: 5 additions & 0 deletions mesa/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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
from mesa.examples.basic.schelling.model import Schelling
from mesa.examples.basic.virus_on_network.model import VirusOnNetwork


__all__ = [
"BoidFlockers",
"BoltzmannWealthModel",
Expand All @@ -14,4 +17,6 @@
"VirusOnNetwork",
"EpsteinCivilViolence",
"PdGrid",
"SugarscapeG1mt",
"PdGrid"
]
3 changes: 1 addition & 2 deletions mesa/examples/advanced/pd_grid/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ def pd_agent_portrayal(agent):
model_params=model_params,
name="Spatial Prisoner's Dilemma",
)

page
page #noqa B018
6 changes: 3 additions & 3 deletions mesa/examples/advanced/sugarscape_g1mt/agents.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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):
Expand Down
7 changes: 3 additions & 4 deletions mesa/examples/advanced/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion mesa/examples/advanced/sugarscape_g1mt/tests.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
2 changes: 1 addition & 1 deletion mesa/examples/advanced/wolf_sheep/app.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 3 additions & 4 deletions mesa/examples/advanced/wolf_sheep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -87,7 +86,7 @@ def __init__(
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)
Expand Down Expand Up @@ -130,5 +129,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()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
29 changes: 29 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
BoidFlockers,
BoltzmannWealthModel,
ConwaysGameOfLife,
EpsteinCivilViolence,
PdGrid,
Schelling,
SugarscapeG1mt,
VirusOnNetwork,
WolfSheep,
)


Expand Down Expand Up @@ -38,3 +42,28 @@ 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()
Loading