Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FIX:_safe_first_finite on all non-finite array#25547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ksunden merged 1 commit intomatplotlib:mainfromrcomer:safe_first_finite
May 12, 2023

Conversation

rcomer
Copy link
Member

@rcomerrcomer commentedMar 25, 2023
edited
Loading

PR Summary

This function used to be named_safe_first_non_none (#23751), and at v3.6.0 we have

importnumpyasnpimportmatplotlib.cbookarr=np.full(5,np.nan)print(matplotlib.cbook._safe_first_non_none(arr))
nan

So this PR reinstates previous behaviour. Currently onmain,_safe_first_finite raisesStopIteration when passed an all-nan array.

Fixes#18294 andfixes#24818. The examples from both those issues now successfully produce empty plots. The example from#18294 no longer throws the originally reported warning, but the example from#24818 does throw

/home/ruth/git_stuff/matplotlib/lib/matplotlib/axes/_axes.py:1185: RuntimeWarning: All-NaN axis encountered  miny = np.nanmin(masked_verts[..., 1])/home/ruth/git_stuff/matplotlib/lib/matplotlib/axes/_axes.py:1186: RuntimeWarning: All-NaN axis encountered  maxy = np.nanmax(masked_verts[..., 1])

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (andpytest passes)
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).
  • [N/A] New plotting related features are documented with examples.

Release Notes

  • [N/A] New features are marked with a.. versionadded:: directive in the docstring and documented indoc/users/next_whats_new/
  • [N/A] API changes are marked with a.. versionchanged:: directive in the docstring and documented indoc/api/next_api_changes/
  • [N/A] Release notes conform with instructions innext_whats_new/README.rst ornext_api_changes/README.rst

@rcomerrcomer added the PR: bugfixPull requests that fix identified bugs labelMar 25, 2023
@rcomerrcomer mentioned this pull requestMar 25, 2023
2 tasks
@rcomerrcomer added this to thev3.7.2 milestoneMar 25, 2023
@rcomer
Copy link
MemberAuthor

I see that a different approach was taken to handle the change in behaviour forbar#24149.

@oscargus
Copy link
Member

I think that this approach is quite a bit simpler than the one used in#24149. On the other hand it is a bit more explicit what happens there.

My suggestions:

  • Pick the first element of the array instead of np.nan (may be all np.inf and then returning np.nan is not so obvious...)
  • Update the doc-string and state what happens if no parameter is finite
  • Update the doc-string to replace skip_none with skip_nonfinite (old issue)
  • (Changebar to use the new method?)
rcomer reacted with thumbs up emoji

@rcomer
Copy link
MemberAuthor

Thanks@oscargus your suggestions make sense to me. I’ll put this in draft until I have time to implement them.

@rcomerrcomer marked this pull request as draftApril 7, 2023 18:43
@rcomerrcomer marked this pull request as ready for reviewApril 8, 2023 12:47
@rcomerrcomer changed the titleFIX:_safe_first_finite on all nan arrayFIX:_safe_first_finite on all non-finite arrayApr 8, 2023
except StopIteration:
# this means we found no finite element, fall back to first
# element unconditionally
x0 = cbook.safe_first_element(x0)

try:
x = cbook._safe_first_finite(xconv)
except (TypeError, IndexError, KeyError):
Copy link
Member

Choose a reason for hiding this comment

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

I think there a few other places where StopIteration is try/excepted for _safe_first_element? Did you check all those?

rcomer reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I hadn't, but I have now. I only found these two:

try:
x=cbook._safe_first_finite(x)
except (TypeError,StopIteration):
pass

try:# If cache lookup fails, look up based on first element...
first=cbook._safe_first_finite(x)
except (TypeError,StopIteration):
pass

These both predate the change that led toStopIteration being thrown for all-nan arrays. Thecommit that introduced the first says it was for zero length objects, and these will still trigger aStopIteration when the logic arriveshere. I'm not sure about the second - shouldunits.Registry.get_converter also be able to handle zero length objects?

Copy link
Member

Choose a reason for hiding this comment

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

I don't really know - just noted that it happens a few other places, and the more logic we can move into a consistent helper function, the better in my opinion.

rcomer reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

the more logic we can move into a consistent helper function, the better in my opinion.

I certainly agree with that. I wonder if further consolidation should wait for a separate PR though, given recent clarifications about what should and should not be backported

  • my current change fixes a regression from v3.6, so it would be good to get in a patch release
  • a change that tidies the code but (hopefully) makes no difference to the user should maybe wait till v3.8

@ksundenksunden merged commitb61bb0b intomatplotlib:mainMay 12, 2023
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull requestMay 12, 2023
greglucas added a commit that referenced this pull requestMay 13, 2023
…547-on-v3.7.xBackport PR#25547 on branch v3.7.x (FIX: `_safe_first_finite` on all non-finite array)
@rcomerrcomer deleted the safe_first_finite branchMay 30, 2023 19:47
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jklymakjklymakjklymak left review comments

@ksundenksundenksunden approved these changes

@oscargusoscargusoscargus approved these changes

Assignees
No one assigned
Labels
PR: bugfixPull requests that fix identified bugs
Projects
None yet
Milestone
v3.7.2
Development

Successfully merging this pull request may close these issues.

[Bug]: ax.errorbar raises for all-nan data on matplotlib 3.6.2 UserWarning thrown when all values are "bad", but not when only some are
4 participants
@rcomer@oscargus@jklymak@ksunden

[8]ページ先頭

©2009-2025 Movatter.jp