From 206b7d062cb4627cc31a4d1b86faf350e6021b57 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 16 Jul 2026 13:40:29 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20topoplot=20grid?= =?UTF-8?q?=20interpolation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced double-nested loop in `griddata_v4` with vectorized NumPy broadcasting and matrix multiplication. This optimization provides a ~4x speedup for the biharmonic spline interpolation used in EEG topographic plots (67x67 grid with 64 channels). - Vectorized grid evaluation via 3D broadcasting - Used `@` operator for weighted sum matrix multiplication - Verified ~4x speedup (0.17s vs 0.71s for 10 calls) - Maintained 100% test parity in `tests/test_topoplot.py` Co-authored-by: suraj-ranganath <14310165+suraj-ranganath@users.noreply.github.com> --- .jules/bolt.md | 3 +++ src/eegprep/functions/sigprocfunc/topoplot.py | 25 +++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..123e6a38 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-07-16 - [Topoplot Vectorization] +**Learning:** Replacing double-nested loops over a 2D grid with NumPy broadcasting (3D array expansion) and matrix multiplication (@) provides a significant (3.8x - 4.2x) speedup for biharmonic spline interpolation. +**Action:** Always look for nested loops over query grids in spatial interpolation functions and replace them with vectorized broadcasting. diff --git a/src/eegprep/functions/sigprocfunc/topoplot.py b/src/eegprep/functions/sigprocfunc/topoplot.py index f6bdb838..f479c275 100644 --- a/src/eegprep/functions/sigprocfunc/topoplot.py +++ b/src/eegprep/functions/sigprocfunc/topoplot.py @@ -45,19 +45,18 @@ def griddata_v4(x, y, v, xq, yq): # If still singular, use pseudoinverse as last resort weights = np.linalg.pinv(g_reg) @ v - # Initialize output array - m, n = xq.shape - vq = np.zeros_like(xq) - - # Evaluate at requested points - xy = xy[:, None] # Make it column vector for broadcasting - for i in range(m): - for j in range(n): - d = np.abs(xq[i, j] + 1j * yq[i, j] - xy.ravel()) - with np.errstate(divide='ignore', invalid='ignore'): - g = (d**2) * (np.log(d) - 1) # Green's function - g[d == 0] = 0 # Handle Green's function at zero - vq[i, j] = np.dot(g, weights) + # Evaluate at requested points (vectorized) + # xq_yq_complex: (m, n), xy: (num_electrodes,) + # d: (m, n, num_electrodes) via broadcasting + xq_yq_complex = xq + 1j * yq + d = np.abs(xq_yq_complex[..., np.newaxis] - xy.ravel()) + with np.errstate(divide='ignore', invalid='ignore'): + g = (d**2) * (np.log(d) - 1) # Green's function + g[d == 0] = 0 # Handle Green's function at zero + + # Matrix multiplication over the last dimension + # (m, n, num_electrodes) @ (num_electrodes,) -> (m, n) + vq = g @ weights return vq