From 7e118d3c94f4a9c16a65d20f96dea2c482037111 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 16 Oct 2024 11:57:58 +0200 Subject: [PATCH] Run ruff on basic examples --- examples/basic/boid_flockers/agents.py | 13 +++++-------- examples/basic/boid_flockers/app.py | 5 +++-- examples/basic/boid_flockers/model.py | 15 ++++++--------- examples/basic/boltzmann_wealth_model/app.py | 3 ++- examples/basic/boltzmann_wealth_model/model.py | 4 +++- examples/basic/conways_game_of_life/agents.py | 10 +++------- examples/basic/conways_game_of_life/model.py | 13 +++++-------- examples/basic/conways_game_of_life/portrayal.py | 3 +-- examples/basic/conways_game_of_life/server.py | 4 ++-- examples/basic/schelling/agents.py | 6 ++---- examples/basic/schelling/app.py | 6 +++--- examples/basic/schelling/model.py | 14 +++++--------- examples/basic/virus_on_network/agents.py | 6 +++--- examples/basic/virus_on_network/app.py | 3 ++- examples/basic/virus_on_network/model.py | 9 ++++----- 15 files changed, 49 insertions(+), 65 deletions(-) diff --git a/examples/basic/boid_flockers/agents.py b/examples/basic/boid_flockers/agents.py index 857c7b98f23..45132b5f769 100644 --- a/examples/basic/boid_flockers/agents.py +++ b/examples/basic/boid_flockers/agents.py @@ -1,10 +1,10 @@ -from mesa import Agent import numpy as np +from mesa import Agent + class Boid(Agent): - """ - A Boid-style flocker agent. + """A Boid-style flocker agent. The agent follows three behaviors to flock: - Cohesion: steering towards neighboring agents. @@ -28,8 +28,7 @@ def __init__( separate=0.015, match=0.05, ): - """ - Create a new Boid flocker agent. + """Create a new Boid flocker agent. Args: speed: Distance to move per step. @@ -51,10 +50,8 @@ def __init__( self.neighbors = None def step(self): + """Get the Boid's neighbors, compute the new vector, and move accordingly. """ - Get the Boid's neighbors, compute the new vector, and move accordingly. - """ - self.neighbors = self.model.space.get_neighbors(self.pos, self.vision, False) n = 0 match_vector, separation_vector, cohere = np.zeros((3, 2)) diff --git a/examples/basic/boid_flockers/app.py b/examples/basic/boid_flockers/app.py index 8decb9832c5..add6c4f4f03 100644 --- a/examples/basic/boid_flockers/app.py +++ b/examples/basic/boid_flockers/app.py @@ -1,6 +1,7 @@ from model import BoidFlockers -from mesa.visualization import SolaraViz, make_space_matplotlib -from mesa.visualization import Slider + +from mesa.visualization import Slider, SolaraViz, make_space_matplotlib + def boid_draw(agent): if not agent.neighbors: # Only for the first Frame diff --git a/examples/basic/boid_flockers/model.py b/examples/basic/boid_flockers/model.py index 6f9177163d3..ec56597dc0f 100644 --- a/examples/basic/boid_flockers/model.py +++ b/examples/basic/boid_flockers/model.py @@ -1,18 +1,17 @@ -""" -Flockers +"""Flockers ============================================================= A Mesa implementation of Craig Reynolds's Boids flocker model. Uses numpy arrays to represent vectors. """ -import mesa import numpy as np from agents import Boid +import mesa + class BoidFlockers(mesa.Model): - """ - Flocker model class. Handles agent creation, placement and scheduling. + """Flocker model class. Handles agent creation, placement and scheduling. """ def __init__( @@ -28,8 +27,7 @@ def __init__( separate=0.015, match=0.05, ): - """ - Create a new Flockers model. + """Create a new Flockers model. Args: population: Number of Boids @@ -52,8 +50,7 @@ def __init__( self.make_agents() def make_agents(self): - """ - Create self.population agents, with random positions and starting headings. + """Create self.population agents, with random positions and starting headings. """ for _ in range(self.population): x = self.random.random() * self.space.x_max diff --git a/examples/basic/boltzmann_wealth_model/app.py b/examples/basic/boltzmann_wealth_model/app.py index 199b3a1a50e..0c38c742500 100644 --- a/examples/basic/boltzmann_wealth_model/app.py +++ b/examples/basic/boltzmann_wealth_model/app.py @@ -1,9 +1,10 @@ +from model import BoltzmannWealthModel + from mesa.visualization import ( SolaraViz, make_plot_measure, make_space_matplotlib, ) -from model import BoltzmannWealthModel def agent_portrayal(agent): diff --git a/examples/basic/boltzmann_wealth_model/model.py b/examples/basic/boltzmann_wealth_model/model.py index 26c2f676ddb..e99c046cd03 100644 --- a/examples/basic/boltzmann_wealth_model/model.py +++ b/examples/basic/boltzmann_wealth_model/model.py @@ -1,6 +1,8 @@ -import mesa from agents import MoneyAgent +import mesa + + class BoltzmannWealthModel(mesa.Model): """A simple model of an economy where agents exchange currency at random. diff --git a/examples/basic/conways_game_of_life/agents.py b/examples/basic/conways_game_of_life/agents.py index 92d12bc2380..84313a80189 100644 --- a/examples/basic/conways_game_of_life/agents.py +++ b/examples/basic/conways_game_of_life/agents.py @@ -8,8 +8,7 @@ class Cell(Agent): ALIVE = 1 def __init__(self, pos, model, init_state=DEAD): - """ - Create a cell, in the given state, at the given x, y position. + """Create a cell, in the given state, at the given x, y position. """ super().__init__(model) self.x, self.y = pos @@ -25,14 +24,12 @@ def neighbors(self): return self.model.grid.iter_neighbors((self.x, self.y), True) def determine_state(self): - """ - Compute if the cell will be dead or alive at the next tick. This is + """Compute if the cell will be dead or alive at the next tick. This is based on the number of alive or dead neighbors. The state is not changed here, but is just computed and stored in self._nextState, because our current state may still be necessary for our neighbors to calculate their next state. """ - # Get the neighbors and apply the rules on whether to be alive or dead # at the next tick. live_neighbors = sum(neighbor.isAlive for neighbor in self.neighbors) @@ -47,7 +44,6 @@ def determine_state(self): self._nextState = self.ALIVE def assume_state(self): - """ - Set the state to the new computed state -- computed in step(). + """Set the state to the new computed state -- computed in step(). """ self.state = self._nextState diff --git a/examples/basic/conways_game_of_life/model.py b/examples/basic/conways_game_of_life/model.py index abf068bace3..bd044581718 100644 --- a/examples/basic/conways_game_of_life/model.py +++ b/examples/basic/conways_game_of_life/model.py @@ -1,18 +1,16 @@ +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 + """Represents the 2-dimensional array of cells in Conway's Game of Life. """ def __init__(self, width=50, height=50): - """ - Create a new playing area of (width, height) cells. + """Create a new playing area of (width, height) cells. """ super().__init__() # Use a simple grid, where edges wrap around. @@ -29,8 +27,7 @@ def __init__(self, width=50, height=50): self.running = True def step(self): - """ - Perform the model step in two stages: + """Perform the model step in two stages: - First, all cells assume their next state (whether they will be dead or alive) - Then, all cells change state to their next state """ diff --git a/examples/basic/conways_game_of_life/portrayal.py b/examples/basic/conways_game_of_life/portrayal.py index 4f68468d857..1c8d6d0fadd 100644 --- a/examples/basic/conways_game_of_life/portrayal.py +++ b/examples/basic/conways_game_of_life/portrayal.py @@ -1,6 +1,5 @@ def portrayCell(cell): - """ - This function is registered with the visualization server to be called + """This function is registered with the visualization server to be called each tick to indicate how to draw the cell in its current state. :param cell: the cell in the simulation :return: the portrayal dictionary. diff --git a/examples/basic/conways_game_of_life/server.py b/examples/basic/conways_game_of_life/server.py index 8277437d6bb..c1a7afbd14b 100644 --- a/examples/basic/conways_game_of_life/server.py +++ b/examples/basic/conways_game_of_life/server.py @@ -1,8 +1,8 @@ -import mesa - from model import ConwaysGameOfLife from portrayal import portrayCell +import mesa + # Make a world that is 50x50, on a 250x250 display. canvas_element = mesa.visualization.CanvasGrid(portrayCell, 50, 50, 250, 250) diff --git a/examples/basic/schelling/agents.py b/examples/basic/schelling/agents.py index 80bd5cadcd0..9c98a187151 100644 --- a/examples/basic/schelling/agents.py +++ b/examples/basic/schelling/agents.py @@ -2,13 +2,11 @@ class SchellingAgent(Agent): - """ - Schelling segregation agent + """Schelling segregation agent """ def __init__(self, model: Model, agent_type: int) -> None: - """ - Create a new Schelling agent. + """Create a new Schelling agent. Args: agent_type: Indicator for the agent's type (minority=1, majority=0) diff --git a/examples/basic/schelling/app.py b/examples/basic/schelling/app.py index fb837351d16..80ba2cc46c1 100644 --- a/examples/basic/schelling/app.py +++ b/examples/basic/schelling/app.py @@ -1,16 +1,16 @@ import solara +from model import Schelling + from mesa.visualization import ( Slider, SolaraViz, make_plot_measure, make_space_matplotlib, ) -from model import Schelling def get_happy_agents(model): - """ - Display a text count of how many happy agents there are. + """Display a text count of how many happy agents there are. """ return solara.Markdown(f"**Happy agents: {model.happy}**") diff --git a/examples/basic/schelling/model.py b/examples/basic/schelling/model.py index 16b8fe5da8c..ba3f46f3b36 100644 --- a/examples/basic/schelling/model.py +++ b/examples/basic/schelling/model.py @@ -1,12 +1,11 @@ +from agents import SchellingAgent + import mesa from mesa import Model -from agents import SchellingAgent - class Schelling(Model): - """ - Model class for the Schelling segregation model. + """Model class for the Schelling segregation model. """ def __init__( @@ -19,8 +18,7 @@ def __init__( minority_pc=0.2, seed=None, ): - """ - Create a new Schelling model. + """Create a new Schelling model. Args: width, height: Size of the space. @@ -30,7 +28,6 @@ def __init__( radius: Search radius for checking similarity seed: Seed for Reproducibility """ - super().__init__(seed=seed) self.homophily = homophily self.radius = radius @@ -55,8 +52,7 @@ def __init__( self.datacollector.collect(self) def step(self): - """ - Run one step of the model. + """Run one step of the model. """ self.happy = 0 # Reset counter of happy agents self.agents.shuffle_do("step") diff --git a/examples/basic/virus_on_network/agents.py b/examples/basic/virus_on_network/agents.py index 3d9486aa4eb..d854222aca3 100644 --- a/examples/basic/virus_on_network/agents.py +++ b/examples/basic/virus_on_network/agents.py @@ -1,6 +1,7 @@ -from mesa import Agent from enum import Enum +from mesa import Agent + class State(Enum): SUSCEPTIBLE = 0 @@ -9,8 +10,7 @@ class State(Enum): class VirusAgent(Agent): - """ - Individual Agent definition and its properties/interaction methods + """Individual Agent definition and its properties/interaction methods """ def __init__( diff --git a/examples/basic/virus_on_network/app.py b/examples/basic/virus_on_network/app.py index 89c377b25d2..8a5b0e6f6e8 100644 --- a/examples/basic/virus_on_network/app.py +++ b/examples/basic/virus_on_network/app.py @@ -3,9 +3,10 @@ import solara from matplotlib.figure import Figure from matplotlib.ticker import MaxNLocator -from mesa.visualization import SolaraViz, Slider, make_space_matplotlib from model import State, VirusOnNetwork, number_infected +from mesa.visualization import Slider, SolaraViz, make_space_matplotlib + def agent_portrayal(graph): def get_agent(node): diff --git a/examples/basic/virus_on_network/model.py b/examples/basic/virus_on_network/model.py index d0ad600b8d7..d3f736097ab 100644 --- a/examples/basic/virus_on_network/model.py +++ b/examples/basic/virus_on_network/model.py @@ -1,10 +1,10 @@ import math -import mesa -from mesa import Model import networkx as nx +from agents import State, VirusAgent -from agents import VirusAgent, State +import mesa +from mesa import Model def number_state(model, state): @@ -24,8 +24,7 @@ def number_resistant(model): class VirusOnNetwork(Model): - """ - A virus model with some number of agents + """A virus model with some number of agents """ def __init__(