Plot graph to graph.png in canonize_and_save_graph after setting planar positions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-25 03:20:43 -04:00
parent 9e3e525a5b
commit 9107a72e0c
2 changed files with 21 additions and 1 deletions
+14 -1
View File
@@ -1,4 +1,4 @@
"""Utilities for canonizing and saving colored graphs."""
"""Utilities for canonizing and saving graphs."""
import base64
import tempfile
from collections import defaultdict
@@ -53,6 +53,19 @@ def plot_colored_graph_to_data_uri(g: Graph, coloring: VertexColoring) -> str:
return f"data:image/png;base64,{base64.b64encode(png_bytes).decode()}"
def canonize_and_save_graph(g: Graph) -> Graph:
"""Canonize g, save to data/graphs/<graph_id>/graph.sobj, and return the canonical graph."""
canonical = cast(Graph, g.canonical_label())
graph_id = base64.urlsafe_b64encode(canonical.graph6_string().encode()).decode()
out_dir = DIR / "data" / "graphs" / graph_id
out_dir.mkdir(parents=True, exist_ok=True)
print(graph_id)
canonical.is_planar(set_embedding=True, set_pos=True)
canonical.plot().save(out_dir / 'graph.png')
save(canonical, str(out_dir / 'graph'))
return canonical
def save_colored_graph(g: Graph, coloring: VertexColoring) -> tuple[Graph, VertexColoring, ColoredGraphId]:
"""
Canonize g and coloring, save to disk, and return the canonical forms with id.
+7
View File
@@ -2,6 +2,7 @@
import random
from typing import Any, TypedDict
from sage.all import Graph, graphs # type: ignore[attr-defined] # pylint: disable=no-name-in-module
from lib.colored_graphs import canonize_and_save_graph
class DeeplyEmbeddedGraph(TypedDict):
@@ -63,3 +64,9 @@ def generate_example(n: int) -> DeeplyEmbeddedGraph:
outer_cycle = [u for u, v in random.choice(faces)]
plane_depth_labelling = get_plane_depth_labelling(g, outer_cycle)
return DeeplyEmbeddedGraph(graph=g, outer_cycle=outer_cycle, plane_depth_labelling=plane_depth_labelling, deep_embedding=deep_embedding(g, outer_cycle, plane_depth_labelling))
if __name__ == "__main__":
example = generate_example(10)
canonical = canonize_and_save_graph(example['graph'])
print(canonical)