Load a .dot file and display it using both GraphViz and Matplotlib.
66def load_and_display_dot_file(dot_file_path):
67 """
68 Load a .dot file and display it using both GraphViz and Matplotlib.
69
70 Args:
71 dot_file_path: Path to the .dot file
72 """
73
74
75
76 os.makedirs("graph_output", exist_ok=True)
77
78
79 base_name = os.path.splitext(os.path.basename(dot_file_path))[0]
80
81
82 graph = direct_load_dot(dot_file_path)
83
84 if graph:
85 print(f"\nGraph Information:")
86 print(f"Nodes: {graph.number_of_nodes()}")
87 print(f"Edges: {graph.number_of_edges()}")
88
89
90 print("\nSaving visualizations to files:")
91
92 try:
93
94 print("Saving with pydot...")
95 pydot_graph = nx.drawing.nx_pydot.to_pydot(graph)
96
97
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}")
101
102
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}")
106
107 except Exception as e:
108 print(f"Error saving with pydot: {e}")
109
110 try:
111
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)
116 nx.draw(
117 graph,
118 pos,
119 with_labels=True,
120 node_color="lightblue",
121 node_size=1500,
122 arrows=True if nx.is_directed(graph) else False,
123 arrowsize=20,
124 edge_color="gray",
125 )
126 plt.savefig(output_file)
127 plt.close()
128 print(f"Matplotlib visualization saved to {output_file}")
129
130 except Exception as e:
131 print(f"Error saving with matplotlib: {e}")
132
133 print(f"\nAll available visualizations saved to 'graph_output' directory.")
134 else:
135 print(f"Failed to load {dot_file_path}")
136
137
138 print("\nTrying with original Graph class...")
139 try:
140 g = Graph()
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()}")
145
146
147 print("\nSaving visualizations to files:")
148
149
150 output_file = f"graph_output/{base_name}_dot.png"
151 g.display_with_graphviz(layout="dot", output_file=output_file)
152
153
154 output_file = f"graph_output/{base_name}_neato.png"
155 g.display_with_graphviz(layout="neato", output_file=output_file)
156
157
158 output_file = f"graph_output/{base_name}_matplotlib.png"
159 g.display_with_matplotlib(output_file=output_file)
160
161 print(f"\nAll visualizations saved to 'graph_output' directory.")
162 else:
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}")
166 import traceback
167
168 traceback.print_exc()
169
170