8000 Remove/rework uses of np.where where possible. by anntzer · Pull Request #13385 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Remove/rework uses of np.where where possible. #13385

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2019

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Feb 7, 2019

In a few places, np.where can be replaced by direct use of boolean
arrays, or other constructs.

In colors.py, the use of putmask may be slightly obscure (so I left a
comment explaining it), but avoids repeating the modified array twice,
so I think is more legible.

In polar.py, preallocating xy is just pointless given that we're
constructing intermediate arrays for x and y and filling them into xy
afterwards.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@@ -43,27 +43,19 @@ def __str__(self):
self._apply_theta_transforms))

def transform_non_affine(self, tr):
xy = np.empty(tr.shape, float)

t = tr[:, 0:1]
Copy link
Member

Choose a reason for hiding this comment

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

This slice makes t 2D; I don't remember why that was important though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know, but staring at the code it seems unlikely to matter (it's been like that ever since polar.py appeared in 2008, so probably just "historical").

@anntzer anntzer force-pushed the unwhere branch 2 times, most recently from e629ee1 to 09467ce Compare February 11, 2019 12:57
@@ -18,7 +17,7 @@
s2 = 2 * np.sin(2 * np.pi * 400 * t)

# create a transient "chirp"
mask = np.where(np.logical_and(t > 10, t < 12), 1.0, 0.0)
mask = (10 < t) & (t < 12)
s2 = s2 * mask
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 I'd rather write this as:

s2[t <= 10] = 0
s2[12 <= t] = 0

(or using an or)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, fixed (and rebased)

@QuLogic
Copy link
Member
QuLogic commented Feb 17, 2019

Seems to need a rebase though.

In a few places, np.where can be replaced by direct use of boolean
arrays, or other constructs.

In colors.py, the use of putmask may be slightly obscure (so I left a
comment explaining it), but avoids repeating the modified array twice,
so I think is more legible.

In polar.py, preallocating xy is just pointless given that we're
constructing intermediate arrays for x and y and filling them into xy
afterwards.
@timhoffm timhoffm merged commit 657e158 into matplotlib:master Feb 17, 2019
@QuLogic QuLogic added this to the v3.1.0 milestone Feb 17, 2019
@anntzer anntzer deleted the unwhere branch February 18, 2019 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0