8000 Cleanups: np.clip and np.ptp are awesome by anntzer · Pull Request #7488 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanups: np.clip and np.ptp are awesome #7488

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 3 commits into from
Jul 2, 2017

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Nov 20, 2016

All's in the title.

Also clarified that draw_rubberband does not require its arguments to be ordered... tested on all backends except on gtk, which seems broken as of master for me, and macos, which I cannot test and for which I would appreciate a confirmation (see https://gitter.im/matplotlib/matplotlib?at=5831157a238757566ccb3580).

x, lastx = max(min(x, lastx), x1), min(max(x, lastx), x2)
y, lasty = max(min(y, lasty), y1), min(max(y, lasty), y2)

(x1, y1), (x2, y2) = np.clip(
Copy link
Member

Choose a reason for hiding this comment

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

This is on a hot-path (mouse move event callback), might be worth micro-benchmarking this.

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 couldn't see any slow down when trying the different backends manually (because the drawing of the canvas is probably way slower than this, even though it's obvious the new version will be a bit slower). I can roll back this patch if you prefer...

Copy link
Contributor
@afvincent afvincent left a comment

Choose a reason for hiding this comment

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

I still did not have time to do a thourough check on the “draw_rubberband does not require its arguments to be ordered” thing. But I have another comment that may be worth to be read before I actually manage to find the time to finish the review (if nobody else does it before), so…

@@ -3675,8 +3675,7 @@ def dopatch(xs, ys, **kwargs):

# width
if widths is None:
distance = max(positions) - min(positions)
widths = [min(0.15 * max(distance, 1.0), 0.5)] * N
widths = [np.clip(np.ptp(positions), 0.15, 0.5)] * N
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I am missing something, but this does not seem to be equivalent to me. For example, with positions = [1.1, 0], i.e. (distance = 1.1) then [min(0.15 * max(distance, 1.0), 0.5)] * N => [0.165] * N but [np.clip(np.ptp(positions), 0.15, 0.5)] * N => [0.5] * N.

[np.clip(0.15*np.ptp(positions), 0.15, 0.5)] * N should be equivalent to the old approach.

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 updated the docstring to reflect the actual behavior (which was misdocumented).

@QuLogic
Copy link
Member
QuLogic commented May 28, 2017

This needs some small conflict resolution.

@anntzer
Copy link
Contributor Author
anntzer commented May 29, 2017

Fixed.
Thanks for looking at this.

@anntzer
Copy link
Contributor Author
anntzer commented Jun 6, 2017

smuggled in an additional (unrelated, but tiny) cleanup (duplicated line, see last commit)

@QuLogic
Copy link
Member
QuLogic commented Jun 9, 2017

Conflicts (from your other PR?)

@anntzer
Copy link
Contributor Author
anntzer commented Jun 10, 2017

rebased

@tacaswell tacaswell closed this Jun 13, 2017
@tacaswell tacaswell reopened this Jun 13, 2017
@dstansby dstansby merged commit 9422409 into matplotlib:master Jul 2, 2017
@anntzer anntzer deleted the clip-ptp branch July 2, 2017 14:29
@QuLogic QuLogic added this to the 2.1 (next point release) milestone Jul 2, 2017
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.

5 participants
0