From f48e94fa25682350768e255d77b0e1f64ab4eda5 Mon Sep 17 00:00:00 2001 From: p-zach Date: Mon, 14 Apr 2025 19:14:52 -0400 Subject: [PATCH] Graphviz/dot cleanup --- gtsam/inference/doc/BayesNet.ipynb | 39 ++++++------------------ gtsam/inference/doc/BayesTree.ipynb | 34 ++++++--------------- gtsam/inference/doc/DotWriter.ipynb | 3 +- gtsam/inference/doc/FactorGraph.ipynb | 43 ++++++--------------------- 4 files changed, 28 insertions(+), 91 deletions(-) diff --git a/gtsam/inference/doc/BayesNet.ipynb b/gtsam/inference/doc/BayesNet.ipynb index c2b8ff67f..7a0964f73 100644 --- a/gtsam/inference/doc/BayesNet.ipynb +++ b/gtsam/inference/doc/BayesNet.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "metadata": { "id": "bayesnet_import_code" }, @@ -65,6 +65,7 @@ "source": [ "import gtsam\n", "import numpy as np\n", + "import graphviz\n", "\n", "# We need concrete graph types and elimination to get a BayesNet\n", "from gtsam import GaussianFactorGraph, Ordering, GaussianBayesNet\n", @@ -87,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -184,7 +185,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -235,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -282,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -291,22 +292,6 @@ "outputId": "3456789a-bcde-f012-3456-789abcdef012" }, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "digraph {\n", - " size=\"5,5\";\n", - "\n", - " var8646911284551352320[label=\"x0\"];\n", - " var8646911284551352321[label=\"x1\"];\n", - " var8646911284551352322[label=\"x2\"];\n", - "\n", - " var8646911284551352322->var8646911284551352321\n", - " var8646911284551352321->var8646911284551352320\n", - "}\n" - ] - }, { "data": { "image/svg+xml": [ @@ -354,22 +339,16 @@ "\n" ], "text/plain": [ - "" + "" ] }, - "execution_count": 5, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dot_string = bayes_net.dot()\n", - "print(dot_string)\n", - "\n", - "# To render:\n", - "# dot -Tpng bayesnet.dot -o bayesnet.png\n", - "import graphviz\n", - "graphviz.Source(dot_string)" + "graphviz.Source(bayes_net.dot())" ] } ], diff --git a/gtsam/inference/doc/BayesTree.ipynb b/gtsam/inference/doc/BayesTree.ipynb index 14100971d..fcefe9821 100644 --- a/gtsam/inference/doc/BayesTree.ipynb +++ b/gtsam/inference/doc/BayesTree.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "metadata": { "id": "bayestree_import_code" }, @@ -65,6 +65,7 @@ "source": [ "import gtsam\n", "import numpy as np\n", + "import graphviz\n", "\n", "# We need concrete graph types and elimination to get a BayesTree\n", "from gtsam import GaussianFactorGraph, Ordering, GaussianBayesTree, VariableIndex\n", @@ -239,7 +240,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -262,7 +263,7 @@ "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[4], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBayesTree number of cliques: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbayes_tree\u001b[38;5;241m.\u001b[39msize()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m# Access roots\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m roots \u001b[38;5;241m=\u001b[39m \u001b[43mbayes_tree\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mroots\u001b[49m()\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNumber of roots: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(roots)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m roots:\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Access the conditional associated with the first root clique\u001b[39;00m\n", + "Cell \u001b[1;32mIn[5], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBayesTree number of cliques: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbayes_tree\u001b[38;5;241m.\u001b[39msize()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m# Access roots\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m roots \u001b[38;5;241m=\u001b[39m \u001b[43mbayes_tree\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mroots\u001b[49m()\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNumber of roots: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(roots)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m roots:\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Access the conditional associated with the first root clique\u001b[39;00m\n", "\u001b[1;31mAttributeError\u001b[0m: 'gtsam.gtsam.GaussianBayesTree' object has no attribute 'roots'" ] } @@ -298,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -377,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -386,17 +387,6 @@ "outputId": "789abcde-f012-3456-789a-bcdef0123456" }, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "digraph G{\n", - "0[label=\"x1, l2, x2\"];\n", - "0->1\n", - "1[label=\"l1, x0 : x1\"];\n", - "}\n" - ] - }, { "data": { "image/svg+xml": [ @@ -433,22 +423,16 @@ "\n" ], "text/plain": [ - "" + "" ] }, - "execution_count": 6, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dot_string = bayes_tree.dot()\n", - "print(dot_string)\n", - "\n", - "# To render:\n", - "# dot -Tpng bayestree.dot -o bayestree.png\n", - "import graphviz\n", - "graphviz.Source(dot_string)" + "graphviz.Source(bayes_tree.dot())" ] } ], diff --git a/gtsam/inference/doc/DotWriter.ipynb b/gtsam/inference/doc/DotWriter.ipynb index 81a72316d..6ad795add 100644 --- a/gtsam/inference/doc/DotWriter.ipynb +++ b/gtsam/inference/doc/DotWriter.ipynb @@ -123,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -300,7 +300,6 @@ "\n", "# Generate dot string using the configured writer\n", "dot_string = graph.dot(writer=writer)\n", - "# print(dot_string)\n", "\n", "# Render the graph\n", "graphviz.Source(dot_string)" diff --git a/gtsam/inference/doc/FactorGraph.ipynb b/gtsam/inference/doc/FactorGraph.ipynb index 07633ae9c..a48d56ef7 100644 --- a/gtsam/inference/doc/FactorGraph.ipynb +++ b/gtsam/inference/doc/FactorGraph.ipynb @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "id": "fg_import_code" }, @@ -66,6 +66,7 @@ "source": [ "import gtsam\n", "import numpy as np\n", + "import graphviz\n", "\n", "# Example uses NonlinearFactorGraph, but concepts apply to others\n", "from gtsam import NonlinearFactorGraph, PriorFactorPose2, BetweenFactorPose2, Pose2, Point3\n", @@ -87,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -134,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -191,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -237,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -246,25 +247,6 @@ "outputId": "56789abc-def0-1234-5678-9abcdef01234" }, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "graph {\n", - " size=\"5,5\";\n", - "\n", - " var8646911284551352320[label=\"x0\", pos=\"0,0!\"];\n", - " var8646911284551352321[label=\"x1\", pos=\"0,1!\"];\n", - "\n", - " factor0[label=\"\", shape=point];\n", - " var8646911284551352320--factor0;\n", - " factor1[label=\"\", shape=point];\n", - " var8646911284551352320--factor1;\n", - " var8646911284551352321--factor1;\n", - "}\n", - "\n" - ] - }, { "data": { "image/svg+xml": [ @@ -319,23 +301,16 @@ "\n" ], "text/plain": [ - "" + "" ] }, - "execution_count": 8, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dot_string = graph.dot(values)\n", - "print(dot_string)\n", - "\n", - "# To render, save dot_string to a file (e.g., graph.dot) and run:\n", - "# dot -Tpng graph.dot -o graph.png\n", - "# Or use a Python library like graphviz\n", - "import graphviz\n", - "graphviz.Source(dot_string)" + "graphviz.Source(graph.dot(values))" ] }, {