-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Question: MultiGraph with to_dict_of_dicts with edge data #4305
Comments
Hi @rossbar. The original code in convert.py directly replace edge_data with original edge attributes. When the input is multiGraph, it is natural that we lose the multi-edge information. # original version
# edge_data is not None
for u, nbrdict in G.adjacency():
dod[u] = dod.fromkeys(nbrdict, edge_data) I think the naive method to solve this issue is to consider Single-Graph and Multi-Graphs separately. We should keep different edge keys in MultiGraphs, like the modifications below. Hope to get any feedback or good ideas. Thank you. # revised version draft
# edge_data is not None
for u, nbrdict in G.adjacency():
if 'Multi' in nx.info(G).split("\n", 2)[1]: # multi-graphs
udict = {}
for nbr, edgedict in nbrdict.items():
udict[nbr] = dict.fromkeys(edgedict, edge_data)
dod[u] = udict
else: # single graph
dod[u] = dod.fromkeys(nbrdict, edge_data) |
First of all, the docs are incorrect when they say that I suspect historically this was created for symmetry with This is an artifact that shows our initial neglect of multigraphs and edge attributes. I think v3.0 and a reconsideration of the matrix interface might give us a chance to enrich the treatment of attributes. For example, requiring We could also remove the function -- or at least the |
After more thought: Let's change the doc_string of
to a more accurate and help version including a one-liner to build your own:
Maybe the one-liner should be used in an example in the doc_string. |
I was reviewing the tests for
nx.convert.to_dict_of_dicts
which led to a question about what the expected behavior ofnx.to_dict_of_dicts(G)
is when G is a MultiGraph andedge_data
is not None.Things seem to make sense when
edge_data
is None:In the above example, if I call
nx.to_dict_of_dicts(G, edge_data={'weight': 1})
, my naive expectation was that i would getInstead, you get:
which seems to clobber the multiple edges. The code doesn't have any special cases for MultiGraphs, and I couldn't tell from there and/or docstring whether this behavior is expected.
I expect I'm missing something, so I thought I'd ask! Adding examples to the docstring would also be a nice improvement to show the expected behavior.
The text was updated successfully, but these errors were encountered: