Skip to content

Commit

Permalink
Merge pull request #30 from mohawk2/fixups
Browse files Browse the repository at this point in the history
Update to networkx 2.0 API.
  • Loading branch information
annaritz authored Jan 26, 2021
2 parents 6eb2746 + fcb1fab commit 4da6162
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
10 changes: 5 additions & 5 deletions halp/utilities/directed_graph_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ def to_networkx_digraph(H):
nx_graph = nx.DiGraph()

for node in G.node_iterator():
nx_graph.add_node(node, G.get_node_attributes(node))
nx_graph.add_node(node, **G.get_node_attributes(node))

for hyperedge_id in G.hyperedge_id_iterator():
tail_node = G.get_hyperedge_tail(hyperedge_id).pop()
head_node = G.get_hyperedge_head(hyperedge_id).pop()
edge_attributes = G.get_hyperedge_attributes(hyperedge_id)
nx_graph.add_edge(tail_node, head_node, edge_attributes)
nx_graph.add_edge(tail_node, head_node, **edge_attributes)

return nx_graph

Expand All @@ -97,10 +97,10 @@ def from_networkx_digraph(nx_digraph):

G = DirectedHypergraph()

for node in nx_digraph.nodes_iter():
G.add_node(node, copy.copy(nx_digraph.node[node]))
for node in nx_digraph.nodes():
G.add_node(node, **copy.copy(nx_digraph.nodes[node]))

for edge in nx_digraph.edges_iter():
for edge in nx_digraph.edges():
tail_node = edge[0]
head_node = edge[1]
G.add_hyperedge(tail_node,
Expand Down
10 changes: 5 additions & 5 deletions halp/utilities/undirected_graph_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ def to_networkx_graph(H):
nx_graph = nx.Graph()

for node in G.node_iterator():
nx_graph.add_node(node, G.get_node_attributes(node))
nx_graph.add_node(node, **G.get_node_attributes(node))

for hyperedge_id in G.hyperedge_id_iterator():
edge_nodes = G.get_hyperedge_nodes(hyperedge_id)
edge_attributes = G.get_hyperedge_attributes(hyperedge_id)
nx_graph.add_edge(edge_nodes[0], edge_nodes[1], edge_attributes)
nx_graph.add_edge(edge_nodes[0], edge_nodes[1], **edge_attributes)

return nx_graph

Expand All @@ -97,10 +97,10 @@ def from_networkx_graph(nx_graph):

G = UndirectedHypergraph()

for node in nx_graph.nodes_iter():
G.add_node(node, copy.copy(nx_graph.node[node]))
for node in nx_graph.nodes():
G.add_node(node, copy.copy(nx_graph.nodes[node]))

for edge in nx_graph.edges_iter():
for edge in nx_graph.edges():
G.add_hyperedge([edge[0], edge[1]],
copy.copy(nx_graph[edge[0]][edge[1]]))

Expand Down
19 changes: 19 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# - pip install git+http://github.com/numpy/numpy/
# - pip install git+http://github.com/scipy/scipy/
# - pip install git+http://github.com/scipy/scipy.git@maintenance/0.14.x
# - coverage run --source=halp setup.py test

numpy==1.19.5
scipy==1.6.0
networkx==2.5
## The following requirements were added by pip freeze:
attrs==20.3.0
coverage==5.3.1
decorator==4.4.2
iniconfig==1.1.1
packaging==20.8
pluggy==0.13.1
py==1.10.0
pyparsing==2.4.7
pytest==6.2.1
toml==0.10.2
2 changes: 1 addition & 1 deletion tests/algorithms/test_k_shortest_hyperpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def test_returns_3_shortest_hypergraphs_for_nielsen_example_with_k_equal_3(
self):
threeShortest = ksh.k_shortest_hyperpaths(
self.nielsenGraph, 's', 't', 3)
self.assertEquals(len(threeShortest), 3)
self.assertEqual(len(threeShortest), 3)
# shortest path
hyperpath = threeShortest[0]
self.assertEqual(hyperpath.get_node_set(), {'s', '1', '2', 't'})
Expand Down
8 changes: 4 additions & 4 deletions tests/utilities/test_directed_graph_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def test_to_networkx_digraph():
G = directed_graph_transformations.to_networkx_digraph(H)

H_nodes = H.get_node_set()
G_nodes = G.node.keys()
G_nodes = G.nodes.keys()

assert H_nodes == set(G_nodes)

H_nodes_attributes = [H.get_node_attributes(node) for node in H_nodes]
for node in G_nodes:
assert G.node[node] in H_nodes_attributes
assert G.nodes[node] in H_nodes_attributes

for hyperedge_id in H.hyperedge_id_iterator():
tail_set = H.get_hyperedge_tail(hyperedge_id)
Expand All @@ -69,12 +69,12 @@ def test_from_networkx_digraph():

G = directed_graph_transformations.from_networkx_digraph(nxG)

nxG_nodes = nxG.node.keys()
nxG_nodes = nxG.nodes.keys()
G_nodes = G.get_node_set()

assert G_nodes == set(nxG_nodes)

for edge in nxG.edges_iter():
for edge in nxG.edges():
tail_node = edge[0]
head_node = edge[1]
assert G.has_hyperedge(tail_node, head_node)
Expand Down
8 changes: 4 additions & 4 deletions tests/utilities/test_undirected_graph_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def test_to_networkx_graph():
G = undirected_graph_transformations.to_networkx_graph(H)

H_nodes = H.get_node_set()
G_nodes = G.node.keys()
G_nodes = G.nodes.keys()

assert H_nodes == set(G_nodes)

H_nodes_attributes = [H.get_node_attributes(node) for node in H_nodes]
for node in G_nodes:
assert G.node[node] in H_nodes_attributes
assert G.nodes[node] in H_nodes_attributes

for hyperedge_id in H.hyperedge_id_iterator():
hyperedge_nodes = H.get_hyperedge_nodes(hyperedge_id)
Expand Down Expand Up @@ -69,12 +69,12 @@ def test_from_networkx_graph():

G = undirected_graph_transformations.from_networkx_graph(nxG)

nxG_nodes = nxG.node.keys()
nxG_nodes = nxG.nodes.keys()
G_nodes = G.get_node_set()

assert G_nodes == set(nxG_nodes)

for edge in nxG.edges_iter():
for edge in nxG.edges():
assert G.has_hyperedge((edge[0], edge[1]))

# Try transforming an invalid undirected hypergraph
Expand Down

0 comments on commit 4da6162

Please sign in to comment.