Skip to content

Commit

Permalink
feat(tests): add unit tests (#28)
Browse files Browse the repository at this point in the history
* feat(tests): add unit tests

* feat(test): add .yaml for tests running

* fix(ci): fix k3d requirements

* fix(backend): fix unused_features property (#29)

* fix(backend): fix unused_features property

* fix(ci): fix flake8 issue
  • Loading branch information
pmokeev authored Dec 5, 2023
1 parent 9647ba1 commit 3374af3
Show file tree
Hide file tree
Showing 22 changed files with 734 additions and 5 deletions.
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
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

0 comments on commit 3374af3

Please sign in to comment.