GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: ensure that errorbar does not error on masked negative errors. by mhvk · Pull Request #29897 · matplotlib/matplotlib

Conversation

@mhvk

@mhvk mhvk commented Apr 10, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

errorbar checks that errors are not negative, but a bit convolutedly, in order to avoid triggering on nan. Unfortunately, the work-around for nan means that possible masks get discarded, and hence passing in a masked error array that has a negative but masked value leads to an exception. This PR solves that by simply combining the test for
negative values with the indirect isnan test (err == err), so that if a masked array is passed, the test values are masked and ignored in the check.

As a bonus, this also means that astropy's Masked arrays can now be used -- those refuse to write output of tests to unmasked values since that would discard the mask (which is indeed the underlying problem here) -- see
astropy/astropy#17996

p.s. I checked that the test case that I added fails on current main. The test case is based on the one for nan right above - let me know if this is an OK location for it.

EDIT: updated to simplify the check without using any storing of outputs. This means we can also continue to use astropy's Quantity for error bars (and just shows how damn careful one has to be, sigh).

EDIT 2: second commit prevents any comparison of nan < nan, which causes RuntimeError that I did not see locally.

PR checklist

  • [N/A] "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • [N/A] Documentation complies with general and docstring guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
github-actions[bot]
github-actions Bot reviewed Apr 10, 2025
View reviewed changes

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

You can also join us on gitter 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhvk
mhvk force-pushed the allow_masked_negative_errors branch from c22cac0 to ae1edf8 Compare April 10, 2025 16:23
@mhvk
BUG: ensure that errorbar does not error on masked negative errors.
d91d216
errorbar checks that errors are not negative, but a bit convolutedly,
in order to avoid triggering on nan. Unfortunately, the work-around
for nan means that possible masks get discarded, and hence passing in
a masked error array that has a negative but masked value leads to
an exception. This PR solves that by simply combining the test for
negative values with the indirect isnan test (err == err), so that
if a masked array is passed, the test values are masked and ignored
in the check.

As a bonus, this also means that astropy's ``Masked`` arrays can now
be used -- those refuse to write output of tests to unmasked values
since that would discard the mask (which is indeed the underlying
problem here).
@mhvk mhvk mentioned this pull request Apr 10, 2025
BUG: MaskedNDArray error when trying to plot astropy/astropy#17996
Open
@mhvk
mhvk force-pushed the allow_masked_negative_errors branch from ae1edf8 to 5c42d71 Compare April 10, 2025 16:44
@bsipocz bsipocz mentioned this pull request Apr 10, 2025
Use astropy.units to do flux--magnitude conversions in euclid CMD Caltech-IPAC/irsa-tutorials#94
Open
@tacaswell tacaswell added this to the v3.10.2 milestone Apr 10, 2025
@tacaswell

tacaswell commented Apr 10, 2025

Copy link
Copy Markdown
Member

The underlying (underlying) problem is that np.where does not respect the mask and __getitem__ does?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhvk

mhvk commented Apr 10, 2025

Copy link
Copy Markdown
Contributor Author

The underlying (underlying) problem is that np.where does not respect the mask and __getitem__ does?

No, the problem is that when writing directly to a regular array, as happened in np.less(err, -err, out=res) (where res was a regular array created with np.zeros), the mask gets dropped (more specifically, np.less(err, -err) would normally return a masked boolean array, with the mask a copy of the mask of err, but writing that to res drops the mask).

p.s. What was a surprise is that np.less raises a RuntimeWarning for np.nan unless that particular element is not included in where -- a surprise mostly because I know from my ufunc work that generally the where argument does not actually prevent the function to be done on the arguments, it just avoids the result being written back. But perhaps it is different for the comparisons - nan are of course notorious and there is a lot of extra code to work around them... Anyway, unrelated to the issue at hand!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhvk

mhvk commented Apr 10, 2025

Copy link
Copy Markdown
Contributor Author

Actually, I guess I misread your question: yes, it is also true that the where argument ignores the mask on the output of err == err.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tacaswell
tacaswell approved these changes Apr 11, 2025
View reviewed changes
dstansby
dstansby reviewed Apr 13, 2025
View reviewed changes
Comment thread lib/matplotlib/tests/test_axes.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/matplotlib/axes/_axes.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/matplotlib/tests/test_axes.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

mhvk added 2 commits April 13, 2025 08:14
@mhvk
BUG: ensure we never do nan < nan
bbe738f
@mhvk
MAINT: make test code a little more explicit in what's done.
ebeb83a
@mhvk
mhvk force-pushed the allow_masked_negative_errors branch from 5c42d71 to ebeb83a Compare April 13, 2025 12:18
@mhvk

mhvk commented Apr 13, 2025

Copy link
Copy Markdown
Contributor Author

@dstansby - comments addressed (I hope).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dstansby
dstansby approved these changes Apr 13, 2025
View reviewed changes

@dstansby dstansby left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@QuLogic
QuLogic merged commit e1887b8 into matplotlib:main Apr 16, 2025
@QuLogic

QuLogic commented Apr 16, 2025

Copy link
Copy Markdown
Member

Thanks @mhvk! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

bsipocz reacted with hooray emoji bsipocz reacted with heart emoji
All reactions
  • 🎉 1 reaction
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Apr 16, 2025
@QuLogic @meeseeksmachine
Backport PR matplotlib#29897: BUG: ensure that errorbar does not erro…
25b9377
…r on masked negative errors.
@meeseeksmachine meeseeksmachine mentioned this pull request Apr 16, 2025
Backport PR #29897 on branch v3.10.x (BUG: ensure that errorbar does not error on masked negative errors.) #29927
Merged
@mhvk
mhvk deleted the allow_masked_negative_errors branch April 16, 2025 11:14
@bsipocz

bsipocz commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

Thanks @mhvk for fixing this!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

QuLogic added a commit that referenced this pull request Apr 17, 2025
@QuLogic
Merge pull request #29927 from meeseeksmachine/auto-backport-of-pr-29…
f72525a
…897-on-v3.10.x

Backport PR #29897 on branch v3.10.x (BUG: ensure that errorbar does not error on masked negative errors.)
@ksunden ksunden mentioned this pull request May 9, 2025
merge up 3.10.3 #30031
Closed
5 tasks
@ksunden ksunden mentioned this pull request Jul 25, 2025
Manual Backport PR #30195 on branch v3.10.x (ci: Enable wheel builds on Python 3.14) #30356
Merged
5 tasks
@ksunden ksunden mentioned this pull request Aug 7, 2025
merge up v3.10.5 #30401
Merged
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@github-actions github-actions[bot] github-actions[bot] left review comments
@tacaswell tacaswell tacaswell approved these changes
@dstansby dstansby dstansby approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

v3.10.3

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@mhvk @tacaswell @QuLogic @bsipocz @dstansby
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.