Skip to content

Replace matlab_find with numpy functions in mapgen#609

Merged
waltsims merged 4 commits into
masterfrom
chore/do-not-use-matlab_find-in-mapgen
May 16, 2026
Merged

Replace matlab_find with numpy functions in mapgen#609
waltsims merged 4 commits into
masterfrom
chore/do-not-use-matlab_find-in-mapgen

Conversation

@faridyagubbayli
Copy link
Copy Markdown
Collaborator

@faridyagubbayli faridyagubbayli commented May 15, 2025

Greptile Summary

This PR replaces MATLAB-compatibility shim calls (matlab_find, ind2sub, sub2ind, matlab_assign) with native NumPy equivalents in make_line and the DIMENSION 1 block of make_bowl in mapgen.py. The index-arithmetic equivalence is correct: matlab_find(diff == min(diff))[0]np.argmin(diff) preserves first-occurrence tie-breaking, and the 1-based ↔ 0-based offset adjustments are properly removed.

  • In make_line, six identical index-selection sites are simplified: np.argmin(diff) replaces the matlab_find round-trip, and the downstream poss_x[index[0] - 1] accesses become plain poss_x[index].
  • In make_bowl, the DIMENSION 1 forward/backward surface-extraction pipeline is rewritten using np.where + direct 3-D array assignment, correctly mapping the flipped backward index via (Nx - 1) - index_back_flipped. DIMENSION 2 and DIMENSION 3 are left unchanged, still relying on matlab_find.

Confidence Score: 4/5

The changed logic is a correct mechanical translation from MATLAB-style helpers to native NumPy — index equivalence holds in both make_line and the make_bowl DIMENSION 1 block. The only concern is that DIMENSION 2 and DIMENSION 3 of make_bowl were not migrated, leaving the function internally inconsistent.

The six make_line sites and the make_bowl DIMENSION 1 block all translate cleanly, and the 0-based/1-based offset adjustments are handled correctly throughout. The remaining make_bowl dimensions still use the old matlab_find path, which is not a correctness issue but does leave an incomplete job that will need a follow-up.

kwave/utils/mapgen.py — specifically the DIMENSION 2 (lines ~1745–1763) and DIMENSION 3 (lines ~1783–1801) blocks in make_bowl, which still use the old matlab_find pattern.

Important Files Changed

Filename Overview
kwave/utils/mapgen.py Replaces matlab_find-based index arithmetic with np.argmin in make_line and with np.where/direct array indexing in the DIMENSION 1 block of make_bowl; DIMENSION 2 and DIMENSION 3 in make_bowl still use the old matlab_find pattern

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[pixel_map shape Nx x Ny x Nz] --> B[min along axis 0\nvalue_forw, index_forw]
    A --> C[flip along axis 0\nthen min along axis 0\nvalue_back, index_back]
    B --> D["np.where(value_forw < THRESHOLD)\ny_coords_forw, z_coords_forw"]
    C --> E["np.where(value_back < THRESHOLD)\ny_coords_back, z_coords_back"]
    D --> F["x_ind_forw = index_forw[y, z]"]
    E --> G["x_ind_back_flipped = index_back[y, z]\nx_ind_back = (Nx-1) - x_ind_back_flipped"]
    F --> H["bowl_sm[x_ind_forw, y_coords_forw, z_coords_forw] = 1"]
    G --> I["bowl_sm[x_ind_back, y_coords_back, z_coords_back] = 1"]
Loading

Comments Outside Diff (1)

  1. kwave/utils/mapgen.py, line 1745-1763 (link)

    P2 Incomplete migration — DIMENSION 2 still uses matlab_find

    The DIMENSION 1 block was fully migrated to native numpy operations, but DIMENSION 2 (lines 1745–1763) and DIMENSION 3 (lines 1783–1801) still use matlab_find, ind2sub, sub2ind, and matlab_assign. This leaves the refactoring half-done and makes the three dimension blocks inconsistent with each other.

Reviews (1): Last reviewed commit: "Merge branch 'master' into chore/do-not-..." | Re-trigger Greptile

@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2025

Codecov Report

❌ Patch coverage is 84.37500% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.04%. Comparing base (cf4ec8f) to head (40b2c4b).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
kwave/utils/mapgen.py 84.37% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #609      +/-   ##
==========================================
- Coverage   75.06%   75.04%   -0.02%     
==========================================
  Files          57       57              
  Lines        8126     8128       +2     
  Branches     1582     1584       +2     
==========================================
  Hits         6100     6100              
  Misses       1405     1405              
- Partials      621      623       +2     
Flag Coverage Δ
3.10 75.02% <84.37%> (-0.02%) ⬇️
3.11 75.02% <84.37%> (-0.02%) ⬇️
3.12 75.02% <84.37%> (-0.02%) ⬇️
3.13 75.02% <84.37%> (-0.02%) ⬇️
macos-latest 74.97% <84.37%> (-0.02%) ⬇️
ubuntu-latest 74.97% <84.37%> (-0.02%) ⬇️
windows-latest 74.91% <84.37%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@waltsims waltsims merged commit 7260829 into master May 16, 2026
91 checks passed
@waltsims waltsims deleted the chore/do-not-use-matlab_find-in-mapgen branch May 16, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants