Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bolt's Journal - Critical Learnings Only

## 2025-02-17 - Vectorizing `griddata_v4` in Topoplot
**Learning:** Bi-harmonic spline interpolation in `topoplot.py` (`griddata_v4`) was bottlenecked by a double-nested Python loop over the query coordinates grid (`m x n`). Since `GRID_SCALE` is typically small/medium (e.g., 67x67), vectorizing via 3D broadcasting and a single `@` matrix multiplication has very low memory overhead and achieves massive speedups.
**Action:** Replace the loop over grid rows and columns with 3D broadcasting `np.abs(q[:, :, None] - xy[None, None, :])` and matrix-multiply with `weights`.
27 changes: 14 additions & 13 deletions src/eegprep/functions/sigprocfunc/topoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ 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 using vectorized broadcasting
# Flat representation of query points as complex numbers
q_flat = (xq + 1j * yq).ravel()

# Calculate distance matrix of size (N_query, N_known)
d = np.abs(q_flat[:, np.newaxis] - xy[np.newaxis, :])

# Compute Green's function
with np.errstate(divide='ignore', invalid='ignore'):
g = (d**2) * (np.log(d) - 1)
g[d == 0] = 0 # Handle Green's function at zero

# Perform matrix-vector multiplication and reshape back to original grid shape
vq = (g @ weights).reshape(xq.shape)

return vq

Expand Down
Loading