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 calls to np.ma.masked_where#20511

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
dstansby merged 2 commits intomatplotlib:masterfromQuLogic:fix-ma.masked_where
Jul 5, 2021

Conversation

@QuLogic
Copy link
Member

PR Summary

Due tonumpy/numpy#18967, I've audited all our calls tonp.ma.masked_*(copy=False). When the input comes directly from the user, or is already stored somewhere already, then we don't want these calls to modify the existing values.

This affectsnp.ma.masked_where and allsimple wrappers around it likemasked_less_equal. It doesnot affectmasked_invalid, which continues to not modify the input. I have asked upstreamnumpy/numpy#19332 whether that is intended. If they intend to changemasked_invalid also, then we will need to make a few more changes like this, but we are mostly okay.

PR Checklist

  • Has pytest style unit tests (andpytest passes).
  • IsFlake 8 compliant (runflake8 on changed files to check).
  • [n/a] New features are documented, with examples if plot related.
  • [n/a] Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).
  • Conforms to Matplotlib style conventions (installflake8-docstrings and runflake8 --docstring-convention=all).
  • [n/a] New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • [n/a] API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).

For the same reason as Quiver inmatplotlib#4250.
defautoscale(self,A):
# docstring inherited.
super().autoscale(np.ma.masked_less_equal(A,0,copy=False))
super().autoscale(np.ma.array(A,mask=(A<=0)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this save the copy step? It seems like this is creating a new array anyways, so I'm wondering if it is gaining anything relative to just removing thecopy=False?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

The default fornp.ma.array iscopy=False, keep_mask=True. The former will not copy the data, and the latter will use the existing mask (if any) with the new mask, which will make a copy. Removingcopy=False frommasked_less_equal makes a copy of both.

greglucas reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying! I was curious how to test if this actually holds and learned aboutnp.shares_memory today. I did verify this assertion with that.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

You can also check, if you haveb = np.ma.array(a), thatb.base is a (ifa is originally a plain array), orb.base is a.base (ifa is originally a masked array).

NumPy 1.21.0 fixed a bug with `np.ma.masked_where` where `copy=False`now modifies the input if it is masked, which we do not want to do.`np.ma.array` defaults to not copying the data, but copies the mask(adding the new mask), which is what we wanted with `copy=False`.
@dstansbydstansby merged commit33beff9 intomatplotlib:masterJul 5, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull requestJul 5, 2021
@QuLogicQuLogic deleted the fix-ma.masked_where branchJuly 5, 2021 21:37
QuLogic added a commit that referenced this pull requestJul 6, 2021
…511-on-v3.4.xBackport PR#20511 on branch v3.4.x (Fix calls to np.ma.masked_where)
@jorisvandenbossche
Copy link

@QuLogic a small note that Ithink this change can cause a bug when the arrayA is not an array but a list. Innp.ma.masked_less_equal(A, 0, copy=False), theA as input to the function is converted an array, while innp.ma.array(A, mask=(A <= 0)) it already needs to be an array up front (otherwiseA <= 0 will fail).

We ran into this with GeoPandas (geopandas/geopandas#2066, didn't test it, but I assume this PR is the cause), where we were (incorrectly) setting the array of a color map with a listcmap.set_array(np.array([])).
This was of course wrong to do, as the docs clearly say it should be an ndarray (although in general people might expect that functions needing an array also take general "array-likes", as do most functions in numpy).

Just noting it here, I'll let it to your judgement whether this is worth addressing or not (it's certainly not needed just for GeoPandas).

@greglucas
Copy link
Contributor

Just a note that I am pretty sure this is fixed in master due to a new call to a call tocbook.safe_masked_invalid() inset_array() now.

A=cbook.safe_masked_invalid(A,copy=True)

v3.4.3

>>>sm.set_array([])>>>sm.get_array()[]

master

>>>sm.set_array([])>>>sm.get_array()masked_array(data=[],mask=False,fill_value=1e+20,dtype=float64)

I don't know if another 3.4.x release is planned sometime, but it should be a pretty simple fix to add that call in to cast to array immediately in the set_array call.

@QuLogic
Copy link
MemberAuthor

That came in from#18870, which I don't think we can backport fully. But we can make a small correction to this, as it's not required thatautoscale must be called internally only.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dstansbydstansbydstansby approved these changes

@greglucasgreglucasgreglucas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

v3.4.3

Development

Successfully merging this pull request may close these issues.

4 participants

@QuLogic@jorisvandenbossche@greglucas@dstansby

[8]ページ先頭

©2009-2025 Movatter.jp