Skip to content

Commit

Permalink
Deprecate neighbor_iter in favor of iter_neighbors 🇺🇦
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Mar 12, 2022
1 parent 68b6631 commit bcb71ed
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def step(self):
A choice is made at random in case of a tie
The next state is stored until all cells have been polled
"""
neighbor_iter_ = self.model.grid.neighbor_iter((self._row, self._col), True)
neighbors_opinion = Counter(n.get_state() for n in neighbor_iter_)
_neighbor_iter = self.model.grid.iter_neighbors((self._row, self._col), True)
neighbors_opinion = Counter(n.get_state() for n in _neighbor_iter)
# Following is a a tuple (attribute, occurrences)
polled_opinions = neighbors_opinion.most_common()
tied_opinions = []
Expand Down
2 changes: 1 addition & 1 deletion examples/conways_game_of_life/conways_game_of_life/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def isAlive(self):

@property
def neighbors(self):
return self.model.grid.neighbor_iter((self.x, self.y), True)
return self.model.grid.iter_neighbors((self.x, self.y), True)

def step(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion examples/forest_fire/forest_fire/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def step(self):
If the tree is on fire, spread it to fine trees nearby.
"""
if self.condition == "On Fire":
for neighbor in self.model.grid.neighbor_iter(self.pos):
for neighbor in self.model.grid.iter_neighbors(self.pos, True):
if neighbor.condition == "Fine":
neighbor.condition = "On Fire"
self.condition = "Burned Out"
2 changes: 1 addition & 1 deletion examples/hex_snowflake/hex_snowflake/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def isAlive(self):

@property
def neighbors(self):
return self.model.grid.neighbor_iter((self.x, self.y))
return self.model.grid.iter_neighbors((self.x, self.y))

@property
def considered(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/schelling/analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
" segregated_agents = 0\n",
" for agent in model.schedule.agents:\n",
" segregated = True\n",
" for neighbor in model.grid.neighbor_iter(agent.pos):\n",
" for neighbor in model.grid.iter_neighbors(agent.pos, True):\n",
" if neighbor.type != agent.type:\n",
" segregated = False\n",
" break\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, pos, model, agent_type):

def step(self):
similar = 0
for neighbor in self.model.grid.neighbor_iter(self.pos):
for neighbor in self.model.grid.iter_neighbors(self.pos, True):
if neighbor.type == self.type:
similar += 1

Expand Down
20 changes: 15 additions & 5 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,13 @@ def neighbor_iter(
moore: Boolean for whether to use Moore neighborhood (including
diagonals) or Von Neumann (only up/down/left/right).
"""
neighborhood = self.get_neighborhood(pos, moore=moore)
return self.iter_cell_list_contents(neighborhood)
from warnings import warn

warn(
"`neighbor_iter` is deprecated in favor of `iter_neighbors` "
"and will be removed in the subsequent version."
)
return self.iter_neighbors(pos, moore)

def iter_neighborhood(
self,
Expand Down Expand Up @@ -606,7 +611,7 @@ class HexGrid(Grid):
Methods:
get_neighbors: Returns the objects surrounding a given cell.
get_neighborhood: Returns the cells surrounding a given cell.
neighbor_iter: Iterates over position neighbors.
iter_neighbors: Iterates over position neighbors.
iter_neighborhood: Returns an iterator over cell coordinates that are
in the neighborhood of a certain point.
"""
Expand Down Expand Up @@ -679,8 +684,13 @@ def neighbor_iter(self, pos: Coordinate) -> Iterator[GridContent]:
Args:
pos: (x,y) coords tuple for the position to get the neighbors of.
"""
neighborhood = self.iter_neighborhood(pos)
return self.iter_cell_list_contents(neighborhood)
from warnings import warn

warn(
"`neighbor_iter` is deprecated in favor of `iter_neighbors` "
"and will be removed in the subsequent version."
)
return self.iter_neighbors(pos)

def get_neighborhood(
self, pos: Coordinate, include_center: bool = False, radius: int = 1
Expand Down

0 comments on commit bcb71ed

Please sign in to comment.