Skip to content
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

griddata constant spacing check needs tweaking #666

Closed
ivanov opened this issue Jan 10, 2012 · 4 comments
Closed

griddata constant spacing check needs tweaking #666

ivanov opened this issue Jan 10, 2012 · 4 comments

Comments

@ivanov
Copy link
Member

ivanov commented Jan 10, 2012

as reported by Ethan Swint to the mpl-users mailing list:

I was working off of the example listed at
http://matplotlib.sourceforge.net/examples/pylab_examples/griddata_demo.html,
adapting it to my own data, and encountered the following error on MLAB
1.1.0, Python 2.7.2:

from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy as np
# make up data.
#npts = int(raw_input('enter # of random points to plot:'))
seed(0)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)
xi = np.linspace(0,60,100)  # <= my xdata range
yi = np.linspace(0,0.9,200) # <= my ydata range
zi = griddata(x,y,z,xi,yi,interp='linear')

which produces

------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\Ethan\code\PythonScripts\<ipython-input-7-51e10e494403> in
<module>()
----> 1 zi = griddata(x,y,z,xi,yi,interp='linear')

   matplotlib/mlab.pyc in griddata(x, y, z, xi, yi, interp)
   2778             epsy = np.finfo(yi.dtype).resolution
   2779             if dx.max()-dx.min() > epsx or dy.max()-dy.min() > epsy:
-> 2780                 raise ValueError("output grid must have constant spacing"
   2781                                  " when using interp='linear'")
   2782             interp = tri.linear_interpolator(z)

ValueError: output grid must have constant spacing when using interp='linear'
@ivanov
Copy link
Member Author

ivanov commented Jan 11, 2012

In general, such an error can arise due to the use of linspace, instead of arange, as described here. However, in this particular case, the two methods produce identical results.

The reason I'm not sure how to proceed is that the difference between dx.max() and dx.min() can be an order of magnitude greater than the epsx value it is compared against. Here's a modification to the original example that does this:

In [155]: xi = np.linspace(0,603,100);dx =np.diff(xi); dx.ptp()
Out[155]: 0.00000000000011368684

In [157]: zi = griddata(x,y,z,xi,yi,interp='linear')
--error--
In [158]: debug
> /home/pi/usr/local/lib/python2.6/site-packages/matplotlib/mlab.py(2780)griddata()
   2779             if dx.max()-dx.min() > epsx or dy.max()-dy.min() > epsy:
-> 2780                 raise ValueError("output grid must have constant spacing"
   2781                                  " when using interp='linear'")

ipdb> dx.ptp()
1.1368683772161603e-13
ipdb> epsx
1.0000000000000001e-15
ipdb>

I believe that this has to do with the limited precision and non-uniformity of the possible numbers represented by the floating point standard. The check for constant spacing likely should take into account and compare dx.ptp() to the average dx itself, or something like that.

@efiring
Copy link
Member

efiring commented Jul 21, 2013

@ivanov, I think you are exactly right. Perhaps you would generate a small PR to take care of this?

@maxhgerlach
Copy link

I ran into the same problem today. I think the fix in my pull request should implement ivanov's proposed solution.

@maxhgerlach
Copy link

With the current 1.4.x code based on matplotlib.tri.LinearTriInterpolator the original issue no longer occurs for me and the pull-request is no longer necessary.

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

No branches or pull requests

4 participants