Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added s parameter to neighbors #450

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions tests/core/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
from xgi.exception import IDNotFound, XGIError


def test_neighbors(edgelist1, edgelist2):
el1 = edgelist1
el2 = edgelist2
H1 = xgi.Hypergraph(el1)
H2 = xgi.Hypergraph(el2)
def test_neighbors(edgelist1, edgelist2, edgelist7):
H1 = xgi.Hypergraph(edgelist1)
H2 = xgi.Hypergraph(edgelist2)
H3 = xgi.Hypergraph(edgelist7)
assert H1.nodes.neighbors(1) == {2, 3}
assert H1.nodes.neighbors(4) == set()
assert H1.nodes.neighbors(6) == {5, 7, 8}
Expand All @@ -20,6 +19,10 @@ def test_neighbors(edgelist1, edgelist2):
assert H1.edges.neighbors(1) == set()
assert H1.edges.neighbors(2) == {3}
assert H1.edges.neighbors(3) == {2}
assert H3.edges.neighbors(0, s=2) == {1}
assert H3.edges.neighbors(1, s=2) == {0, 2}
assert H3.edges.neighbors(2, s=2) == {1}
assert H3.edges.neighbors(3, s=2) == set()


def test_edge_order(edgelist3):
Expand Down
20 changes: 16 additions & 4 deletions xgi/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def filterby_attr(self, attr, val, mode="eq", missing=None):
)
return type(self).from_view(self, bunch)

def neighbors(self, id):
def neighbors(self, id, s=1):
"""Find the neighbors of an ID.

The neighbors of an ID are those IDs that share at least one bipartite ID.
Expand All @@ -339,6 +339,10 @@ def neighbors(self, id):
----------
id : hashable
ID to find neighbors of.
s : int, optional
The intersection size s for two edges or nodes to be considered neighbors.
By default, 1.

Returns
-------
set
Expand All @@ -359,9 +363,17 @@ def neighbors(self, id):
{1, 3, 4}

"""
return {i for n in self._id_dict[id] for i in self._bi_id_dict[n]}.difference(
{id}
)
if s == 1:
return {
i for n in self._id_dict[id] for i in self._bi_id_dict[n]
}.difference({id})
else:
return {
i
for n in self._id_dict[id]
for i in self._bi_id_dict[n]
if len(self._id_dict[id].intersection(self._id_dict[i])) >= s
}.difference({id})

def duplicates(self):
"""Find IDs that have a duplicate.
Expand Down
Loading