Skip to content

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

Conversation

ImportanceOfBeingErnest
Copy link
Member

@ImportanceOfBeingErnest ImportanceOfBeingErnest commented May 6, 2018

PR Summary

In the course of deprecating many of the mlab functions (#9151), the griddata vs tricontour example got lost, due to matplotlib.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 be scipy.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 of matplotlib.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

  • Code is PEP 8 compliant
  • Documentation is sphinx and numpydoc compliant

@ImportanceOfBeingErnest ImportanceOfBeingErnest changed the title Revive Irregularly spaced data contour example DOC: Revive Irregularly spaced data contour example May 6, 2018
# 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)
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member Author

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).

Copy link
Member

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:

  1. User specifies the 2D points and requests a Delaunay triangulation is calculated for them.
  2. The same as 1, and then the user masks out some of the unwanted triangles.
  3. 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.

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

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.

@ianthomas23
Copy link
Member

@ImportanceOfBeingErnest Thank you for resurrecting this example, and indeed on improving on the original. You have my full support.

@ImportanceOfBeingErnest ImportanceOfBeingErnest force-pushed the revive-irregular-data-contour-example branch from 46079a0 to 9dd6ea5 Compare May 6, 2018 19:08
@ImportanceOfBeingErnest ImportanceOfBeingErnest force-pushed the revive-irregular-data-contour-example branch from 9dd6ea5 to 2b1afa9 Compare May 6, 2018 23:24
@dstansby
Copy link
Member

dstansby commented May 8, 2018

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!

@dstansby dstansby merged commit 8a29c40 into matplotlib:master May 8, 2018
@ImportanceOfBeingErnest
Copy link
Member Author

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.

@dstansby dstansby added this to the v2.2-doc milestone May 8, 2018
@dstansby
Copy link
Member

dstansby commented May 8, 2018

Woops, sorry, thanks for reminding me. Will try to backport it

@dstansby
Copy link
Member

dstansby commented May 8, 2018

@meeseeksdev backport to v2.2-doc

@lumberbot-app
Copy link

lumberbot-app bot commented May 8, 2018

Something went wrong ... Please have a look at my logs.

@dstansby
Copy link
Member

dstansby commented May 8, 2018

@meeseeksdev backport to v2.2.2-doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants