Skip to content

bugfix-for-issue-4353: modify default edge_id format - #4842

Merged
rossbar merged 7 commits into
networkx:mainfrom
berlincho:graphML_edge_id
Jun 14, 2021
Merged

rossbar merged 7 commits into
networkx:mainfrom
berlincho:graphML_edge_id

Conversation

@berlincho

Copy link
Copy Markdown
Contributor

The issue is about failing to preserve edge global uniqueness while calling write_graphml() in MultiGraph format with the keys field.

My idea is to write the edge_id in id = (src, dest, key) format, instead of id = key. While selecting one of the edge attributes to be the edge_id might be viable, it would cause the loss of local uniqueness when reading the graphML.

This is the first time that I open a pull request in this project. Happy to discuss the issue with the contributors.

@dschult

dschult commented May 27, 2021

Copy link
Copy Markdown
Member

Thanks for the PR!

I don't understand the concern about global uniqueness. If the user is able to select the edge attribute used for the edge_id, they can make it unique on whatever scale is needed. If someone prefers to use (rsc, dst, id), then they can create an edge attribute with that string and use it for the edge_id.

MG = nx.MultiGraph([(1, 2), (2, 3), (1, 2)])
nx.set_edge_attributes(MG, {e: str(e) for e in MG.edges}, "eid")
nx.write_graphml(MG, path, graphml_edge_id_attribute="eid")

@berlincho

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion!!

I agree that it will be flexible with the selection of edge_attribute. (e.g. edge_id=data[edge_attribute]). I will follow the idea to fix #4353. My original concern is that the argument key (current ver. edge_id=str(key)) will be overwritten by edge attribute and cannot be recovered. But later I think it's not the issue at all since it depends on users' settings.

BTW, I have no idea what the cause of build errors in the CI pipeline is. Do I miss something important?

@jarrodmillman

Copy link
Copy Markdown
Member

The circleci build error is confusing. It is using an old setup to run, but it looks like your branch has the new stuff.

For instance, it is installing dependencies for cartopy. But we don't do that now and you seem to have the new circleci config that doesn't install those dependencies. Circleci is also installing Sphinx==3.5.4, but we now require Sphinx~=4.0. The newer version of Sphinx should fix the problem.

@rossbar

rossbar commented May 27, 2021 •

Copy link
Copy Markdown
Contributor

I think circleci itself recently had a "release" where a bunch of new features were added - it looks like this PR may have just been caught up in some hiccups related to circleCI's changes - I agree that there's no reason why this should've had problems.

Hopefully things are settling down a bit now - restarting the workflow seems to have done the trick

@berlincho

Copy link
Copy Markdown
Contributor Author

Thanks for your support for the circleCI build errors.

I remove my previous implementation and add one extra input argument, edge_id_from_attribute, in graphML_writer objects and functions with some comments. I also try to add one test for this update. Users can assign one of the edge attributes for edge id in MultiGraph.

The usage like below

G = nx.MultiDiGraph(((0, 1, 0, {"eid": 1}), (0, 1, 1, {"eid": 2}), (1, 2, 0, {"eid": 3}), (2, 0, 0, {"eid": 4})))
nx.write_graphml(G, path, edge_id_from_attribute="eid")
nx.generate_graphml(G, edge_id_from_attribute="eid")

@dschult

dschult commented Jun 3, 2021

Copy link
Copy Markdown
Member

The new input argument should be made the last argument to avoid messing up the calling order with legacy code.

It looks like you only use this functionality for MultiGraph inputs. Can you extend it to work for the other types: Graph, ..., ...

What does the named_key_ids argument do and how is it different from this argument?

@berlincho

Copy link
Copy Markdown
Contributor Author

I update the code to move the new input argument to the last and also extend the functionality to non-MultiGraph objects (e.g. Graph, DiGraph).

Regarding the testing, I split MultiGraph and simple Graph into two different cases since networkX handles edge_id differently while calling read_graphml(). Specifically, in Simple Graph, read_graphml() adds explicit edge_id as attribute named id, while in MultiGraph, read_graphml() uses edge_id as edge_key.

I think named_key_ids (Boolean) is a different feature. Here is a simple example (stackoverflow link). The default key for edge/node attribute starts from d0, d1, d2 (Reference). If the argument sets True, the key would keep the same name as the attr.name.

@berlincho

Copy link
Copy Markdown
Contributor Author

Hi @dschult, is there anyone who could take a look at my fix? Thanks for your time.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ready to merge.
Thanks very much!

@dschult dschult added this to the networkx-2.6 milestone Jun 12, 2021
@dschult dschult assigned rossbar and unassigned rossbar Jun 12, 2021
@dschult
dschult requested a review from rossbar June 12, 2021 15:12

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation + tests LGTM. I think the parameter description in the docstring needs to clarified a bit though as I wasn't able to understand what this new parameter does without reading the code itself. See comment for further discussion.

Comment thread networkx/readwrite/graphml.py Resolved

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thanks @berlincho !

@rossbar
rossbar merged commit b442619 into networkx:main Jun 14, 2021
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
Allow selection of an edge attribute to be the edge id when writing GraphML.
Adds edge_id_from_attribute kwarg.
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
Allow selection of an edge attribute to be the edge id when writing GraphML.
Adds edge_id_from_attribute kwarg.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants