Skip to content

Commit

Permalink
New recipe addressing #549 (#574)
Browse files Browse the repository at this point in the history
* New recipe addressing #549

* Update recipes.ipynb

* response to review
  • Loading branch information
nwlandry authored Aug 30, 2024
1 parent f25e0ca commit bc3d59e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 24 deletions.
2 changes: 2 additions & 0 deletions tests/core/test_dihypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ def test_cleanup(dihypergraph1):

# test relabel
cleanDH = dihypergraph1.copy()
cleanDH["name"] = "test"
cleanDH.cleanup()
assert set(cleanDH.nodes) == {0, 1, 2, 3}
assert cleanDH.num_edges == 3
Expand All @@ -735,3 +736,4 @@ def test_cleanup(dihypergraph1):
edge_out = {i: e["out"] for i, e in cleanDH._edge.items()}
assert edge_in == xgi.dual_dict(node_out)
assert edge_out == xgi.dual_dict(node_in)
assert cleanDH["name"] == "test"
2 changes: 2 additions & 0 deletions tests/core/test_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,13 @@ def test_cleanup():

# test relabel
cleanH = H.copy()
cleanH["name"] = "test"
cleanH.cleanup(connected=False)
assert set(cleanH.nodes) == {0, 1, 2, 3, 4}
assert cleanH.num_edges == 2
edges = cleanH.edges.members()
assert {0, 1, 2} in edges
assert {3, 4} in edges
assert cleanH["name"] == "test"

assert cleanH._edge == xgi.dual_dict(cleanH._node)
2 changes: 2 additions & 0 deletions tests/core/test_simplicialcomplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,9 @@ def test_cleanup():

# test relabel
cleanSC = SC.copy()
cleanSC["name"] = "test"
cleanSC.cleanup(connected=False)
assert cleanSC["name"] == "test"
assert set(cleanSC.nodes) == {0, 1, 2, 3, 4}
assert cleanSC.num_edges == 5
simplices = cleanSC.edges.members()
Expand Down
92 changes: 76 additions & 16 deletions tutorials/recipes/recipes.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions xgi/core/dihypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,14 @@ def set_edge_attributes(self, values, name=None):
"dict-of-dicts has not been provided."
)

def clear(self, hypergraph_attr=True):
def clear(self, remove_net_attr=True):
"""Remove all nodes and edges from the graph.
Also removes node and edge attributes, and optionally hypergraph attributes.
Parameters
----------
hypergraph_attr : bool, optional
remove_net_attr : bool, optional
Whether to remove hypergraph attributes as well.
By default, True.
Expand All @@ -994,7 +994,7 @@ def clear(self, hypergraph_attr=True):
self._node_attr.clear()
self._edge.clear()
self._edge_attr.clear()
if hypergraph_attr:
if remove_net_attr:
self._net_attr.clear()

def copy(self):
Expand Down
7 changes: 3 additions & 4 deletions xgi/core/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,14 +1195,14 @@ def update(self, *, edges=None, nodes=None):
if edges:
self.add_edges_from(edges)

def clear(self, hypergraph_attr=True):
def clear(self, remove_net_attr=True):
"""Remove all nodes and edges from the graph.
Also removes node and edge attributes, and optionally hypergraph attributes.
Parameters
----------
hypergraph_attr : bool, optional
remove_net_attr : bool, optional
Whether to remove hypergraph attributes as well.
By default, True.
Expand All @@ -1211,7 +1211,7 @@ def clear(self, hypergraph_attr=True):
self._node_attr.clear()
self._edge.clear()
self._edge_attr.clear()
if hypergraph_attr:
if remove_net_attr:
self._net_attr.clear()

def clear_edges(self):
Expand Down Expand Up @@ -1467,7 +1467,6 @@ def cleanup(
_H = self
else:
_H = self.copy()

if not multiedges:
_H.merge_duplicate_edges()
if not singletons:
Expand Down
2 changes: 1 addition & 1 deletion xgi/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def convert_labels_to_integers(net, label_attribute="label", in_place=False):
node_attrs = net._node_attr.copy()
edge_attrs = net._edge_attr.copy()
edges = net._edge.copy()
net.clear()
net.clear(remove_net_attr=False)
net.add_nodes_from((id, deepcopy(node_attrs[n])) for n, id in node_dict.items())
net.set_node_attributes({id: {label_attribute: n} for n, id in node_dict.items()})
if isinstance(net, SimplicialComplex):
Expand Down

0 comments on commit bc3d59e

Please sign in to comment.