Add unit test for interpolateArray with order=0

docstring update
This commit is contained in:
Luke Campagnola 2016-10-12 09:58:03 -07:00
parent be07979b39
commit 92fc9dbe2f
2 changed files with 22 additions and 22 deletions

View File

@ -536,12 +536,15 @@ def interpolateArray(data, x, default=0.0, order=1):
N-dimensional interpolation similar to scipy.ndimage.map_coordinates. N-dimensional interpolation similar to scipy.ndimage.map_coordinates.
This function returns linearly-interpolated values sampled from a regular This function returns linearly-interpolated values sampled from a regular
grid of data. grid of data. It differs from `ndimage.map_coordinates` by allowing broadcasting
within the input array.
============== =========================================================================================== ============== ===========================================================================================
**Arguments:** **Arguments:**
*data* Array of any shape containing the values to be interpolated. *data* Array of any shape containing the values to be interpolated.
*x* Array with (shape[-1] <= data.ndim) containing the locations within *data* to interpolate. *x* Array with (shape[-1] <= data.ndim) containing the locations within *data* to interpolate.
(note: the axes for this argument are transposed relative to the same argument for
`ndimage.map_coordinates`).
*default* Value to return for locations in *x* that are outside the bounds of *data*. *default* Value to return for locations in *x* that are outside the bounds of *data*.
*order* Order of interpolation: 0=nearest, 1=linear. *order* Order of interpolation: 0=nearest, 1=linear.
============== =========================================================================================== ============== ===========================================================================================

View File

@ -22,9 +22,17 @@ def testSolve3D():
assert_array_almost_equal(tr[:3], tr2[:3]) assert_array_almost_equal(tr[:3], tr2[:3])
def test_interpolateArray(): def test_interpolateArray_order0():
check_interpolateArray(order=0)
def test_interpolateArray_order1():
check_interpolateArray(order=1)
def check_interpolateArray(order):
def interpolateArray(data, x): def interpolateArray(data, x):
result = pg.interpolateArray(data, x) result = pg.interpolateArray(data, x, order=order)
assert result.shape == x.shape[:-1] + data.shape[x.shape[-1]:] assert result.shape == x.shape[:-1] + data.shape[x.shape[-1]:]
return result return result
@ -48,7 +56,6 @@ def test_interpolateArray():
with pytest.raises(TypeError): with pytest.raises(TypeError):
interpolateArray(data, np.ones((5, 5, 3,))) interpolateArray(data, np.ones((5, 5, 3,)))
x = np.array([[ 0.3, 0.6], x = np.array([[ 0.3, 0.6],
[ 1. , 1. ], [ 1. , 1. ],
[ 0.5, 1. ], [ 0.5, 1. ],
@ -56,9 +63,10 @@ def test_interpolateArray():
[ 10. , 10. ]]) [ 10. , 10. ]])
result = interpolateArray(data, x) result = interpolateArray(data, x)
#import scipy.ndimage # make sure results match ndimage.map_coordinates
#spresult = scipy.ndimage.map_coordinates(data, x.T, order=1) import scipy.ndimage
spresult = np.array([ 5.92, 20. , 11. , 0. , 0. ]) # generated with the above line spresult = scipy.ndimage.map_coordinates(data, x.T, order=order)
#spresult = np.array([ 5.92, 20. , 11. , 0. , 0. ]) # generated with the above line
assert_array_almost_equal(result, spresult) assert_array_almost_equal(result, spresult)
@ -78,24 +86,13 @@ def test_interpolateArray():
[[1.5, 0.5], [1.5, 1.0], [1.5, 1.5]]]) [[1.5, 0.5], [1.5, 1.0], [1.5, 1.5]]])
r1 = interpolateArray(data, x) r1 = interpolateArray(data, x)
#r2 = scipy.ndimage.map_coordinates(data, x.transpose(2,0,1), order=1) r2 = scipy.ndimage.map_coordinates(data, x.transpose(2,0,1), order=order)
r2 = np.array([[ 8.25, 11. , 16.5 ], # generated with the above line #r2 = np.array([[ 8.25, 11. , 16.5 ], # generated with the above line
[ 82.5 , 110. , 165. ]]) #[ 82.5 , 110. , 165. ]])
assert_array_almost_equal(r1, r2) assert_array_almost_equal(r1, r2)
# test interpolate where data.ndim > x.shape[1]
data = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) # 2x2x3
x = np.array([[1, 1], [0, 0.5], [5, 5]])
r1 = interpolateArray(data, x)
assert np.all(r1[0] == data[1, 1])
assert np.all(r1[1] == 0.5 * (data[0, 0] + data[0, 1]))
assert np.all(r1[2] == 0)
def test_subArray(): def test_subArray():
a = np.array([0, 0, 111, 112, 113, 0, 121, 122, 123, 0, 0, 0, 211, 212, 213, 0, 221, 222, 223, 0, 0, 0, 0]) a = np.array([0, 0, 111, 112, 113, 0, 121, 122, 123, 0, 0, 0, 211, 212, 213, 0, 221, 222, 223, 0, 0, 0, 0])
b = pg.subArray(a, offset=2, shape=(2,2,3), stride=(10,4,1)) b = pg.subArray(a, offset=2, shape=(2,2,3), stride=(10,4,1))