Skip to content

Commit 0d94366

Browse files
authored
Merge pull request #33 from brooksandrew/nx2.0
Updating to networkx 2.0
2 parents b7a13c9 + e8a886b commit 0d94366

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

postman_problems/graph.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def create_networkx_graph_from_edgelist(edgelist, edge_id='id'):
5151
edge_attr_dict = row[1][2:].to_dict()
5252
if edge_id not in edge_attr_dict:
5353
edge_attr_dict[edge_id] = i
54-
g.add_edge(row[1][0], row[1][1], attr_dict=edge_attr_dict)
54+
g.add_edge(row[1][0], row[1][1], **edge_attr_dict)
5555
return g
5656

5757

@@ -66,7 +66,7 @@ def _get_even_or_odd_nodes(graph, mod):
6666
list[str]: list of node names of odd or even degree
6767
"""
6868
degree_nodes = []
69-
for v, d in graph.degree_iter():
69+
for v, d in graph.degree():
7070
if d % 2 == mod:
7171
degree_nodes.append(v)
7272
return degree_nodes
@@ -132,7 +132,7 @@ def create_complete_graph(pair_weights, flip_weights=True):
132132
g = nx.Graph()
133133
for k, v in pair_weights.items():
134134
wt_i = -v if flip_weights else v
135-
g.add_edge(k[0], k[1], attr_dict={'distance': v, 'weight': wt_i})
135+
g.add_edge(k[0], k[1], **{'distance': v, 'weight': wt_i})
136136
return g
137137

138138

@@ -169,8 +169,8 @@ def add_augmenting_path_to_graph(graph, min_weight_pairs, edge_weight_name='weig
169169
for pair in min_weight_pairs:
170170
graph_aug.add_edge(pair[0],
171171
pair[1],
172-
attr_dict={'distance': nx.dijkstra_path_length(graph, pair[0], pair[1], weight=edge_weight_name),
173-
'augmented': True}
172+
**{'distance': nx.dijkstra_path_length(graph, pair[0], pair[1], weight=edge_weight_name),
173+
'augmented': True}
174174
)
175175
return graph_aug
176176

@@ -194,7 +194,7 @@ def create_eulerian_circuit(graph_augmented, graph_original, start_node=None):
194194

195195
euler_circuit = list(nx.eulerian_circuit(graph_augmented, source=start_node))
196196
assert len(graph_augmented.edges()) == len(euler_circuit), 'graph and euler_circuit do not have equal number of edges.'
197-
edge_data = graph_augmented.edges(data=True)
197+
edge_data = list(graph_augmented.edges(data=True))
198198

199199
for edge in euler_circuit:
200200
possible_edges = [e for e in edge_data if set([e[0], e[1]]) == set([edge[0], edge[1]])]
@@ -215,6 +215,7 @@ def create_eulerian_circuit(graph_augmented, graph_original, start_node=None):
215215
yield(edge_aug + (edge_aug_shortest,))
216216
else:
217217
yield(edge + (possible_edges[edge_key][2],))
218+
218219
edge_data.remove(possible_edges[edge_key])
219220

220221

postman_problems/tests/test_example_sleeping_giant.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def test_add_node_attributes():
5858
assert 'Y' in v
5959

6060
# spot check node attributes for first node
61-
print(graph_node_attrs.nodes(data=True)[0][0])
62-
assert graph_node_attrs.nodes(data=True)[0][0] == 'rs_end_north'
63-
assert graph_node_attrs.nodes(data=True)[0][1]['X'] == 1772
64-
assert graph_node_attrs.nodes(data=True)[0][1]['Y'] == 172
61+
node_data_from_graph = list(graph_node_attrs.nodes(data=True))
62+
assert node_data_from_graph[0][0] == 'rs_end_north'
63+
assert node_data_from_graph[0][1]['X'] == 1772
64+
assert node_data_from_graph[0][1]['Y'] == 172
6565

6666

6767
def test_get_shortest_paths_distances():

postman_problems/viz.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import imageio
44
import tqdm
55
import numpy as np
6+
import networkx as nx
67
import graphviz as gv
78
from collections import defaultdict
89

@@ -22,7 +23,7 @@ def add_node_attributes(graph, nodelist):
2223
networkx graph: original `graph` augmented w node attributes
2324
"""
2425
for i, row in nodelist.iterrows():
25-
graph.node[row['id']] = row.to_dict()
26+
nx.set_node_attributes(graph, {row['id']: row.to_dict()})
2627
return graph
2728

2829

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def read(fname):
4747
python_requires='>=2.7',
4848
install_requires=[
4949
'pandas',
50-
'networkx==1.11'
50+
'networkx>=2.0'
5151
],
5252
extras_require={
5353
'viz': ['imageio', 'matplotlib', 'graphviz', 'tqdm'],

0 commit comments

Comments
 (0)