-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
DOC: Revive Irregularly spaced data contour example #11180
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
DOC: Revive Irregularly spaced data contour example #11180
Conversation
# Perform linear interpolation of the data (x,y) | ||
# on a grid defined by (xi,yi) | ||
triang = tri.Triangulation(x, y) | ||
interpolator = tri.LinearTriInterpolator(triang, z) |
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.
OK, Hmmmm. I'm not 100% convinced that we should suggest people use the in-house triangular and interpolator. In the same vein that mlab
is going away, my guess is that tri
will go away. I'd rather we tell people the "best practice" and I'm not sure our sparsely documented in-house triangulation routine is that. Happy to be corrected, but I'd lean towards the scipy reference.
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.
@jklymak I assure you that tri
is not going to "go away". What on earth possessed you to assert such a guess?
I am 100% convinced that we should suggest people use the in-house triangulator and interpolator, and unlike yourself I am an expert in the area. Consider yourself corrected, and in future kindly refrain from expressing derogatory opinions on functionality you have no knowledge of.
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.
I wasn’t meaning to be offensive about the actual algorithm, any more that I would be about mlab.psd. Just that it does something that scipy also does. Do we need to be providing duplicate functionality, and more importantly do we want to guide users to this code?
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.
I don't think what @jklymak said was particularly derogatory...
Is there a reason Matplotlib runs a custom triangulator, and doesn't use an upstream one? And if ours is better than what is available in either numpy or scipy, should we try and push the mpl triangulation code upstream? It feels to me like that's where good triangulation code should live.
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.
I don't think this is about triangulation. It's about the interpolators from tri.triinterpolate
.
I cannot judge on whether they are needed or not, but here are the timings from the example:
* LinearTriInterpolator 0.0444 s
* scipy griddata 0.0728 s
* matplotlib tricontour 0.0301 s
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.
I'm totally in favor of sharing the love in any stream direction.
In this particular case the motivation is to share the love with users looking for ways to plot irregularly spaced data.
One way of doing so is to inform them about the in-built option for interpolation. Since this in-built option will not vanish from the 2.2 branch with 110% certainty, there is nothing wrong with using that for now. And it would prevent the necessity to include a new large dependecy for the docs during the final stage of this branch.
For the development branch, one may sure discuss if it's useful to change the example and include scipy (a 12 Mb package, 40 Mb extracted) as documentation dependency.
I do not have a clear opinion on that, but I wouldn't want to wait to include this example until someone may or may not have shared their love for tri
upstream (and got their love returned).
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.
There is no 'upstream', no de facto standard library for representing and manipulating triangular grids in python. If there was, we should certainly use it. Until then, we are one of the gathering places for such functionality. It started as just rendering triangular grids, which is clearly within our remit, and when interpolation and mesh refinement were implemented they were added to our code as the best (least bad?) place to put them as our code is actively maintained and will be around for a long time.
Numpy isn't the place for such functionality, scipy would be more appropriate but I don't think it is as suitable as a specific tri grids library (named tripy
perhaps?). Every so often there is an interest in such as library, but after lots of chat my summary is that there are plenty of people who want to use such a library, nobody wants to write it. From my point of view, even if a tripy
existed the rendering of tri grids, which is what I am mostly interested it, would still reside within mpl.
Without delving too deeply into the workings of our tri code, there are three use cases for creating and using a triangulation object:
- User specifies the 2D points and requests a Delaunay triangulation is calculated for them.
- The same as 1, and then the user masks out some of the unwanted triangles.
- User specifies both the 2D points and the triangulation.
scipy.griddata can only deal with use case 1, our code deals with all three. Unfortunately use case 1 is the one that most examples refer to as it is the entry point to triangular grids for those not used to them. But nearly all serious tri grid work uses the other 2 use cases.
Hence we should not recommend using scipy.griddata, it simply doesn't include the functionality that our code requires and has. But unfortunately we can't just ignore it and not mention it in such examples; we need to keep it as there are lots of historical web pages mentioning griddata, and we need to catch the attention of people looking for it so that we can point out our alternatives. Keeping it in this example but just within a comment is an approach that I like.
Unfortunately griddata have been around for years and people are familiar with it, so anyone who asks about tri grids receives a well-intended but sub-optimal answer to interpolate to a regular grid. It is simply unnecessary. Everyone should use tricontour
for optimal results: the boundaries and masked areas are correct, it has higher resolution where the grid has higher resolution. If higher resolution is required, there is an excellent TriRefiner
that creates a new triangulation, keeping sensible boundaries, etc and can avoid badly-formed triangles (e.g. long and thin); it is featured in the tricontour_smooth_delaunay
and tricontour_smooth_user
examples.
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.
This is quite a bit of useful information. I think it shouldn't stay hidden inside the review section of a PR; but is there any good place for such meta information to be stored?
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.
Thanks for taking the time to explain, and sorry for all of our questions.
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.
@ianthomas23 Thanks for the explanations. I agree that the tri-mesh plotting utilities that I assume you have developed are very impressive. Having the ability to plot directly from a tessellation is hugely useful, particularly in fields where a finite-volume modelling approach has been used. I certainly never meant that those capabilities should be curtailed or go away. OTOH I agree w/ @ImportanceOfBeingErnest that they should get their own tutorial. Unless you are looking for tri-grid solutions, the examples in the examples directory are clearly labeled, but obscure.
Unfortunately griddata have been around for years and people are familiar with it, so anyone who asks about tri grids receives a well-intended but sub-optimal answer to interpolate to a regular grid. It is simply unnecessary. Everyone should use tricontour for optimal results: the boundaries and masked areas are correct, it has higher resolution where the grid has higher resolution.
gridding data sets is often necessary for reasons other than direct data presentation, usually in the context of comparing to another data set that is supplied on a regular grid.
* Interpolate the data to a regular grid first. This can be done with on-borad | ||
means, e.g. via `~.tri.LinearTriInterpolator` or using external functionality | ||
e.g. via `scipy.interpolate.griddata`. The latter is more versatile | ||
as it allows for different kinds of interpolation (e.g. cubic). Then plot the |
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.
Not true. The file triinterpolator.py
that contains LinearTriInterpolator
also contains CubicInterpolator
, for example, a mere 56 lines later.
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.
Good point. I guess one can just leave the sentence out completely.
@ImportanceOfBeingErnest Thank you for resurrecting this example, and indeed on improving on the original. You have my full support. |
46079a0
to
9dd6ea5
Compare
9dd6ea5
to
2b1afa9
Compare
I'm going to merge this, since it's a doc change, is putting back something that was lost, and hopefully isn't too controversial! |
Can someone check if this should have gotten a milestone? Intuitively I would have said this could go to the 2.2-doc branch because it fixes the missing example. But tbh I am more than confused about backporting or not and milestones by now. |
Woops, sorry, thanks for reminding me. Will try to backport it |
@meeseeksdev backport to v2.2-doc |
Something went wrong ... Please have a look at my logs. |
@meeseeksdev backport to v2.2.2-doc |
Backport PR #11180 on branch v2.2.2-doc
PR Summary
In the course of deprecating many of the
mlab
functions (#9151), the griddata vs tricontour example got lost, due tomatplotlib.mlab.griddata
being deprectated.I would like to revive this example because it is useful to show how to plot irregularly spaced data.
While a direct replacement of the
mlab.griddata
function would bescipy.interpolate.griddata
,scipy
is not a dependency of matplotlib docs. Although @tacaswell said he would be ok to introduce this, it seems a bit overkill for a single example. Instead one can use the functionality ofmatplotlib.tri
for the sake of the example and comment on the option to use scipy's griddata as well.Note that there had been another example which got lost in the deprectation process. Because this is highly redundant with the now proposed example, I would propose to leave it out completely.
PR Checklist