Skip to content

Fix #501: voxel_plot swapped x and z axes#734

Merged
waltsims merged 2 commits into
masterfrom
fix/501-voxel-plot-axes
May 16, 2026
Merged

Fix #501: voxel_plot swapped x and z axes#734
waltsims merged 2 commits into
masterfrom
fix/501-voxel-plot-axes

Conversation

@waltsims
Copy link
Copy Markdown
Owner

@waltsims waltsims commented May 16, 2026

Summary

  • Fixes [BUG]Voxel Plot switched axes #501. voxel_plot in kwave/utils/plot.py was using sz[2] for the x-axis limit and sz[0] for the z-axis limit, which mirrored the rendered output relative to the input array's first and last dimensions (and relative to MATLAB k-Wave's voxelPlot).
  • Now set_xlim/ylim/zlim use sz[0]/sz[1]/sz[2] (the fix the issue reporter suggested).
  • Added a regression test in tests/test_utils.py that calls voxel_plot on a (4, 8, 12) array (asymmetric so a swap is visible) and asserts each axis limit follows the corresponding shape dim.

Test plan

  • New test test_voxel_plot_axis_limits_match_input_shape passes locally
  • ruff check + ruff format --check clean
  • CI green

Closes #501

🤖 Generated with Claude Code

Greptile Summary

Fixes the swapped x/z axis limits in voxel_plot — the old code mapped sz[2] to xlim and sz[0] to zlim, mirroring the output relative to the input array's first and last dimensions. The fix swaps them back so each axis limit follows the corresponding shape dimension, matching MATLAB k-Wave's voxelPlot behavior.

  • kwave/utils/plot.py: Two-line swap restoring set_xlimsz[0] and set_zlimsz[2].
  • tests/test_utils.py: matplotlib.use("Agg") moved to module level (before kwave.utils.plot is imported, which itself imports matplotlib.pyplot), and a new regression test asserts each axis limit against the expected shape dimension using an asymmetric (4, 8, 12) array.

Confidence Score: 5/5

Safe to merge — the change is a minimal two-line swap that corrects a long-standing axis-mapping bug, backed by a targeted regression test.

The fix is isolated to two lines in voxel_plot, the logic is straightforward and directly matches the MATLAB reference implementation, and the new test uses an asymmetric array that would fail on the old code, confirming the correction.

No files require special attention.

Important Files Changed

Filename Overview
kwave/utils/plot.py Minimal, correct two-line swap: xlim now uses sz[0] and zlim now uses sz[2], fixing the previously mirrored axis limits.
tests/test_utils.py Adds module-level Agg backend setup and a regression test with an asymmetric array that directly asserts each axis limit; covers the fix thoroughly.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["voxel_plot(mat)"] --> B["mat.shape → sz"]
    B --> C{axis_tight?}
    C -- "False (default)" --> D["set_xlim([0.5, sz[0]+0.5])"]
    D --> E["set_ylim([0.5, sz[1]+0.5])"]
    E --> F["set_zlim([0.5, sz[2]+0.5])"]
    C -- "True" --> G["skip axis limit setup"]
Loading

Reviews (2): Last reviewed commit: "Set matplotlib Agg backend at module lev..." | Re-trigger Greptile

ax.set_xlim was using sz[2] and ax.set_zlim was using sz[0], which
flipped the x and z axes vs. the input array's first and last
dimensions. With this fix, set_xlim/ylim/zlim follow sz[0]/sz[1]/sz[2]
so the rendered axes match the input shape and MATLAB k-Wave's
voxelPlot behaviour.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment thread tests/test_utils.py
Comment thread tests/test_utils.py
@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.84%. Comparing base (9f25de1) to head (57ebec7).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #734      +/-   ##
==========================================
+ Coverage   74.82%   74.84%   +0.02%     
==========================================
  Files          56       57       +1     
  Lines        8095     8114      +19     
  Branches     1577     1579       +2     
==========================================
+ Hits         6057     6073      +16     
- Misses       1422     1423       +1     
- Partials      616      618       +2     
Flag Coverage Δ
3.10 74.82% <100.00%> (+0.02%) ⬆️
3.11 74.82% <100.00%> (+0.02%) ⬆️
3.12 74.82% <100.00%> (+0.02%) ⬆️
3.13 74.82% <100.00%> (+0.02%) ⬆️
macos-latest 74.77% <100.00%> (+0.02%) ⬆️
ubuntu-latest 74.77% <100.00%> (+0.02%) ⬆️
windows-latest 74.78% <100.00%> (+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.

Address two related P2 findings from Greptile on PR #734:

- The previous `matplotlib.use("Agg", force=True)` call lived inside
  the test body, but kwave.utils.plot imports matplotlib.pyplot at
  module load, so the backend was already chosen by the time the
  module-level `from kwave.utils.plot import voxel_plot` ran. On
  headless CI without DISPLAY, that import could fail before the
  test ever executed.
- With the backend set at module level, the in-test use(...) call
  and `import matplotlib` were redundant.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@waltsims waltsims merged commit 9cc6219 into master May 16, 2026
33 checks passed
@waltsims waltsims deleted the fix/501-voxel-plot-axes branch May 16, 2026 17:08
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.

[BUG]Voxel Plot switched axes

1 participant