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

Cannot add attributes to existing edges #241

Closed
tlarock opened this issue Dec 5, 2022 · 5 comments
Closed

Cannot add attributes to existing edges #241

tlarock opened this issue Dec 5, 2022 · 5 comments

Comments

@tlarock
Copy link
Collaborator

tlarock commented Dec 5, 2022

I want to add attributes to edges after I have created a hypergraph with H = xgi.Hypergraph(incoming_data=hyperedge_list) and I do not think it is possible right now (please correct if I am wrong).

Note that this issue is only relevant when the hypergraph is constructed first, then edge attributes added later. I could instead create an empty hypergraph and add edges one at a time with the relevant attributes (this is what I will do for now). In fact this would save me looping over the edges a second time after construction. However, in my particular use case I would prefer the semantics I describe because I think (1) they are more clear/general and (ii) would allow me to add attributes without constructing a new hypergraph. But the functionality is not sctrictly necessary.

It is possible to add or update attributes of existing nodes with H.add_node(existing_node, **attr), since per the docs

If node is already in the hypergraph, its attributes are still updated.

However, the same does not work for edges, because H.add_edge(existing_edge_members, **attr) creates a multi-edge, and H.add_edge(edge_id, **attr) fails because H.add_edge expects an iterable representing a hyperedge as its first argument, not an edge ID.

The simplest fix I can think of is to allow H.add_edge to accept an edge ID, check if the edge exists and if so modify provided attributes, or raise an error if the edge ID is not present. However, I think that overloads add_edge in a confusing way (already the case with add_node), so it may be better to define new functions for attribute addition/updates, i.e.,

H.update_node_attributes(node, **attr) and H.update_edge_attributes(edge_id, **attr),

with the edge function accepting an edge ID rather than the hyperedge itself. These functions might return an error if the node/edge does not exist and/or a warning if the attribute does not already exist for the node/edge, both of which could be managed with boolean arguments to the functions. This setup pushes the multi-edge issue onto the user, since whether multi-edges should have the same attributes is generally ambiguous.

I think this is related to discussions in #126 and #175.

@leotrs
Copy link
Collaborator

leotrs commented Dec 5, 2022

I believe you can do this with H.add_edges_from to add all attributes to all existing edges using "format 4" here.

I agree this is far from optimal though and I kinda hate that right now we have to use add_node to add attributes for existing nodes, and yes, for edges it's even worse :(.

@nwlandry
Copy link
Collaborator

nwlandry commented Dec 5, 2022

@tlarock
Copy link
Collaborator Author

tlarock commented Dec 5, 2022

Yes @nwlandry I think that is very close to what I want, only difference is I was looking for something that operated on a single edge at a time rather than a dictionary of edges, but that is fine.

Perhaps this should be added to "See Also" for xgi.Hypergraph.add_edge etc.? I was looking through the doucmentation for Hypergraph and never saw this function, looks like it only appears in the documentation of xgi.Hypergraph.merge_duplicate_edges right now.

@nwlandry
Copy link
Collaborator

nwlandry commented Dec 5, 2022

These are great suggestions. Let's keep this issue open until I implement your suggestions.

@nwlandry
Copy link
Collaborator

I added See Also links to the docs and we moved the add/remove attribute functionality to the Hypergraph class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants