test_helper.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # Created by Pearu Peterson, September 2002
  2. from __future__ import division, print_function, absolute_import
  3. __usage__ = """
  4. Build fftpack:
  5. python setup_fftpack.py build
  6. Run tests if scipy is installed:
  7. python -c 'import scipy;scipy.fftpack.test(<level>)'
  8. Run tests if fftpack is not installed:
  9. python tests/test_helper.py [<level>]
  10. """
  11. from pytest import raises as assert_raises
  12. from numpy.testing import assert_array_almost_equal, assert_equal, assert_
  13. from scipy.fftpack import fftshift,ifftshift,fftfreq,rfftfreq
  14. from scipy.fftpack.helper import (next_fast_len,
  15. _init_nd_shape_and_axes,
  16. _init_nd_shape_and_axes_sorted)
  17. from numpy import pi, random
  18. import numpy as np
  19. class TestFFTShift(object):
  20. def test_definition(self):
  21. x = [0,1,2,3,4,-4,-3,-2,-1]
  22. y = [-4,-3,-2,-1,0,1,2,3,4]
  23. assert_array_almost_equal(fftshift(x),y)
  24. assert_array_almost_equal(ifftshift(y),x)
  25. x = [0,1,2,3,4,-5,-4,-3,-2,-1]
  26. y = [-5,-4,-3,-2,-1,0,1,2,3,4]
  27. assert_array_almost_equal(fftshift(x),y)
  28. assert_array_almost_equal(ifftshift(y),x)
  29. def test_inverse(self):
  30. for n in [1,4,9,100,211]:
  31. x = random.random((n,))
  32. assert_array_almost_equal(ifftshift(fftshift(x)),x)
  33. class TestFFTFreq(object):
  34. def test_definition(self):
  35. x = [0,1,2,3,4,-4,-3,-2,-1]
  36. assert_array_almost_equal(9*fftfreq(9),x)
  37. assert_array_almost_equal(9*pi*fftfreq(9,pi),x)
  38. x = [0,1,2,3,4,-5,-4,-3,-2,-1]
  39. assert_array_almost_equal(10*fftfreq(10),x)
  40. assert_array_almost_equal(10*pi*fftfreq(10,pi),x)
  41. class TestRFFTFreq(object):
  42. def test_definition(self):
  43. x = [0,1,1,2,2,3,3,4,4]
  44. assert_array_almost_equal(9*rfftfreq(9),x)
  45. assert_array_almost_equal(9*pi*rfftfreq(9,pi),x)
  46. x = [0,1,1,2,2,3,3,4,4,5]
  47. assert_array_almost_equal(10*rfftfreq(10),x)
  48. assert_array_almost_equal(10*pi*rfftfreq(10,pi),x)
  49. class TestNextOptLen(object):
  50. def test_next_opt_len(self):
  51. random.seed(1234)
  52. def nums():
  53. for j in range(1, 1000):
  54. yield j
  55. yield 2**5 * 3**5 * 4**5 + 1
  56. for n in nums():
  57. m = next_fast_len(n)
  58. msg = "n=%d, m=%d" % (n, m)
  59. assert_(m >= n, msg)
  60. # check regularity
  61. k = m
  62. for d in [2, 3, 5]:
  63. while True:
  64. a, b = divmod(k, d)
  65. if b == 0:
  66. k = a
  67. else:
  68. break
  69. assert_equal(k, 1, err_msg=msg)
  70. def test_np_integers(self):
  71. ITYPES = [np.int16, np.int32, np.int64, np.uint16, np.uint32, np.uint64]
  72. for ityp in ITYPES:
  73. x = ityp(12345)
  74. testN = next_fast_len(x)
  75. assert_equal(testN, next_fast_len(int(x)))
  76. def test_next_opt_len_strict(self):
  77. hams = {
  78. 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 8, 8: 8, 14: 15, 15: 15,
  79. 16: 16, 17: 18, 1021: 1024, 1536: 1536, 51200000: 51200000,
  80. 510183360: 510183360, 510183360 + 1: 512000000,
  81. 511000000: 512000000,
  82. 854296875: 854296875, 854296875 + 1: 859963392,
  83. 196608000000: 196608000000, 196608000000 + 1: 196830000000,
  84. 8789062500000: 8789062500000, 8789062500000 + 1: 8796093022208,
  85. 206391214080000: 206391214080000,
  86. 206391214080000 + 1: 206624260800000,
  87. 470184984576000: 470184984576000,
  88. 470184984576000 + 1: 470715894135000,
  89. 7222041363087360: 7222041363087360,
  90. 7222041363087360 + 1: 7230196133913600,
  91. # power of 5 5**23
  92. 11920928955078125: 11920928955078125,
  93. 11920928955078125 - 1: 11920928955078125,
  94. # power of 3 3**34
  95. 16677181699666569: 16677181699666569,
  96. 16677181699666569 - 1: 16677181699666569,
  97. # power of 2 2**54
  98. 18014398509481984: 18014398509481984,
  99. 18014398509481984 - 1: 18014398509481984,
  100. # above this, int(ceil(n)) == int(ceil(n+1))
  101. 19200000000000000: 19200000000000000,
  102. 19200000000000000 + 1: 19221679687500000,
  103. 288230376151711744: 288230376151711744,
  104. 288230376151711744 + 1: 288325195312500000,
  105. 288325195312500000 - 1: 288325195312500000,
  106. 288325195312500000: 288325195312500000,
  107. 288325195312500000 + 1: 288555831593533440,
  108. # power of 3 3**83
  109. 3990838394187339929534246675572349035227 - 1:
  110. 3990838394187339929534246675572349035227,
  111. 3990838394187339929534246675572349035227:
  112. 3990838394187339929534246675572349035227,
  113. # power of 2 2**135
  114. 43556142965880123323311949751266331066368 - 1:
  115. 43556142965880123323311949751266331066368,
  116. 43556142965880123323311949751266331066368:
  117. 43556142965880123323311949751266331066368,
  118. # power of 5 5**57
  119. 6938893903907228377647697925567626953125 - 1:
  120. 6938893903907228377647697925567626953125,
  121. 6938893903907228377647697925567626953125:
  122. 6938893903907228377647697925567626953125,
  123. # http://www.drdobbs.com/228700538
  124. # 2**96 * 3**1 * 5**13
  125. 290142196707511001929482240000000000000 - 1:
  126. 290142196707511001929482240000000000000,
  127. 290142196707511001929482240000000000000:
  128. 290142196707511001929482240000000000000,
  129. 290142196707511001929482240000000000000 + 1:
  130. 290237644800000000000000000000000000000,
  131. # 2**36 * 3**69 * 5**7
  132. 4479571262811807241115438439905203543080960000000 - 1:
  133. 4479571262811807241115438439905203543080960000000,
  134. 4479571262811807241115438439905203543080960000000:
  135. 4479571262811807241115438439905203543080960000000,
  136. 4479571262811807241115438439905203543080960000000 + 1:
  137. 4480327901140333639941336854183943340032000000000,
  138. # 2**37 * 3**44 * 5**42
  139. 30774090693237851027531250000000000000000000000000000000000000 - 1:
  140. 30774090693237851027531250000000000000000000000000000000000000,
  141. 30774090693237851027531250000000000000000000000000000000000000:
  142. 30774090693237851027531250000000000000000000000000000000000000,
  143. 30774090693237851027531250000000000000000000000000000000000000 + 1:
  144. 30778180617309082445871527002041377406962596539492679680000000,
  145. }
  146. for x, y in hams.items():
  147. assert_equal(next_fast_len(x), y)
  148. class Test_init_nd_shape_and_axes(object):
  149. def test_py_0d_defaults(self):
  150. x = 4
  151. shape = None
  152. axes = None
  153. shape_expected = np.array([])
  154. axes_expected = np.array([])
  155. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  156. assert_equal(shape_res, shape_expected)
  157. assert_equal(axes_res, axes_expected)
  158. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  159. assert_equal(shape_res, shape_expected)
  160. assert_equal(axes_res, axes_expected)
  161. def test_np_0d_defaults(self):
  162. x = np.array(7.)
  163. shape = None
  164. axes = None
  165. shape_expected = np.array([])
  166. axes_expected = np.array([])
  167. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  168. assert_equal(shape_res, shape_expected)
  169. assert_equal(axes_res, axes_expected)
  170. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  171. assert_equal(shape_res, shape_expected)
  172. assert_equal(axes_res, axes_expected)
  173. def test_py_1d_defaults(self):
  174. x = [1, 2, 3]
  175. shape = None
  176. axes = None
  177. shape_expected = np.array([3])
  178. axes_expected = np.array([0])
  179. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  180. assert_equal(shape_res, shape_expected)
  181. assert_equal(axes_res, axes_expected)
  182. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  183. assert_equal(shape_res, shape_expected)
  184. assert_equal(axes_res, axes_expected)
  185. def test_np_1d_defaults(self):
  186. x = np.arange(0, 1, .1)
  187. shape = None
  188. axes = None
  189. shape_expected = np.array([10])
  190. axes_expected = np.array([0])
  191. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  192. assert_equal(shape_res, shape_expected)
  193. assert_equal(axes_res, axes_expected)
  194. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  195. assert_equal(shape_res, shape_expected)
  196. assert_equal(axes_res, axes_expected)
  197. def test_py_2d_defaults(self):
  198. x = [[1, 2, 3, 4],
  199. [5, 6, 7, 8]]
  200. shape = None
  201. axes = None
  202. shape_expected = np.array([2, 4])
  203. axes_expected = np.array([0, 1])
  204. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  205. assert_equal(shape_res, shape_expected)
  206. assert_equal(axes_res, axes_expected)
  207. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  208. assert_equal(shape_res, shape_expected)
  209. assert_equal(axes_res, axes_expected)
  210. def test_np_2d_defaults(self):
  211. x = np.arange(0, 1, .1).reshape(5, 2)
  212. shape = None
  213. axes = None
  214. shape_expected = np.array([5, 2])
  215. axes_expected = np.array([0, 1])
  216. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  217. assert_equal(shape_res, shape_expected)
  218. assert_equal(axes_res, axes_expected)
  219. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  220. assert_equal(shape_res, shape_expected)
  221. assert_equal(axes_res, axes_expected)
  222. def test_np_5d_defaults(self):
  223. x = np.zeros([6, 2, 5, 3, 4])
  224. shape = None
  225. axes = None
  226. shape_expected = np.array([6, 2, 5, 3, 4])
  227. axes_expected = np.array([0, 1, 2, 3, 4])
  228. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  229. assert_equal(shape_res, shape_expected)
  230. assert_equal(axes_res, axes_expected)
  231. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  232. assert_equal(shape_res, shape_expected)
  233. assert_equal(axes_res, axes_expected)
  234. def test_np_5d_set_shape(self):
  235. x = np.zeros([6, 2, 5, 3, 4])
  236. shape = [10, -1, -1, 1, 4]
  237. axes = None
  238. shape_expected = np.array([10, 2, 5, 1, 4])
  239. axes_expected = np.array([0, 1, 2, 3, 4])
  240. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  241. assert_equal(shape_res, shape_expected)
  242. assert_equal(axes_res, axes_expected)
  243. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  244. assert_equal(shape_res, shape_expected)
  245. assert_equal(axes_res, axes_expected)
  246. def test_np_5d_set_axes(self):
  247. x = np.zeros([6, 2, 5, 3, 4])
  248. shape = None
  249. axes = [4, 1, 2]
  250. shape_expected = np.array([4, 2, 5])
  251. axes_expected = np.array([4, 1, 2])
  252. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  253. assert_equal(shape_res, shape_expected)
  254. assert_equal(axes_res, axes_expected)
  255. def test_np_5d_set_axes_sorted(self):
  256. x = np.zeros([6, 2, 5, 3, 4])
  257. shape = None
  258. axes = [4, 1, 2]
  259. shape_expected = np.array([2, 5, 4])
  260. axes_expected = np.array([1, 2, 4])
  261. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  262. assert_equal(shape_res, shape_expected)
  263. assert_equal(axes_res, axes_expected)
  264. def test_np_5d_set_shape_axes(self):
  265. x = np.zeros([6, 2, 5, 3, 4])
  266. shape = [10, -1, 2]
  267. axes = [1, 0, 3]
  268. shape_expected = np.array([10, 6, 2])
  269. axes_expected = np.array([1, 0, 3])
  270. shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes)
  271. assert_equal(shape_res, shape_expected)
  272. assert_equal(axes_res, axes_expected)
  273. def test_np_5d_set_shape_axes_sorted(self):
  274. x = np.zeros([6, 2, 5, 3, 4])
  275. shape = [10, -1, 2]
  276. axes = [1, 0, 3]
  277. shape_expected = np.array([6, 10, 2])
  278. axes_expected = np.array([0, 1, 3])
  279. shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes)
  280. assert_equal(shape_res, shape_expected)
  281. assert_equal(axes_res, axes_expected)
  282. def test_errors(self):
  283. with assert_raises(ValueError,
  284. match="when given, axes values must be a scalar"
  285. " or vector"):
  286. _init_nd_shape_and_axes([0], shape=None, axes=[[1, 2], [3, 4]])
  287. with assert_raises(ValueError,
  288. match="when given, axes values must be integers"):
  289. _init_nd_shape_and_axes([0], shape=None, axes=[1., 2., 3., 4.])
  290. with assert_raises(ValueError,
  291. match="axes exceeds dimensionality of input"):
  292. _init_nd_shape_and_axes([0], shape=None, axes=[1])
  293. with assert_raises(ValueError,
  294. match="axes exceeds dimensionality of input"):
  295. _init_nd_shape_and_axes([0], shape=None, axes=[-2])
  296. with assert_raises(ValueError,
  297. match="all axes must be unique"):
  298. _init_nd_shape_and_axes([0], shape=None, axes=[0, 0])
  299. with assert_raises(ValueError,
  300. match="when given, shape values must be a scalar "
  301. "or vector"):
  302. _init_nd_shape_and_axes([0], shape=[[1, 2], [3, 4]], axes=None)
  303. with assert_raises(ValueError,
  304. match="when given, shape values must be integers"):
  305. _init_nd_shape_and_axes([0], shape=[1., 2., 3., 4.], axes=None)
  306. with assert_raises(ValueError,
  307. match="when given, axes and shape arguments"
  308. " have to be of the same length"):
  309. _init_nd_shape_and_axes(np.zeros([1, 1, 1, 1]),
  310. shape=[1, 2, 3], axes=[1])
  311. with assert_raises(ValueError,
  312. match="invalid number of data points"
  313. r" \(\[0\]\) specified"):
  314. _init_nd_shape_and_axes([0], shape=[0], axes=None)
  315. with assert_raises(ValueError,
  316. match="invalid number of data points"
  317. r" \(\[-2\]\) specified"):
  318. _init_nd_shape_and_axes([0], shape=-2, axes=None)