Repository: projectmesa/mesa
Pull request #1542 (2022-12-02 00:42:15)

breaking: NetworkGrid: modify get_neighbors and create get_neighborhood
NetworkGrid didn't have a get_neighbors function which returns the adjacent agents before this PR, but I think it's an important part of the API, unfortunately it has already a get_neighbors function which returns ids, but clearly changing it would be a breaking change. 

I think if we want to break it in 2.0 we could rename get_neighbors to get_neighborhood and rename this function to get_neighbors. Is it better to include this PR now as it is and make a separate PR with the breaking change or modify this one with the proposed changes?

codecov[bot]: # [Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1542?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) Report
Base: **91.28**% // Head: **91.15**% // Decreases project coverage by **`-0.13%`** :warning:
> Coverage data is based on head [(`9aa1215`)](https://codecov.io/gh/projectmesa/mesa/pull/1542?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) compared to base [(`c9f10a6`)](https://codecov.io/gh/projectmesa/mesa/commit/c9f10a64be30247923a6381fdb378a75285fac52?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).
> Patch coverage: 50.00% of modified lines in pull request are covered.

> :exclamation: Current head 9aa1215 differs from pull request most recent head a606bfa. Consider uploading reports for the commit a606bfa to get more accurate results

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1542      +/-   ##
==========================================
- Coverage   91.28%   91.15%   -0.14%     
==========================================
  Files          15       15              
  Lines        1308     1311       +3     
  Branches      229      229              
==========================================
+ Hits         1194     1195       +1     
- Misses         80       82       +2     
  Partials       34       34              
```


| [Impacted Files](https://codecov.io/gh/projectmesa/mesa/pull/1542?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) | Coverage Δ | |
|---|---|---|
| [mesa/space.py](https://codecov.io/gh/projectmesa/mesa/pull/1542/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa#diff-bWVzYS9zcGFjZS5weQ==) | `95.30% <50.00%> (-0.49%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1542?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).

rht: It would be less confusing to do this all at once for 2.0, instead of defining a new temporary API `get_agent_neighbors`, only for it to be renamed. I have updated #1511 to include this PR.
Tortar: ok @rht, not it should be ready to be merged in 2.0
rht: I have rebased the PR, resolving the merge conflicts, and create https://github.com/projectmesa/mesa-examples/pull/40 for the examples update. Merging; thank you @Tortar.

Patch:
diff --git a/mesa/space.py b/mesa/space.py
index 0820aee3..abdb1869 100644
--- a/mesa/space.py
+++ b/mesa/space.py
@@ -1069,22 +1069,27 @@ class NetworkGrid:
         self.G.nodes[node_id]["agent"].append(agent)
         agent.pos = node_id
 
-    def get_neighbors(
+    def get_neighborhood(
         self, node_id: int, include_center: bool = False, radius: int = 1
     ) -> list[int]:
         """Get all adjacent nodes within a certain radius"""
         if radius == 1:
-            neighbors = list(self.G.neighbors(node_id))
+            neighborhood = list(self.G.neighbors(node_id))
             if include_center:
-                neighbors.append(node_id)
+                neighborhood.append(node_id)
         else:
             neighbors_with_distance = nx.single_source_shortest_path_length(
                 self.G, node_id, radius
             )
             if not include_center:
                 del neighbors_with_distance[node_id]
-            neighbors = sorted(neighbors_with_distance.keys())
-        return neighbors
+            neighborhood = sorted(neighbors_with_distance.keys())
+        return neighborhood
+
+    def get_neighbors(self, node_id: int, include_center: bool = False) -> list[Agent]:
+        """Get all agents in adjacent nodes."""
+        neighborhood = self.get_neighborhood(node_id, include_center)
+        return self.get_cell_list_contents(neighborhood)
 
     def move_agent(self, agent: Agent, node_id: int) -> None:
         """Move an agent from its current node to a new node."""


Test patch:
diff --git a/tests/test_space.py b/tests/test_space.py
index a7aba252..c445135c 100644
--- a/tests/test_space.py
+++ b/tests/test_space.py
@@ -354,10 +354,10 @@ class TestSingleNetworkGrid(unittest.TestCase):
             assert a.pos == pos
 
     def test_get_neighbors(self):
-        assert len(self.space.get_neighbors(0, include_center=True)) == 3
-        assert len(self.space.get_neighbors(0, include_center=False)) == 2
-        assert len(self.space.get_neighbors(2, include_center=True, radius=3)) == 7
-        assert len(self.space.get_neighbors(2, include_center=False, radius=3)) == 6
+        assert len(self.space.get_neighborhood(0, include_center=True)) == 3
+        assert len(self.space.get_neighborhood(0, include_center=False)) == 2
+        assert len(self.space.get_neighborhood(2, include_center=True, radius=3)) == 7
+        assert len(self.space.get_neighborhood(2, include_center=False, radius=3)) == 6
 
     def test_move_agent(self):
         initial_pos = 1
@@ -426,11 +426,11 @@ class TestMultipleNetworkGrid(unittest.TestCase):
 
     def test_get_neighbors(self):
         assert (
-            len(self.space.get_neighbors(0, include_center=True))
+            len(self.space.get_neighborhood(0, include_center=True))
             == TestMultipleNetworkGrid.GRAPH_SIZE
         )
         assert (
-            len(self.space.get_neighbors(0, include_center=False))
+            len(self.space.get_neighborhood(0, include_center=False))
             == TestMultipleNetworkGrid.GRAPH_SIZE - 1
         )