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

Add eq methods to IDView and IDStat #646

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ def diedgelist2():
return [({0, 1}, {2}), ({1, 2}, {4}), ({2, 3, 4}, {4, 5})]


@pytest.fixture
def diedgelist3():
return [({0}, {1, 2}), ({1}, {2, 4}), ({2, 3, 4}, {4, 5})]


@pytest.fixture
def diedgedict1():
return {0: ({1, 2, 3}, {4}), 1: ({5, 6}, {6, 7, 8})}
Expand Down
19 changes: 19 additions & 0 deletions tests/core/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ def test_neighbors(edgelist1, edgelist2, edgelist7):
assert H3.edges.neighbors(3, s=2) == set()


def test_hypergraph_eq(edgelist2, edgelist3, edgelist4, hyperwithattrs):
H2 = xgi.Hypergraph(edgelist2)
H3 = xgi.Hypergraph(edgelist3)
assert H2.nodes != H3.nodes
assert H2.edges != H3.edges

H4 = xgi.Hypergraph(edgelist4)
assert H4.nodes != hyperwithattrs.nodes
assert H4.edges == hyperwithattrs.edges


def test_dihypergraph_eq(diedgelist2, diedgelist3, dihyperwithattrs):
H2 = xgi.DiHypergraph(diedgelist2)
H3 = xgi.DiHypergraph(diedgelist3)
assert H2.nodes != H3.nodes
assert H2.edges != H3.edges
assert H2.edges != dihyperwithattrs.edges


def test_edge_order(edgelist3, diedgelist1):
# undirected
H = xgi.Hypergraph(edgelist3)
Expand Down
12 changes: 12 additions & 0 deletions tests/stats/test_core_stats_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ def test_dihypergraph_stats_items(diedgelist2):
assert deg[n] == d


def test_hypergraph_eq(edgelist2, edgelist3):
H2 = xgi.Hypergraph(edgelist2)
H3 = xgi.Hypergraph(edgelist3)
assert H2.nodes.degree != H3.nodes.degree


def test_dihypergraph_eq(diedgelist2, diedgelist3):
H2 = xgi.DiHypergraph(diedgelist2)
H3 = xgi.DiHypergraph(diedgelist3)
assert H2.nodes.out_degree != H3.nodes.out_degree


### Numerical statistics


Expand Down
21 changes: 21 additions & 0 deletions xgi/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ def __call__(self, bunch):
"""
return self.from_view(self, bunch)

def __eq__(self, view):
"""Checks whether the members and attributes of an IDView are equal.

Parameters
----------
view : IDView
the IDView to be compared

Returns
-------
bool
Whether they are equal
"""
return (
{idx: self._id_dict[idx] for idx in self._ids}
== {idx: view._id_dict[idx] for idx in view._ids}
) and (
{idx: self._id_attr[idx] for idx in self._ids}
== {idx: view._id_attr[idx] for idx in view._ids}
)

def filterby(self, stat, val, mode="eq"):
"""Filter the IDs in this view by a statistic.

Expand Down
7 changes: 5 additions & 2 deletions xgi/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def __repr__(self):
def __len__(self):
return len(self.view)

def __eq__(self, stat):
return self.asdict() == stat.asdict()

@property
def name(self):
"""Name of this stat.
Expand Down Expand Up @@ -141,12 +144,12 @@ def asdict(self):

"""
val = self._val
return {n: val[n] for n in self.view}
return {idx: val[idx] for idx in self.view}

def aslist(self):
"""Output the stat as a list."""
val = self._val
return [val[n] for n in self.view]
return [val[idx] for idx in self.view]

def asnumpy(self):
"""Output the stat as a numpy array."""
Expand Down
Loading