Move plot_to_data_uri to lib/colored_graphs and rename to plot_colored_graph_to_data_uri

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-24 11:18:23 -04:00
parent 07ad553568
commit 49f456e467
4 changed files with 42 additions and 60 deletions
+23 -1
View File
@@ -1,7 +1,9 @@
import hashlib
import json
from pathlib import Path
from typing import Any, TypedDict
from sage.all import Graph # type: ignore[attr-defined] # pylint: disable=no-name-in-module
from lib.colored_graphs import ColoredGraphId, VertexColoring
from lib.colored_graphs import ColoredGraphId, VertexColoring, plot_colored_graph_to_data_uri
class Operation(TypedDict):
"""Information about a change made to a (colored) graph"""
@@ -22,3 +24,23 @@ def operation_to_string(op: Operation) -> str:
def operation_sequence_id(ops: list[Operation]) -> str:
joined = "\n".join(operation_to_string(op) for op in ops)
return hashlib.sha256(joined.encode()).hexdigest()
def strip_graphs(obj: Any) -> Any:
if isinstance(obj, dict):
return {k: strip_graphs(v) for k, v in obj.items() if not isinstance(v, Graph)}
if isinstance(obj, list):
return [strip_graphs(v) for v in obj]
return obj
def save_operation_sequence(op_sequence: list[Operation], g: Graph, coloring: VertexColoring, save_dir: Path) -> str:
"""Save op_sequence as JSON and Markdown under save_dir/data/operations/<seq_id>. Returns the sequence id."""
op_seq_id = operation_sequence_id(op_sequence)
op_dir = save_dir / "data" / "operations" / op_seq_id
op_dir.mkdir(parents=True, exist_ok=True)
(op_dir / "colored_pentagon_contractions.json").write_text(json.dumps(strip_graphs(op_sequence), indent=2))
md_lines = [f"## start\n\n![start]({plot_colored_graph_to_data_uri(g, coloring)})"]
for op in op_sequence:
meta_json = json.dumps(op['meta'])
md_lines.append(f"## {op['name']} {meta_json}\n\n![b]({plot_colored_graph_to_data_uri(op['result_graph'], op['result_coloring'])})")
(op_dir / "colored_pentagon_contractions.md").write_text("\n".join(md_lines) + "\n")
return op_seq_id