Skip to content

Commit

Permalink
Make BaseEdge.calc_chi2_gradient_hessian return lists instead of dicts (
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Nov 8, 2023
1 parent ba631b5 commit 83f7b70
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions graphslam/edge/base_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def calc_chi2_gradient_hessian(self):
-------
float
The :math:`\chi^2` error for the edge
dict
list[tuple[int, np.ndarray]]
The edge's contribution(s) to the gradient
dict
list[tuple[tuple[int, int], np.ndarray]]
The edge's contribution(s) to the Hessian
"""
Expand All @@ -99,8 +99,8 @@ def calc_chi2_gradient_hessian(self):
# fmt: off
return (
chi2,
{v.gradient_index: np.dot(np.dot(np.transpose(err), self.information), jacobian) for v, jacobian in zip(self.vertices, jacobians)},
{(self.vertices[i].gradient_index, self.vertices[j].gradient_index): np.dot(np.dot(np.transpose(jacobians[i]), self.information), jacobians[j]) for i in range(len(jacobians)) for j in range(i, len(jacobians))},
[(v.gradient_index, np.dot(np.dot(np.transpose(err), self.information), jacobian)) for v, jacobian in zip(self.vertices, jacobians)],
[((self.vertices[i].gradient_index, self.vertices[j].gradient_index), np.dot(np.dot(np.transpose(jacobians[i]), self.information), jacobians[j])) for i in range(len(jacobians)) for j in range(i, len(jacobians))],
)
# fmt: on

Expand Down
4 changes: 2 additions & 2 deletions graphslam/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ def update(chi2_grad_hess, incoming):
"""
chi2_grad_hess.chi2 += incoming[0]

for idx, contrib in incoming[1].items():
for idx, contrib in incoming[1]:
chi2_grad_hess.gradient[idx] += contrib

for (idx1, idx2), contrib in incoming[2].items():
for (idx1, idx2), contrib in incoming[2]:
if idx1 <= idx2:
chi2_grad_hess.hessian[idx1, idx2] += contrib
else:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_base_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def test_calc_chi2_gradient_hessian(self):

self.assertEqual(chi2, 2.0)

self.assertAlmostEqual(np.linalg.norm(gradient[v1.gradient_index] + np.ones(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(gradient[v2.gradient_index] - np.ones(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(gradient[0][1] + np.ones(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(gradient[1][1] - np.ones(2)), 0.0)

self.assertAlmostEqual(np.linalg.norm(hessian[(v1.gradient_index, v1.gradient_index)] - np.eye(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(hessian[(v1.gradient_index, v2.gradient_index)] + np.eye(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(hessian[(v2.gradient_index, v2.gradient_index)] - np.eye(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(hessian[0][1] - np.eye(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(hessian[1][1] + np.eye(2)), 0.0)
self.assertAlmostEqual(np.linalg.norm(hessian[2][1] - np.eye(2)), 0.0)

def test_equals(self):
"""Test that the ``equals`` method works as expected."""
Expand Down

0 comments on commit 83f7b70

Please sign in to comment.