borglab / gtsam

GTSAM is a library of C++ classes that implement smoothing and mapping (SAM) in robotics and vision, using factor graphs and Bayes networks as the underlying computing paradigm rather than sparse matrices.
http://gtsam.org
Other
2.62k stars 766 forks source link

ISAM2Result.newFactorsIndices() missing in Python? #1133

Closed roger- closed 2 years ago

roger- commented 2 years ago

I'm trying to remove some factors from ISAM2 (similar to #1119), and it seems that I need to track their indices using gtsame.ISAM2Result.newFactorsIndices.

Unfortunately this seems to be missing in Python:

> isam = gtsam.ISAM2()
...
> res = isam.update(graph, init_vals)
> type(res)
<class 'gtsam.gtsam.ISAM2Result'>
> print(res.newFactorsIndices)
AttributeError: 'gtsam.gtsam.ISAM2Result' object has no attribute 'newFactorsIndices'

Is there a workaround or am I doing something wrong?

Setup: gtsam 4.1.1 / Ubuntu 20.04.4 / Python 3.8

hovren commented 2 years ago

I had the same problem recently, and I ended up with this work around:

to_remove = set()
to_remove.add(factor)
indices_to_remove = [i for i in range(graph.size()) if curr_graph.at(i) in to_remove]
isam.update(gtsam.NonlinearFactorGraph(), gtsam.Values(), indices_to_remove)

I am not 100% sure that this is a valid solution though.

roger- commented 2 years ago

That's good idea, thanks! Seems like I'll have to keep track of the entire graph instead of just handing it off to ISAM2, but should work for now.

Seems like it would be easier if factors had keys, like variables do. Wonder why indices are used?

hovren commented 2 years ago

I accessed the factors using isam.getFactorsUnsafe(), so there should be no reason to make a full graph yourself.

As in, above there should be a line that says:

graph = isam.getFactorsUnsafe()

roger- commented 2 years ago

Ah, missed that method. Thanks!

varunagrawal commented 2 years ago

PR #1146 should add the desired method as getNewFactorsIndices().