8000 Small optimizations to scale and translate of Affine2D by Tillsten · Pull Request #17165 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Small optimizations to scale and translate of Affine2D #17165

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 2 commits into from
Apr 30, 2020

Conversation

Tillsten
Copy link
Contributor

PR Summary

Instead of building a transformation matrix and do the matrix multiplication, just change the matrix directly. This change also exposed a bug in draw_path_collection where Affine2D was called with the wrong argument.

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

@anntzer
Copy link
Contributor
anntzer commented Apr 17, 2020

Do you have a profiling script that shows the difference?

@Tillsten
Copy link
Contributor Author
import matplotlib.transforms as tr
import numpy as np

a = tr.Affine2D.from_values(1, 0, 0, 1, 5, 5)
arr = a.get_matrix().copy()
arr2 = a.get_matrix().copy()
arr3 = a.get_matrix().copy()
print(a.get_matrix())

def scale_mpl(arr, sx, sy):
    scale_mtx = np.array(
        [[sx, 0.0, 0.0], [0.0, sy, 0.0], [0.0, 0.0, 1.0]])
    return np.dot(scale_mtx, arr, out=arr)

def scale(arr, sx, sy):
    arr[0, :] *= sx
    arr[1, :] *= sy
    return arr

def scale_2(arr, sx, sy):
    arr[0, 0] *= sx
    arr[0, 1] *= sx
    arr[0, 2] *= sx
    arr[1, 0] *= sy
    arr[1, 1] *= sy
    arr[1, 2] *= sy
    return arr
        
scale_mpl(arr, 2, 5), scale(arr2, 2, 5),  scale_2(arr3, 2, 5)
# In[143]:
get_ipython().run_cell_magic('timeit', 'arr = a.get_matrix().copy()', 'scale_mpl(arr, 2, 2)\nscale_mpl(arr, 0.5, 0.5)')

# In[144]:
get_ipython().run_cell_magic('timeit', 'arr = a.get_matrix().copy()', 'scale(arr, 2, 2)\nscale(arr, 0.5, 0.5)')

# In[145]:
get_ipython().run_cell_magic('timeit', 'arr = a.get_matrix().copy()', 'scale_2(arr, 2, 2)\nscale_2(arr, 0.5, 0.5)')

# In[149]:
a = tr.Affine2D.from_values(1, 0, 0, 1, 5, 5)
arr = a.get_matrix().copy()
arr2 = a.get_matrix().copy()
arr3 = a.get_matrix().copy()

def translate_mpl(arr, tx, ty):
    scale_mtx = np.array(
        [[1.0, 0.0, tx], [0.0, 1.0, ty], [0.0, 0.0, 1.0]])
    return np.dot(scale_mtx, arr, out=arr)

def translate(arr, tx, ty):
    arr[0, 2] += tx
    arr[1, 2] += ty
    return arr
        
translate_mpl(arr, 2, 5), translate(arr2, 2, 5)
# In[150]:
get_ipython().run_cell_magic('timeit', 'arr = a.get_matrix().copy()', 'translate(arr, 2, 2)\ntranslate(arr, -2, -2)')

# In[151]:
get_ipython().run_cell_magic('timeit', 'arr = a.get_matrix().copy()', 'translate_mpl(arr, 2, 2)\ntranslate_mpl(arr, -2, -2)')

@anntzer
Copy link
Contributor
anntzer commented Apr 17, 2020

CI seems to be possibly genuinely failing.

@Tillsten
Copy link
Contributor Author

Yep, it is a real issue. I think it is caused by the fact that neither the Affine2D constructor nor the get_matrix method make copy. Since transformation are initiallized by matricies of other transformation this is a problem.

@Tillsten
Copy link
Contributor Author

This issue was hidden by the fact that all transformations created a new array.

@Tillsten Tillsten force-pushed the optimize_transform branch from 17fc9e1 to 3f26a2f Compare April 17, 2020 13:53
@timhoffm
Copy link
Member

flake8 check fails with

./lib/matplotlib/backend_bases.py:232:38: W291 trailing whitespace

@Tillsten Tillsten force-pushed the optimize_transform branch 2 times, most recently from 68006e7 to b1bbb8d Compare April 20, 2020 20:21
@timhoffm timhoffm dismissed their stale review April 21, 2020 18:38

Comment handled

@Tillsten Tillsten force-pushed the optimize_transform branch from 36d66cf to 0013958 Compare April 29, 2020 20:34
…tion.

Fix revealed bug in backend bases, save some transformation copys
@Tillsten Tillsten force-pushed the optimize_transform branch from 0013958 to 88e40cb Compare April 29, 2020 20:38
@Tillsten Tillsten force-pushed the optimize_transform branch from 7bd9d3c to 4f66d31 Compare April 30, 2020 08:16
@Tillsten Tillsten force-pushed the optimize_transform branch from 4f66d31 to 740ffbf Compare April 30, 2020 08:22
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.

post-ci

@timhoffm timhoffm added this to the v3.3.0 milestone Apr 30, 2020
@timhoffm timhoffm merged commit 99ba9f9 into matplotlib:master Apr 30, 2020
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.

5 participants
0