pnm 1.0.3
peclet.pnm — pore-network extraction from SDF geometry (Kokkos)
Loading...
Searching...
No Matches
test_input_validation.py
Go to the documentation of this file.
1"""Contract: the entry points refuse a wrong-length triple, NaN/Inf or inadmissible value with ValueError.
2
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.
7
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.
12
13Run: PYTHONPATH=<build> python -m pytest -q tests/python/test_input_validation.py
14"""
15import math
16import re
17
18import numpy as np
19import pytest
20from peclet import pnm
21
22NAN, INF = math.nan, math.inf
23N = 8
24O = [0.0, 0.0, 0.0]
25S = [1.0, 1.0, 1.0]
26HAS_MPI = hasattr(pnm, "mpi_block")
27
28
29def _sdf():
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)
33
34
35def _raises(call, param):
36 with pytest.raises(ValueError, match=re.escape(param)):
37 call()
38
39
40# Bad triples of origin_zyx (a coordinate) and spacing_zyx (a cell size), with the parameter the
41# message must name.
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]")]
49
50
51# --- Pore ------------------------------------------------------------------------------------
52
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")])
56def test_pore_refuses(args, param):
57 _raises(lambda: pnm.Pore(*args), f"Pore: {param} must be")
58
59
60@pytest.mark.parametrize("args", [(1.5, -2.0, 3.0, 0.75), (0, 0, 0, 0)])
62 p = pnm.Pore(*args)
63 assert (p.x, p.y, p.z, p.radius) == args
64
65
66# --- the physical grid of extract_pores / extract_pore_network(_mpi) -------------------------
67
68GRID_FUNCS = {
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),
71}
72if HAS_MPI:
73 GRID_FUNCS["extract_pore_network_mpi"] = (
74 lambda sdf, o, s: pnm.extract_pore_network_mpi(sdf, [N, N, N], o, s))
75
76
77@pytest.mark.parametrize("name", sorted(GRID_FUNCS))
78@pytest.mark.parametrize("origin, param", BAD_ORIGIN)
79def test_grid_refuses_origin(name, origin, param):
80 _raises(lambda: GRID_FUNCS[name](_sdf(), origin, S), f"{name}: {param} must be")
81
82
83@pytest.mark.parametrize("name", sorted(GRID_FUNCS))
84@pytest.mark.parametrize("spacing, param", BAD_SPACING)
85def test_grid_refuses_spacing(name, spacing, param):
86 _raises(lambda: GRID_FUNCS[name](_sdf(), O, spacing), f"{name}: {param} must be")
87
88
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,
96 -3.0 + 4 * 2.0))
97 if HAS_MPI:
98 mpores, mseg, mconns = pnm.extract_pore_network_mpi(_sdf(), [N, N, N], o, s)
99 assert len(mpores) == 1 and np.array_equal(mseg, seg) and np.array_equal(mconns, conns)
100
101
102# --- segment_volume --------------------------------------------------------------------------
103
104@pytest.mark.parametrize("spacing, param", BAD_SPACING)
105def test_segment_volume_refuses(spacing, param):
106 _raises(lambda: pnm.segment_volume(_sdf(), spacing), f"segment_volume: {param} must be")
107
108
110 seg = pnm.segment_volume(_sdf(), [0.5, 0.25, 2.0])
111 assert seg.shape == (N, N, N) and seg.max() == 1
112
113
114# --- extract_topology ------------------------------------------------------------------------
115
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")
123
124
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))
134
135
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)
141
142
143# --- the MPI entry points (a singleton world outside mpirun) ---------------------------------
144
145mpi = pytest.mark.skipif(not HAS_MPI, reason="module built without PECLET_PNM_MPI")
146
147
148@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]")])
152def test_mpi_block_refuses(shape, param):
153 _raises(lambda: pnm.mpi_block(shape), f"mpi_block: {param} must be")
154
155
156@mpi
158 with pytest.raises(RuntimeError, match="global_shape_zyx must be"):
159 pnm.mpi_block([N, N])
160
161
162@mpi
164 offset, shape = pnm.mpi_block([N, N, N])
165 assert len(offset) == len(shape) == 3 and all(s >= 1 for s in shape)
166
167
168@mpi
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]")])
173 _raises(lambda: pnm.extract_pore_network_mpi(_sdf(), shape, O, S),
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.
test_extract_pore_network_mpi_refuses_global_shape(shape, param)
test_segment_volume_refuses(spacing, param)
test_grid_refuses_spacing(name, spacing, param)
test_extract_topology_refuses(shape, param)
test_grid_refuses_origin(name, origin, param)