-
Notifications
You must be signed in to change notification settings - Fork 365
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
contourf method significantly slower than basemap #1291
Comments
This is a critical problem. In trying to translate one of my oceanography class notebooks from basemap to cartopy, a 2-second basemap plot took over 2 minutes with cartopy. That's prohibitive. As @crazyapril notes, the problem seems to be coming from a calculation added in 2016: |
Fundamentally cartopy's contour implementation is doing a lot more work than Basemap needs to do, as we are projecting the contour lines once they have been calculated. The commit referenced is ensuring that the data limits are respected by transforming the coordinates from source to target projection. If this calculation is a prohibitive/expensive operation, then it is always possible to fall back to the equivalent Basemap behaviour - namely, you transform your data yourself, and pass the reprojected data to be contoured in the target project. It isn't ideal as the whole point of cartopy is that you shouldn't have to worry about this stuff (like datelines and poles), but it does highlight that cartopy is a superset of the capabilities available with Basemap. To illustrate my meaning: Current code:
Becomes:
|
In the case of contouring, There is still the question of why the expensive calculation to check data limits is invoked only for contourf and not for contour. And, I suspect the calculation is itself much more expensive than it might need to be, because it is having to navigate a tangle of python steps to do many transform calls instead of just transforming a very large number of points in one call into proj4. I appreciate the convenience, generality, and elegance of cartopy's design. I'm hoping that performance in some of the worst cases can be greatly improved with some optimizations. |
I've experienced this issue as well, generating a lot of contourf maps in Basemap takes me 15 minutes but in Cartopy it takes 1 hour 4 minutes. I tried working around it as suggested above with Anyway, hoping that whatever causes this slowdown can be fixed, or a workaround appears. |
To do it manually with lat/lon arrays that each define 1 dimension of a 2D region, you need to do the work of assembling them into the same size arrays. You can accomplish this using |
So one year later I restarted the migration based on the suggestion above and in case someone may need it, just replace ret = ax.projection.transform_points(source_proj, np.array(x),
np.array(y), np.array(data)) # This method only accept ndarray!
xx = ret[..., 0]
yy = ret[..., 1]
data = ret[..., 2]
ax.contourf(xx, yy, data) Still, I think in this case the slowdown it brings somewhat outweigh the convenience. It would be better if this behavior is explained in the documentation. |
Unless your data actually represent a height above ground, you shouldn't be passing that to ret = ax.projection.transform_points(source_proj, np.array(x),
np.array(y)) # This method only accept ndarray!
xx = ret[..., 0]
yy = ret[..., 1]
ax.contourf(xx, yy, data) |
I don't think the data transformation is the bottleneck. It may make it slower, but the problem here as noted above is the calculation of the data limits. After doing some testing, even without a data transformation cartopy is MUCH slower than matplotlib with contouring. I use cartopy in several operational scripts and it has gotten to the point where the plotting speed (specifically when using contourf) is a major limiting factor; the plots just can't keep up with the data coming in (unless we had 100's of cpu's available). As the community continues to migrate to cartopy, I think others will run into the same problem. I think this is an issue which should get some more attention to find out where exactly the bottlenecks are and what can be done. I would think that all of the contour math calculations should be vectorized, if possible, as a start. |
I remember profiling all of this a while ago and the bottleneck is in the If you need it operationally fast, did you try the workaround mentioned above? |
Thanks @karlwx! I did not realize that was the case even without transforms. I also don't get why the same accurate bounding box calculations aren't computed for the I agree this seems like a pretty big speed problem, especially if someone is already setting the extent manually and doesn't care about the limits being updated accurately. I think the lowest hanging fruit would be to add a keyword argument to cartopy/lib/cartopy/mpl/geoaxes.py Lines 1574 to 1580 in d12c86c
|
I have run into this issue as well trying to use
|
I had some success improving the speed of hi-res plots like that by using xarray coarsen to resample the data to a lower resolution (did this with National Blend of Models), but it kind of defeats the purpose of the hi-res guidance in the first place. The bottom line is, the plotting time is still excessive. |
I'll mess around with things a little more this evening. I pulled up some code from when I was playing with GOES16 data a year ago or so. In that code, I used |
So the difference is |
I'll run a benchmark between the two and also drop the images to show comparisons in their quality. This could be an alternative at least when dealing with hi-res data being displayed in CONUS. |
Putting a few cross-referenced issues here with other libraries dealing with the non-affine transforms being slow. MPL issue with slow non-affine contours: Astropy's fix: I think there will still be issues with the contours if one of them gets wrapped and needs to be cut along a boundary or something like that though, so the Astropy fix may not be totally suitable for our use-case. |
Hi greglucas: was this ever implemented? I've hunted around for any speed optimisations recently and I don't see anything. I've just benchmarked a whole suite of plotting jobs in NCL, basemap, and cartopy, and NCL beats cartopy in over half the time, and I'm banging my head on a wall trying to rectify this. Any help - or a lay-of-the-land in case I missed something - would be greatly appeared, thanks. |
Hi John,
I can tell you that the code changes do indeed work. I helped to verify the
code when this PR came up because I had the exact same issue you are
describing. With the issue still being open, the changes were not merged
in. What I've personally done is make an Anaconda build with the latest
MetPy build and then take the code changes from the PR and apply them to
your build.
Or if you're not an Anaconda type person and you just need the cartopy
build, then clone the PR'd branch here:
https://github.com/greglucas/cartopy/tree/contour_fastpath
Best,
Garrett GwiazdaMeteorologist
Senior Software Engineer
***@***.***
(802)399-4157*wxyard.com* <http://www.wxyard.com>
Queensbury, NY 12804
------------------------------
…On Wed, Jun 16, 2021 at 11:38 AM John Lawson ***@***.***> wrote:
@karlwx <https://github.com/karlwx> and anyone else interested, I opened
up PR #1690 <#1690> with a faster
contour option. Feel free to take a look at that and comment there if you
have other thoughts/ideas on it, or if it doesn't work for your use-case.
Hi greglucas: was this ever implemented? I've hunted around for any speed
optimisations recently and I don't see anything. I've just benchmarked a
whole suite of plotting jobs in NCL, basemap, and cartopy, and NCL beats
cartopy in over half the time, and I'm banging my head on a wall trying to
rectify this. Any help - or a lay-of-the-land in case I missed something -
qouls be greatly appeared, thanks.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1291 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABK6TXB3IH37G2QWKRFELITTTDARFANCNFSM4HDJAQWA>
.
|
Well, that is highly appreciated, thank you! I am a "conda person" so I'll crack on with this. Many thanks. |
Would you two mind commenting on the PR with your thoughts? If this is a feature that would help you it would be great to get feedback from users on it and try to get it in a release! :) |
Just a nice extra verification, here is my RabbitMQ generating thousands of
HRRR graphics an hour with the upload rate at 2 images a second on a
rickety old server (2 - X5650 using 14 of the 24 threads) :
[image: image.png]
Garrett GwiazdaMeteorologist
Senior Software Engineer
***@***.***
(802)399-4157*wxyard.com* <http://www.wxyard.com>
Queensbury, NY 12804
------------------------------
On Wed, Jun 16, 2021 at 11:53 AM John Lawson ***@***.***>
wrote:
… Hi John, I can tell you that the code changes do indeed work. I helped to
verify the code when this PR came up because I had the exact same issue you
are describing. With the issue still being open, the changes were not
merged in. What I've personally done is make an Anaconda build with the
latest MetPy build and then take the code changes from the PR and apply
them to your build. Or if you're not an Anaconda type person and you just
need the cartopy build, then clone the PR'd branch here:
https://github.com/greglucas/cartopy/tree/contour_fastpath Best, Garrett
GwiazdaMeteorologist Senior Software Engineer *@*.*** (802)399-4157*wxyard.com
<http://wxyard.com>* http://www.wxyard.com Queensbury, NY 12804
------------------------------
… <#m_-7520066267390444570_>
On Wed, Jun 16, 2021 at 11:38 AM John Lawson *@*.***> wrote: @karlwx
<https://github.com/karlwx> https://github.com/karlwx and anyone else
interested, I opened up PR #1690
<#1690> <#1690
<#1690>> with a faster contour
option. Feel free to take a look at that and comment there if you have
other thoughts/ideas on it, or if it doesn't work for your use-case. Hi
greglucas: was this ever implemented? I've hunted around for any speed
optimisations recently and I don't see anything. I've just benchmarked a
whole suite of plotting jobs in NCL, basemap, and cartopy, and NCL beats
cartopy in over half the time, and I'm banging my head on a wall trying to
rectify this. Any help - or a lay-of-the-land in case I missed something -
qouls be greatly appeared, thanks. — You are receiving this because you
commented. Reply to this email directly, view it on GitHub <#1291
(comment)
<#1291 (comment)>>,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABK6TXB3IH37G2QWKRFELITTTDARFANCNFSM4HDJAQWA
.
Well, that is highly appreciated, thank you! I am a "conda person" so I'll
crack on with this. Many thanks.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1291 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABK6TXDBPW2T4ZCGPLOCZ3TTTDCHTANCNFSM4HDJAQWA>
.
|
Hi all-- Is this PR still open and ongoing? I'll admit it's been an exciting find. We're currently in the middle of a (long overdue) migration from basemap, but are really struggling to engineer a solution to this problem. Contourf just takes too long, especially the dozens--if not hundreds--of images we create routinely through one programming cycle. I'd love to test this, if it's still an option. I'm working in a Conda framework, which I readily admit is not my specialty, so I'm a little out of my element. I've attempted to PIP Greg's clone for the fast_transform (https://github.com/greglucas/cartopy.git) using 'git' inside the Conda framework, but I must have fouled up the process. When I attempt to employ the 'fast' or 'fast_transform' in code, it fails, telling me it can't recognize that kwarg: ax.contourf(lons, lats, data, transform=ccrs.PlateCarree(), fast=True) Anyway, I'm sure I'm doing something wrong...but if anybody can give me a kick in the pants so I can give this a try, I'd appreciate it. As always, thanks! |
@wxninja, you'll have to be able to install from source to get it to work. But, you will want to checkout the specific branch contour_fastpath, not the master branch. If you go over to the PR #1690, you should be able to keep up-to-date with what is going on. Here are the GitHub suggested commands you need to check that PR out locally and test it. git checkout -b greglucas-contour_fastpath master
git pull https://github.com/greglucas/cartopy.git contour_fastpath Feel free to chime in on the PR over there with comments/suggestions! |
@greglucas Thanks! I'm up and running now. Turns out I just kept getting the syntax wrong when trying to pull 1690. Duh. Just wanted to chime in and say thank you for engineering this potential solution. I think it'll be a great help, and expand the 'usability' of Cartopy for those who need to make large amounts of graphics, or make graphics quickly. |
Greg is an absolute treasure to this group and goodness knows how many people he has saved in the more “operational” world. I mean, you’re all treasures really…
On a serious note, I must add yet another two cents that this speed issue is critical for so many users told to migrate from basemap to cartopy. I appreciate the lack of time and resources. I wonder if there’s any ideas or desire over writing a grant proposal to get some real labour devoted to optimisation of cartopy. I’ve only had experience leading proposals in academia, strictly on weather science and not things like this, but the people volunteering their free time on here deserve support! And more help! (I’m running at 100% myself so it’s frustrating I can’t offer more help more often).
John
… On Sep 16, 2021, at 06:17, wxninja ***@***.***> wrote:
@greglucas Thanks! I'm up and running now. Turns out I just kept getting the syntax wrong when trying to pull 1690. Duh.
Just wanted to chime in and say thank you for engineering this potential solution. I think it'll be a great help, and expand the 'usability' of Cartopy for those who need to make large amounts of graphics, or make graphics quickly.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Thanks for the kind words, @johnrobertlawson! Note to everyone in this thread: The PR addressing this issue has just been merged and you will be able to use the keyword argument |
Greg,
I would also like to say thank you again for your fix. I have been using it
since November and it has worked like a charm. It was so desperately
needed! And with my best knowledge being in other software libraries, you
really did come in and save the day...month...year!?!?!
Best,
Garrett GwiazdaMeteorologist
Senior Software Engineer
***@***.***
*wxyard.com* <http://www.wxyard.com>
Queensbury, NY 12804
------------------------------
…On Thu, Sep 16, 2021 at 4:52 PM Greg Lucas ***@***.***> wrote:
Thanks for the kind words, @johnrobertlawson
<https://github.com/johnrobertlawson>!
*Note to everyone in this thread:* The PR addressing this issue has just
been merged and you will be able to use the keyword argument
transform_first=True to get the faster behavior in the next release. Be
warned that it does not handle wrapped coordinates as well, but on limited
domains it should provide a significant speed improvement!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1291 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABK6TXERTHCMJUEH27UCV53UCJKIDANCNFSM4HDJAQWA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Would it be possible to implement something similar with barbs? I am experiencing significant slowdowns with the barb transformation with high-resolution data. Appears I'm not the only one: https://stackoverflow.com/questions/71578820/use-transform-first-keyword-in-barbs |
Description
I'm migrating my codes from Basemap to Cartopy. Generally Cartopy is always faster, however I find contourf method can take me seconds. Here's my example code.
The result:
which means Cartopy can be up to 16x slower on my old laptop. After some profiling work I find the culprit is https://github.com/SciTools/cartopy/blob/master/lib/cartopy/mpl/geoaxes.py#L1505, it seems this step is to calculate new datalim and autoscale the extent. This is how far I can get to since I don't have enough time to dig in.
Environment
Thanks!
The text was updated successfully, but these errors were encountered: