Skip to content

Commit

Permalink
Feat: now NodeViews and EdgeViews support set operations. Fixes #206.
Browse files Browse the repository at this point in the history
  • Loading branch information
leotrs committed Nov 1, 2022
1 parent 93320f5 commit a99ce15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/classes/test_reportviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,23 @@ def test_bool(edgelist1):
assert bool(H.edges) is False
H = xgi.Hypergraph(edgelist1)
assert bool(H.edges) is True


def test_set_operations(hyperwithattrs):
H = hyperwithattrs

nodes1 = H.nodes.filterby_attr("color", "blue")
nodes2 = H.nodes.filterby("degree", 2, "geq")
assert set(nodes2 - nodes1) == {3, 4}
assert set(nodes1 - nodes2) == set()
assert set(nodes1 & nodes2) == {2, 5}
assert set(nodes1 | nodes2) == {2, 3, 4, 5}
assert set(nodes1 ^ nodes2) == {3, 4}

edges1 = H.edges
edges2 = H.edges.filterby("size", 3, "leq")
assert set(edges2 - edges1) == set()
assert set(edges1 - edges2) == {1}
assert set(edges1 & edges2) == {0, 2}
assert set(edges1 | edges2) == {0, 1, 2}
assert set(edges1 ^ edges2) == {1}
9 changes: 9 additions & 0 deletions xgi/classes/reportviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ def from_view(cls, view, bunch=None):
newview._ids = bunch
return newview

def _from_iterable(self, it):
"""Construct an instance of the class from any iterable input.
This overrides collections.abc.Set._from_iterable, which is in turn used to
implement set operations such as &, |, ^, -.
"""
return self.from_view(self, it)


class NodeView(IDView):
"""An IDView that keeps track of node ids.
Expand Down

0 comments on commit a99ce15

Please sign in to comment.