1"""Contract: the entry points refuse a wrong-length triple, NaN/Inf or inadmissible value with ValueError.
3Validation lives in the binding (src/pnm_bindings_checks.hpp, used from src/pnm_bindings.cpp); the
4message names the function, the parameter, the admissible range with its dimension and the
5offending value. Each validated argument is covered twice: every bad value raises ValueError naming
6the parameter, and a valid call still returns what it returned before.
8origin_zyx and spacing_zyx are narrowed to float32 before the kernels, so they are checked as
9float32 (1e39 is not finite there, 1e-50 is not > 0). origin_zyx is a coordinate: finite only, any
10sign. The SDF values themselves are not scanned. extract_network_flow(_mpi) are outside this
11round (owned by paper A7's branch) and are not covered here.
13Run: PYTHONPATH=<build> python -m pytest -q tests/python/test_input_validation.py
22NAN, INF = math.nan, math.inf
26HAS_MPI = hasattr(pnm,
"mpi_block")
30 """One sphere of radius 3.5 in an 8^3 box: one pore."""
31 z, y, x = np.meshgrid(*(np.arange(N, dtype=np.float32),) * 3, indexing=
"ij")
32 return (3.5 - np.sqrt((x - 4) ** 2 + (y - 4) ** 2 + (z - 4) ** 2)).astype(np.float32)
36 with pytest.raises(ValueError, match=re.escape(param)):
42BAD_ORIGIN = [([0.0, 0.0],
"origin_zyx"), ([],
"origin_zyx"), ([0.0] * 4,
"origin_zyx"),
43 ([NAN, 0.0, 0.0],
"origin_zyx[0]"), ([0.0, INF, 0.0],
"origin_zyx[1]"),
44 ([0.0, 0.0, -INF],
"origin_zyx[2]"), ([1e39, 0.0, 0.0],
"origin_zyx[0]")]
45BAD_SPACING = [([1.0, 1.0],
"spacing_zyx"), ([],
"spacing_zyx"), ([1.0] * 4,
"spacing_zyx"),
46 ([0.0, 1.0, 1.0],
"spacing_zyx[0]"), ([1.0, -1.0, 1.0],
"spacing_zyx[1]"),
47 ([1.0, 1.0, NAN],
"spacing_zyx[2]"), ([INF, 1.0, 1.0],
"spacing_zyx[0]"),
48 ([1e-50, 1.0, 1.0],
"spacing_zyx[0]"), ([1e39, 1.0, 1.0],
"spacing_zyx[0]")]
53@pytest.mark.parametrize(
"args, param", [
54 ((NAN, 0, 0, 1),
"x"), ((0, INF, 0, 1),
"y"), ((0, 0, -INF, 1),
"z"),
55 ((0, 0, 0, -1),
"radius"), ((0, 0, 0, NAN),
"radius"), ((0, 0, 0, 1e39),
"radius")])
60@pytest.mark.parametrize("args", [(1.5, -2.0, 3.0, 0.75), (0, 0, 0, 0)])
63 assert (p.x, p.y, p.z, p.radius) == args
69 "extract_pores":
lambda sdf, o, s: pnm.extract_pores(sdf, o, s),
70 "extract_pore_network":
lambda sdf, o, s: pnm.extract_pore_network(sdf, o, s),
73 GRID_FUNCS[
"extract_pore_network_mpi"] = (
77@pytest.mark.parametrize("name", sorted(GRID_FUNCS))
78@pytest.mark.parametrize("origin, param", BAD_ORIGIN)
80 _raises(
lambda: GRID_FUNCS[name](
_sdf(), origin, S), f
"{name}: {param} must be")
83@pytest.mark.parametrize("name", sorted(GRID_FUNCS))
84@pytest.mark.parametrize("spacing, param", BAD_SPACING)
86 _raises(
lambda: GRID_FUNCS[name](
_sdf(), O, spacing), f
"{name}: {param} must be")
90 """A valid physical grid (negative origin, anisotropic spacing) still extracts the one pore."""
91 o, s = [-1.0, 2.0, -3.0], [0.5, 0.25, 2.0]
92 pores = pnm.extract_pores(
_sdf(), o, s)
93 fused, seg, conns = pnm.extract_pore_network(
_sdf(), o, s)
94 assert len(pores) == len(fused) == 1
and seg.shape == (N, N, N)
95 assert (pores[0].z, pores[0].y, pores[0].x) == pytest.approx((-1.0 + 4 * 0.5, 2.0 + 4 * 0.25,
99 assert len(mpores) == 1
and np.array_equal(mseg, seg)
and np.array_equal(mconns, conns)
104@pytest.mark.parametrize("spacing, param", BAD_SPACING)
106 _raises(
lambda: pnm.segment_volume(
_sdf(), spacing), f
"segment_volume: {param} must be")
110 seg = pnm.segment_volume(
_sdf(), [0.5, 0.25, 2.0])
111 assert seg.shape == (N, N, N)
and seg.max() == 1
116@pytest.mark.parametrize(
"shape, param", [
117 ([-1, -1, N ** 3],
"shape_zyx[0]"), ([N, -N, N],
"shape_zyx[1]"), ([N, N, -1],
"shape_zyx[2]"),
118 ([2 ** 30, 2 ** 30, 16],
"shape_zyx")])
120 seg = pnm.segment_volume(
_sdf(), S)
121 flat = seg.ravel()
if shape[0] != 2 ** 30
else np.zeros(0, np.int32)
122 _raises(
lambda: pnm.extract_topology(flat, shape), f
"extract_topology: {param} must be")
126 """The shape checks that predate this round stay RuntimeError (length, mismatch, rank)."""
127 seg = pnm.segment_volume(
_sdf(), S)
128 with pytest.raises(RuntimeError, match=
"shape_zyx must be"):
129 pnm.extract_topology(seg.ravel(), [N, N])
130 with pytest.raises(RuntimeError, match=
"does not match"):
131 pnm.extract_topology(seg.ravel(), [N, N, N + 1])
132 with pytest.raises(RuntimeError, match=
"or flat"):
133 pnm.extract_topology(np.zeros((N, N), np.int32))
137 seg = pnm.segment_volume(
_sdf(), S)
138 conns = pnm.extract_topology(seg)
139 assert np.array_equal(pnm.extract_topology(seg.ravel(), [N, N, N]), conns)
140 assert pnm.extract_topology(np.zeros(0, np.int32), [0, N, N]).shape == (0, 2)
145mpi = pytest.mark.skipif(
not HAS_MPI, reason=
"module built without PECLET_PNM_MPI")
149@pytest.mark.parametrize(
"shape, param", [
150 ([0, N, N],
"global_shape_zyx[0]"), ([N, -1, N],
"global_shape_zyx[1]"),
151 ([N, N, -N],
"global_shape_zyx[2]")])
153 _raises(
lambda: pnm.mpi_block(shape), f
"mpi_block: {param} must be")
158 with pytest.raises(RuntimeError, match=
"global_shape_zyx must be"):
159 pnm.mpi_block([N, N])
164 offset, shape = pnm.mpi_block([N, N, N])
165 assert len(offset) == len(shape) == 3
and all(s >= 1
for s
in shape)
169@pytest.mark.parametrize(
"shape, param", [
170 ([N, N],
"global_shape_zyx"), ([],
"global_shape_zyx"), ([N] * 4,
"global_shape_zyx"),
171 ([0, N, N],
"global_shape_zyx[0]"), ([N, -1, N],
"global_shape_zyx[1]")])
174 f
"extract_pore_network_mpi: {param} must be")
MpiPoreNetwork extract_pore_network_mpi(const std::vector< float > &sdf_local, std::array< int, 3 > gdims, std::array< float, 3 > origin, std::array< float, 3 > spacing, MPI_Comm comm)
Distributed fused extraction.