Fix NoNorm cursor formatting for uint8 images - #31967
Conversation
|
Thank you for opening your first PR into Matplotlib! If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process. You can also join us on discourse chat for real-time discussion. For details on testing, writing docs, and our review process, please see the developer guide. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
scottshambaugh
left a comment
There was a problem hiding this comment.
Please use our PR template that auto-populates when you open a PR. Specifically, you are missing its AI Disclosure section.
|
|
||
|
|
||
| def test_format_cursor_data_uint8_no_norm(): | ||
| from matplotlib.backend_bases import MouseEvent |
There was a problem hiding this comment.
Could you move this import (and the others in the surrounding functions) to the top of the file instead of in-line?
There was a problem hiding this comment.
There are more of these in the file that can be cleaned up. Once that's done, this looks good to me
e4cb56a to
34c6aed
Compare
|
Thanks @scottshambaugh addressed the comments. I moved the |
Convert normalized scalar values to Python scalars before computing neighboring color intervals. This avoids NumPy scalar overflow when NoNorm returns a uint8 cursor value for image data. Add a regression test for cursor formatting on uint8 image data with NoNorm.
34c6aed to
657864d
Compare
…967-on-v3.11.x Backport PR #31967 on branch v3.11.x (Fix NoNorm cursor formatting for uint8 images)
PR summary
closes #31960
This fixes cursor formatting for
uint8image data displayed withcolors.NoNorm().NoNormreturns the original NumPy scalar value. With recent NumPy versions, multiplying auint8cursor value by the colormap size can raiseOverflowErrorwhile Matplotlib computes neighboring color intervals for cursor display precision.This change converts the normalized scalar to a Python scalar at the point where that interval arithmetic is needed, preserving the existing cursor readout behavior while avoiding the narrow-dtype overflow.
Before this change, formatting a
uint8cursor value from aNoNormimage raisedOverflowError. After this change, it returns the expected cursor label.Breaking changes: None.
Tests run:
python -P -m pytest lib/matplotlib/tests/test_image.py::test_format_cursor_data_uint8_no_norm lib/matplotlib/tests/test_image.py::test_format_cursor_datapython -P -m pytest lib/matplotlib/tests/test_image.py::test_format_cursor_data_multinorm lib/matplotlib/tests/test_artist.py::test_format_cursor_data_BoundaryNormpython -P -m pytest lib/matplotlib/tests/test_image.py -k cursor_dataAI Disclosure
I used Codex to review the relevant code and understand the existing codebase.
PR checklist