Skip to content

Commit da1f55c

Browse files
committed
test(data): add unit tests the data factory
1 parent 9a8c61f commit da1f55c

3 files changed

Lines changed: 114 additions & 56 deletions

File tree

edge_mender/data_factory.py

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,17 @@
1+
"""Contains a class with a set of test case data sets."""
2+
13
import cc3d
24
import fill_voids
35
import numpy as np
46
from numpy.typing import NDArray
57

68

79
class DataFactory:
8-
@staticmethod
9-
def kill_you() -> NDArray:
10-
# Create initial data
11-
data = np.zeros((10, 10, 10))
12-
data[3:-3, 3:-3, 3:-3] = 1
13-
14-
# Case 1 - simple extrusion
15-
data[2, 4, 4] = 1
16-
data[2, 5, 5] = 1
17-
18-
# Case 2 - double extrusion
19-
data[4, 4, 2] = 1
20-
data[4, 4, 1] = 1
21-
data[5, 5, 2] = 1
22-
data[5, 5, 1] = 1
23-
24-
# Case 3 - triple extrusion
25-
data[4, 7, 4] = 1
26-
data[4, 8, 4] = 1
27-
data[4, 9, 4] = 1
28-
data[5, 7, 5] = 1
29-
data[5, 8, 5] = 1
30-
data[5, 9, 5] = 1
31-
32-
# Case 4 - stairs
33-
data[4, 4, 7] = 1
34-
data[4, 4, 8] = 1
35-
data[4, 5, 7] = 1
36-
data[5, 5, 7] = 1
37-
data[5, 5, 8] = 1
38-
data[5, 5, 9] = 1
39-
40-
# Case 5 - ceiling
41-
data[4, 1, 4] = 1
42-
data[4, 1, 5] = 1
43-
data[4, 2, 4] = 1
44-
data[5, 1, 4] = 1
45-
data[5, 1, 5] = 1
46-
data[5, 2, 5] = 1
47-
48-
# Case 6 - checkboard
49-
data[7, 5, 4] = 1
50-
data[7, 4, 5] = 1
51-
data[8, 4, 4] = 1
52-
data[8, 3, 4] = 1
53-
data[8, 3, 3] = 1
54-
data[7, 3, 3] = 1
55-
data[8, 5, 5] = 1
56-
data[8, 6, 5] = 1
57-
data[8, 6, 6] = 1
58-
data[7, 6, 6] = 1
59-
60-
return data
10+
"""A set of test cases for testing, evaluation, and demonstration."""
6111

6212
@staticmethod
6313
def simple_extrusion() -> NDArray:
14+
"""Create a test case with a simple extrusion."""
6415
# Create initial data
6516
data = np.zeros((4, 4, 4))
6617

@@ -75,6 +26,7 @@ def simple_extrusion() -> NDArray:
7526

7627
@staticmethod
7728
def double_extrusion() -> NDArray:
29+
"""Create a test case with a double extrusion."""
7830
# Create initial data
7931
data = np.zeros((4, 5, 4))
8032

@@ -89,6 +41,7 @@ def double_extrusion() -> NDArray:
8941

9042
@staticmethod
9143
def triple_extrusion() -> NDArray:
44+
"""Create a test case with a triple extrusion."""
9245
# Create initial data
9346
data = np.zeros((4, 6, 4))
9447

@@ -103,6 +56,7 @@ def triple_extrusion() -> NDArray:
10356

10457
@staticmethod
10558
def stairs() -> NDArray:
59+
"""Create a test case with stairs."""
10660
# Create initial data
10761
data = np.zeros((4, 6, 4))
10862

@@ -122,6 +76,7 @@ def stairs() -> NDArray:
12276

12377
@staticmethod
12478
def ceiling() -> NDArray:
79+
"""Create a test case with a ceiling."""
12580
# Create initial data
12681
data = np.zeros((4, 5, 4))
12782

@@ -139,6 +94,7 @@ def ceiling() -> NDArray:
13994

14095
@staticmethod
14196
def double_tower_ceiling() -> NDArray:
97+
"""Create a test case with a double tower ceiling."""
14298
# Create initial data
14399
data = np.zeros((4, 5, 5))
144100

@@ -165,6 +121,7 @@ def double_tower_ceiling() -> NDArray:
165121

166122
@staticmethod
167123
def hanging_points() -> NDArray:
124+
"""Create a test case with hanging points."""
168125
# Create initial data
169126
data = np.zeros((5, 5, 3))
170127

@@ -180,6 +137,7 @@ def hanging_points() -> NDArray:
180137

181138
@staticmethod
182139
def checkerboard() -> NDArray:
140+
"""Create a test case with a checkerboard pattern."""
183141
# Create initial data
184142
data = np.zeros((4, 5, 5))
185143

@@ -206,7 +164,7 @@ def checkerboard() -> NDArray:
206164

207165
@staticmethod
208166
def hole() -> NDArray:
209-
"""faces_to_flip=[44, 45, 54, 55, 66, 67, 68, 69, 70, 71]"""
167+
"""Create a test case with a hole in the middle of the data."""
210168
# Create initial data
211169
data = np.zeros((5, 5, 5))
212170

@@ -228,12 +186,81 @@ def hole() -> NDArray:
228186

229187
return data
230188

189+
@staticmethod
190+
def kill_you() -> NDArray:
191+
"""Create a complex test case combining several simpler cases."""
192+
# Create initial data
193+
data = np.zeros((10, 10, 10))
194+
data[3:-3, 3:-3, 3:-3] = 1
195+
196+
# Case 1 - simple extrusion
197+
data[2, 4, 4] = 1
198+
data[2, 5, 5] = 1
199+
200+
# Case 2 - double extrusion
201+
data[4, 4, 2] = 1
202+
data[4, 4, 1] = 1
203+
data[5, 5, 2] = 1
204+
data[5, 5, 1] = 1
205+
206+
# Case 3 - triple extrusion
207+
data[4, 7, 4] = 1
208+
data[4, 8, 4] = 1
209+
data[4, 9, 4] = 1
210+
data[5, 7, 5] = 1
211+
data[5, 8, 5] = 1
212+
data[5, 9, 5] = 1
213+
214+
# Case 4 - stairs
215+
data[4, 4, 7] = 1
216+
data[4, 4, 8] = 1
217+
data[4, 5, 7] = 1
218+
data[5, 5, 7] = 1
219+
data[5, 5, 8] = 1
220+
data[5, 5, 9] = 1
221+
222+
# Case 5 - ceiling
223+
data[4, 1, 4] = 1
224+
data[4, 1, 5] = 1
225+
data[4, 2, 4] = 1
226+
data[5, 1, 4] = 1
227+
data[5, 1, 5] = 1
228+
data[5, 2, 5] = 1
229+
230+
# Case 6 - checkboard
231+
data[7, 5, 4] = 1
232+
data[7, 4, 5] = 1
233+
data[8, 4, 4] = 1
234+
data[8, 3, 4] = 1
235+
data[8, 3, 3] = 1
236+
data[7, 3, 3] = 1
237+
data[8, 5, 5] = 1
238+
data[8, 6, 5] = 1
239+
data[8, 6, 6] = 1
240+
data[7, 6, 6] = 1
241+
242+
return data
243+
231244
@staticmethod
232245
def random(*, size: int = 16, seed: int | None = None) -> NDArray:
246+
"""Create a random test case.
247+
248+
Parameters
249+
----------
250+
size : int, optional
251+
The size of the data cube, by default 16
252+
seed : int | None, optional
253+
The random seed, by default None
254+
255+
Returns
256+
-------
257+
NDArray
258+
The random test case.
259+
"""
233260
rng = np.random.default_rng(seed)
234261
data = rng.integers(0, 2, (size, size, size), dtype=np.uint8)
235262
data = cc3d.largest_k(data, k=1, connectivity=6, binary_image=True).astype(
236-
np.uint8
263+
np.uint8,
237264
)
238265
data = fill_voids.fill(data, in_place=True)
239266
data = fill_voids.fill(data, in_place=True)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
import pytest
3+
from numpy.typing import NDArray
4+
5+
from edge_mender.data_factory import DataFactory
6+
7+
8+
@pytest.mark.parametrize(
9+
"data",
10+
[
11+
DataFactory.simple_extrusion(),
12+
DataFactory.double_extrusion(),
13+
DataFactory.triple_extrusion(),
14+
DataFactory.stairs(),
15+
DataFactory.ceiling(),
16+
DataFactory.double_tower_ceiling(),
17+
DataFactory.hanging_points(),
18+
DataFactory.checkerboard(),
19+
DataFactory.hole(),
20+
DataFactory.kill_you(),
21+
DataFactory.random(seed=0),
22+
],
23+
)
24+
def test_data_factory(data: NDArray) -> None:
25+
"""Test that the data factory creates the expected data."""
26+
assert data is not None
27+
assert isinstance(data, np.ndarray)
28+
assert data.ndim == 3 # noqa: PLR2004
29+
assert np.any(data == 1)

edge_mender/tests/test_edge_mender.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
DataFactory.hanging_points(),
2424
DataFactory.checkerboard(),
2525
DataFactory.hole(),
26+
DataFactory.kill_you(),
2627
],
2728
)
2829
def test_validate(data: NDArray, spacing: tuple[float, float, float]) -> None:
@@ -192,7 +193,8 @@ def test_find_non_manifold_edges(
192193
DataFactory.checkerboard(),
193194
# TODO: This test case fails due to a bug with SurfaceNets from VTK
194195
# https://gitlab.kitware.com/vtk/vtk/-/issues/19156, fixed, but not released yet
195-
# DataFactory.hole(.)
196+
# DataFactory.hole(), # noqa: ERA001
197+
DataFactory.kill_you(),
196198
],
197199
)
198200
def test_repair(data: NDArray) -> None:

0 commit comments

Comments
 (0)