Skip to content

Commit 85c57eb

Browse files
authored
Merge pull request #404 from PyAutoLabs/claude/g-heart-green-fable-opus-uuiaqf
fix: guard weighted image-mesh weight map against a blank adapt image
2 parents e921eb9 + 74661b3 commit 85c57eb

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

autoarray/inversion/mesh/image_mesh/abstract_weighted.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ def weight_map_from(self, adapt_data: np.ndarray):
5757
The weight map which is used to adapt the Delaunay pixels in the image-plane to components in the data.
5858
"""
5959

60-
weight_map = np.abs(adapt_data) / np.max(adapt_data)
60+
max_value = np.max(adapt_data)
61+
62+
if max_value <= 0.0:
63+
# A blank adapt image (all-zero, or with no positive signal) carries
64+
# no structure to adapt the mesh to. Normalising by ``np.max`` would
65+
# divide by zero (or a non-positive peak), producing NaN/negative
66+
# weights that propagate into NaN mesh coordinates and crash the
67+
# downstream Delaunay/Voronoi triangulation ("Points cannot contain
68+
# NaN"). Fall back to a uniform weight map so the mesh degrades to
69+
# uniform sampling instead of failing.
70+
return np.ones_like(adapt_data, dtype=float)
71+
72+
weight_map = np.abs(adapt_data) / max_value
6173
weight_map = weight_map**self.weight_power
6274

6375
weight_map[weight_map < self.weight_floor] = self.weight_floor

test_autoarray/inversion/pixelization/image_mesh/test_abstract_weighted.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,24 @@ def test__weight_map_from():
2424
weight_map = pixelization.weight_map_from(adapt_data=adapt_data)
2525

2626
assert weight_map == pytest.approx([1.0, 1.0, 1.0], 1.0e-4)
27+
28+
29+
def test__weight_map_from__blank_adapt_image_returns_uniform_not_nan():
30+
# A blank (all-zero) adapt image has ``np.max == 0``; the normalisation must
31+
# not divide by zero and produce NaN weights, which would propagate into NaN
32+
# mesh coordinates and crash the downstream Delaunay triangulation.
33+
pixelization = aa.image_mesh.Hilbert(
34+
pixels=5, weight_floor=0.01, weight_power=3.5
35+
)
36+
37+
weight_map = pixelization.weight_map_from(adapt_data=np.zeros(4))
38+
39+
assert np.all(np.isfinite(weight_map))
40+
assert weight_map == pytest.approx([1.0, 1.0, 1.0, 1.0], 1.0e-4)
41+
42+
# An adapt image with no positive signal (non-positive peak) is equally
43+
# degenerate and must also fall back to a finite, uniform weight map.
44+
weight_map = pixelization.weight_map_from(adapt_data=np.array([-2.0, -1.0, -3.0]))
45+
46+
assert np.all(np.isfinite(weight_map))
47+
assert weight_map == pytest.approx([1.0, 1.0, 1.0], 1.0e-4)

0 commit comments

Comments
 (0)