test_savitzky_golay.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import pytest
  2. import numpy as np
  3. from numpy.testing import (assert_allclose, assert_equal,
  4. assert_almost_equal, assert_array_equal,
  5. assert_array_almost_equal)
  6. from scipy.ndimage import convolve1d
  7. from scipy.signal import savgol_coeffs, savgol_filter
  8. from scipy.signal._savitzky_golay import _polyder
  9. def check_polyder(p, m, expected):
  10. dp = _polyder(p, m)
  11. assert_array_equal(dp, expected)
  12. def test_polyder():
  13. cases = [
  14. ([5], 0, [5]),
  15. ([5], 1, [0]),
  16. ([3, 2, 1], 0, [3, 2, 1]),
  17. ([3, 2, 1], 1, [6, 2]),
  18. ([3, 2, 1], 2, [6]),
  19. ([3, 2, 1], 3, [0]),
  20. ([[3, 2, 1], [5, 6, 7]], 0, [[3, 2, 1], [5, 6, 7]]),
  21. ([[3, 2, 1], [5, 6, 7]], 1, [[6, 2], [10, 6]]),
  22. ([[3, 2, 1], [5, 6, 7]], 2, [[6], [10]]),
  23. ([[3, 2, 1], [5, 6, 7]], 3, [[0], [0]]),
  24. ]
  25. for p, m, expected in cases:
  26. check_polyder(np.array(p).T, m, np.array(expected).T)
  27. #--------------------------------------------------------------------
  28. # savgol_coeffs tests
  29. #--------------------------------------------------------------------
  30. def alt_sg_coeffs(window_length, polyorder, pos):
  31. """This is an alternative implementation of the SG coefficients.
  32. It uses numpy.polyfit and numpy.polyval. The results should be
  33. equivalent to those of savgol_coeffs(), but this implementation
  34. is slower.
  35. window_length should be odd.
  36. """
  37. if pos is None:
  38. pos = window_length // 2
  39. t = np.arange(window_length)
  40. unit = (t == pos).astype(int)
  41. h = np.polyval(np.polyfit(t, unit, polyorder), t)
  42. return h
  43. def test_sg_coeffs_trivial():
  44. # Test a trivial case of savgol_coeffs: polyorder = window_length - 1
  45. h = savgol_coeffs(1, 0)
  46. assert_allclose(h, [1])
  47. h = savgol_coeffs(3, 2)
  48. assert_allclose(h, [0, 1, 0], atol=1e-10)
  49. h = savgol_coeffs(5, 4)
  50. assert_allclose(h, [0, 0, 1, 0, 0], atol=1e-10)
  51. h = savgol_coeffs(5, 4, pos=1)
  52. assert_allclose(h, [0, 0, 0, 1, 0], atol=1e-10)
  53. h = savgol_coeffs(5, 4, pos=1, use='dot')
  54. assert_allclose(h, [0, 1, 0, 0, 0], atol=1e-10)
  55. def compare_coeffs_to_alt(window_length, order):
  56. # For the given window_length and order, compare the results
  57. # of savgol_coeffs and alt_sg_coeffs for pos from 0 to window_length - 1.
  58. # Also include pos=None.
  59. for pos in [None] + list(range(window_length)):
  60. h1 = savgol_coeffs(window_length, order, pos=pos, use='dot')
  61. h2 = alt_sg_coeffs(window_length, order, pos=pos)
  62. assert_allclose(h1, h2, atol=1e-10,
  63. err_msg=("window_length = %d, order = %d, pos = %s" %
  64. (window_length, order, pos)))
  65. def test_sg_coeffs_compare():
  66. # Compare savgol_coeffs() to alt_sg_coeffs().
  67. for window_length in range(1, 8, 2):
  68. for order in range(window_length):
  69. compare_coeffs_to_alt(window_length, order)
  70. def test_sg_coeffs_exact():
  71. polyorder = 4
  72. window_length = 9
  73. halflen = window_length // 2
  74. x = np.linspace(0, 21, 43)
  75. delta = x[1] - x[0]
  76. # The data is a cubic polynomial. We'll use an order 4
  77. # SG filter, so the filtered values should equal the input data
  78. # (except within half window_length of the edges).
  79. y = 0.5 * x ** 3 - x
  80. h = savgol_coeffs(window_length, polyorder)
  81. y0 = convolve1d(y, h)
  82. assert_allclose(y0[halflen:-halflen], y[halflen:-halflen])
  83. # Check the same input, but use deriv=1. dy is the exact result.
  84. dy = 1.5 * x ** 2 - 1
  85. h = savgol_coeffs(window_length, polyorder, deriv=1, delta=delta)
  86. y1 = convolve1d(y, h)
  87. assert_allclose(y1[halflen:-halflen], dy[halflen:-halflen])
  88. # Check the same input, but use deriv=2. d2y is the exact result.
  89. d2y = 3.0 * x
  90. h = savgol_coeffs(window_length, polyorder, deriv=2, delta=delta)
  91. y2 = convolve1d(y, h)
  92. assert_allclose(y2[halflen:-halflen], d2y[halflen:-halflen])
  93. def test_sg_coeffs_deriv():
  94. # The data in `x` is a sampled parabola, so using savgol_coeffs with an
  95. # order 2 or higher polynomial should give exact results.
  96. i = np.array([-2.0, 0.0, 2.0, 4.0, 6.0])
  97. x = i ** 2 / 4
  98. dx = i / 2
  99. d2x = np.full_like(i, 0.5)
  100. for pos in range(x.size):
  101. coeffs0 = savgol_coeffs(5, 3, pos=pos, delta=2.0, use='dot')
  102. assert_allclose(coeffs0.dot(x), x[pos], atol=1e-10)
  103. coeffs1 = savgol_coeffs(5, 3, pos=pos, delta=2.0, use='dot', deriv=1)
  104. assert_allclose(coeffs1.dot(x), dx[pos], atol=1e-10)
  105. coeffs2 = savgol_coeffs(5, 3, pos=pos, delta=2.0, use='dot', deriv=2)
  106. assert_allclose(coeffs2.dot(x), d2x[pos], atol=1e-10)
  107. def test_sg_coeffs_deriv_gt_polyorder():
  108. """
  109. If deriv > polyorder, the coefficients should be all 0.
  110. This is a regression test for a bug where, e.g.,
  111. savgol_coeffs(5, polyorder=1, deriv=2)
  112. raised an error.
  113. """
  114. coeffs = savgol_coeffs(5, polyorder=1, deriv=2)
  115. assert_array_equal(coeffs, np.zeros(5))
  116. coeffs = savgol_coeffs(7, polyorder=4, deriv=6)
  117. assert_array_equal(coeffs, np.zeros(7))
  118. def test_sg_coeffs_large():
  119. # Test that for large values of window_length and polyorder the array of
  120. # coefficients returned is symmetric. The aim is to ensure that
  121. # no potential numeric overflow occurs.
  122. coeffs0 = savgol_coeffs(31, 9)
  123. assert_array_almost_equal(coeffs0, coeffs0[::-1])
  124. coeffs1 = savgol_coeffs(31, 9, deriv=1)
  125. assert_array_almost_equal(coeffs1, -coeffs1[::-1])
  126. # --------------------------------------------------------------------
  127. # savgol_coeffs tests for even window length
  128. # --------------------------------------------------------------------
  129. def test_sg_coeffs_even_window_length():
  130. # Simple case - deriv=0, polyorder=0, 1
  131. window_lengths = [4, 6, 8, 10, 12, 14, 16]
  132. for length in window_lengths:
  133. h_p_d = savgol_coeffs(length, 0, 0)
  134. assert_allclose(h_p_d, 1/length)
  135. # Verify with closed forms
  136. # deriv=1, polyorder=1, 2
  137. def h_p_d_closed_form_1(k, m):
  138. return 6*(k - 0.5)/((2*m + 1)*m*(2*m - 1))
  139. # deriv=2, polyorder=2
  140. def h_p_d_closed_form_2(k, m):
  141. numer = 15*(-4*m**2 + 1 + 12*(k - 0.5)**2)
  142. denom = 4*(2*m + 1)*(m + 1)*m*(m - 1)*(2*m - 1)
  143. return numer/denom
  144. for length in window_lengths:
  145. m = length//2
  146. expected_output = [h_p_d_closed_form_1(k, m)
  147. for k in range(-m + 1, m + 1)][::-1]
  148. actual_output = savgol_coeffs(length, 1, 1)
  149. assert_allclose(expected_output, actual_output)
  150. actual_output = savgol_coeffs(length, 2, 1)
  151. assert_allclose(expected_output, actual_output)
  152. expected_output = [h_p_d_closed_form_2(k, m)
  153. for k in range(-m + 1, m + 1)][::-1]
  154. actual_output = savgol_coeffs(length, 2, 2)
  155. assert_allclose(expected_output, actual_output)
  156. actual_output = savgol_coeffs(length, 3, 2)
  157. assert_allclose(expected_output, actual_output)
  158. #--------------------------------------------------------------------
  159. # savgol_filter tests
  160. #--------------------------------------------------------------------
  161. def test_sg_filter_trivial():
  162. """ Test some trivial edge cases for savgol_filter()."""
  163. x = np.array([1.0])
  164. y = savgol_filter(x, 1, 0)
  165. assert_equal(y, [1.0])
  166. # Input is a single value. With a window length of 3 and polyorder 1,
  167. # the value in y is from the straight-line fit of (-1,0), (0,3) and
  168. # (1, 0) at 0. This is just the average of the three values, hence 1.0.
  169. x = np.array([3.0])
  170. y = savgol_filter(x, 3, 1, mode='constant')
  171. assert_almost_equal(y, [1.0], decimal=15)
  172. x = np.array([3.0])
  173. y = savgol_filter(x, 3, 1, mode='nearest')
  174. assert_almost_equal(y, [3.0], decimal=15)
  175. x = np.array([1.0] * 3)
  176. y = savgol_filter(x, 3, 1, mode='wrap')
  177. assert_almost_equal(y, [1.0, 1.0, 1.0], decimal=15)
  178. def test_sg_filter_basic():
  179. # Some basic test cases for savgol_filter().
  180. x = np.array([1.0, 2.0, 1.0])
  181. y = savgol_filter(x, 3, 1, mode='constant')
  182. assert_allclose(y, [1.0, 4.0 / 3, 1.0])
  183. y = savgol_filter(x, 3, 1, mode='mirror')
  184. assert_allclose(y, [5.0 / 3, 4.0 / 3, 5.0 / 3])
  185. y = savgol_filter(x, 3, 1, mode='wrap')
  186. assert_allclose(y, [4.0 / 3, 4.0 / 3, 4.0 / 3])
  187. def test_sg_filter_2d():
  188. x = np.array([[1.0, 2.0, 1.0],
  189. [2.0, 4.0, 2.0]])
  190. expected = np.array([[1.0, 4.0 / 3, 1.0],
  191. [2.0, 8.0 / 3, 2.0]])
  192. y = savgol_filter(x, 3, 1, mode='constant')
  193. assert_allclose(y, expected)
  194. y = savgol_filter(x.T, 3, 1, mode='constant', axis=0)
  195. assert_allclose(y, expected.T)
  196. def test_sg_filter_interp_edges():
  197. # Another test with low degree polynomial data, for which we can easily
  198. # give the exact results. In this test, we use mode='interp', so
  199. # savgol_filter should match the exact solution for the entire data set,
  200. # including the edges.
  201. t = np.linspace(-5, 5, 21)
  202. delta = t[1] - t[0]
  203. # Polynomial test data.
  204. x = np.array([t,
  205. 3 * t ** 2,
  206. t ** 3 - t])
  207. dx = np.array([np.ones_like(t),
  208. 6 * t,
  209. 3 * t ** 2 - 1.0])
  210. d2x = np.array([np.zeros_like(t),
  211. np.full_like(t, 6),
  212. 6 * t])
  213. window_length = 7
  214. y = savgol_filter(x, window_length, 3, axis=-1, mode='interp')
  215. assert_allclose(y, x, atol=1e-12)
  216. y1 = savgol_filter(x, window_length, 3, axis=-1, mode='interp',
  217. deriv=1, delta=delta)
  218. assert_allclose(y1, dx, atol=1e-12)
  219. y2 = savgol_filter(x, window_length, 3, axis=-1, mode='interp',
  220. deriv=2, delta=delta)
  221. assert_allclose(y2, d2x, atol=1e-12)
  222. # Transpose everything, and test again with axis=0.
  223. x = x.T
  224. dx = dx.T
  225. d2x = d2x.T
  226. y = savgol_filter(x, window_length, 3, axis=0, mode='interp')
  227. assert_allclose(y, x, atol=1e-12)
  228. y1 = savgol_filter(x, window_length, 3, axis=0, mode='interp',
  229. deriv=1, delta=delta)
  230. assert_allclose(y1, dx, atol=1e-12)
  231. y2 = savgol_filter(x, window_length, 3, axis=0, mode='interp',
  232. deriv=2, delta=delta)
  233. assert_allclose(y2, d2x, atol=1e-12)
  234. def test_sg_filter_interp_edges_3d():
  235. # Test mode='interp' with a 3-D array.
  236. t = np.linspace(-5, 5, 21)
  237. delta = t[1] - t[0]
  238. x1 = np.array([t, -t])
  239. x2 = np.array([t ** 2, 3 * t ** 2 + 5])
  240. x3 = np.array([t ** 3, 2 * t ** 3 + t ** 2 - 0.5 * t])
  241. dx1 = np.array([np.ones_like(t), -np.ones_like(t)])
  242. dx2 = np.array([2 * t, 6 * t])
  243. dx3 = np.array([3 * t ** 2, 6 * t ** 2 + 2 * t - 0.5])
  244. # z has shape (3, 2, 21)
  245. z = np.array([x1, x2, x3])
  246. dz = np.array([dx1, dx2, dx3])
  247. y = savgol_filter(z, 7, 3, axis=-1, mode='interp', delta=delta)
  248. assert_allclose(y, z, atol=1e-10)
  249. dy = savgol_filter(z, 7, 3, axis=-1, mode='interp', deriv=1, delta=delta)
  250. assert_allclose(dy, dz, atol=1e-10)
  251. # z has shape (3, 21, 2)
  252. z = np.array([x1.T, x2.T, x3.T])
  253. dz = np.array([dx1.T, dx2.T, dx3.T])
  254. y = savgol_filter(z, 7, 3, axis=1, mode='interp', delta=delta)
  255. assert_allclose(y, z, atol=1e-10)
  256. dy = savgol_filter(z, 7, 3, axis=1, mode='interp', deriv=1, delta=delta)
  257. assert_allclose(dy, dz, atol=1e-10)
  258. # z has shape (21, 3, 2)
  259. z = z.swapaxes(0, 1).copy()
  260. dz = dz.swapaxes(0, 1).copy()
  261. y = savgol_filter(z, 7, 3, axis=0, mode='interp', delta=delta)
  262. assert_allclose(y, z, atol=1e-10)
  263. dy = savgol_filter(z, 7, 3, axis=0, mode='interp', deriv=1, delta=delta)
  264. assert_allclose(dy, dz, atol=1e-10)
  265. def test_sg_filter_valid_window_length_3d():
  266. """Tests that the window_length check is using the correct axis."""
  267. x = np.ones((10, 20, 30))
  268. savgol_filter(x, window_length=29, polyorder=3, mode='interp')
  269. with pytest.raises(ValueError, match='window_length must be less than'):
  270. # window_length is more than x.shape[-1].
  271. savgol_filter(x, window_length=31, polyorder=3, mode='interp')
  272. savgol_filter(x, window_length=9, polyorder=3, axis=0, mode='interp')
  273. with pytest.raises(ValueError, match='window_length must be less than'):
  274. # window_length is more than x.shape[0].
  275. savgol_filter(x, window_length=11, polyorder=3, axis=0, mode='interp')