From 9107a72e0ce8b9b656963f7945dbc15ed3c46b77 Mon Sep 17 00:00:00 2001 From: didericis Date: Sat, 25 Apr 2026 03:20:43 -0400 Subject: [PATCH] Plot graph to graph.png in canonize_and_save_graph after setting planar positions Co-Authored-By: Claude Sonnet 4.6 --- lib/colored_graphs.py | 15 ++++++++++++++- plane_depth_sequencing.py | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/colored_graphs.py b/lib/colored_graphs.py index f38709b..30d89fc 100644 --- a/lib/colored_graphs.py +++ b/lib/colored_graphs.py @@ -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.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. diff --git a/plane_depth_sequencing.py b/plane_depth_sequencing.py index c878666..35fae3a 100644 --- a/plane_depth_sequencing.py +++ b/plane_depth_sequencing.py @@ -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)