Skip to content

Commit

Permalink
utilities: add output_graph_png
Browse files Browse the repository at this point in the history
writes an image representation of a graph to a png file
  • Loading branch information
kmantel committed Dec 8, 2023
1 parent 43d221f commit 1c2c20a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/graph_scheduler/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = [
'clone_graph', 'dependency_dict_to_networkx_digraph',
'disable_debug_logging', 'enable_debug_logging',
'networkx_digraph_to_dependency_dict',
'networkx_digraph_to_dependency_dict', 'output_graph_png',
]


Expand Down Expand Up @@ -199,3 +199,26 @@ def networkx_digraph_to_dependency_dict(
res_graph[rec] = set()
res_graph[rec].add(sender)
return res_graph


def output_graph_png(graph: typing_graph_dependency_dict, filename: str = None):
"""
Writes an image representation of **graph** to file **filename**
Args:
graph: a graph in dependency dict form
filename (str, optional): full path of image to write. Appends
'.png' if not present. Defaults to
'graph-scheduler-figure-<graph id>.png' in the current
directory.
"""
ext = '.png'
if filename is None:
filename = f'graph-scheduler-figure-{id(graph)}{ext}'
elif not filename.endswith(ext):
filename += ext

nx_graph = dependency_dict_to_networkx_digraph(graph)
pd = nx.drawing.nx_pydot.to_pydot(nx_graph)
pd.write_png(filename)
print(f'graph_scheduler.output_graph_png: wrote {ext[1:]} to {filename}')
5 changes: 5 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ def test_convert_from_nx_graph(graph, nx_type):
)
assert nx_graph.nodes == nx_graph.nodes
assert nx_graph.edges == res.edges


@pytest.mark.parametrize('graph', test_graphs)
def test_output_graph_png(graph, tmp_path):
gs.output_graph_png(graph, f'{tmp_path}_fig.png')

0 comments on commit 1c2c20a

Please sign in to comment.