def test_simple_axis1(self): a = np.arange(12).reshape(3, 4) r = iintrp.Linear1dExtrapolator(interp1d(np.arange(4), a, axis=1)) # check non-extrapolation given the Extrapolator object np.testing.assert_array_equal(r(0), np.array([0., 4., 8.])) # check the result's shape in a 1d array (of len 0 & 1) np.testing.assert_array_equal(r(np.array(0)), np.array([0., 4., 8.])) np.testing.assert_array_equal(r(np.array([0])), np.array([[0.], [4.], [8.]])) # check extrapolation below the minimum value (and check the equivalent 0d & 1d arrays) np.testing.assert_array_equal(r(-1), np.array([-1., 3., 7.])) np.testing.assert_array_equal(r(np.array(-1)), np.array([-1., 3., 7.])) np.testing.assert_array_equal(r(np.array([-1])), np.array([[-1.], [3.], [7.]])) # check extrapolation above the maximum value np.testing.assert_array_equal(r(3), np.array([3., 7., 11.])) np.testing.assert_array_equal(r(2.5), np.array([2.5, 6.5, 10.5])) # 2 Non-extrapolation point np.testing.assert_array_equal( r(np.array([1.5, 2])), np.array([[1.5, 2.], [5.5, 6.], [9.5, 10.]])) # 1 Non-extrapolation point & 1 upper value extrapolation np.testing.assert_array_equal( r(np.array([1.5, 5])), np.array([[1.5, 5.], [5.5, 9.], [9.5, 13.]])) # 2 upper value extrapolation np.testing.assert_array_equal( r(np.array([4.5, 5])), np.array([[4.5, 5.], [8.5, 9.], [12.5, 13.]])) # 1 lower value extrapolation & 1 Non-extrapolation point np.testing.assert_array_equal( r(np.array([-0.5, 1.5])), np.array([[-0.5, 1.5], [3.5, 5.5], [7.5, 9.5]])) # 2 lower value extrapolation np.testing.assert_array_equal( r(np.array([-1.5, -1])), np.array([[-1.5, -1.], [2.5, 3.], [6.5, 7.]])) # 2 lower value extrapolation, 2 Non-extrapolation point & 2 upper value extrapolation np.testing.assert_array_equal( r(np.array([-1.5, -1, 1.5, 2, 4.5, 5])), np.array([[-1.5, -1., 1.5, 2., 4.5, 5.], [2.5, 3., 5.5, 6., 8.5, 9.], [6.5, 7., 9.5, 10., 12.5, 13.]]))
def test_variable_gradient(self): a = np.array([[2, 4, 8], [0, 5, 11]]) r = iintrp.Linear1dExtrapolator(interp1d(np.arange(2), a, axis=0)) np.testing.assert_array_equal(r(0), np.array([ 2., 4., 8.])) np.testing.assert_array_equal(r(-1), np.array([ 4., 3., 5.])) np.testing.assert_array_equal(r(3), np.array([ -4., 7., 17.])) np.testing.assert_array_equal(r(2.5), np.array([ -3., 6.5, 15.5])) np.testing.assert_array_equal(r(np.array([1.5, 2])), np.array([[ -1., 5.5, 12.5], [ -2., 6., 14. ]])) np.testing.assert_array_equal(r(np.array([-1.5, 3.5])), np.array([[ 5., 2.5, 3.5], [ -5., 7.5, 18.5]]))
def test_simple_axis0(self): a = np.arange(12.).reshape(3, 4) r = iintrp.Linear1dExtrapolator(interp1d(np.arange(3), a, axis=0)) np.testing.assert_array_equal(r(0), np.array([0., 1., 2., 3.])) np.testing.assert_array_equal(r(-1), np.array([-4., -3., -2., -1.])) np.testing.assert_array_equal(r(3), np.array([12., 13., 14., 15.])) np.testing.assert_array_equal(r(2.5), np.array([10., 11., 12., 13.])) # 2 Non-extrapolation point np.testing.assert_array_equal( r(np.array([1.5, 2])), np.array([[6., 7., 8., 9.], [8., 9., 10., 11.]])) # 1 Non-extrapolation point & 1 upper value extrapolation np.testing.assert_array_equal( r(np.array([1.5, 3])), np.array([[6., 7., 8., 9.], [12., 13., 14., 15.]])) # 2 upper value extrapolation np.testing.assert_array_equal( r(np.array([2.5, 3])), np.array([[10., 11., 12., 13.], [12., 13., 14., 15.]])) # 1 lower value extrapolation & 1 Non-extrapolation point np.testing.assert_array_equal( r(np.array([-1, 1.5])), np.array([[-4., -3., -2., -1.], [6., 7., 8., 9.]])) # 2 lower value extrapolation np.testing.assert_array_equal( r(np.array([-1.5, -1])), np.array([[-6., -5., -4., -3.], [-4., -3., -2., -1.]])) # 2 lower value extrapolation, 2 Non-extrapolation point & 2 upper value extrapolation np.testing.assert_array_equal( r(np.array([-1.5, -1, 1, 1.5, 2.5, 3])), np.array([[-6., -5., -4., -3.], [-4., -3., -2., -1.], [4., 5., 6., 7.], [6., 7., 8., 9.], [10., 11., 12., 13.], [12., 13., 14., 15.]]))
def test_simple_3d_axis1(self): a = np.arange(24.).reshape(3, 4, 2) r = iintrp.Linear1dExtrapolator(interp1d(np.arange(4.), a, axis=1)) # a: # [[[ 0 1] # [ 2 3] # [ 4 5] # [ 6 7]] # # [[ 8 9] # [10 11] # [12 13] # [14 15]] # # [[16 17] # [18 19] # [20 21] # [22 23]] # ] np.testing.assert_array_equal(r(0), np.array([[ 0., 1.], [ 8., 9.], [ 16., 17.]])) np.testing.assert_array_equal(r(1), np.array([[ 2., 3.], [ 10., 11.], [ 18., 19.]])) np.testing.assert_array_equal(r(-1), np.array([[ -2., -1.], [ 6., 7.], [ 14., 15.]])) np.testing.assert_array_equal(r(4), np.array([[ 8., 9.], [ 16., 17.], [ 24., 25.]])) np.testing.assert_array_equal(r(0.25), np.array([[ 0.5, 1.5], [ 8.5, 9.5], [ 16.5, 17.5]])) np.testing.assert_array_equal(r(-0.25), np.array([[ -0.5, 0.5], [ 7.5, 8.5], [ 15.5, 16.5]])) np.testing.assert_array_equal(r(4.25), np.array([[ 8.5, 9.5], [ 16.5, 17.5], [ 24.5, 25.5]])) np.testing.assert_array_equal(r(np.array([0.5, 1])), np.array([[[ 1., 2.], [ 2., 3.]], [[ 9., 10.], [ 10., 11.]], [[ 17., 18.], [ 18., 19.]]])) np.testing.assert_array_equal(r(np.array([0.5, 4])), np.array([[[ 1., 2.], [ 8., 9.]], [[ 9., 10.], [ 16., 17.]], [[ 17., 18.], [ 24., 25.]]])) np.testing.assert_array_equal(r(np.array([-0.5, 0.5])), np.array([[[ -1., 0.], [ 1., 2.]], [[ 7., 8.], [ 9., 10.]], [[ 15., 16.], [ 17., 18.]]])) np.testing.assert_array_equal(r(np.array([-1.5, -1, 0.5, 1, 4.5, 5])), np.array([[[ -3., -2.], [ -2., -1.], [ 1., 2.], [ 2., 3.], [ 9., 10.], [ 10., 11.]], [[ 5., 6.], [ 6., 7.], [ 9., 10.], [ 10., 11.], [ 17., 18.], [ 18., 19.]], [[ 13., 14.], [ 14., 15.], [ 17., 18.], [ 18., 19.], [ 25., 26.], [ 26., 27.]]]))