Skip to content

Commit

Permalink
Move edge classes to edge_types.py (#73)
Browse files Browse the repository at this point in the history
* Move edge classes to edge_types.py

* black
  • Loading branch information
JeffLIrion committed Nov 11, 2023
1 parent 391b4f3 commit 85f549d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
44 changes: 44 additions & 0 deletions tests/edge_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np

from graphslam.edge.base_edge import BaseEdge
from graphslam.util import upper_triangular_matrix_to_full_matrix


class BaseEdgeForTests(BaseEdge):
Expand All @@ -26,6 +27,49 @@ def plot(self, color=""):
"""Not supported, so don't do anything."""


class EdgeWithoutToG2OWithoutFromG2O(BaseEdgeForTests):
"""An edge class without ``to_g2o`` and ``from_g2o`` support.
This class is only compatible with ``PoseR2`` poses.
"""

def calc_error(self):
"""Return an error vector."""
return np.array([1.0, 2.0])


class EdgeWithToG2OWithoutFromG2O(EdgeWithoutToG2OWithoutFromG2O):
"""An edge class with a ``to_g2o`` method but not a ``from_g2o`` method."""

def to_g2o(self):
"""Write to g2o format."""
# fmt: off
return "TestEdge {} {} {} ".format(self.vertex_ids[0], self.estimate[0], self.estimate[1]) + " ".join([str(x) for x in self.information[np.triu_indices(2, 0)]]) + "\n"
# fmt: on


class EdgeWithoutToG2OWithFromG2O(EdgeWithoutToG2OWithoutFromG2O):
"""An edge class with a ``from_g2o`` method but not a ``to_g2o`` method."""

@classmethod
def from_g2o(cls, line):
"""Write to g2o format."""
if line.startswith("TestEdge "):
numbers = line[len("TestEdge "):].split() # fmt: skip
arr = np.array([float(number) for number in numbers[1:]], dtype=np.float64)
vertex_ids = [int(numbers[0])]
estimate = arr[:2]
information = upper_triangular_matrix_to_full_matrix(arr[2:], 2)
return cls(vertex_ids, information, estimate)

return None


class EdgeWithToG2OWithFromG2O(EdgeWithToG2OWithoutFromG2O, EdgeWithoutToG2OWithFromG2O):
"""An edge class with ``to_g2o`` and ``from_g2o`` methods."""


class EdgeOPlus(BaseEdgeForTests):
"""A simple edge class for testing."""

Expand Down
51 changes: 6 additions & 45 deletions tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,17 @@
from graphslam.graph import Graph
from graphslam.load import load_g2o, load_g2o_r2, load_g2o_r3, load_g2o_se2, load_g2o_se3
from graphslam.pose.r2 import PoseR2
from graphslam.util import upper_triangular_matrix_to_full_matrix
from graphslam.vertex import Vertex

from .edge_types import BaseEdgeForTests
from .edge_types import (
EdgeWithoutToG2OWithoutFromG2O,
EdgeWithToG2OWithoutFromG2O,
EdgeWithoutToG2OWithFromG2O,
EdgeWithToG2OWithFromG2O,
)
from .patchers import FAKE_FILE, open_fake_file


class EdgeWithoutToG2OWithoutFromG2O(BaseEdgeForTests):
"""An edge class without ``to_g2o`` and ``from_g2o`` support.
This class is only compatible with ``PoseR2`` poses.
"""

def calc_error(self):
"""Return an error vector."""
return np.array([1.0, 2.0])


class EdgeWithToG2OWithoutFromG2O(EdgeWithoutToG2OWithoutFromG2O):
"""An edge class with a ``to_g2o`` method but not a ``from_g2o`` method."""

def to_g2o(self):
"""Write to g2o format."""
# fmt: off
return "TestEdge {} {} {} ".format(self.vertex_ids[0], self.estimate[0], self.estimate[1]) + " ".join([str(x) for x in self.information[np.triu_indices(2, 0)]]) + "\n"
# fmt: on


class EdgeWithoutToG2OWithFromG2O(EdgeWithoutToG2OWithoutFromG2O):
"""An edge class with a ``from_g2o`` method but not a ``to_g2o`` method."""

@classmethod
def from_g2o(cls, line):
"""Write to g2o format."""
if line.startswith("TestEdge "):
numbers = line[len("TestEdge "):].split() # fmt: skip
arr = np.array([float(number) for number in numbers[1:]], dtype=np.float64)
vertex_ids = [int(numbers[0])]
estimate = arr[:2]
information = upper_triangular_matrix_to_full_matrix(arr[2:], 2)
return cls(vertex_ids, information, estimate)

return None


class EdgeWithToG2OWithFromG2O(EdgeWithToG2OWithoutFromG2O, EdgeWithoutToG2OWithFromG2O):
"""An edge class with ``to_g2o`` and ``from_g2o`` methods."""


class TestLoad(unittest.TestCase):
"""Tests for the ``load`` functions."""

Expand Down

0 comments on commit 85f549d

Please sign in to comment.