cartopy: Major slowdown of app after upgrading to cartopy 0.20 that uses pyproj
I am still investigating but I found that somewhere in my app, the projecting is taking 97% of the load time.
97.00% 97.00% 322.7s 322.8s __init__ (pyproj/crs/crs.py:313)
1.00% 1.00% 14.25s 14.25s equals (pyproj/crs/crs.py:933)
Will update this as I figure out where it’s coming from.
Some context, I am using geoviews, datashader to plot millions of points. I am not sure if geoviews / datashader is using an outdated cartopy method to transform the points; will test.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 32 (16 by maintainers)
I’ve also had numerous problems with Cartopy 0.20.1/Shapely 1.8 suddenly being unbearably slow after I recently upgraded to them. Plotting the terrain field for a large WRF domain of 1062 x 512 points (dx = 1 km) suddenly took nearly 11m 30s. That’s unacceptably slow. With my prior version of Cartopy (0.16, I think??), it took “only” 7 min, which was still far too slow to be useful. (I’m running this on NCAR’s Cheyenne, on a head node.)
I set
PYPROJ_GLOBAL_CONTEXT=ON
, but that had little effect on the speed for this plot. (I noticed more of a speed-up for other plots I tried of different data, though.)I then used the
transform_first
argument toplt.contourf
([https://scitools.org.uk/cartopy/docs/latest/gallery/scalar_data/contour_transforms.html]), and that resulted in a noticeable speed-up, to 10m 45s.However, commenting out the call to draw the oceans (
ax.add_feature(oceans)
), but instead assigning my ocean color usingax.set_facecolor
, resulted in a SIGNIFICANT speed-up, all the way down to 12 s. If I eliminated thetransform_first
argument again, it slowed down to just over 1 min, so transform_first is still quite helpful.So for a temporary workaround to anyone experiencing similar major slowdowns with Cartopy 0.20+, hopefully implementing these steps will help:
transform_first
keyword in thecontourf
call (moderate speed-up).PYPROJ_GLOBAL_CONTEXT=ON
for a potential speed-up (in some situations, but not all).I also very much look forward to Cartopy 0.20.2 release to eliminate the ShapelyDeprecationErrors (#1936) that also started happening constantly once I upgraded.
Are you setting
PYPROJ_GLOBAL_CONTEXT=ON
? See the note on the new docs about that speed-up: https://scitools.org.uk/cartopy/docs/latest/index.htmlHowever, this looks like a lot of pyproj CRS objects are being created, rather than re-using the same object which would be causing the slowdown. I wonder if we should look at putting a cache on the CRS constructors in addition to the Transformers?
Hi All
this seems like a pretty major performance hit on the 0.19 0.20 transition
does it seem like this is an edge case? is this a performance hit that could be seen (oom) for many cases?
Has any consideration has been given to providing access to the proj transforms for users, enabling them to opt out of the pyproj transforms if they desire?
I’m a bit worried that the documentation note:
I feel that there is more that should be considered here than only
PYPROJ_GLOBAL_CONTEXT=ON
many thanks marqh
D’oh! Thanks for the better programmatic access.
While this is definitely faster, it still gets slow as I zoom in further, even without the ocean. Here’s my current example:
It took about a minute to zoom in on a region roughly corresponding to Scotland (including the Hebrides and a bit of Northern Ireland). Here’s the start of the
cProfile
output:I’m not sure if I should expect Cartopy to take that long but it surprises me.
Thanks for posting these results! I think this points to the need for some documentation around potential speedups that people can try all in one place rather than spread throughout the GitHub Issues.
From my reading of everything, it seems like the major issue is the
oceans
feature and notland
orcoastlines
? This may mean we should look into optimizing polygon edge closing with the boundaries.You should be able to get the colors programmatically from that location too:
cartopy.feature.COLORS["water"]
https://scitools.org.uk/cartopy/docs/latest/reference/generated/cartopy.feature.COLORS.html#cartopy.feature.COLORSThanks @jaredalee! I found this very helpful, reducing the time to zoom in on my point map from ~30s to about ~3s.
If anyone else is curious, Cartopy’s water colour is
np.array((152, 183, 226)) / 256.
:https://github.com/SciTools/cartopy/blob/591fb5450e11b42b6de1cebe4f240112f915bd52/lib/cartopy/feature/__init__.py#L22-L24
Sorry for the delay, sadly not much.
~13.8 seconds to ~13.4 seconds.
@ktyle Yeah, I realized that the ocean is not a necessary feature to draw. Just make the axes background the ocean color and then draw the land, which is much less expensive to do.
So I think I just reproduced what @greglucas stated above, the segment-by-segment reprojection in
trace.pyx
is what is very slow and what causescfeature.LAND
to be so slow, given the large number of segments it has.@akrherz I find an even larger performance penalty by adding cfeature.OCEAN (esp. with 10m res and transforming to another CRS), but this pre-dates 0.20.