-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
Do you have a profiling script that shows the difference? |
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)') |
CI seems to be possibly genuinely failing. |
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. |
This issue was hidden by the fact that all transformations created a new array. |
17fc9e1
to
3f26a2f
Compare
flake8 check fails with
|
68006e7
to
b1bbb8d
Compare
36d66cf
to
0013958
Compare
…tion. Fix revealed bug in backend bases, save some transformation copys
0013958
to
88e40cb
Compare
7bd9d3c
to
4f66d31
Compare
4f66d31
to
740ffbf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
post-ci
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