Skip to content

Fix colorbar() for two-dimensional spectral arrays - #19

Merged
roytsmart merged 1 commit into
mainfrom
fix-colorbar-2d
Aug 19, 2026
Merged

Fix colorbar() for two-dimensional spectral arrays#19
roytsmart merged 1 commit into
mainfrom
fix-colorbar-2d

Conversation

@roytsmart

Copy link
Copy Markdown
Contributor

colorbar(), and therefore rgb_and_colorbar(), raised an exception for every two-dimensional spd, while one- and three-dimensional arrays worked. A two-dimensional spectral array is a natural input, since a single slit spectrogram is position along the slit against wavelength, so this blocked colorizing one slit position with a matching colorbar. rgb() was unaffected.

The failure was present in the released 1.0.0, so it is long-standing rather than a recent regression. It went unnoticed because the tests only ever passed spd of shape (101,) and (64, 64, 101), stepping over the broken case, and because the returned arrays were only checked for their type.

Cause. intensity is built on top of the normalization bounds and so carries ndim + 2 dimensions, where ndim is the number of dimensions of spd broadcast against wavelength. wavelength2 is built directly from wavelength and so carries wavelength.ndim + 2 dimensions, which is three for the usual one-dimensional wavelength grid. The two only line up when ndim is one, or three or more. At exactly ndim == 2 the intensity axis collides with the wavelength axis, giving ValueError: shape mismatch, or a confusing AxisError: source: axis 1 is out of bounds for array of dimension 1 in the special case where the number of wavelengths happens to equal num_intensity.

The fix reshapes wavelength to the full number of dimensions of the broadcast shape, by prepending length-one axes, before the colorbar axes are added. This is a view, so it costs no memory, and it leaves every other use of wavelength unchanged because broadcasting already right-aligns axes.

This also fixes an inconsistency in the axis order. The documented default is intensity along the first axis and wavelength along the second, which is what a one-dimensional spd produced. A three-dimensional spd produced them the other way around, because the returned arrays only reached the intended layout after squeeze() happened to remove the right axes. All numbers of dimensions now agree with the documentation.

For a three-dimensional spd the returned arrays are therefore transposed relative to before. The documented usage is not affected: I confirmed that the arrays are equal to the previous ones after transposition, and that the image rendered by pcolormesh(*colorbar(...)) is byte for byte identical, since transposing the coordinates and the colors together draws the same quads.

Tests. Adds test_colorbar_shape, which asserts the returned shapes are exactly (num_intensity, num_wavelength) for spd of one, two, three, and four dimensions, for a leading as well as a trailing wavelength axis, and for two values of num_intensity. It replaces test_colorbar_num_intensity, which it fully subsumes.

One pre-existing limitation is left alone and is now documented in the test. If spd is one-dimensional there are no axes orthogonal to the wavelength axis, so the automatic bounds collapse to vmin == vmax and the normalization divides by zero, giving a colorbar containing NaN and infinity. Passing spd_min and spd_max explicitly avoids it. Deciding what a single spectrum should normalize against is a separate question from this shape fix, so the finiteness assertion is skipped in that case rather than papered over.

🤖 Generated with Claude Code

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0e6a894) to head (6f787e4).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #19   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          420       431   +11     
=========================================
+ Hits           420       431   +11     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

☔ View full report in Codecov by Harness.
📢 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.

@roytsmart
roytsmart merged commit 9027720 into main Aug 19, 2026
16 checks passed
@roytsmart
roytsmart deleted the fix-colorbar-2d branch August 19, 2026 20:18
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.

1 participant