From ef464672971ce8f304b8cfc62a894c05b9345bf5 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Sat, 19 Oct 2024 17:13:18 +0200 Subject: [PATCH 1/7] explicitly test basic examples --- .../basic/boltzmann_wealth_model/model.py | 4 +- .../basic/conways_game_of_life/model.py | 4 +- mesa/examples/basic/virus_on_network/model.py | 3 +- tests/test_examples.py | 108 +++++------------- 4 files changed, 33 insertions(+), 86 deletions(-) diff --git a/mesa/examples/basic/boltzmann_wealth_model/model.py b/mesa/examples/basic/boltzmann_wealth_model/model.py index 249778cd6a1..a8e96a79ea3 100644 --- a/mesa/examples/basic/boltzmann_wealth_model/model.py +++ b/mesa/examples/basic/boltzmann_wealth_model/model.py @@ -11,8 +11,8 @@ class BoltzmannWealthModel(mesa.Model): highly skewed distribution of wealth. """ - def __init__(self, n=100, width=10, height=10): - super().__init__() + def __init__(self, n=100, width=10, height=10, seed=None): + super().__init__(seed=seed) self.num_agents = n self.grid = mesa.space.MultiGrid(width, height, True) diff --git a/mesa/examples/basic/conways_game_of_life/model.py b/mesa/examples/basic/conways_game_of_life/model.py index 183cfd92988..cc3fa996409 100644 --- a/mesa/examples/basic/conways_game_of_life/model.py +++ b/mesa/examples/basic/conways_game_of_life/model.py @@ -7,9 +7,9 @@ class ConwaysGameOfLife(Model): """Represents the 2-dimensional array of cells in Conway's Game of Life.""" - def __init__(self, width=50, height=50): + def __init__(self, width=50, height=50, seed=None): """Create a new playing area of (width, height) cells.""" - super().__init__() + super().__init__(seed=seed) # Use a simple grid, where edges wrap around. self.grid = SingleGrid(width, height, torus=True) diff --git a/mesa/examples/basic/virus_on_network/model.py b/mesa/examples/basic/virus_on_network/model.py index 73abf75771a..4618fb9ed90 100644 --- a/mesa/examples/basic/virus_on_network/model.py +++ b/mesa/examples/basic/virus_on_network/model.py @@ -36,8 +36,9 @@ def __init__( virus_check_frequency=0.4, recovery_chance=0.3, gain_resistance_chance=0.5, + seed=None, ): - super().__init__() + super().__init__(seed=seed) self.num_nodes = num_nodes prob = avg_node_degree / self.num_nodes self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob) diff --git a/tests/test_examples.py b/tests/test_examples.py index 010081aab74..0761caee1f3 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -1,90 +1,36 @@ # noqa: D100 -import contextlib -import importlib -import os.path -import sys -import unittest - - -def test_examples_imports(): - """Test examples imports.""" - from mesa.examples import ( - BoidFlockers, - BoltzmannWealthModel, - ConwaysGameOfLife, - Schelling, - VirusOnNetwork, - ) - - BoltzmannWealthModel() - Schelling() - BoidFlockers() - ConwaysGameOfLife() - VirusOnNetwork() - +from mesa.examples import ( + BoidFlockers, + BoltzmannWealthModel, + ConwaysGameOfLife, + Schelling, + VirusOnNetwork, +) -def classcase(name): # noqa: D103 - return "".join(x.capitalize() for x in name.replace("-", "_").split("_")) +def test_boltzmann_model(): + model = BoltzmannWealthModel(seed=42) + for _i in range(10): + model.step() -@unittest.skip( - "Skipping TextExamples, because examples folder was moved. More discussion needed." -) -class TestExamples(unittest.TestCase): - """Test examples' models. - This creates a model object and iterates it through - some steps. The idea is to get code coverage, rather than to test the - details of each example's model. - """ +def test_conways_game_model(): + model = ConwaysGameOfLife(seed=42) + for _i in range(10): + model.step() - EXAMPLES = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../mesa/examples") - ) +def test_schelling_model(): + model = Schelling(seed=42) + for _i in range(10): + model.step() - @contextlib.contextmanager - def active_example_dir(self, example): - """Save and restore sys.path and sys.modules.""" - old_sys_path = sys.path[:] - old_sys_modules = sys.modules.copy() - old_cwd = os.getcwd() - example_path = os.path.abspath(os.path.join(self.EXAMPLES, example)) - try: - sys.path.insert(0, example_path) - os.chdir(example_path) - yield - finally: - os.chdir(old_cwd) - added = [m for m in sys.modules if m not in old_sys_modules] - for mod in added: - del sys.modules[mod] - sys.modules.update(old_sys_modules) - sys.path[:] = old_sys_path +def test_virus_on_network(): + model = VirusOnNetwork(seed=42) + for _i in range(10): + model.step() - def test_examples(self): # noqa: D102 - for example in os.listdir(self.EXAMPLES): - if not os.path.isdir(os.path.join(self.EXAMPLES, example)): - continue - if hasattr(self, f"test_{example.replace('-', '_')}"): - # non-standard example; tested below - continue +def test_boid_flockers(): + model = BoidFlockers(seed=42) - print(f"testing example {example!r}") - with self.active_example_dir(example): - try: - # epstein_civil_violence.py at the top level - mod = importlib.import_module("model") - server = importlib.import_module("server") - server.server.render_model() - except ImportError: - # /epstein_civil_violence.py - mod = importlib.import_module(f"{example.replace('-', '_')}.model") - server = importlib.import_module( - f"{example.replace('-', '_')}.server" - ) - server.server.render_model() - model_class = getattr(mod, classcase(example)) - model = model_class() - for _ in range(10): - model.step() - self.assertEqual(model.steps, 10) + for _i in range(10): + model.step() \ No newline at end of file From 54c1b729117f7b320d0978d640dc3de143596f0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 15:14:36 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_examples.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_examples.py b/tests/test_examples.py index 0761caee1f3..8a7120dbd68 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -7,6 +7,7 @@ VirusOnNetwork, ) + def test_boltzmann_model(): model = BoltzmannWealthModel(seed=42) @@ -19,18 +20,21 @@ def test_conways_game_model(): for _i in range(10): model.step() + def test_schelling_model(): model = Schelling(seed=42) for _i in range(10): model.step() + def test_virus_on_network(): model = VirusOnNetwork(seed=42) for _i in range(10): model.step() + def test_boid_flockers(): model = BoidFlockers(seed=42) for _i in range(10): - model.step() \ No newline at end of file + model.step() From f3c4add4b374e6dc38e003292f9e2951a771c177 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Sat, 19 Oct 2024 17:15:31 +0200 Subject: [PATCH 3/7] ruff related fixes --- tests/test_examples.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/test_examples.py b/tests/test_examples.py index 8a7120dbd68..c2abc917772 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -8,32 +8,29 @@ ) -def test_boltzmann_model(): +def test_boltzmann_model(): # noqa: D103 model = BoltzmannWealthModel(seed=42) for _i in range(10): model.step() -def test_conways_game_model(): +def test_conways_game_model(): # noqa: D103 model = ConwaysGameOfLife(seed=42) for _i in range(10): model.step() - -def test_schelling_model(): +def test_schelling_model(): # noqa: D103 model = Schelling(seed=42) for _i in range(10): model.step() - -def test_virus_on_network(): +def test_virus_on_network(): # noqa: D103 model = VirusOnNetwork(seed=42) for _i in range(10): model.step() - -def test_boid_flockers(): +def test_boid_flockers(): # noqa: D103 model = BoidFlockers(seed=42) for _i in range(10): From 578ff722aff6118cdbf2485931955d366c7a2459 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 15:18:43 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_examples.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_examples.py b/tests/test_examples.py index c2abc917772..09f4f09a9b7 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -20,16 +20,19 @@ def test_conways_game_model(): # noqa: D103 for _i in range(10): model.step() + def test_schelling_model(): # noqa: D103 model = Schelling(seed=42) for _i in range(10): model.step() + def test_virus_on_network(): # noqa: D103 model = VirusOnNetwork(seed=42) for _i in range(10): model.step() + def test_boid_flockers(): # noqa: D103 model = BoidFlockers(seed=42) From 79dd733526be432a9b06972c2e4b23a46b0dbd52 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Sat, 19 Oct 2024 17:25:09 +0200 Subject: [PATCH 5/7] remove run_model from virus --- mesa/examples/basic/virus_on_network/model.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/mesa/examples/basic/virus_on_network/model.py b/mesa/examples/basic/virus_on_network/model.py index 4618fb9ed90..3eceac13d6f 100644 --- a/mesa/examples/basic/virus_on_network/model.py +++ b/mesa/examples/basic/virus_on_network/model.py @@ -95,6 +95,3 @@ def step(self): # collect data self.datacollector.collect(self) - def run_model(self, n): - for _ in range(n): - self.step() From 08c3972c3d4867736920bece3e0d5c4203300a90 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Sat, 19 Oct 2024 17:27:11 +0200 Subject: [PATCH 6/7] Update model.py --- mesa/examples/basic/virus_on_network/model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mesa/examples/basic/virus_on_network/model.py b/mesa/examples/basic/virus_on_network/model.py index 3eceac13d6f..9b59bc55375 100644 --- a/mesa/examples/basic/virus_on_network/model.py +++ b/mesa/examples/basic/virus_on_network/model.py @@ -57,6 +57,7 @@ def __init__( "Infected": number_infected, "Susceptible": number_susceptible, "Resistant": number_resistant, + "R over S": self.resistant_susceptible_ratio } ) From b14a9d87b142eea3906866c6968c9b2145ab9387 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 15:27:24 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/examples/basic/virus_on_network/model.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mesa/examples/basic/virus_on_network/model.py b/mesa/examples/basic/virus_on_network/model.py index 9b59bc55375..04e457e8bf3 100644 --- a/mesa/examples/basic/virus_on_network/model.py +++ b/mesa/examples/basic/virus_on_network/model.py @@ -57,7 +57,7 @@ def __init__( "Infected": number_infected, "Susceptible": number_susceptible, "Resistant": number_resistant, - "R over S": self.resistant_susceptible_ratio + "R over S": self.resistant_susceptible_ratio, } ) @@ -95,4 +95,3 @@ def step(self): self.agents.shuffle_do("step") # collect data self.datacollector.collect(self) -