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 7, 2023
1 parent a699df4 commit 78482d2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/graph_scheduler/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
__all__ = [
'dependency_dict_to_networkx_digraph', 'disable_debug_logging',
'enable_debug_logging', 'networkx_graph_to_dependency_dict',
'output_graph_png',
]


Expand Down Expand Up @@ -184,3 +185,26 @@ def networkx_graph_to_dependency_dict(graph: nx.Graph) -> typing_graph_dependenc
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}')

0 comments on commit 78482d2

Please sign in to comment.