Fix colorbar() for two-dimensional spectral arrays - #19
Merged
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
colorbar(), and thereforergb_and_colorbar(), raised an exception for every two-dimensionalspd, 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
spdof shape(101,)and(64, 64, 101), stepping over the broken case, and because the returned arrays were only checked for their type.Cause.
intensityis built on top of the normalization bounds and so carriesndim + 2dimensions, wherendimis the number of dimensions ofspdbroadcast againstwavelength.wavelength2is built directly fromwavelengthand so carrieswavelength.ndim + 2dimensions, which is three for the usual one-dimensional wavelength grid. The two only line up whenndimis one, or three or more. At exactlyndim == 2the intensity axis collides with the wavelength axis, givingValueError: shape mismatch, or a confusingAxisError: source: axis 1 is out of bounds for array of dimension 1in the special case where the number of wavelengths happens to equalnum_intensity.The fix reshapes
wavelengthto 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 ofwavelengthunchanged 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
spdproduced. A three-dimensionalspdproduced them the other way around, because the returned arrays only reached the intended layout aftersqueeze()happened to remove the right axes. All numbers of dimensions now agree with the documentation.For a three-dimensional
spdthe 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 bypcolormesh(*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)forspdof one, two, three, and four dimensions, for a leading as well as a trailing wavelength axis, and for two values ofnum_intensity. It replacestest_colorbar_num_intensity, which it fully subsumes.One pre-existing limitation is left alone and is now documented in the test. If
spdis one-dimensional there are no axes orthogonal to the wavelength axis, so the automatic bounds collapse tovmin == vmaxand the normalization divides by zero, giving a colorbar containing NaN and infinity. Passingspd_minandspd_maxexplicitly 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