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

Fix to #171 #172

Merged
merged 2 commits into from
Sep 14, 2022
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
17 changes: 7 additions & 10 deletions benchmarks/benchmarks/bench_core_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ def setup(self):

def time_edgelist_construction(self):
xgi.Hypergraph(self.disgene_edgelist)

def time_edgedict_construction(self):
xgi.Hypergraph(self.disgene_edgedict)

def time_df_construction(self):
xgi.Hypergraph(self.disgene_df)

def time_node_memberships(self):
[
self.hypergraph.nodes.memberships(n)
for n in self.hypergraph.nodes
]
[self.hypergraph.nodes.memberships(n) for n in self.hypergraph.nodes]

def time_edge_members(self):
[self.hypergraph.edges.members(e) for e in self.hypergraph.edges]
Expand All @@ -47,12 +44,12 @@ def time_edge_size(self):

def time_isolates(self):
self.hypergraph.nodes.isolates()

def time_singletons(self):
self.hypergraph.edges.singletons()

def time_copy(self):
self.hypergraph.copy()

def time_dual(self):
self.hypergraph.dual()
self.hypergraph.dual()
3 changes: 2 additions & 1 deletion tests/classes/test_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ def test_hypergraph_attr(edgelist1):
assert H["color"] == "red"


def test_members(edgelist1):
def test_memberships(edgelist1):
H = xgi.Hypergraph(edgelist1)
assert H.nodes.memberships(1) == {0}
assert H.nodes.memberships(2) == {0}
assert H.nodes.memberships(3) == {0}
assert H.nodes.memberships(4) == {1}
assert H.nodes.memberships(6) == {2, 3}
assert H.nodes([1, 2, 6]).memberships() == {1: {0}, 2: {0}, 6: {2, 3}}
with pytest.raises(IDNotFound):
H.nodes.memberships(0)
with pytest.raises(TypeError):
Expand Down
11 changes: 9 additions & 2 deletions xgi/classes/reportviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,17 @@ def __init__(self, H, bunch=None):
def memberships(self, n=None):
"""Get the edge ids of which a node is a member.

Gets all the node memberships for all nodes in the view if n
not specified.

Parameters
----------
n : hashable
Node ID.

Returns
-------
set
dict of sets if n is None, otherwise a set
Edge memberships.

Raises
Expand All @@ -545,7 +548,11 @@ def memberships(self, n=None):
If `n` is not hashable or if it is not in the hypergraph.

"""
return self._id_dict.copy() if n is None else self._id_dict[n].copy()
return (
{key: self._id_dict[key] for key in self}
if n is None
else self._id_dict[n].copy()
)

def isolates(self, ignore_singletons=True):
"""Nodes that belong to no edges.
Expand Down