Skip to content

Vectorize patch extraction in Axes3D.plot_surface #16675

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

Conversation

apaszke
Copy link
Contributor

@apaszke apaszke commented Mar 5, 2020

PR Summary

First of the improvements suggested in #16659.

When rstride and cstride divide the numbers of rows and columns minus 1,
all polygons have the same number of vertices which can be recovered
using some stride tricks on NumPy arrays. On a toy benchmark of a
800x800 grid this allows to reduce the time it takes to produce this
list of polygons from ~10s to ~60ms.

The formatting gets a bit awkward in some places due to the 79 column limit 😕 Also, Flake 8 seems to report many errors in the files I've modified, but I've only tried to make sure the lines I've added don't show up to minimize the diff.

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

@apaszke
Copy link
Contributor Author

apaszke commented Mar 5, 2020

I just did a quick benchmark, and this PR reduces the runtime of the following script from 16s to 10s on my old MBP (speedup might be higher on systems with more cores):

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

x = y = np.linspace(-1, 1, 400)
x, y = np.meshgrid(x, y)
z = x ** 2 + y ** 2

ax.plot_surface(x, y, z, rstride=1, cstride=1)
fig.savefig('dump.png')
plt.close()

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

Some minor comments which need to be addressed but are not actually really blocking, this basically looks great.

@apaszke
Copy link
Contributor Author

apaszke commented Mar 5, 2020

Also note that I haven't added any tests because I've checked that existing plot_surface tests trigger both code paths.

@apaszke
Copy link
Contributor Author

apaszke commented Mar 6, 2020

@anntzer I added the padding in #16699.

@anntzer
Copy link
Contributor

anntzer commented Mar 6, 2020

let's keep that discussion in the corresponding PR then.

@apaszke
Copy link
Contributor Author

apaszke commented Mar 6, 2020

@anntzer Please see if the _array_patch_perimeters comment is satisfactory now. I think all your concerns should be addressed now.

@anntzer
Copy link
Contributor

anntzer commented Mar 6, 2020

looks great, just needs a second positive review.
you may also consider squashing your commits.

@apaszke
Copy link
Contributor Author

apaszke commented Mar 6, 2020

Is there any reason to prefer squashing on my side to merge + squash on GitHub? I've usually done the latter for most projects. What's the policy here?

@apaszke apaszke force-pushed the vectorize_surface_plot_perimeters branch from 50e2157 to 5a8a5f4 Compare March 6, 2020 14:23
@anntzer
Copy link
Contributor

anntzer commented Mar 6, 2020

I think it's mostly habit, though perhaps @tacaswell will have an opinion there :)

@QuLogic
Copy link
Member

QuLogic commented Mar 12, 2020

Personally, I dislike GitHub squash merge because it breaks contributors' git prune + git branch -d.

@QuLogic
Copy link
Member

QuLogic commented Mar 12, 2020

Small correction, parameter names in docstrings should be *var*, not **var**.

@apaszke
Copy link
Contributor Author

apaszke commented Mar 12, 2020

@QuLogic I've applied the suggested changes and added an example for _unfold.

@QuLogic
Copy link
Member

QuLogic commented Mar 13, 2020

Thanks, the example is quite helpful.

@QuLogic
Copy link
Member

QuLogic commented Mar 13, 2020

Do you want to squash this?

@apaszke apaszke force-pushed the vectorize_surface_plot_perimeters branch from 6af6818 to ae5c7b3 Compare March 13, 2020 09:54
When rstride and cstride divide the numbers of rows and columns minus 1,
all polygons have the same number of vertices which can be recovered
using some stride tricks on NumPy arrays. On a toy benchmark of a
800x800 grid this allows to reduce the time it takes to produce this
list of polygons from ~10s to ~60ms.
@apaszke apaszke force-pushed the vectorize_surface_plot_perimeters branch from ae5c7b3 to 907235d Compare March 13, 2020 09:55
@apaszke
Copy link
Contributor Author

apaszke commented Mar 13, 2020

@QuLogic done!

@tacaswell tacaswell added this to the v3.3.0 milestone Mar 13, 2020
@tacaswell
Copy link
Member

Failure looks like disk / filesystem related problems.

@tacaswell tacaswell merged commit 8094fb1 into matplotlib:master Mar 13, 2020
@tacaswell
Copy link
Member

Thanks @apaszke ! Congratulations on your first Matplotlib PR 🎉 hopefully we will hear from you again.

@QuLogic
Copy link
Member

QuLogic commented Mar 13, 2020

Failure was #16735.

@apaszke
Copy link
Contributor Author

apaszke commented Mar 13, 2020

@tacaswell Thanks! I already have a few followup PRs posted, so I'd appreciate reviews on those!

Also thanks to reviewers for helping to get this in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants