-
Notifications
You must be signed in to change notification settings - Fork 389
ENH: Adding a contour fast-path to speed up contouring #1690
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
|
I'm certainly interested just based on the performance. I'm trying to work through the mental lift here to decide if the new approach is somehow wrong... |
fa65eee to
2f9fedd
Compare
|
It sounds like this option makes the behavior match Basemap, correct? |
|
I would agree that the |
|
I think this is the behavior of Basemap, but I am not 100% sure and I don't have a local install on hand right now to test it out. I am curious if Basemap cuts off the edges of the example or if there is potentially a better way of accounting for the wrapped coordinates in projection-space within Basemap. |
|
I was able to verify that the images look very similar with Basemap. i.e. Basemap does not handle the wrapped coordinates either. from mpl_toolkits.basemap import Basemap
fig4 = plt.figure(figsize=(12, 9))
m = Basemap(projection='robin', resolution='c', lon_0=0)
t0 = time.time()
m.contourf(lons, lats, data, latlon=True)
m.drawcoastlines()
fig4.savefig('contourf-basemap.png')
print("Basemap:", time.time() - t0) |
|
I had another thought, maybe rather than
|
d585962 to
6484d6e
Compare
|
I agree that the kwarg name could be improved. Among your suggestions, only "transform_early" works for me. (Drawing is always in projection space; and speed is a "why" not a "what is done".) Other possibilities:
|
|
Given that cartopy is relatively immature, I think the choice of default behavior should be considered carefully, and not be assumed to match current behavior. When the transform is done matters most when the grid is coarse. In that case, is it even clear that the contour vertex calculation is done better in the original coordinate system, in general? And, for the expected long-term user base of cartopy, how common are cases where the path transform approach is superior? |
|
I think you bring up some great points. The biggest difference between the approaches is with wrapped coordinates. If you have a model that produces output on a longitude grid from 160 - 200, the current behavior produces a patch that goes all the way out to the (-180, 180) bounds with a big gap in the middle. This new method sorts the transformed points (required by contour) making the bounds go from (-179 to 180) in the contouring algorithm which then fills the internal gap where we really didn't have data originally. I do think making the calculations in data-space is the better default, but I also think that there are good reasons for wanting to transform the points first and skip the slow non-affine projection of patches. keyword namesThe more I think about this, the more I wonder if we should make this an option that could be used by all of the plotting methods, similar to how we have the I like your |
|
Any progress on this? |
This adds an option to change the contouring method to use the coordinates in projection-space rather than data-space. It is much faster to project the points beforehand, rather than projecting the contours themselves.
Contouring requires the x/y arguments to be gridded and sorted, so adding in an argsort based on the x input array.
Making sure the fast_transform option gives the expected bounds and raises the proper errors. There is no speed test here because that may produce misleading results and errors depending on what capabilities the computer running the test suite has.
Change to the keyword argument transform_first to describe what the algorithm is doing.
6484d6e to
47372bb
Compare
|
I've just updated this PR for other folks that have been requesting it.
|
|
Should we name it something with |
This moves the logic for using transform_first into a decorator that can be applied to multiple functions. Currently, contour and contourf make use of it.
This parametrizes the test for transform_first and tests both contour and contourf.
47372bb to
df84beb
Compare
|
|
|
Hmm, I just re-read the docstring, and I wonder if you were interested in writing an example that shows what it means? |




This adds an option to change the contouring method to use the coordinates in projection-space rather than data-space. It is much faster to project the points beforehand, rather than projecting the contours themselves.
Before adding tests, I'd like to get a feel for whether people even think this is a good idea to have in Cartopy or not? This update basically removes the "transform" keyword from
matplotlib.axes.Axes.contourfafter already transforming the points to projection-space.Rationale
#1291 shows how slow filled contours can be in Cartopy because it is projecting the contour regions, not the points. This ends up slowing down the transformation pipeline because of the non-affine transforms of potentially many contour regions.
Implications
A new keyword argument to contourf, but no changes to the defaults.
Closes #1291
Timing example
Similar to standard MPL now, ~100x faster than the standard Cartopy for this example.