12 @brief Load a DOT file directly with pydot, bypassing NetworkX's parser.
14 @param dot_file_path: Path to the DOT file
16 @return A NetworkX DiGraph object
19 print(
"Attempting to load DOT file directly with pydot...")
20 graphs = pydot.graph_from_dot_file(dot_file_path)
23 print(
"No graphs found in the DOT file.")
27 print(f
"DOT graph loaded successfully. Type: {dot_graph.get_type()}")
30 if dot_graph.get_type() ==
"graph":
37 for node
in dot_graph.get_nodes():
38 node_name = node.get_name().strip(
'"')
40 k.strip(
'"'): v.strip(
'"')
for k, v
in node.get_attributes().items()
42 graph.add_node(node_name, **attrs)
45 for edge
in dot_graph.get_edges():
46 source = edge.get_source().strip(
'"')
47 target = edge.get_destination().strip(
'"')
49 k.strip(
'"'): v.strip(
'"')
for k, v
in edge.get_attributes().items()
51 graph.add_edge(source, target, **attrs)
54 f
"Converted to NetworkX graph with {graph.number_of_nodes()} nodes and {graph.number_of_edges()} edges"
58 except Exception
as e:
59 print(f
"Error in direct_load_dot: {e}")
68 Load a .dot file and display it using both GraphViz and Matplotlib.
71 dot_file_path: Path to the .dot file
76 os.makedirs(
"graph_output", exist_ok=
True)
79 base_name = os.path.splitext(os.path.basename(dot_file_path))[0]
85 print(f
"\nGraph Information:")
86 print(f
"Nodes: {graph.number_of_nodes()}")
87 print(f
"Edges: {graph.number_of_edges()}")
90 print(
"\nSaving visualizations to files:")
94 print(
"Saving with pydot...")
95 pydot_graph = nx.drawing.nx_pydot.to_pydot(graph)
98 output_file = f
"graph_output/{base_name}_dot.png"
99 pydot_graph.write_png(output_file, prog=
"dot")
100 print(f
"GraphViz dot visualization saved to {output_file}")
103 output_file = f
"graph_output/{base_name}_neato.png"
104 pydot_graph.write_png(output_file, prog=
"neato")
105 print(f
"GraphViz neato visualization saved to {output_file}")
107 except Exception
as e:
108 print(f
"Error saving with pydot: {e}")
112 print(
"Saving with matplotlib...")
113 output_file = f
"graph_output/{base_name}_matplotlib.png"
114 plt.figure(figsize=(12, 10))
115 pos = nx.spring_layout(graph, seed=42)
120 node_color=
"lightblue",
122 arrows=
True if nx.is_directed(graph)
else False,
126 plt.savefig(output_file)
128 print(f
"Matplotlib visualization saved to {output_file}")
130 except Exception
as e:
131 print(f
"Error saving with matplotlib: {e}")
133 print(f
"\nAll available visualizations saved to 'graph_output' directory.")
135 print(f
"Failed to load {dot_file_path}")
138 print(
"\nTrying with original Graph class...")
141 if g.load_from_dot(dot_file_path):
142 print(f
"\nGraph Information:")
143 print(f
"Nodes: {g.get_node_count()}")
144 print(f
"Edges: {g.get_edge_count()}")
147 print(
"\nSaving visualizations to files:")
150 output_file = f
"graph_output/{base_name}_dot.png"
151 g.display_with_graphviz(layout=
"dot", output_file=output_file)
154 output_file = f
"graph_output/{base_name}_neato.png"
155 g.display_with_graphviz(layout=
"neato", output_file=output_file)
158 output_file = f
"graph_output/{base_name}_matplotlib.png"
159 g.display_with_matplotlib(output_file=output_file)
161 print(f
"\nAll visualizations saved to 'graph_output' directory.")
163 print(f
"Failed to load {dot_file_path} with Graph class")
164 except Exception
as e:
165 print(f
"Error with Graph class: {e}")
168 traceback.print_exc()