Skip to content

Commit 3a89c2c

Browse files
committed
Pull request pnnl#157: Fix bugs in _add_item; add tests for add_edge
Merge in HYP/hypernetx from bugfix/add-edge to develop * commit '857130acb010befd6b47e432a8225a7f98219672': Fix bugs in _add_item; add tests for add_edge
2 parents d42a698 + 857130a commit 3a89c2c

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

hypernetx/classes/hypergraph.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# All rights reserved.
33
from __future__ import annotations
44

5+
import random
56
import warnings
67

78
warnings.filterwarnings("default", category=DeprecationWarning)
@@ -1216,7 +1217,7 @@ def add_incidences_from(self, incidences, inplace=True):
12161217

12171218
def _add_item(self, uid, level=2, data=None, inplace=True):
12181219
data = data or {}
1219-
self._add_items_from([(uid, data)], level=level, inplace=inplace)
1220+
return self._add_items_from([(uid, data)], level=level, inplace=inplace)
12201221

12211222
def _add_items_from(self, items, level=2, inplace=True):
12221223
"""Items must be a list of uids and/or tuples
@@ -1243,10 +1244,12 @@ def _add_items_from(self, items, level=2, inplace=True):
12431244
df.properties, edge_ps=ep, node_ps=np, name=self.name
12441245
)
12451246
return self
1246-
else:
1247-
return self._construct_hyp_from_stores(
1248-
df.properties, edge_ps=ep, node_ps=np, name=name
1249-
)
1247+
return self._construct_hyp_from_stores(
1248+
df.properties,
1249+
edge_ps=ep,
1250+
node_ps=np,
1251+
name=f"{self.name}-{random.randint(1, 100)}",
1252+
)
12501253

12511254
#### This should follow behavior of restrictions
12521255
def remove_edges(self, keys, name=None, inplace=True):

hypernetx/classes/tests/test_hypergraph.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,46 @@ def test_hypergraph_from_bipartite(sbsd_hypergraph):
6969
assert len(HB.nodes) == 8
7070

7171

72-
@pytest.mark.skip("Deprecated method; will support in later release")
73-
def test_add_node_to_edge(sbs):
74-
H = Hypergraph(sbs.edgedict)
75-
assert H.shape == (7, 6)
76-
node = "B"
77-
edge = "P"
78-
H.add_node_to_edge(node, edge)
79-
assert H.shape == (8, 6)
80-
# add edge with nodes already in hypergraph
81-
H.add_edge({"Z": ["A", "B"]})
82-
assert H.shape == (8, 7)
83-
# add edge not in hypergraph with nodes not in hypergraph
84-
H.add_edge({"Y": ["M", "N"]})
85-
assert H.shape == (10, 8)
72+
def test_add_edge_inplace(sbs):
73+
h = Hypergraph(sbs.edgedict)
74+
assert h.shape == (7, 6)
75+
76+
# add a new edge in place; i.e. the current hypergraph should be mutated
77+
new_edge = "X"
78+
h.add_edge(new_edge)
79+
80+
# the Hypergraph should not increase its number of edges and incidences because the current behavior of adding
81+
# an edge does not connect two or more nodes.
82+
# In other words, adding an edge with no nodes
83+
assert h.shape == (7, 6)
84+
assert new_edge not in h.edges.elements
85+
86+
# the new edge has no user-defined property data, so it should not be listed in the PropertyStore
87+
assert new_edge not in h.edges.properties
88+
89+
# However, the new_edge will be listed in the complete list of all user and non-user-define properties for all edges
90+
assert new_edge in h.edges.to_dataframe.index.tolist()
91+
92+
assert new_edge in h.edges.to_dataframe.index.tolist()
93+
94+
95+
def test_add_edge_not_inplace(sbs):
96+
h = Hypergraph(sbs.edgedict)
97+
assert h.shape == (7, 6)
98+
99+
# add a new edge not in place; the current hypergraph should be diffrent from the new hypergraph
100+
# created from add_edge
101+
new_edge = "X"
102+
new_hg = h.add_edge(new_edge, inplace=False)
103+
104+
assert new_hg.shape == (7, 6)
105+
assert new_edge not in new_hg.edges.elements
106+
107+
assert new_edge not in new_hg.edges.properties
108+
assert new_edge in new_hg.edges.to_dataframe.index.tolist()
109+
110+
# verify that the new edge is not in the old HyperGraph
111+
assert new_edge not in h.edges.to_dataframe.index.tolist()
86112

87113

88114
def test_remove_edges(sbs):

0 commit comments

Comments
 (0)