From d1e8fd3863cfea2f7a5d120fdba55849d7b99928 Mon Sep 17 00:00:00 2001 From: p-zach Date: Mon, 14 Apr 2025 20:01:02 -0400 Subject: [PATCH] Ordering fixes --- gtsam/inference/doc/Ordering.ipynb | 59 +++++++----------------------- 1 file changed, 14 insertions(+), 45 deletions(-) diff --git a/gtsam/inference/doc/Ordering.ipynb b/gtsam/inference/doc/Ordering.ipynb index 9dfcd0b4a..7e3bab97e 100644 --- a/gtsam/inference/doc/Ordering.ipynb +++ b/gtsam/inference/doc/Ordering.ipynb @@ -45,7 +45,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": { "id": "ordering_import_code" }, @@ -74,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -99,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -109,14 +109,11 @@ }, "outputs": [ { - "ename": "TypeError", - "evalue": "Colamd(): incompatible function arguments. The following argument types are supported:\n 1. (variableIndex: gtsam::VariableIndex) -> gtsam.gtsam.Ordering\n\nInvoked with: kwargs: graph=SymbolicFactorGraph \nsize: 7\nfactor 0: x0\nfactor 1: x0 x1\nfactor 2: x1 x2\nfactor 3: x0 l1\nfactor 4: x1 l1\nfactor 5: x1 l2\nfactor 6: x2 l2\n", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[3], line 13\u001b[0m\n\u001b[0;32m 10\u001b[0m graph\u001b[38;5;241m.\u001b[39mpush_factor(X(\u001b[38;5;241m2\u001b[39m), L(\u001b[38;5;241m2\u001b[39m))\n\u001b[0;32m 12\u001b[0m \u001b[38;5;66;03m# COLAMD (Column Approximate Minimum Degree) ordering\u001b[39;00m\n\u001b[1;32m---> 13\u001b[0m colamd_ordering \u001b[38;5;241m=\u001b[39m \u001b[43mOrdering\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mColamd\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgraph\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgraph\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 14\u001b[0m colamd_ordering\u001b[38;5;241m.\u001b[39mprint(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCOLAMD Ordering: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 16\u001b[0m \u001b[38;5;66;03m# Constrained COLAMD (force x0 and x2 to be eliminated last)\u001b[39;00m\n", - "\u001b[1;31mTypeError\u001b[0m: Colamd(): incompatible function arguments. The following argument types are supported:\n 1. (variableIndex: gtsam::VariableIndex) -> gtsam.gtsam.Ordering\n\nInvoked with: kwargs: graph=SymbolicFactorGraph \nsize: 7\nfactor 0: x0\nfactor 1: x0 x1\nfactor 2: x1 x2\nfactor 3: x0 l1\nfactor 4: x1 l1\nfactor 5: x1 l2\nfactor 6: x2 l2\n" + "name": "stdout", + "output_type": "stream", + "text": [ + "COLAMD Ordering: Position 0: l1, x0, x1, l2, x2\n", + "Constrained COLAMD (x0, x2 last): Position 0: l2, l1, x1, x2, x0\n" ] } ], @@ -133,23 +130,12 @@ "graph.push_factor(X(2), L(2))\n", "\n", "# COLAMD (Column Approximate Minimum Degree) ordering\n", - "colamd_ordering = Ordering.Colamd(graph)\n", + "colamd_ordering = Ordering.Colamd(VariableIndex(graph))\n", "colamd_ordering.print(\"COLAMD Ordering: \")\n", "\n", "# Constrained COLAMD (force x0 and x2 to be eliminated last)\n", - "constrained_ordering = Ordering.ColamdConstrainedLast(graph, gtsam.KeyVector([X(0), X(2)]))\n", - "constrained_ordering.print(\"Constrained COLAMD (x0, x2 last): \")\n", - "\n", - "# METIS ordering (if GTSAM was built with METIS support)\n", - "try:\n", - " metis_ordering = Ordering.Metis(graph)\n", - " # metis_ordering.print(\"METIS Ordering: \")\n", - "except RuntimeError as e:\n", - " print(f\"\\nSkipping METIS: {e}\")\n", - "\n", - "# Natural ordering (based on key magnitude)\n", - "natural_ordering = Ordering.Natural(graph)\n", - "# natural_ordering.print(\"Natural Ordering: \")" + "constrained_ordering = Ordering.ColamdConstrainedLastSymbolicFactorGraph(graph, gtsam.KeyVector([X(0), X(2)]))\n", + "constrained_ordering.print(\"Constrained COLAMD (x0, x2 last): \")" ] }, { @@ -165,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -181,17 +167,6 @@ "Ordering size: 5\n", "Key at position 0: l1\n" ] - }, - { - "ename": "AttributeError", - "evalue": "'gtsam.gtsam.Ordering' object has no attribute 'invert'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[11], line 8\u001b[0m\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;124mKey at position 0: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgtsam\u001b[38;5;241m.\u001b[39mDefaultKeyFormatter(key_at_0)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Invert the ordering (map from Key to its position)\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m inverted \u001b[38;5;241m=\u001b[39m \u001b[43mcolamd_ordering\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvert\u001b[49m()\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInverted map (Key -> Position):\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m key, pos \u001b[38;5;129;01min\u001b[39;00m inverted\u001b[38;5;241m.\u001b[39mitems():\n", - "\u001b[1;31mAttributeError\u001b[0m: 'gtsam.gtsam.Ordering' object has no attribute 'invert'" - ] } ], "source": [ @@ -199,13 +174,7 @@ "\n", "# Access by index\n", "key_at_0 = colamd_ordering.at(0)\n", - "print(f\"Key at position 0: {gtsam.DefaultKeyFormatter(key_at_0)}\")\n", - "\n", - "# Invert the ordering (map from Key to its position)\n", - "inverted = colamd_ordering.invert()\n", - "print(\"Inverted map (Key -> Position):\")\n", - "for key, pos in inverted.items():\n", - " print(f\" {gtsam.DefaultKeyFormatter(key)} -> {pos}\")" + "print(f\"Key at position 0: {gtsam.DefaultKeyFormatter(key_at_0)}\")" ] }, { @@ -216,7 +185,7 @@ "source": [ "## Appending Keys\n", "\n", - "You can append keys using `push_back`, `+=`, or `,`." + "You can append keys using `push_back`." ] }, {