xgi-org / xgi

CompleX Group Interactions (XGI) is a Python package for higher-order networks.
https://xgi.readthedocs.io
Other
186 stars 30 forks source link

bug: add_edge removes other edge #224

Closed maximelucas closed 1 year ago

maximelucas commented 2 years ago

Create a Hypergraph:

import xgi
H = xgi.Hypergraph({0: {1, 2}, 1: {1, 4}, 
                     2: {0, 1, 8}, 3: {0, 3, 5}})
H._edge
# -> {0: {1, 2}, 1: {1, 4}, 2: {0, 1, 8}, 3: {0, 3, 5}} # all fine

Then remove an edge

H.remove_edge(2)
H._edge
# -> {0: {1, 2}, 1: {1, 4}, 3: {0, 3, 5}} # all fine

But then, when adding a new hyperedge, [1,9,2], it is assigned the ID 0 already in use, and the previous hyperedge with ID 0 has disappeared:

H.add_edge([1,9,2])
H._edge
# {0: {1, 2, 9}, 1: {1, 4}, 3: {0, 3, 5}} # wrong

It should just be assigned a new ID, probably 4. Not sure what is causing this yet.

Instead, specifying the ID explicitly works, and gives the expected result, but the above is still wrong:

H.add_edge([1,9,2], id=4)
H._edge
# -> {0: {1, 2}, 1: {1, 4}, 3: {0, 3, 5}, 4: {1, 2, 9}} # correct
leotrs commented 2 years ago

When we pass in edge data to the constructor, the self._edge_uid is not being initializer correctly. It always starts at zero.

maximelucas commented 2 years ago

Exactly, but only in the {id : members} format apparently

H = xgi.Hypergraph({0: {1, 2}, 1: {1, 4}, 2: {0, 1, 8}, 3: {0, 3, 5}})
H._edge_uid
# -> count(0) # wrong

But

H = xgi.Hypergraph([{1, 2}, {1, 4}, {0, 1, 8}, {0, 3, 5}])
H._edge_uid
# -> count(4) # correct
leotrs commented 2 years ago

convert_to_hypergraph calls H.add_edges_from which in turn increments _edge_uid but ONLY when the incoming_data is a list. All other cases use different mechanisms to add the edges so count is not incremented.

maximelucas commented 2 years ago

Yep, my conclusion too.