|
| 1 | +def visualize_graph(graph): |
| 2 | + """ |
| 3 | + Visualize SCC using Graphviz with a strictly enforced circular layout |
| 4 | + """ |
| 5 | + import pygraphviz as pgv |
| 6 | + |
| 7 | + # Create a new pygraphviz graph directly (instead of converting) |
| 8 | + A = pgv.AGraph(strict=False, directed=True) |
| 9 | + |
| 10 | + # Set graph attributes for strict circular layout |
| 11 | + A.graph_attr.update( |
| 12 | + { |
| 13 | + "layout": "circo", |
| 14 | + "root": "circle", |
| 15 | + "splines": "curved", |
| 16 | + "overlap": "false", |
| 17 | + "sep": "+25,25", |
| 18 | + "pad": "0.5", |
| 19 | + "ranksep": "2.0", |
| 20 | + "nodesep": "0.8", |
| 21 | + "mindist": "2.0", |
| 22 | + "start": "regular", |
| 23 | + "ordering": "out", |
| 24 | + "concentrate": "false", |
| 25 | + "ratio": "1.0", |
| 26 | + } |
| 27 | + ) |
| 28 | + |
| 29 | + # Set node attributes for consistent sizing |
| 30 | + A.node_attr.update({"shape": "circle", "fixedsize": "true", "width": "1.5", "height": "1.5", "style": "filled", "fillcolor": "lightblue", "fontsize": "11", "fontname": "Arial"}) |
| 31 | + |
| 32 | + # Set default edge attributes |
| 33 | + A.edge_attr.update({"penwidth": "1.5", "arrowsize": "0.8", "len": "2.0", "weight": "1", "dir": "forward"}) |
| 34 | + |
| 35 | + # Add nodes first |
| 36 | + for node in graph.nodes(): |
| 37 | + short_name = node.split("/")[-1] |
| 38 | + A.add_node(node, label=short_name) |
| 39 | + |
| 40 | + # Add edges with their attributes |
| 41 | + for u, v, key, data in graph.edges(data=True, keys=True): |
| 42 | + # Create a unique key for this edge |
| 43 | + edge_key = f"{u}_{v}_{key}" |
| 44 | + |
| 45 | + # Set edge attributes based on the data |
| 46 | + edge_attrs = { |
| 47 | + "key": edge_key, # Ensure unique edge |
| 48 | + "color": "red" if data.get("color") == "red" else "#666666", |
| 49 | + "style": "dashed" if data.get("color") == "red" else "solid", |
| 50 | + "label": "dynamic" if data.get("color") == "red" else "", |
| 51 | + "fontcolor": "red" if data.get("color") == "red" else "#666666", |
| 52 | + "fontsize": "10", |
| 53 | + } |
| 54 | + |
| 55 | + A.add_edge(u, v, **edge_attrs) |
| 56 | + |
| 57 | + # Force circo layout with specific settings |
| 58 | + A.layout(prog="circo") |
| 59 | + |
| 60 | + # Save with a larger size |
| 61 | + A.draw("import_cycle.png", format="png", prog="circo", args="-Gsize=12,12!") |
| 62 | + |
| 63 | + from IPython.display import Image |
| 64 | + |
| 65 | + return Image("import_cycle.png") |
0 commit comments