Skip to content

Commit

Permalink
to_networkx_graph needs .update() instead of new dict
Browse files Browse the repository at this point in the history
In convert.to_networkx_graph, we need to use
create_using.node.update() instead of creating a new dict
for the node atrributes. We were overwriting instead of updating.
  • Loading branch information
dschult committed Aug 7, 2015
1 parent c06c618 commit e10ecc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions networkx/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def to_networkx_graph(data,create_using=None,multigraph_input=False):
result= from_dict_of_dicts(data.adj,\
create_using=create_using,\
multigraph_input=data.is_multigraph())
if hasattr(data,'graph') and isinstance(data.graph,dict):
result.graph=data.graph.copy()
if hasattr(data,'node') and isinstance(data.node,dict):
result.node=dict( (n,dd.copy()) for n,dd in data.node.items() )
if hasattr(data,'graph'): # data.graph should be dict-like
result.graph.update(data.graph)
if hasattr(data,'node'): # data.node should be dict-like
result.node.update( (n,dd.copy()) for n,dd in data.node.items() )
return result
except:
raise nx.NetworkXError("Input is not a correct NetworkX graph.")
Expand Down
9 changes: 9 additions & 0 deletions networkx/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,12 @@ def test_directed_to_undirected(self):

assert_true(self.edgelists_equal(nx.Graph(nx.MultiDiGraph(edges1)).edges(),edges1))
assert_true(self.edgelists_equal(nx.Graph(nx.MultiDiGraph(edges2)).edges(),edges1))

def test_attribute_dict_integrity(self):
# we must not replace dict-like graph data structures with dicts
G=OrderedGraph()
G.add_nodes_from("abc")
H=to_networkx_graph(G, create_using=OrderedGraph())
assert_equal(list(H.node),list(G.node))
H=OrderedDiGraph(G)
assert_equal(list(H.node),list(G.node))

0 comments on commit e10ecc4

Please sign in to comment.