test_interpolate_wrapper.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """ module to test interpolate_wrapper.py
  2. """
  3. from __future__ import division, print_function, absolute_import
  4. from numpy import arange, allclose, ones, isnan
  5. import numpy as np
  6. from numpy.testing import (assert_, assert_allclose)
  7. from scipy._lib._numpy_compat import suppress_warnings
  8. # functionality to be tested
  9. from scipy.interpolate.interpolate_wrapper import (linear, logarithmic,
  10. block_average_above, nearest)
  11. class Test(object):
  12. def assertAllclose(self, x, y, rtol=1.0e-5):
  13. for i, xi in enumerate(x):
  14. assert_(allclose(xi, y[i], rtol) or (isnan(xi) and isnan(y[i])))
  15. def test_nearest(self):
  16. N = 5
  17. x = arange(N)
  18. y = arange(N)
  19. with suppress_warnings() as sup:
  20. sup.filter(DeprecationWarning, "`nearest` is deprecated")
  21. assert_allclose(y, nearest(x, y, x+.1))
  22. assert_allclose(y, nearest(x, y, x-.1))
  23. def test_linear(self):
  24. N = 3000.
  25. x = arange(N)
  26. y = arange(N)
  27. new_x = arange(N)+0.5
  28. with suppress_warnings() as sup:
  29. sup.filter(DeprecationWarning, "`linear` is deprecated")
  30. new_y = linear(x, y, new_x)
  31. assert_allclose(new_y[:5], [0.5, 1.5, 2.5, 3.5, 4.5])
  32. def test_block_average_above(self):
  33. N = 3000
  34. x = arange(N, dtype=float)
  35. y = arange(N, dtype=float)
  36. new_x = arange(N // 2) * 2
  37. with suppress_warnings() as sup:
  38. sup.filter(DeprecationWarning, "`block_average_above` is deprecated")
  39. new_y = block_average_above(x, y, new_x)
  40. assert_allclose(new_y[:5], [0.0, 0.5, 2.5, 4.5, 6.5])
  41. def test_linear2(self):
  42. N = 3000
  43. x = arange(N, dtype=float)
  44. y = ones((100,N)) * arange(N)
  45. new_x = arange(N) + 0.5
  46. with suppress_warnings() as sup:
  47. sup.filter(DeprecationWarning, "`linear` is deprecated")
  48. new_y = linear(x, y, new_x)
  49. assert_allclose(new_y[:5,:5],
  50. [[0.5, 1.5, 2.5, 3.5, 4.5],
  51. [0.5, 1.5, 2.5, 3.5, 4.5],
  52. [0.5, 1.5, 2.5, 3.5, 4.5],
  53. [0.5, 1.5, 2.5, 3.5, 4.5],
  54. [0.5, 1.5, 2.5, 3.5, 4.5]])
  55. def test_logarithmic(self):
  56. N = 4000.
  57. x = arange(N)
  58. y = arange(N)
  59. new_x = arange(N)+0.5
  60. with suppress_warnings() as sup:
  61. sup.filter(DeprecationWarning, "`logarithmic` is deprecated")
  62. new_y = logarithmic(x, y, new_x)
  63. correct_y = [np.NaN, 1.41421356, 2.44948974, 3.46410162, 4.47213595]
  64. assert_allclose(new_y[:5], correct_y)
  65. def runTest(self):
  66. test_list = [name for name in dir(self) if name.find('test_') == 0]
  67. for test_name in test_list:
  68. exec("self.%s()" % test_name)