Skip to content

Commit

Permalink
Make Grid.coord_iter return content, pos
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar committed Dec 23, 2022
1 parent fc013ab commit 5111908
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
3 changes: 1 addition & 2 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,7 @@
"import numpy as np\n",
"\n",
"agent_counts = np.zeros((model.grid.width, model.grid.height))\n",
"for cell in model.grid.coord_iter():\n",
" cell_content, x, y = cell\n",
"for cell_content, (x, y) in model.grid.coord_iter():\n",
" agent_count = len(cell_content)\n",
" agent_counts[x][y] = agent_count\n",
"plt.imshow(agent_counts, interpolation=\"nearest\")\n",
Expand Down
5 changes: 2 additions & 3 deletions docs/tutorials/intro_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,14 @@ Now let’s use matplotlib and numpy to visualize the number of agents
residing in each cell. To do that, we create a numpy array of the same
size as the grid, filled with zeros. Then we use the grid object’s
``coord_iter()`` feature, which lets us loop over every cell in the
grid, giving us each cell’s coordinates and contents in turn.
grid, giving us each cell’s positions and contents in turn.

.. code:: ipython3
import numpy as np
agent_counts = np.zeros((model.grid.width, model.grid.height))
for cell in model.grid.coord_iter():
cell_content, x, y = cell
for cell_content, (x, y) in model.grid.coord_iter():
agent_count = len(cell_content)
agent_counts[x][y] = agent_count
plt.imshow(agent_counts, interpolation="nearest")
Expand Down
8 changes: 3 additions & 5 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,9 @@ def __iter__(self) -> Iterator[GridContent]:
as if it is one list:"""
return itertools.chain(*self.grid)

def coord_iter(self) -> Iterator[tuple[GridContent, int, int]]:
"""An iterator that returns coordinates as well as cell contents."""
for row in range(self.width):
for col in range(self.height):
yield self.grid[row][col], row, col # agent, x, y
def coord_iter(self) -> Iterator[tuple[GridContent, Coordinate]]:
"""An iterator that returns positions as well as cell contents."""
return zip(iter(self), itertools.product(range(self.width), range(self.height)))

def neighbor_iter(self, pos: Coordinate, moore: bool = True) -> Iterator[Agent]:
"""Iterate over position neighbors.
Expand Down
11 changes: 5 additions & 6 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,13 @@ def test_coord_iter(self):
# no agent in first space
first = next(ci)
assert first[0] is None
assert first[1] == 0
assert first[2] == 0
assert first[1] == (0, 0)

# first agent in the second space
second = next(ci)
assert second[0].unique_id == 1
assert second[0].pos == (0, 1)
assert second[1] == 0
assert second[2] == 1
assert second[1] == (0, 1)

def test_agent_move(self):
# get the agent at [0, 1]
Expand Down Expand Up @@ -509,8 +507,9 @@ def test_neighbors(self):
class TestIndexing:
# Create a grid where the content of each coordinate is a tuple of its coordinates
grid = Grid(3, 5, True)
for _, x, y in grid.coord_iter():
grid.grid[x][y] = (x, y)
for _, pos in grid.coord_iter():
x, y = pos
grid.grid[x][y] = pos

def test_int(self):
assert self.grid[0][0] == (0, 0)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self, width, height, key1=103, key2=104):
self.schedule = SimultaneousActivation(self)
self.grid = Grid(width, height, torus=True)

for (c, x, y) in self.grid.coord_iter():
for c, pos in self.grid.coord_iter():
x, y = pos
a = MockAgent(x + y * 100, self, x * y * 3)
self.grid.place_agent(a, (x, y))
self.schedule.add(a)
Expand Down

0 comments on commit 5111908

Please sign in to comment.