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

Implement set operations for NodeView and EdgeView #208

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Feat: now NodeViews and EdgeViews support set operations. Fixes #206.
  • Loading branch information
leotrs committed Nov 1, 2022
commit a99ce1589997d776e9c90a0d171f5b24326e8d2a
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