symforce-org / symforce

Fast symbolic computation, code generation, and nonlinear optimization for robotics
https://symforce.org
Apache License 2.0
1.41k stars 145 forks source link

Incompatible function arguments error when optimizing Unit3 in Python #305

Closed johhat closed 1 year ago

johhat commented 1 year ago

Describe the bug Optimizing a factor with an sf.Unit3 type as input in python yields an incompatible function arguments error.

To Reproduce See the error of the following example

import symforce

symforce.set_epsilon_to_symbol("epsilon")

import sym
import numpy as np

import symforce.symbolic as sf
from symforce.opt.factor import Factor
from symforce.opt.optimizer import Optimizer
from symforce.values import Values

def residual(unit3: sf.Unit3) -> sf.V3:
    return unit3.to_unit_vector()

def main():
    values = Values(
        {
            "unit3": sym.Unit3.from_vector(np.array([0, 0, 1]), sym.epsilon),
        }
    )

    optimizer = Optimizer(
        factors=[
            Factor(
                residual=residual,
                keys=["unit3"],
            )
        ],
        optimized_keys=["unit3"],
    )

    # Fails here
    result = optimizer.optimize(values)

if __name__ == "__main__":
    main()

Expected behavior I'd expect the optimization to run without error.

Environment

Example dockerfile

FROM python:3.8.16
RUN pip3 install symforce
COPY example.py /example.py
CMD ["python", "example.py"]

Additional context Excerpt of the error message

TypeError: set(): incompatible function arguments. The following argument types are supported:
    1. (self: cc_sym.Values, key: cc_sym.Key, value: float) -> bool
    2. (self: cc_sym.Values, key: index_entry_t, value: float) -> None
    3. (self: cc_sym.Values, key: cc_sym.Key, value: Rot2) -> bool
    ...

Invoked with: <Valuesd entries=0 array=0 storage_dim=0 tangent_dim=0
>, x_0, <Unit3 [0.0, 0.0, 0.0, 1.0]>
aaron-skydio commented 1 year ago

Thanks for reporting, we forgot to add Unit3 to the Python wrappers when we added it - will have a fix shortly