Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): add unit tests #28

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests

on: [push]

jobs:
test-and-lint:
strategy:
matrix:
os: [ ubuntu-22.04 ]
python-version: [ "3.10" ]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Python${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt

- name: Run pytest
run: |
python -m pytest tests/
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ numpy==1.25.2
open3d==0.17.0
octreelib @ git+https://github.com/true-real-michael/octreelib@v0.0.5
scikit-learn==1.3.1
PyYAML~=6.0.1
PyYAML~=6.0.1
pytest~=7.4.3
k3d==2.16.0
6 changes: 5 additions & 1 deletion slam/backend/mrob_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ def __get_unused_features(self) -> List[int]:
unused_features: List[int]
IDs list of unused features
"""
robust_mask = self._graph.get_eigen_factors_robust_mask()
try:
robust_mask = self._graph.get_eigen_factors_robust_mask()
except AttributeError:
return []

unused_features = []

for voxel_id, plane_id in self._planes.items():
Expand Down
11 changes: 7 additions & 4 deletions slam/utils/pose_readwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ def write(filepath: str, pose: Array4x4[float]) -> None:
pose: Array4x4[float]
Matrix with pose values to write
"""
assert pose.shape == (4, 4)

with open(filepath, "w+") as file:
for pose_line in pose:
for value in pose_line:
file.write(f"{value} ")
file.write("\n")
for ind, pose_line in enumerate(pose):
file.write(" ".join(str(x) for x in pose_line))

if ind != pose.shape[0]:
file.write("\n")
4 changes: 4 additions & 0 deletions tests/data/poses/correct_pose.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 2 3 4
1 2 3 4
1 2 3 4
0 0 0 1
2 changes: 2 additions & 0 deletions tests/data/poses/incorrect_pose.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 2 3 4
1 2
24 changes: 24 additions & 0 deletions tests/data/yaml_configurations/correct_configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dataset:
path: "path/to/dataset"
patches:
start: 0
end: 100
step: 10
iterations: 1
grid:
voxel_edge_length: 8
subdividers:
size: 2
segmenters:
ransac:
threshold: 0.01
initial_points: 6
iterations: 5000
backend:
type: "eigen_factor"
parameters:
iterations_number: 5000
robust_type: HUBER
output:
visualization_path: "output"
optimisation_path: "output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dataset:
path: "path/to/dataset"
patches:
start: 0
end: 100
step: 10
iterations: 1
grid:
voxel_edge_length: 8
subdividers:
size: 2
segmenters:
ransac:
threshold: 0.01
initial_points: 6
iterations: 5000
output:
visualization_path: "output"
optimisation_path: "output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
grid:
voxel_edge_length: 8
subdividers:
size: 2
segmenters:
ransac:
threshold: 0.01
initial_points: 6
iterations: 5000
backend:
type: "eigen_factor"
parameters:
iterations_number: 5000
robust_type: HUBER
output:
visualization_path: "output"
optimisation_path: "output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dataset:
path: "path/to/dataset"
grid:
voxel_edge_length: 8
subdividers:
size: 2
segmenters:
ransac:
threshold: 0.01
initial_points: 6
iterations: 5000
backend:
type: "eigen_factor"
parameters:
iterations_number: 5000
robust_type: HUBER
output:
visualization_path: "output"
optimisation_path: "output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dataset:
path: "path/to/dataset"
patches:
start: 0
end: 100
step: 10
iterations: 1
grid:
voxel_edge_length: 8
subdividers:
size: 2
backend:
type: "eigen_factor"
parameters:
iterations_number: 5000
robust_type: HUBER
output:
visualization_path: "output"
optimisation_path: "output"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
dataset:
path: "path/to/dataset"
patches:
start: 0
end: 100
step: 10
iterations: 1
grid:
voxel_edge_length: 8
segmenters:
ransac:
threshold: 0.01
initial_points: 6
iterations: 5000
backend:
type: "eigen_factor"
parameters:
iterations_number: 5000
robust_type: HUBER
output:
visualization_path: "output"
optimisation_path: "output"
42 changes: 42 additions & 0 deletions tests/test_cape_segmenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
import pytest

import random

from slam.segmenter import CAPESegmenter
from slam.typing import ArrayNx3


@pytest.mark.parametrize(
"points, correlation, points_size",
[
(
[
np.array(
[random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]
)
for _ in range(100)
],
100,
100,
),
(
[
np.array(
[random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]
)
for _ in range(100)
],
0,
0,
),
],
)
def test_cape_segmenter(
points: ArrayNx3[float],
correlation: float,
points_size: int,
):
cape_segmenter = CAPESegmenter(correlation)
actual_points = cape_segmenter(points)
assert len(actual_points) == points_size
23 changes: 23 additions & 0 deletions tests/test_count_segmenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import pytest

from slam.segmenter import CountSegmenter
from slam.typing import ArrayNx3


@pytest.mark.parametrize(
"points, count, expected_points",
[
(np.array([[0, 0, 0]]), 0, np.array([[0, 0, 0]])),
(np.array([[0, 0, 0]]), 10, np.empty((0, 3))),
],
)
def test_count_segmenter(
points: ArrayNx3[float],
count: int,
expected_points: ArrayNx3[float],
):
count_segmenter = CountSegmenter(count=count)
actual_points = count_segmenter(points)
assert len(actual_points) == len(expected_points)
assert np.all(actual_points == expected_points)
46 changes: 46 additions & 0 deletions tests/test_count_subdivider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import numpy as np
import pytest

from slam.subdivider import CountSubdivider
from slam.typing import ArrayNx3


@pytest.mark.parametrize(
"points, count, expected_decision",
[
(
np.array(
[
[0, 0, 0],
[0, 1, 0],
[1, 1, 1],
]
),
1,
True,
),
(
np.array(
[
[0, 0, 0],
[0, 1, 0],
[1, 1, 1],
]
),
5,
False,
),
(
np.empty((0, 3)),
1,
False,
),
],
)
def test_count_subdivider(
points: ArrayNx3[float],
count: float,
expected_decision: bool,
):
count_subdivider = CountSubdivider(count=count)
assert expected_decision == count_subdivider(points)
46 changes: 46 additions & 0 deletions tests/test_eigen_value_subdivider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import numpy as np
import pytest

import random

from slam.subdivider import EigenValueSubdivider
from slam.typing import ArrayNx3


@pytest.mark.parametrize(
"points, eigen_value, expected_decision",
[
(
[
np.array(
[random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]
)
for _ in range(100)
],
1,
False,
),
(
[
np.array(
[random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]
)
for _ in range(100)
],
1000,
False,
),
(
np.empty((0, 3)),
1,
False,
),
],
)
def test_eigen_value_subdivider(
points: ArrayNx3[float],
eigen_value: float,
expected_decision: bool,
):
eigen_value_subdivider = EigenValueSubdivider(value=eigen_value)
assert expected_decision == eigen_value_subdivider(points)
17 changes: 17 additions & 0 deletions tests/test_empty_voxel_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np
import pytest

from slam.filter import EmptyVoxel
from slam.typing import ArrayNx3


@pytest.mark.parametrize(
"points, expected_decision",
[
(np.array([0, 0, 0]), True),
(np.empty(0), False),
],
)
def test_empty_voxel(points: ArrayNx3[float], expected_decision: bool):
empty_voxel_filter = EmptyVoxel()
assert empty_voxel_filter(points) == expected_decision
Loading