Notebook errors fixed after wrapper updates

release/4.3a0
p-zach 2025-04-16 00:41:29 -04:00
parent 29f62f1d03
commit 0cbd9cd4de
6 changed files with 185 additions and 114 deletions

View File

@ -57,7 +57,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 1,
"metadata": { "metadata": {
"id": "bayestree_import_code" "id": "bayestree_import_code"
}, },
@ -88,7 +88,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 2,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -185,7 +185,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -219,7 +219,7 @@
], ],
"source": [ "source": [
"# Eliminate multifrontally using COLAMD ordering\n", "# Eliminate multifrontally using COLAMD ordering\n",
"ordering = Ordering.Colamd(VariableIndex(graph))\n", "ordering = Ordering.ColamdGaussianFactorGraph(graph)\n",
"# Note: Multifrontal typically yields multiple roots if graph is disconnected\n", "# Note: Multifrontal typically yields multiple roots if graph is disconnected\n",
"bayes_tree = graph.eliminateMultifrontal(ordering)\n", "bayes_tree = graph.eliminateMultifrontal(ordering)\n",
"\n", "\n",
@ -229,7 +229,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 6,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -263,7 +263,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 7,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -342,7 +342,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 8,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -387,10 +387,10 @@
"</svg>\n" "</svg>\n"
], ],
"text/plain": [ "text/plain": [
"<graphviz.sources.Source at 0x1698193de80>" "<graphviz.sources.Source at 0x1d6fb25a900>"
] ]
}, },
"execution_count": 9, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }

View File

@ -49,23 +49,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"metadata": { "metadata": {
"id": "etree_import_code" "id": "etree_import_code"
}, },
"outputs": [ "outputs": [],
{
"ename": "ImportError",
"evalue": "cannot import name 'GaussianEliminationTree' from 'gtsam' (c:\\Users\\porte\\miniconda3\\envs\\gtsam\\Lib\\site-packages\\gtsam\\__init__.py)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[2], line 5\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnumpy\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# EliminationTree is templated, need concrete types\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgtsam\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m GaussianFactorGraph, Ordering, GaussianEliminationTree, GaussianBayesNet\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgtsam\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m symbol_shorthand\n\u001b[0;32m 8\u001b[0m X \u001b[38;5;241m=\u001b[39m symbol_shorthand\u001b[38;5;241m.\u001b[39mX\n",
"\u001b[1;31mImportError\u001b[0m: cannot import name 'GaussianEliminationTree' from 'gtsam' (c:\\Users\\porte\\miniconda3\\envs\\gtsam\\Lib\\site-packages\\gtsam\\__init__.py)"
]
}
],
"source": [ "source": [
"import gtsam\n", "import gtsam\n",
"import numpy as np\n", "import numpy as np\n",
@ -91,7 +79,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -104,25 +92,71 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Elimination Tree: \n", "Elimination Tree: -(x2)\n",
"Root(s):\n", "Elimination Tree: | -(l2)\n",
"Node (x2)\n", "Elimination Tree: | -\n",
"JacobianFactor(keys = [8070450532247928833; 8070450532247928834], A[0] = [ -1 1 ], b = [ 0 ], model = diagonal sigmas [1])\n", " A[l2] = [\n",
"JacobianFactor(keys = [7783684379976990721; 8070450532247928834], A[0] = [ -1 1 ], b = [ 0 ], model = diagonal sigmas [1])\n", "\t-1\n",
" Children:\n", "]\n",
" Node (l2)\n", " A[x2] = [\n",
" JacobianFactor(keys = [7783684379976990721; 8070450532247928833], A[0] = [ -1 1 ], b = [ 0 ], model = diagonal sigmas [1])\n", "\t1\n",
" Children:\n", "]\n",
" Node (x1)\n", " b = [ 0 ]\n",
" JacobianFactor(keys = [8070450532247928832; 8070450532247928833], A[0] = [ -1 1 ], b = [ 0 ], model = diagonal sigmas [1])\n", " Noise model: unit (1) \n",
" JacobianFactor(keys = [7783684379976990720; 8070450532247928833], A[0] = [ -1 1 ], b = [ 0 ], model = diagonal sigmas [1])\n", "Elimination Tree: | | -(x1)\n",
" Children:\n", "Elimination Tree: | | -\n",
" Node (l1)\n", " A[x1] = [\n",
" JacobianFactor(keys = [7783684379976990720; 8070450532247928832], A[0] = [ -1 1 ], b = [ 0 ], model = diagonal sigmas [1])\n", "\t-1\n",
" Children:\n", "]\n",
" Node (x0)\n", " A[x2] = [\n",
" JacobianFactor(keys = [8070450532247928832], Z = [ -1 ], b = [ 0 ], model = diagonal sigmas [1])\n", "\t1\n",
"\n" "]\n",
" b = [ 0 ]\n",
" Noise model: unit (1) \n",
"Elimination Tree: | | -\n",
" A[l2] = [\n",
"\t-1\n",
"]\n",
" A[x1] = [\n",
"\t1\n",
"]\n",
" b = [ 0 ]\n",
" Noise model: unit (1) \n",
"Elimination Tree: | | | -(l1)\n",
"Elimination Tree: | | | -\n",
" A[l1] = [\n",
"\t-1\n",
"]\n",
" A[x1] = [\n",
"\t1\n",
"]\n",
" b = [ 0 ]\n",
" Noise model: unit (1) \n",
"Elimination Tree: | | | | -(x0)\n",
"Elimination Tree: | | | | -\n",
" A[x0] = [\n",
"\t-1\n",
"]\n",
" b = [ 0 ]\n",
" Noise model: unit (1) \n",
"Elimination Tree: | | | | -\n",
" A[x0] = [\n",
"\t-1\n",
"]\n",
" A[x1] = [\n",
"\t1\n",
"]\n",
" b = [ 0 ]\n",
" Noise model: unit (1) \n",
"Elimination Tree: | | | | -\n",
" A[l1] = [\n",
"\t-1\n",
"]\n",
" A[x0] = [\n",
"\t1\n",
"]\n",
" b = [ 0 ]\n",
" Noise model: unit (1) \n"
] ]
} }
], ],
@ -160,7 +194,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -173,13 +207,42 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"BayesNet from EliminationTree: size 5\n", "BayesNet from EliminationTree:\n",
"Conditional 0: GaussianConditional( P(x0 | l1) = dx0 - R*dl1 - d), R = [ 0.5 ], d = [ 0 ], sigmas = [ 0.866025 ])\n", "\n",
"Conditional 1: GaussianConditional( P(l1 | x1) = dl1 - R*dx1 - d), R = [ 0.5 ], d = [ 0 ], sigmas = [ 0.866025 ])\n", "size: 5\n",
"Conditional 2: GaussianConditional( P(x1 | l2, x2) = dx1 - R1*dl2 - R2*dx2 - d), R1 = [ 0.333333 ], R2 = [ 0.333333 ], d = [ 0 ], sigmas = [ 0.745356 ])\n", "conditional 0: p(x0 | l1 x1)\n",
"Conditional 3: GaussianConditional( P(l2 | x2) = dl2 - R*dx2 - d), R = [ 0.5 ], d = [ 0 ], sigmas = [ 0.866025 ])\n", " R = [ 1.73205 ]\n",
"Conditional 4: GaussianConditional( P(x2) = dx2 - d), d = [ 0 ], sigmas = [ 0.774597 ])\n", " S[l1] = [ -0.57735 ]\n",
"\n" " S[x1] = [ -0.57735 ]\n",
" d = [ 0 ]\n",
" logNormalizationConstant: -0.369632\n",
" No noise model\n",
"conditional 1: p(l1 | x1)\n",
" R = [ 1.29099 ]\n",
" S[x1] = [ -1.0328 ]\n",
" d = [ 0 ]\n",
" logNormalizationConstant: -0.663526\n",
" No noise model\n",
"conditional 2: p(x1 | l2 x2)\n",
" R = [ 1.61245 ]\n",
" S[l2] = [ -0.620174 ]\n",
" S[x2] = [ -0.620174 ]\n",
" d = [ 0 ]\n",
" logNormalizationConstant: -0.441183\n",
" No noise model\n",
"conditional 3: p(l2 | x2)\n",
" R = [ 1.27098 ]\n",
" S[x2] = [ -1.08941 ]\n",
" d = [ 0 ]\n",
" logNormalizationConstant: -0.679152\n",
" No noise model\n",
"conditional 4: p(x2)\n",
" R = [ 0.654654 ]\n",
" d = [ 0 ]\n",
" mean: 1 elements\n",
" x2: 0\n",
" logNormalizationConstant: -1.34259\n",
" No noise model\n"
] ]
} }
], ],

View File

@ -47,7 +47,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"metadata": { "metadata": {
"id": "isam_import_code" "id": "isam_import_code"
}, },
@ -77,7 +77,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 2,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -98,19 +98,17 @@
" mean: 1 elements\n", " mean: 1 elements\n",
" x0: 0\n", " x0: 0\n",
" logNormalizationConstant: -0.918939\n", " logNormalizationConstant: -0.918939\n",
" No noise model\n",
"ISAM from BayesTree:\n",
"GaussianISAM: : cliques: 1, variables: 1\n",
"GaussianISAM: - p(x0)\n",
" R = [ 1 ]\n",
" d = [ 0 ]\n",
" mean: 1 elements\n",
" x0: 0\n",
" logNormalizationConstant: -0.918939\n",
" No noise model\n" " No noise model\n"
] ]
},
{
"ename": "TypeError",
"evalue": "__init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.GaussianISAM()\n\nInvoked with: : cliques: 1, variables: 1\n- p(x0)\n R = [ 1 ]\n d = [ 0 ]\n mean: 1 elements\n x0: 0\n logNormalizationConstant: -0.918939\n No noise model\n",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[5], line 13\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInitial BayesTree:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 11\u001b[0m initial_bayes_tree\u001b[38;5;241m.\u001b[39mprint()\n\u001b[1;32m---> 13\u001b[0m isam2 \u001b[38;5;241m=\u001b[39m \u001b[43mGaussianISAM\u001b[49m\u001b[43m(\u001b[49m\u001b[43minitial_bayes_tree\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mISAM from BayesTree:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 15\u001b[0m isam2\u001b[38;5;241m.\u001b[39mprint()\n",
"\u001b[1;31mTypeError\u001b[0m: __init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.GaussianISAM()\n\nInvoked with: : cliques: 1, variables: 1\n- p(x0)\n R = [ 1 ]\n d = [ 0 ]\n mean: 1 elements\n x0: 0\n logNormalizationConstant: -0.918939\n No noise model\n"
]
} }
], ],
"source": [ "source": [
@ -144,7 +142,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 4,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -154,14 +152,38 @@
}, },
"outputs": [ "outputs": [
{ {
"ename": "TypeError", "name": "stdout",
"evalue": "__init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.GaussianISAM()\n\nInvoked with: : cliques: 1, variables: 1\n- p(x0)\n R = [ 1 ]\n d = [ 0 ]\n mean: 1 elements\n x0: 0\n logNormalizationConstant: -0.918939\n No noise model\n", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "ISAM after first update (x0, x1):\n",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "GaussianISAM: : cliques: 1, variables: 2\n",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "GaussianISAM: - p(x1 x0 )\n",
"Cell \u001b[1;32mIn[6], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Start with the ISAM object containing the prior on x0\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m isam \u001b[38;5;241m=\u001b[39m \u001b[43mGaussianISAM\u001b[49m\u001b[43m(\u001b[49m\u001b[43minitial_bayes_tree\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3\u001b[0m model \u001b[38;5;241m=\u001b[39m gtsam\u001b[38;5;241m.\u001b[39mnoiseModel\u001b[38;5;241m.\u001b[39mIsotropic\u001b[38;5;241m.\u001b[39mSigma(\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1.0\u001b[39m)\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# --- First Update ---\u001b[39;00m\n", " R = [ 1 -1 ]\n",
"\u001b[1;31mTypeError\u001b[0m: __init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.GaussianISAM()\n\nInvoked with: : cliques: 1, variables: 1\n- p(x0)\n R = [ 1 ]\n d = [ 0 ]\n mean: 1 elements\n x0: 0\n logNormalizationConstant: -0.918939\n No noise model\n" " [ 0 1 ]\n",
" d = [ 0 0 ]\n",
" mean: 2 elements\n",
" x0: 0\n",
" x1: 0\n",
" logNormalizationConstant: -1.83788\n",
" No noise model\n",
"\n",
"ISAM after second update (x0, x1, x2):\n",
"GaussianISAM: : cliques: 2, variables: 3\n",
"GaussianISAM: - p(x0 x1 )\n",
" R = [ 1.41421 -0.707107 ]\n",
" [ 0 0.707107 ]\n",
" d = [ 0 0 ]\n",
" mean: 2 elements\n",
" x0: 0\n",
" x1: 0\n",
" logNormalizationConstant: -1.83788\n",
" No noise model\n",
"GaussianISAM: | - p(x2 | x1)\n",
" R = [ 1 ]\n",
" S[x1] = [ -1 ]\n",
" d = [ 0 ]\n",
" logNormalizationConstant: -0.918939\n",
" No noise model\n"
] ]
} }
], ],
@ -200,7 +222,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 5,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -210,14 +232,14 @@
}, },
"outputs": [ "outputs": [
{ {
"ename": "NameError", "name": "stdout",
"evalue": "name 'isam' is not defined", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "Optimized Solution after updates:\n",
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "VectorValues: 3 elements\n",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", " x0: 0\n",
"Cell \u001b[1;32mIn[7], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Get the solution from the final ISAM state\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m solution \u001b[38;5;241m=\u001b[39m \u001b[43misam\u001b[49m\u001b[38;5;241m.\u001b[39moptimize()\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOptimized Solution after updates:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m solution\u001b[38;5;241m.\u001b[39mprint()\n", " x1: 0\n",
"\u001b[1;31mNameError\u001b[0m: name 'isam' is not defined" " x2: 0\n"
] ]
} }
], ],

View File

@ -49,7 +49,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 1,
"metadata": { "metadata": {
"id": "jtree_import_code" "id": "jtree_import_code"
}, },
@ -79,7 +79,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 2,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -128,7 +128,7 @@
"graph.add(L(2), -np.eye(1), X(1), np.eye(1), np.zeros(1), model)\n", "graph.add(L(2), -np.eye(1), X(1), np.eye(1), np.zeros(1), model)\n",
"graph.add(L(2), -np.eye(1), X(2), np.eye(1), np.zeros(1), model)\n", "graph.add(L(2), -np.eye(1), X(2), np.eye(1), np.zeros(1), model)\n",
"\n", "\n",
"ordering = Ordering.Colamd(VariableIndex(graph))\n", "ordering = Ordering.ColamdGaussianFactorGraph(graph)\n",
"\n", "\n",
"# Perform multifrontal elimination, which uses a JunctionTree internally\n", "# Perform multifrontal elimination, which uses a JunctionTree internally\n",
"bayes_tree, remaining_graph = graph.eliminatePartialMultifrontal(ordering)\n", "bayes_tree, remaining_graph = graph.eliminatePartialMultifrontal(ordering)\n",

View File

@ -45,7 +45,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 15,
"metadata": { "metadata": {
"id": "ordering_import_code" "id": "ordering_import_code"
}, },
@ -74,7 +74,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 16,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -99,7 +99,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 18,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -130,7 +130,7 @@
"graph.push_factor(X(2), L(2))\n", "graph.push_factor(X(2), L(2))\n",
"\n", "\n",
"# COLAMD (Column Approximate Minimum Degree) ordering\n", "# COLAMD (Column Approximate Minimum Degree) ordering\n",
"colamd_ordering = Ordering.Colamd(VariableIndex(graph))\n", "colamd_ordering = Ordering.ColamdSymbolicFactorGraph(graph)\n",
"colamd_ordering.print(\"COLAMD Ordering: \")\n", "colamd_ordering.print(\"COLAMD Ordering: \")\n",
"\n", "\n",
"# Constrained COLAMD (force x0 and x2 to be eliminated last)\n", "# Constrained COLAMD (force x0 and x2 to be eliminated last)\n",
@ -151,7 +151,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 19,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -190,7 +190,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 20,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"

View File

@ -40,12 +40,12 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"%pip install gtsam" "%pip install gtsam-develop"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 1,
"metadata": { "metadata": {
"id": "vindex_import_code" "id": "vindex_import_code"
}, },
@ -127,7 +127,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 5,
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
@ -142,18 +142,9 @@
"text": [ "text": [
"Number of variables (size): 5\n", "Number of variables (size): 5\n",
"Number of factors (nFactors): 7\n", "Number of factors (nFactors): 7\n",
"Number of variable-factor entries (nEntries): 13\n" "Number of variable-factor entries (nEntries): 13\n",
] "Factors involving x1: [1, 2, 4, 5]\n",
}, "Factors involving l1: [3, 4]\n"
{
"ename": "TypeError",
"evalue": "'gtsam.gtsam.VariableIndex' object is not subscriptable",
"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 6\u001b[0m\n\u001b[0;32m 3\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 variable-factor entries (nEntries): \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mvariable_index\u001b[38;5;241m.\u001b[39mnEntries()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# Get factors involving a specific variable\u001b[39;00m\n\u001b[1;32m----> 6\u001b[0m factors_x1 \u001b[38;5;241m=\u001b[39m \u001b[43mvariable_index\u001b[49m\u001b[43m[\u001b[49m\u001b[43mX\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;66;03m# Returns a FactorIndices (FastVector<size_t>)\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFactors involving x1: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfactors_x1\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 9\u001b[0m \u001b[38;5;66;03m# Use key directly\u001b[39;00m\n",
"\u001b[1;31mTypeError\u001b[0m: 'gtsam.gtsam.VariableIndex' object is not subscriptable"
] ]
} }
], ],
@ -163,17 +154,12 @@
"print(f\"Number of variable-factor entries (nEntries): {variable_index.nEntries()}\")\n", "print(f\"Number of variable-factor entries (nEntries): {variable_index.nEntries()}\")\n",
"\n", "\n",
"# Get factors involving a specific variable\n", "# Get factors involving a specific variable\n",
"factors_x1 = variable_index[X(1)] # Returns a FactorIndices (FastVector<size_t>)\n", "factors_x1 = variable_index.at(X(1)) # Returns a FactorIndices (FastVector<size_t>)\n",
"print(f\"Factors involving x1: {factors_x1}\")\n", "print(f\"Factors involving x1: {factors_x1}\")\n",
"\n", "\n",
"# Use key directly\n", "# Use key directly\n",
"factors_l1 = variable_index[L(1)]\n", "factors_l1 = variable_index.at(L(1))\n",
"print(f\"Factors involving l1: {factors_l1}\")\n", "print(f\"Factors involving l1: {factors_l1}\")"
"\n",
"# Iterate through the index (items are pairs of Key, FactorIndices)\n",
"print(\"Iterating through VariableIndex:\")\n",
"for key, factor_indices in variable_index.items(): # Use .items() like a dict\n",
" print(f\" Variable {gtsam.DefaultKeyFormatter(key)} involves factors: {factor_indices}\")"
] ]
}, },
{ {