test_ndgriddata.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import assert_equal, assert_array_equal, assert_allclose
  4. from pytest import raises as assert_raises
  5. from scipy.interpolate import griddata, NearestNDInterpolator
  6. class TestGriddata(object):
  7. def test_fill_value(self):
  8. x = [(0,0), (0,1), (1,0)]
  9. y = [1, 2, 3]
  10. yi = griddata(x, y, [(1,1), (1,2), (0,0)], fill_value=-1)
  11. assert_array_equal(yi, [-1., -1, 1])
  12. yi = griddata(x, y, [(1,1), (1,2), (0,0)])
  13. assert_array_equal(yi, [np.nan, np.nan, 1])
  14. def test_alternative_call(self):
  15. x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)],
  16. dtype=np.double)
  17. y = (np.arange(x.shape[0], dtype=np.double)[:,None]
  18. + np.array([0,1])[None,:])
  19. for method in ('nearest', 'linear', 'cubic'):
  20. for rescale in (True, False):
  21. msg = repr((method, rescale))
  22. yi = griddata((x[:,0], x[:,1]), y, (x[:,0], x[:,1]), method=method,
  23. rescale=rescale)
  24. assert_allclose(y, yi, atol=1e-14, err_msg=msg)
  25. def test_multivalue_2d(self):
  26. x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)],
  27. dtype=np.double)
  28. y = (np.arange(x.shape[0], dtype=np.double)[:,None]
  29. + np.array([0,1])[None,:])
  30. for method in ('nearest', 'linear', 'cubic'):
  31. for rescale in (True, False):
  32. msg = repr((method, rescale))
  33. yi = griddata(x, y, x, method=method, rescale=rescale)
  34. assert_allclose(y, yi, atol=1e-14, err_msg=msg)
  35. def test_multipoint_2d(self):
  36. x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)],
  37. dtype=np.double)
  38. y = np.arange(x.shape[0], dtype=np.double)
  39. xi = x[:,None,:] + np.array([0,0,0])[None,:,None]
  40. for method in ('nearest', 'linear', 'cubic'):
  41. for rescale in (True, False):
  42. msg = repr((method, rescale))
  43. yi = griddata(x, y, xi, method=method, rescale=rescale)
  44. assert_equal(yi.shape, (5, 3), err_msg=msg)
  45. assert_allclose(yi, np.tile(y[:,None], (1, 3)),
  46. atol=1e-14, err_msg=msg)
  47. def test_complex_2d(self):
  48. x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)],
  49. dtype=np.double)
  50. y = np.arange(x.shape[0], dtype=np.double)
  51. y = y - 2j*y[::-1]
  52. xi = x[:,None,:] + np.array([0,0,0])[None,:,None]
  53. for method in ('nearest', 'linear', 'cubic'):
  54. for rescale in (True, False):
  55. msg = repr((method, rescale))
  56. yi = griddata(x, y, xi, method=method, rescale=rescale)
  57. assert_equal(yi.shape, (5, 3), err_msg=msg)
  58. assert_allclose(yi, np.tile(y[:,None], (1, 3)),
  59. atol=1e-14, err_msg=msg)
  60. def test_1d(self):
  61. x = np.array([1, 2.5, 3, 4.5, 5, 6])
  62. y = np.array([1, 2, 0, 3.9, 2, 1])
  63. for method in ('nearest', 'linear', 'cubic'):
  64. assert_allclose(griddata(x, y, x, method=method), y,
  65. err_msg=method, atol=1e-14)
  66. assert_allclose(griddata(x.reshape(6, 1), y, x, method=method), y,
  67. err_msg=method, atol=1e-14)
  68. assert_allclose(griddata((x,), y, (x,), method=method), y,
  69. err_msg=method, atol=1e-14)
  70. def test_1d_borders(self):
  71. # Test for nearest neighbor case with xi outside
  72. # the range of the values.
  73. x = np.array([1, 2.5, 3, 4.5, 5, 6])
  74. y = np.array([1, 2, 0, 3.9, 2, 1])
  75. xi = np.array([0.9, 6.5])
  76. yi_should = np.array([1.0, 1.0])
  77. method = 'nearest'
  78. assert_allclose(griddata(x, y, xi,
  79. method=method), yi_should,
  80. err_msg=method,
  81. atol=1e-14)
  82. assert_allclose(griddata(x.reshape(6, 1), y, xi,
  83. method=method), yi_should,
  84. err_msg=method,
  85. atol=1e-14)
  86. assert_allclose(griddata((x, ), y, (xi, ),
  87. method=method), yi_should,
  88. err_msg=method,
  89. atol=1e-14)
  90. def test_1d_unsorted(self):
  91. x = np.array([2.5, 1, 4.5, 5, 6, 3])
  92. y = np.array([1, 2, 0, 3.9, 2, 1])
  93. for method in ('nearest', 'linear', 'cubic'):
  94. assert_allclose(griddata(x, y, x, method=method), y,
  95. err_msg=method, atol=1e-10)
  96. assert_allclose(griddata(x.reshape(6, 1), y, x, method=method), y,
  97. err_msg=method, atol=1e-10)
  98. assert_allclose(griddata((x,), y, (x,), method=method), y,
  99. err_msg=method, atol=1e-10)
  100. def test_square_rescale_manual(self):
  101. points = np.array([(0,0), (0,100), (10,100), (10,0), (1, 5)], dtype=np.double)
  102. points_rescaled = np.array([(0,0), (0,1), (1,1), (1,0), (0.1, 0.05)], dtype=np.double)
  103. values = np.array([1., 2., -3., 5., 9.], dtype=np.double)
  104. xx, yy = np.broadcast_arrays(np.linspace(0, 10, 14)[:,None],
  105. np.linspace(0, 100, 14)[None,:])
  106. xx = xx.ravel()
  107. yy = yy.ravel()
  108. xi = np.array([xx, yy]).T.copy()
  109. for method in ('nearest', 'linear', 'cubic'):
  110. msg = method
  111. zi = griddata(points_rescaled, values, xi/np.array([10, 100.]),
  112. method=method)
  113. zi_rescaled = griddata(points, values, xi, method=method,
  114. rescale=True)
  115. assert_allclose(zi, zi_rescaled, err_msg=msg,
  116. atol=1e-12)
  117. def test_xi_1d(self):
  118. # Check that 1-D xi is interpreted as a coordinate
  119. x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)],
  120. dtype=np.double)
  121. y = np.arange(x.shape[0], dtype=np.double)
  122. y = y - 2j*y[::-1]
  123. xi = np.array([0.5, 0.5])
  124. for method in ('nearest', 'linear', 'cubic'):
  125. p1 = griddata(x, y, xi, method=method)
  126. p2 = griddata(x, y, xi[None,:], method=method)
  127. assert_allclose(p1, p2, err_msg=method)
  128. xi1 = np.array([0.5])
  129. xi3 = np.array([0.5, 0.5, 0.5])
  130. assert_raises(ValueError, griddata, x, y, xi1,
  131. method=method)
  132. assert_raises(ValueError, griddata, x, y, xi3,
  133. method=method)
  134. def test_nearest_options():
  135. # smoke test that NearestNDInterpolator accept cKDTree options
  136. npts, nd = 4, 3
  137. x = np.arange(npts*nd).reshape((npts, nd))
  138. y = np.arange(npts)
  139. nndi = NearestNDInterpolator(x, y)
  140. opts = {'balanced_tree': False, 'compact_nodes': False}
  141. nndi_o = NearestNDInterpolator(x, y, tree_options=opts)
  142. assert_allclose(nndi(x), nndi_o(x), atol=1e-14)