Skip to content

Commit

Permalink
Update to current networkx API
Browse files Browse the repository at this point in the history
  • Loading branch information
wosc committed Jun 17, 2022
1 parent dbf2f15 commit 12c37e5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions linesman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from inspect import getmodule

import networkx as nx
import networkx.drawing.nx_agraph


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,7 +49,7 @@ def draw_graph(graph, output_path):
``output_path``:
Location to store the rendered output.
"""
nx.to_agraph(graph).draw(output_path, prog="dot")
networkx.drawing.nx_agraph.to_agraph(graph).draw(output_path, prog="dot")
log.info("Wrote output to `%s'" % output_path)


Expand Down Expand Up @@ -85,7 +86,7 @@ def create_graph(stats):
'reccallcount': stat.reccallcount,
'totaltime': stat.totaltime,
}
g.add_node(caller_key, attr_dict=attrs)
g.add_node(caller_key, **attrs)

# Add all the calls as edges
for call in stat.calls or []:
Expand All @@ -102,7 +103,7 @@ def create_graph(stats):
g.add_edge(caller_key, callee_key,
weight=call.totaltime,
label=call.totaltime,
attr_dict=call_attrs)
**call_attrs)

return g

Expand Down
17 changes: 12 additions & 5 deletions linesman/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tempfile import gettempdir

from PIL import Image
from networkx.classes.reportviews import InDegreeView
import networkx as nx
from mako.lookup import TemplateLookup
from paste.urlparser import StaticURLParser
Expand Down Expand Up @@ -438,13 +439,19 @@ def prepare_graph(source_graph, cutoff_time, break_cycles=False):
# Break cycles
if break_cycles:
for cycle in nx.simple_cycles(graph):
u, v = cycle[0], cycle[1]
if graph.has_edge(u, v):
graph.remove_edge(u, v)
cyclic_breaks.append((u, v))
end = len(cycle) - 1 if len(cycle) > 1 else 1
for i in range(end):
u = cycle[i]
if len(cycle) == 1:
v = u
else:
v = cycle[i + 1]
if graph.has_edge(u, v):
graph.remove_edge(u, v)
cyclic_breaks.append((u, v))

root_nodes = [node
for node, degree in graph.in_degree_iter()
for node, degree in InDegreeView(graph)
if degree == 0]

return graph, root_nodes, cyclic_breaks
Expand Down
2 changes: 1 addition & 1 deletion linesman/templates/tree.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%def name="print_tree(node)">
<%
node_obj = graph.node[node]
node_obj = graph._node[node]
total_time_percentage = round(node_obj.get('totaltime') / session.duration * 100, 2)
inline_time_percentage = round(node_obj.get('inlinetime') / session.duration * 100, 2)
open_or_leaf = 'open' if graph.neighbors(node) else 'leaf'
Expand Down
2 changes: 1 addition & 1 deletion linesman/tests/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TestGraphUtils(unittest.TestCase):

@patch("networkx.to_agraph")
@patch("networkx.drawing.nx_agraph.to_agraph")
def test_draw_graph(self, mock_to_agraph):
""" Test that the graph gets converted to an agraph """
mock_draw = Mock()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import sys

install_requires = ["mako", "networkx==1.7", "pillow", "pygraphviz", 'Paste', 'WebOb']
install_requires = ["mako", "networkx>=2.0", "pillow", "pygraphviz", 'Paste', 'WebOb']

# ordereddict is required for versions < 2.7; its included in collections in
# versions 2.7+ and 3.0+
Expand Down

0 comments on commit 12c37e5

Please sign in to comment.