gtsam/gtsam/inference/doc/Key.ipynb

222 lines
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "key_intro_md"
},
"source": [
"# Key"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "key_desc_md"
},
"source": [
"A `Key` in GTSAM is simply a `typedef` for `std::uint64_t`. It serves as a unique identifier for variables within a factor graph or values within a `Values` container. While you can use raw integer keys, GTSAM provides helper classes like `Symbol` and `LabeledSymbol` to create semantically meaningful keys that encode type and index information within the 64-bit integer."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "key_colab_md"
},
"source": [
"<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/gtsam/inference/doc/Key.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "key_pip_code",
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"%pip install gtsam"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "key_import_code"
},
"outputs": [],
"source": [
"import gtsam\n",
"from gtsam import Symbol, LabeledSymbol\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "key_usage_md"
},
"source": [
"## Basic Usage\n",
"\n",
"Keys are typically created using `Symbol` or `LabeledSymbol` and then implicitly or explicitly cast to the `Key` type (integer)."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "key_create_code",
"outputId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Symbol Key (x0): 8646911284551352320\n",
"Type: <class 'int'>\n"
]
},
{
"ename": "TypeError",
"evalue": "__init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'a', 'B', 1",
"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;124mSymbol Key (x0): \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey_from_symbol\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mType: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(key_from_symbol)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 6\u001b[0m lsym \u001b[38;5;241m=\u001b[39m \u001b[43mLabeledSymbol\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mB\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 7\u001b[0m key_from_labeled_symbol \u001b[38;5;241m=\u001b[39m lsym\u001b[38;5;241m.\u001b[39mkey()\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLabeledSymbol Key (aB1): \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey_from_labeled_symbol\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mTypeError\u001b[0m: __init__(): incompatible constructor arguments. The following argument types are supported:\n 1. gtsam.gtsam.LabeledSymbol(full_key: int)\n 2. gtsam.gtsam.LabeledSymbol(key: gtsam.gtsam.LabeledSymbol)\n 3. gtsam.gtsam.LabeledSymbol(valType: int, label: int, j: int)\n\nInvoked with: 'a', 'B', 1"
]
}
],
"source": [
"sym = Symbol('x', 0)\n",
"key_from_symbol = sym.key() # Or just 'sym' where a Key is expected\n",
"print(f\"Symbol Key (x0): {key_from_symbol}\")\n",
"print(f\"Type: {type(key_from_symbol)}\")\n",
"\n",
"lsym = LabeledSymbol('a', 'B', 1)\n",
"key_from_labeled_symbol = lsym.key()\n",
"print(f\"LabeledSymbol Key (aB1): {key_from_labeled_symbol}\")\n",
"print(f\"Type: {type(key_from_labeled_symbol)}\")\n",
"\n",
"# You can also use plain integers, but it's less descriptive\n",
"plain_key = 12345\n",
"print(f\"Plain Integer Key: {plain_key}\")\n",
"print(f\"Type: {type(plain_key)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "key_formatter_md"
},
"source": [
"## Key Formatting\n",
"\n",
"When printing GTSAM objects that contain keys (like Factor Graphs or Values), you can specify a `KeyFormatter` to control how keys are displayed. The default formatter tries to interpret keys as `Symbol`s. `MultiRobotKeyFormatter` also checks for `LabeledSymbol`s."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "key_format_code",
"outputId": "b2c3d4e5-f6a7-8901-bcde-f12345678901"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Default Formatter:\n",
" Symbol Key: x0\n"
]
},
{
"ename": "NameError",
"evalue": "name 'key_from_labeled_symbol' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[4], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDefault Formatter:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Symbol Key: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgtsam\u001b[38;5;241m.\u001b[39mDefaultKeyFormatter(key_from_symbol)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;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;124m LabeledSymbol Key: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgtsam\u001b[38;5;241m.\u001b[39mDefaultKeyFormatter(\u001b[43mkey_from_labeled_symbol\u001b[49m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Plain Key: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgtsam\u001b[38;5;241m.\u001b[39mDefaultKeyFormatter(plain_key)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMultiRobot Formatter:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mNameError\u001b[0m: name 'key_from_labeled_symbol' is not defined"
]
}
],
"source": [
"print(\"Default Formatter:\")\n",
"print(f\" Symbol Key: {gtsam.DefaultKeyFormatter(key_from_symbol)}\")\n",
"print(f\" LabeledSymbol Key: {gtsam.DefaultKeyFormatter(key_from_labeled_symbol)}\")\n",
"print(f\" Plain Key: {gtsam.DefaultKeyFormatter(plain_key)}\")\n",
"\n",
"print(\"MultiRobot Formatter:\")\n",
"print(f\" Symbol Key: {gtsam.MultiRobotKeyFormatter(key_from_symbol)}\")\n",
"print(f\" LabeledSymbol Key: {gtsam.MultiRobotKeyFormatter(key_from_labeled_symbol)}\")\n",
"print(f\" Plain Key: {gtsam.MultiRobotKeyFormatter(plain_key)}\")\n",
"\n",
"# Example of a custom formatter\n",
"def my_formatter(key):\n",
" # Try interpreting as LabeledSymbol, then Symbol, then default\n",
" try:\n",
" lsym = gtsam.LabeledSymbol(key)\n",
" if lsym.label() != 0: # Check if it's likely a valid LabeledSymbol\n",
" return f\"KEY[{lsym.string()}]\"\n",
" except:\n",
" pass\n",
" try:\n",
" sym = gtsam.Symbol(key)\n",
" if sym.chr() != 0: # Check if it's likely a valid Symbol\n",
" return f\"KEY[{sym.string()}]\"\n",
" except:\n",
" pass\n",
" return f\"KEY[{key}]\"\n",
"\n",
"print(\"Custom Formatter:\")\n",
"print(f\" Symbol Key: {my_formatter(key_from_symbol)}\")\n",
"print(f\" LabeledSymbol Key: {my_formatter(key_from_labeled_symbol)}\")\n",
"print(f\" Plain Key: {my_formatter(plain_key)}\")\n",
"\n",
"# KeyVectors, KeyLists, KeySets can also be printed using formatters\n",
"key_vector = gtsam.KeyVector([key_from_symbol, key_from_labeled_symbol, plain_key])\n",
"# key_vector.print(\"My Vector: \", my_formatter) # .print() method uses formatter directly"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "gtsam",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}