alternate fix for issue #997 #1477

Merged
merged 6 commits into from Dec 4, 2012
@@ -289,8 +289,8 @@ static PyObject *linear_interpolate_grid(double x0, double x1, int xsteps,
if (!z) return NULL;
z_ptr = (double*)PyArray_DATA(z);
- dx = (x1 - x0) / (xsteps-1);
- dy = (y1 - y0) / (ysteps-1);
+ dx = ( xsteps==1 ? 0 : (x1 - x0) / (xsteps-1) );
+ dy = ( ysteps==1 ? 0 : (y1 - y0) / (ysteps-1) );
rowtri = 0;
for (iy=0; iy<ysteps; iy++) {
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -159,7 +159,7 @@ def interpolator(self, func):
z = func(self.x, self.y)
return self.tri.nn_extrapolator(z, bbox=self.xrange+self.yrange)
-def make_all_testfuncs(allfuncs=allfuncs):
+def make_all_2d_testfuncs(allfuncs=allfuncs):
def make_test(func):
filenames = [
'%s-%s' % (func.func_name, x) for x in
@@ -186,4 +186,27 @@ def reference_test():
for func in allfuncs:
globals()['test_%s' % func.func_name] = make_test(func)
-make_all_testfuncs()
+make_all_2d_testfuncs()
+
+# 1d and 0d grid tests
+
+ref_interpolator = Triangulation([0,10,10,0],
@pelson

pelson Nov 13, 2012

Member

Rather than using the module level namespace, it might be worth considering constructing a class which has "test_" methods.

@AmitAronovitch

AmitAronovitch Nov 13, 2012

Contributor

When adding tests to an existing module, I should try staying close to existing style.
make_all_testfuncs() was there (I just renamed it), and since it used module functions rather than class methods, so did I
( and that would have been my default choice anyways in this case).

+ [0,0,10,10]).linear_interpolator([1,10,5,2.0])
+
+def test_1d_grid():
+ res = ref_interpolator[3:6:2j,1:1:1j]
+ assert np.allclose(res, [[1.6],[1.9]], rtol=0)
+
+def test_0d_grid():
+ res = ref_interpolator[3:3:1j,1:1:1j]
+ assert np.allclose(res, [[1.6]], rtol=0)
+
+@image_comparison(baseline_images=['delaunay-1d-interp'], extensions=['png'])
+def test_1d_plots():
+ x_range = slice(0.25,9.75,20j)
+ x = np.mgrid[x_range]
+ ax = plt.gca()
+ for y in xrange(2,10,2):
+ plt.plot(x, ref_interpolator[x_range,y:y:1j])
+ ax.set_xticks([])
+ ax.set_yticks([])