Skip to content

Commit c37cb23

Browse files
authored
Merge pull request #721 from xgi-org/fix_720
fix: account for idx=0 when adding edges
2 parents 6adc35a + 9e1e9af commit c37cb23

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

tests/core/test_hypergraph.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,27 @@ def test_cleanup():
10001000
assert cleanH["name"] == "test"
10011001

10021002
assert cleanH._edge == xgi.dual_dict(cleanH._node)
1003+
1004+
1005+
def test_fix_720():
1006+
# idx=0 must be treated as a valid user-provided ID, not as "no ID given"
1007+
1008+
# Hypergraph.add_edge: counter must advance past 0 so the next auto-ID doesn't collide
1009+
H = xgi.Hypergraph()
1010+
H.add_edge([1, 2], idx=0)
1011+
H.add_edge([2, 3])
1012+
assert H.edges.members(dtype=dict) == {0: {1, 2}, 1: {2, 3}}
1013+
1014+
# DiHypergraph.add_edge: same check for directed hypergraphs
1015+
D = xgi.DiHypergraph()
1016+
D.add_edge(([1], [2]), idx=0)
1017+
D.add_edge(([2], [3]))
1018+
assert D.edges.members(dtype=dict) == {0: {1, 2}, 1: {2, 3}}
1019+
1020+
# SimplicialComplex.add_simplex: idx=0 must not be replaced by an auto-generated ID.
1021+
# Use idx=3 first to advance the counter past 0; without the fix, the subsequent
1022+
# add_simplex with idx=0 would silently use the counter value instead.
1023+
S = xgi.SimplicialComplex()
1024+
S.add_simplex([1, 2], idx=3)
1025+
S.add_simplex([3, 4], idx=0)
1026+
assert S.edges.members(dtype=dict)[0] == {3, 4}

xgi/core/dihypergraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def add_edge(self, members, idx=None, **attr):
581581
self._edge_attr[uid] = self._edge_attr_dict_factory()
582582
self._edge_attr[uid].update(attr)
583583

584-
if idx: # set self._edge_uid correctly
584+
if idx is not None: # set self._edge_uid correctly
585585
update_uid_counter(self, idx)
586586

587587
def add_edges_from(self, ebunch_to_add, **attr):

xgi/core/hypergraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def add_edge(self, members, idx=None, **attr):
627627
self._edge_attr[uid] = self._edge_attr_dict_factory()
628628
self._edge_attr[uid].update(attr)
629629

630-
if idx: # set self._edge_uid correctly
630+
if idx is not None: # set self._edge_uid correctly
631631
update_uid_counter(self, idx)
632632

633633
def add_edges_from(self, ebunch_to_add, **attr):

xgi/core/simplicialcomplex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def add_simplex(self, members, idx=None, **attr):
314314
warn(f"uid {idx} already exists, cannot add simplex {members}")
315315
return
316316

317-
idx = next(self._edge_uid) if not idx else idx
317+
idx = next(self._edge_uid) if idx is None else idx
318318

319319
self._add_simplex(members, idx, **attr)
320320

0 commit comments

Comments
 (0)