Skip to content

Commit

Permalink
breaking: Change coord_iter to return tuple[content, pos]
Browse files Browse the repository at this point in the history
Co-authored-by: rht <rhtbot@protonmail.com>
  • Loading branch information
Tortar and rht committed Jun 21, 2023
1 parent 0db8f7c commit 386d49a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
"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 positions and contents in turn."
]
},
{
Expand All @@ -805,8 +805,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",
"# Plot using seaborn, with a size of 5x5\n",
Expand Down
6 changes: 3 additions & 3 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ 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."""
def coord_iter(self) -> Iterator[tuple[GridContent, Coordinate]]:
"""An iterator that returns positions 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
yield self._grid[row][col], (row, col) # agent, position

def iter_neighborhood(
self,
Expand Down
11 changes: 5 additions & 6 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,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 @@ -480,8 +478,9 @@ def test_neighbors(self):
class TestIndexing:
# Create a grid where the content of each coordinate is a tuple of its coordinates
grid = SingleGrid(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 @@ -40,7 +40,8 @@ def __init__(self, width, height, key1=103, key2=104):
self.schedule = SimultaneousActivation(self)
self.grid = MultiGrid(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 386d49a

Please sign in to comment.