Skip to content

Commit 4226da3

Browse files
authored
Merge pull request #2080 from borglab/gpt-docs
Doc generation with GPT
2 parents d049592 + 2991ce6 commit 4226da3

40 files changed

+4063
-590
lines changed

doc/examples.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
This section contains python examples in interactive Python notebooks (`*.ipynb`). Python notebooks with an <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/> button near the top can be opened in your browser, where you can run the files yourself and make edits to play with and understand GTSAM.

doc/expressions.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Expressions
2+
## Motivation
3+
GTSAM is an optimization library for objective functions expressed as a factor graph over a set of unknown variables. In the continuous case, the variables are typically vectors or elements on a manifold (such as the 3D rotation manifold). The factors compute vector-valued errors that need to be minimized, and are typically only connected to a handful of unknowns.
4+
5+
In the continuous case, the main optimization methods we have implemented are variants of Gauss-Newton non-linear optimization or conjugate gradient methods. Let us assume there are m factors over n unknowns. For either optimization method, we need to evaluate the sparse Jacobian matrix of the entire factor graph, which is a sparse block-matrix of m block-rows and n-block columns.
6+
7+
The sparse Jacobian is built up factor by factor, corresponding to the block-rows. A typical non-linear least-square term is $|h(x)-z|^2$ where $h(x)$ is a measurement function, which we need to be able to linearize as
8+
$$
9+
h(x) \approx h(x_0+dx)+H(x_0)dx
10+
$$
11+
Note the above is for vector unknowns, for Lie groups and manifold variables, see [doc/math.pdf](https://github.com/borglab/gtsam/blob/develop/doc/math.pdf) for details.
12+
13+
## Expressions
14+
In many cases one can use GTSAM 4 Expressions to implement factors. Expressions are objects of type Expression<T>, and there are three main expression flavors:
15+
16+
- constants, e.g., `Expression<Point2> kExpr(Point2(3,4))`
17+
- unknowns, e.g., `Expression<Point3> pExpr(123)` where 123 is a key.
18+
- functions, e.g., `Expression<double> sumExpr(h, kexpr, pExpr)`
19+
20+
The latter case is an example of wrapping a binary measurement function `h`. To be able to wrap `h`, it needs to be able to compute its local derivatives, i.e., it has to have the signature
21+
```c++
22+
double h(const Point2& a, const Point3& b,
23+
OptionalJacobian<1, 2> Ha, OptionalJacobian<1, 3> Hb)
24+
```
25+
In this case the output type 'T' is 'double', the two arguments have type Point2 and Point3 respectively, and the two remaining arguments provide a way to compute the function Jacobians, if needed. The templated type `OptionalJacobian<M,N>` behaves very much like `std::optional<Eigen::Matrix<double,M,N>`. If an actual matrix is passed in, the function is expected to treat it as an output argument in which to write the Jacobian for the result wrp. the corresponding input argument. *The matrix to write in will be allocated before the call.*
26+
27+
Expression constructors exist for both methods and functions with different arities. Note that an expression is templated with the output type T, not with the argument types. However, the constructor will infer the argument types from inspecting the signature of the function f, and will in this example expect two additional arguments of type Expression<Point2> and Expression<Point3>, respectively.
28+
29+
As an example, here is the constructor declaration for wrapping unary functions:
30+
```c++
31+
template<typename A>
32+
Expression(typename UnaryFunction<A>::type function,
33+
const Expression<A>& expression);
34+
```
35+
where (in this case) the function type is defined by
36+
```c++
37+
template<class A1>
38+
struct UnaryFunction {
39+
typedef boost::function<
40+
T(const A1&, typename MakeOptionalJacobian<T, A1>::type)> type;
41+
};
42+
```
43+
## Some measurement function examples
44+
An example of a simple unary function is `gtsam::norm3` in [Point3.cpp](https://github.com/borglab/gtsam/blob/develop/gtsam/geometry/Point3.cpp#L41):
45+
```c++
46+
double norm3(const Point3 & p, OptionalJacobian<1, 3> H = {}) {
47+
double r = sqrt(p.x() * p.x() + p.y() * p.y() + p.z() * p.z());
48+
if (H) *H << p.x() / r, p.y() / r, p.z() / r;
49+
return r;
50+
}
51+
```
52+
The key new concept here is OptionalJacobian, which acts like a std::optional: if it evaluates to true, you should write the Jacobian of the function in it. It acts as a fixed-size Eigen matrix.
53+
54+
As we said above, expressions also support binary functions, ternary functions, and methods. An example of a binary function is 'Point3::cross':
55+
56+
```c++
57+
Point3 cross(const Point3 &p, const Point3 & q,
58+
OptionalJacobian<3, 3> H1 = {}, OptionalJacobian<3, 3> H2 = {}) {
59+
if (H1) *H1 << skewSymmetric(-q.x(), -q.y(), -q.z());
60+
if (H2) *H2 << skewSymmetric(p.x(), p.y(), p.z());
61+
return Point3(p.y() * q.z() - p.z() * q.y(), p.z() * q.x() - p.x() * q.z(), p.x() * q.y() - p.y() * q.x());
62+
}
63+
```
64+
Example of using cross:
65+
```c++
66+
using namespace gtsam;
67+
Matrix3 H1, H2;
68+
Point3 p(1,2,3), q(4,5,6), r = cross(p,q,H1,H2);
69+
```
70+
## Using Expressions for Inference
71+
The way expressions are used is by creating unknown Expressions for the unknown variables we are optimizing for:
72+
```c++
73+
Expression<Point3> x(‘x’,1);
74+
auto h = Expression<Point3>(& norm3, x);
75+
```
76+
For convenient creation of factors with expressions, we provide a new factor graph type `ExpressionFactorGraph`, which is just a `NonlinearFactorGraph` with an extra method addExpressionFactor(h, z, n) that takes a measurement expression h, an actual measurement z, and a measurement noise model R. With this, we can add a GTSAM nonlinear factor $|h(x)-z|^2$ to a `NonlinearFactorGraph` by
77+
```c++
78+
graph.addExpressionFactor(h, z, R)
79+
```
80+
In the above, the unknown in the example can be retrieved by the `gtsam::Symbol(‘x’,1)`, which evaluates to a uint64 identifier.
81+
82+
## Composing Expressions
83+
The key coolness behind expressions, however, is that you can compose them into expression trees, as long as the leaves know how to do their own derivatives:
84+
```c++
85+
Expression<Point3> x1(‘x’1), x2(‘x’,2);
86+
auto h = Expression<Point3>(& cross, x1, x2);
87+
auto g = Expression<Point3>(& norm3, h);
88+
```
89+
Because we typedef Point3_ to Expression<Point3>, we can write this very concisely as
90+
```c++
91+
auto g = Point3_(& norm3, Point3_(& cross, x1(‘x’1), x2(‘x’,2)));
92+
```
93+
## PoseSLAM Example
94+
Using expressions, it is simple to quickly create a factor graph corresponding to a PoseSLAM problem, where our only measurements are relative poses between a series of unknown 2D or 3D poses. The following code snippet from [Pose2SLAMExampleExpressions.cpp](https://github.com/borglab/gtsam/blob/develop/examples/Pose2SLAMExampleExpressions.cpp) is used to create a simple Pose2 example (where the robot is moving on a plane):
95+
```c++
96+
1 ExpressionFactorGraph graph;
97+
2 Expression<Pose2> x1(1), x2(2), x3(3), x4(4), x5(5);
98+
3 graph.addExpressionFactor(x1, Pose2(0, 0, 0), priorNoise);
99+
4 graph.addExpressionFactor(between(x1,x2), Pose2(2, 0, 0 ), model);
100+
5 graph.addExpressionFactor(between(x2,x3), Pose2(2, 0, M_PI_2), model);
101+
6 graph.addExpressionFactor(between(x3,x4), Pose2(2, 0, M_PI_2), model);
102+
7 graph.addExpressionFactor(between(x4,x5), Pose2(2, 0, M_PI_2), model);
103+
8 graph.addExpressionFactor(between(x5,x2), Pose2(2, 0, M_PI_2), model);
104+
```
105+
This is what is going on:
106+
- In line 1, we create an empty factor graph.
107+
- In line 2 we create the 5 unknown poses, of type `Expression<Pose2>`, with keys 1 to 5. These are what we will optimize over.
108+
- Line 3 then creates a simple factor that gives a prior on `x1` (the first argument), namely that it is at the origin `Pose2(0, 0, 0)` (the second argument), with a particular probability density given by `priorNoise` (the third argument).
109+
- Lines 4-7 adds factors for the odometry constraints, i.e., the movement between successive poses of the robot. The function `between(t1,t2)` is implemented in [nonlinear/expressions.h](https://github.com/borglab/gtsam/blob/develop/gtsam/nonlinear/expressions.h) and is equivalent to calling the constructor Expression<T>(traits<T>::Between, t1, t2).
110+
- Finally, line 8 creates a loop closure constraint between poses x2 and x5.
111+
112+
Another good example of its use is in
113+
[SFMExampleExpressions.cpp](https://github.com/borglab/gtsam/blob/develop/examples/SFMExampleExpressions.cpp).

doc/generating/gpt_generate.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""
2+
GTSAM Copyright 2010-2025, Georgia Tech Research Corporation,
3+
Atlanta, Georgia 30332-0415
4+
All Rights Reserved
5+
6+
See LICENSE for the license information
7+
8+
Author: Porter Zach
9+
10+
This script generates interactive Python notebooks (.ipynb) that document GTSAM
11+
header files. Since inserting the text of the file directly into the prompt
12+
might be too many tokens, it retrieves the header file content from the GTSAM
13+
GitHub repository. It then sends it to OpenAI's API for processing, and saves
14+
the generated documentation as a Jupyter notebook.
15+
16+
Functions:
17+
is_url_valid(url: str) -> bool:
18+
Verifies that the supplied URL does not return a 404.
19+
20+
save_ipynb(text: str, file_path: str) -> str:
21+
Saves the provided text to a single Markdown cell in a new .ipynb file.
22+
23+
generate_ipynb(file_path: str, openai_client):
24+
Generates an interactive Python notebook for the given GTSAM header file
25+
by sending a request to OpenAI's API and saving the response.
26+
27+
Usage:
28+
Run the script with paths to GTSAM header files as arguments. For example:
29+
python gpt_generate.py gtsam/geometry/Pose3.h
30+
"""
31+
32+
import os
33+
import time
34+
import requests
35+
import argparse
36+
import nbformat as nbf
37+
from openai import OpenAI
38+
39+
_output_folder = "output"
40+
_gtsam_gh_base = "https://raw.githubusercontent.com/borglab/gtsam/refs/heads/develop/"
41+
_asst_id = "asst_na7wYBtXyGU0x5t2RdcnpxzP"
42+
_request_text = "Document the file found at {}."
43+
44+
45+
def is_url_valid(url):
46+
"""Verify that the supplied URL does not return a 404."""
47+
try:
48+
response = requests.head(url, allow_redirects=True)
49+
return response.status_code != 404
50+
except requests.RequestException:
51+
return False
52+
53+
54+
def save_ipynb(text: str, file_path: str):
55+
"""Save text to a single Markdown cell in a new .ipynb file."""
56+
script_dir = os.path.dirname(os.path.abspath(__file__))
57+
output_dir = os.path.join(script_dir, _output_folder)
58+
os.makedirs(output_dir, exist_ok=True)
59+
output_file = os.path.splitext(os.path.basename(file_path))[0] + ".ipynb"
60+
output_full_path = os.path.join(output_dir, output_file)
61+
62+
nb = nbf.v4.new_notebook()
63+
new_cell = nbf.v4.new_markdown_cell(text)
64+
nb['cells'].append(new_cell)
65+
66+
with open(output_full_path, 'w', encoding='utf-8') as file:
67+
nbf.write(nb, file)
68+
69+
return output_file
70+
71+
72+
def generate_ipynb(file_path: str, openai_client):
73+
"""Generate an interactive Python notebook for the given GTSAM header file.
74+
75+
Args:
76+
file_path (str): The fully-qualified path from the root of the gtsam
77+
repository to the header file that will be documented.
78+
openai_client (openai.OpenAI): The OpenAI client to use.
79+
"""
80+
# Create the URL to get the header file from.
81+
url = _gtsam_gh_base + file_path
82+
83+
if not is_url_valid(url):
84+
print(f"{url} was not found on the server, or an error occurred.")
85+
return
86+
87+
print(f"Sending request to OpenAI to document {url}.")
88+
89+
# Create a new thread and send the request
90+
thread = openai_client.beta.threads.create()
91+
openai_client.beta.threads.messages.create(
92+
thread_id=thread.id, role="user", content=_request_text.format(url))
93+
94+
run = openai_client.beta.threads.runs.create(thread_id=thread.id,
95+
assistant_id=_asst_id)
96+
97+
print("Waiting for the assistant to process the request...")
98+
99+
# Wait for request to be processed
100+
while True:
101+
run_status = openai_client.beta.threads.runs.retrieve(
102+
thread_id=thread.id, run_id=run.id)
103+
if run_status.status == "completed":
104+
break
105+
time.sleep(2)
106+
107+
print("Request processed. Retrieving response...")
108+
109+
# Fetch messages
110+
messages = openai_client.beta.threads.messages.list(thread_id=thread.id)
111+
# Retrieve response text and strip ```markdown ... ```
112+
text = messages.data[0].content[0].text.value.strip('`').strip('markdown')
113+
114+
# Write output to file
115+
output_filename = save_ipynb(text, file_path)
116+
117+
print(f"Response retrieved. Find output in {output_filename}.")
118+
119+
120+
if __name__ == "__main__":
121+
parser = argparse.ArgumentParser(
122+
prog="gpt_generate",
123+
description=
124+
"Generates .ipynb documentation files given paths to GTSAM header files."
125+
)
126+
parser.add_argument(
127+
"file_paths",
128+
nargs='+',
129+
help=
130+
"The paths to the header files from the root gtsam directory, e.g. 'gtsam/geometry/Pose3.h'."
131+
)
132+
args = parser.parse_args()
133+
134+
# Retrieves API key from environment variable OPENAI_API_KEY
135+
client = OpenAI()
136+
137+
for file_path in args.file_paths:
138+
generate_ipynb(file_path, client)
File renamed without changes.

gtsam/geometry/geometry.i

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,14 @@ class PinholeCamera {
10751075
pair<gtsam::Point2, bool> projectSafe(const gtsam::Point3& pw) const;
10761076
gtsam::Point2 project(const gtsam::Point3& point);
10771077
gtsam::Point2 project(const gtsam::Point3& point,
1078-
Eigen::Ref<Eigen::MatrixXd> Dpose,
1079-
Eigen::Ref<Eigen::MatrixXd> Dpoint,
1080-
Eigen::Ref<Eigen::MatrixXd> Dcal);
1078+
Eigen::Ref<Eigen::MatrixXd> Dpose);
1079+
gtsam::Point2 project(const gtsam::Point3& point,
1080+
Eigen::Ref<Eigen::MatrixXd> Dpose,
1081+
Eigen::Ref<Eigen::MatrixXd> Dpoint);
1082+
gtsam::Point2 project(const gtsam::Point3& point,
1083+
Eigen::Ref<Eigen::MatrixXd> Dpose,
1084+
Eigen::Ref<Eigen::MatrixXd> Dpoint,
1085+
Eigen::Ref<Eigen::MatrixXd> Dcal);
10811086
gtsam::Point3 backproject(const gtsam::Point2& p, double depth) const;
10821087
gtsam::Point3 backproject(const gtsam::Point2& p, double depth,
10831088
Eigen::Ref<Eigen::MatrixXd> Dresult_dpose,

gtsam/nonlinear/BatchFixedLagSmoother.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,18 @@ class GTSAM_EXPORT BatchFixedLagSmoother : public FixedLagSmoother {
3333
/// Typedef for a shared pointer to an Incremental Fixed-Lag Smoother
3434
typedef std::shared_ptr<BatchFixedLagSmoother> shared_ptr;
3535

36-
/** default constructor */
36+
/**
37+
* Construct with parameters
38+
*
39+
* @param smootherLag The length of the smoother lag. Any variable older than this amount will be marginalized out.
40+
* @param parameters The L-M optimization parameters
41+
* @param enforceConsistency A flag indicating if the optimizer should enforce probabilistic consistency by maintaining the
42+
* linearization point of all variables involved in linearized/marginal factors at the edge of the
43+
* smoothing window.
44+
*/
3745
BatchFixedLagSmoother(double smootherLag = 0.0, const LevenbergMarquardtParams& parameters = LevenbergMarquardtParams(), bool enforceConsistency = true) :
38-
FixedLagSmoother(smootherLag), parameters_(parameters), enforceConsistency_(enforceConsistency) { }
46+
FixedLagSmoother(smootherLag), parameters_(parameters), enforceConsistency_(enforceConsistency) {
47+
}
3948

4049
/** destructor */
4150
~BatchFixedLagSmoother() override {}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "283174f8",
6+
"metadata": {},
7+
"source": [
8+
"# BatchFixedLagSmoother\n",
9+
"\n",
10+
"## Overview\n",
11+
"\n",
12+
"The `IncrementalFixedLagSmoother` is a [FixedLagSmoother](FixedLagSmoother.ipynb) that uses [LevenbergMarquardtOptimizer](LevenbergMarquardtOptimizer.ipynb) for batch optimization.\n",
13+
"\n",
14+
"This fixed lag smoother will **batch-optimize** at every iteration, but warm-started from the last estimate."
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "92b4f851",
20+
"metadata": {},
21+
"source": [
22+
"## API\n",
23+
"\n",
24+
"### Constructor\n",
25+
"\n",
26+
"You construct a `BatchFixedLagSmoother` object with the following parameters:\n",
27+
"\n",
28+
"- **smootherLag**: The length of the smoother lag. Any variable older than this amount will be marginalized out. *(Default: 0.0)*\n",
29+
"- **parameters**: The Levenberg-Marquardt optimization parameters. *(Default: `LevenbergMarquardtParams()`)* \n",
30+
"- **enforceConsistency**: A flag indicating whether the optimizer should enforce probabilistic consistency by maintaining the linearization point of all variables involved in linearized/marginal factors at the edge of the smoothing window. *(Default: `true`)*\n",
31+
"\n",
32+
"### Smoothing and Optimization\n",
33+
"\n",
34+
"- **update**: This method is the core of the `BatchFixedLagSmoother`. It processes new factors and variables, updating the current estimate of the state. The update method also manages the marginalization of variables that fall outside the fixed lag window.\n",
35+
"\n",
36+
"### Computational Considerations\n",
37+
"\n",
38+
"Every call to `update` triggers a batch LM optimization: use the parameters to control the convergence thresholds to bound computation to fit within your application."
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"id": "4a2bdd3e",
44+
"metadata": {},
45+
"source": [
46+
"## Internals\n",
47+
"\n",
48+
"- **marginalize**: This function handles the marginalization of variables that are no longer within the fixed lag window. Marginalization is a crucial step in maintaining the size of the factor graph, ensuring that only relevant variables are kept for optimization.\n"
49+
]
50+
}
51+
],
52+
"metadata": {
53+
"language_info": {
54+
"name": "python"
55+
}
56+
},
57+
"nbformat": 4,
58+
"nbformat_minor": 5
59+
}

0 commit comments

Comments
 (0)