-
Notifications
You must be signed in to change notification settings - Fork 367
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
MNT: Add a cache to the Transformer creation in transform_points #1918
Conversation
Yep, this seems to solve the issue I was hitting. Here's the benchmark I was using, btw. I wrote it thinking the problem was due to transforming polygon points, but tweaking things it seems the important bits are the grid line labels. import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.patches import Polygon
import cartopy.crs as ccrs
class PolygonAnimation:
params = [
( 8, 128),
(50, 200)
]
param_names = ['n_points', 'n_frames']
def setup(self, n_points, n_frames):
self.polygon_points = \
np.array([[np.cos(x), np.sin(x)]
for x in np.linspace(0, 2 * np.pi, n_points)]) \
* 180 / np.pi
self.proj = ccrs.PlateCarree()
fig, ax = plt.subplots(subplot_kw=dict(projection=self.proj))
ax.set_global()
ax.gridlines(crs=self.proj, draw_labels={'left': 'y'}, x_inline=True)
self.figure = fig
self.polygon = ax.add_patch(Polygon(self.polygon_points,
transform=self.proj.as_geodetic()))
self.animation = FuncAnimation(fig, self.update,
frames=np.linspace(0, 360, n_frames),
blit=True)
def update(self, frame):
self.polygon.set_xy(self.polygon_points + (frame, 0))
return self.polygon,
def time_draw(self, n_points, n_frames):
self.animation.save('/tmp/cartopy-bench.gif')
|
@greglucas Could we instead make our own |
To be clear, something like: @lru_cache
def get_transformer_from_crs(crs):
return Transformer.from_crs(crs) |
5540d79
to
27bf64f
Compare
Yep, good suggestion. Should be updated on both branches now. |
This adds a cache mechanism to creating Transformer objects, which can be slow when called frequently. Additionally, this requires adding attributes to the Mercator creation as the x/y limits are created in the initializer which uses the cache/hash function.
27bf64f
to
28f53f8
Compare
This adds a cache mechanism to creating Transformer objects, which
can be slow when called frequently. Additionally, this requires
adding attributes to the Mercator creation as the x/y limits are
created in the initializer which uses the cache/hash function.
closes #1915
Rationale
Implications