From 11ac4ea238b4873b3e5542212b4658d93ebc2ada Mon Sep 17 00:00:00 2001 From: Istvan N Huszar Date: Sun, 27 Sep 2026 11:09:05 -0400 Subject: [PATCH] Fix Mesh.face_to_vertex_overlay for single-frame overlays Scalar overlays are stored with shape (nfaces,), while the accumulation buffer has an explicit frame axis, (nvertices, nframes). With a single-frame overlay the indexed reductions (np.add.at, np.maximum.at, np.minimum.at) cannot broadcast (nfaces,) values onto the (nfaces, 1) selection and raise "array is not broadcastable to correct shape", so face_to_vertex_overlay() failed for every method on 1-D overlays. Reshape the overlay values to (nfaces, nframes) before the reductions. Multi-frame behaviour is unchanged. Adds test/test_mesh_overlay.py, which compares all four reduction methods against a loop-based reference for 1- and 2-frame overlays. Originally committed in the inhuszar/surfa fork as part of 8ef40a9 ("Bugfix: thread limit, overlay accumulation"); supersedes the earlier local workaround 0ba0004 ("NumPy 2.0 bugfix"). Co-Authored-By: Claude Opus 5.5 (cherry picked from commit e0951dcada65ac8b30490659866cc25eca2a97f2) --- surfa/mesh/mesh.py | 30 +++++++++++++++------------ test/test_mesh_overlay.py | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 test/test_mesh_overlay.py diff --git a/surfa/mesh/mesh.py b/surfa/mesh/mesh.py index f99bcbe..59b26c2 100644 --- a/surfa/mesh/mesh.py +++ b/surfa/mesh/mesh.py @@ -531,31 +531,35 @@ def face_to_vertex_overlay(self, overlay, method='mean'): raise ValueError(f'expected overlay to have {self.nfaces} points to match ' f'the number of mesh faces, but instead got {overlay.shape[0]}') overlay = cast_overlay(overlay) - + shape = (self.nvertices, overlay.nframes) + # Scalar overlays are stored as (nfaces,), whereas the accumulation + # buffer always has an explicit frame axis. Restore that axis before + # the indexed reductions so that the values broadcast exactly. + values = np.asarray(overlay).reshape(self.nfaces, overlay.nframes) method = str(method).lower() if method == 'mean': buffer = np.zeros(shape) - np.add.at(buffer, self.faces[:, 0], overlay) - np.add.at(buffer, self.faces[:, 1], overlay) - np.add.at(buffer, self.faces[:, 2], overlay) + np.add.at(buffer, self.faces[:, 0], values) + np.add.at(buffer, self.faces[:, 1], values) + np.add.at(buffer, self.faces[:, 2], values) buffer /= np.bincount(self.faces.flat).reshape(-1, 1) elif method == 'max': buffer = np.full(shape, overlay.min(), dtype=overlay.dtype) - np.maximum.at(buffer, self.faces[:, 0], overlay) - np.maximum.at(buffer, self.faces[:, 1], overlay) - np.maximum.at(buffer, self.faces[:, 2], overlay) + np.maximum.at(buffer, self.faces[:, 0], values) + np.maximum.at(buffer, self.faces[:, 1], values) + np.maximum.at(buffer, self.faces[:, 2], values) elif method == 'min': buffer = np.full(shape, overlay.max(), dtype=overlay.dtype) - np.minimum.at(buffer, self.faces[:, 0], overlay) - np.minimum.at(buffer, self.faces[:, 1], overlay) - np.minimum.at(buffer, self.faces[:, 2], overlay) + np.minimum.at(buffer, self.faces[:, 0], values) + np.minimum.at(buffer, self.faces[:, 1], values) + np.minimum.at(buffer, self.faces[:, 2], values) elif method == 'sum': buffer = np.zeros(shape, dtype=overlay.dtype) - np.add.at(buffer, self.faces[:, 0], overlay) - np.add.at(buffer, self.faces[:, 1], overlay) - np.add.at(buffer, self.faces[:, 2], overlay) + np.add.at(buffer, self.faces[:, 0], values) + np.add.at(buffer, self.faces[:, 1], values) + np.add.at(buffer, self.faces[:, 2], values) else: raise ValueError(f'unknown method `{method}`, expected one of: mean/min/max/sum') diff --git a/test/test_mesh_overlay.py b/test/test_mesh_overlay.py new file mode 100644 index 0000000..1b3791a --- /dev/null +++ b/test/test_mesh_overlay.py @@ -0,0 +1,43 @@ +import numpy as np +import pytest +import surfa as sf + + +def _octahedron(): + vertices = np.array([ + [1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1], + ], dtype=float) + faces = np.array([ + [0, 2, 4], [2, 1, 4], [1, 3, 4], [3, 0, 4], + [2, 0, 5], [1, 2, 5], [3, 1, 5], [0, 3, 5], + ]) + return sf.Mesh(vertices, faces) + + +def _reference(mesh, values, method): + """Per-vertex reduction over incident faces, computed with a plain loop.""" + reduce = {'mean': np.mean, 'sum': np.sum, 'max': np.max, 'min': np.min}[method] + out = [] + for v in range(mesh.nvertices): + incident = np.any(mesh.faces == v, axis=1) + out.append(reduce(values[incident], axis=0)) + return np.asarray(out) + + +@pytest.mark.parametrize('method', ['mean', 'sum', 'max', 'min']) +def test_face_to_vertex_overlay_single_frame(method): + mesh = _octahedron() + values = np.arange(mesh.nfaces, dtype=float) + result = mesh.face_to_vertex_overlay(sf.Overlay(values), method=method) + assert result.nframes == 1 + assert result.shape[0] == mesh.nvertices + np.testing.assert_allclose(np.asarray(result).ravel(), _reference(mesh, values, method)) + + +@pytest.mark.parametrize('method', ['mean', 'sum', 'max', 'min']) +def test_face_to_vertex_overlay_multi_frame(method): + mesh = _octahedron() + values = np.stack([np.arange(mesh.nfaces), 2.0 * np.arange(mesh.nfaces)], axis=1) + result = mesh.face_to_vertex_overlay(sf.Overlay(values), method=method) + assert result.shape == (mesh.nvertices, 2) + np.testing.assert_allclose(np.asarray(result), _reference(mesh, values, method))