m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
12 KiB

11 months ago
  1. import numpy as np
  2. import pytest
  3. from numpy.random import random
  4. from numpy.testing import (
  5. assert_array_equal, assert_raises, assert_allclose
  6. )
  7. import threading
  8. import queue
  9. def fft1(x):
  10. L = len(x)
  11. phase = -2j*np.pi*(np.arange(L)/float(L))
  12. phase = np.arange(L).reshape(-1, 1) * phase
  13. return np.sum(x*np.exp(phase), axis=1)
  14. class TestFFTShift:
  15. def test_fft_n(self):
  16. assert_raises(ValueError, np.fft.fft, [1, 2, 3], 0)
  17. class TestFFT1D:
  18. def test_identity(self):
  19. maxlen = 512
  20. x = random(maxlen) + 1j*random(maxlen)
  21. xr = random(maxlen)
  22. for i in range(1, maxlen):
  23. assert_allclose(np.fft.ifft(np.fft.fft(x[0:i])), x[0:i],
  24. atol=1e-12)
  25. assert_allclose(np.fft.irfft(np.fft.rfft(xr[0:i]), i),
  26. xr[0:i], atol=1e-12)
  27. def test_fft(self):
  28. x = random(30) + 1j*random(30)
  29. assert_allclose(fft1(x), np.fft.fft(x), atol=1e-6)
  30. assert_allclose(fft1(x), np.fft.fft(x, norm="backward"), atol=1e-6)
  31. assert_allclose(fft1(x) / np.sqrt(30),
  32. np.fft.fft(x, norm="ortho"), atol=1e-6)
  33. assert_allclose(fft1(x) / 30.,
  34. np.fft.fft(x, norm="forward"), atol=1e-6)
  35. @pytest.mark.parametrize('norm', (None, 'backward', 'ortho', 'forward'))
  36. def test_ifft(self, norm):
  37. x = random(30) + 1j*random(30)
  38. assert_allclose(
  39. x, np.fft.ifft(np.fft.fft(x, norm=norm), norm=norm),
  40. atol=1e-6)
  41. # Ensure we get the correct error message
  42. with pytest.raises(ValueError,
  43. match='Invalid number of FFT data points'):
  44. np.fft.ifft([], norm=norm)
  45. def test_fft2(self):
  46. x = random((30, 20)) + 1j*random((30, 20))
  47. assert_allclose(np.fft.fft(np.fft.fft(x, axis=1), axis=0),
  48. np.fft.fft2(x), atol=1e-6)
  49. assert_allclose(np.fft.fft2(x),
  50. np.fft.fft2(x, norm="backward"), atol=1e-6)
  51. assert_allclose(np.fft.fft2(x) / np.sqrt(30 * 20),
  52. np.fft.fft2(x, norm="ortho"), atol=1e-6)
  53. assert_allclose(np.fft.fft2(x) / (30. * 20.),
  54. np.fft.fft2(x, norm="forward"), atol=1e-6)
  55. def test_ifft2(self):
  56. x = random((30, 20)) + 1j*random((30, 20))
  57. assert_allclose(np.fft.ifft(np.fft.ifft(x, axis=1), axis=0),
  58. np.fft.ifft2(x), atol=1e-6)
  59. assert_allclose(np.fft.ifft2(x),
  60. np.fft.ifft2(x, norm="backward"), atol=1e-6)
  61. assert_allclose(np.fft.ifft2(x) * np.sqrt(30 * 20),
  62. np.fft.ifft2(x, norm="ortho"), atol=1e-6)
  63. assert_allclose(np.fft.ifft2(x) * (30. * 20.),
  64. np.fft.ifft2(x, norm="forward"), atol=1e-6)
  65. def test_fftn(self):
  66. x = random((30, 20, 10)) + 1j*random((30, 20, 10))
  67. assert_allclose(
  68. np.fft.fft(np.fft.fft(np.fft.fft(x, axis=2), axis=1), axis=0),
  69. np.fft.fftn(x), atol=1e-6)
  70. assert_allclose(np.fft.fftn(x),
  71. np.fft.fftn(x, norm="backward"), atol=1e-6)
  72. assert_allclose(np.fft.fftn(x) / np.sqrt(30 * 20 * 10),
  73. np.fft.fftn(x, norm="ortho"), atol=1e-6)
  74. assert_allclose(np.fft.fftn(x) / (30. * 20. * 10.),
  75. np.fft.fftn(x, norm="forward"), atol=1e-6)
  76. def test_ifftn(self):
  77. x = random((30, 20, 10)) + 1j*random((30, 20, 10))
  78. assert_allclose(
  79. np.fft.ifft(np.fft.ifft(np.fft.ifft(x, axis=2), axis=1), axis=0),
  80. np.fft.ifftn(x), atol=1e-6)
  81. assert_allclose(np.fft.ifftn(x),
  82. np.fft.ifftn(x, norm="backward"), atol=1e-6)
  83. assert_allclose(np.fft.ifftn(x) * np.sqrt(30 * 20 * 10),
  84. np.fft.ifftn(x, norm="ortho"), atol=1e-6)
  85. assert_allclose(np.fft.ifftn(x) * (30. * 20. * 10.),
  86. np.fft.ifftn(x, norm="forward"), atol=1e-6)
  87. def test_rfft(self):
  88. x = random(30)
  89. for n in [x.size, 2*x.size]:
  90. for norm in [None, 'backward', 'ortho', 'forward']:
  91. assert_allclose(
  92. np.fft.fft(x, n=n, norm=norm)[:(n//2 + 1)],
  93. np.fft.rfft(x, n=n, norm=norm), atol=1e-6)
  94. assert_allclose(
  95. np.fft.rfft(x, n=n),
  96. np.fft.rfft(x, n=n, norm="backward"), atol=1e-6)
  97. assert_allclose(
  98. np.fft.rfft(x, n=n) / np.sqrt(n),
  99. np.fft.rfft(x, n=n, norm="ortho"), atol=1e-6)
  100. assert_allclose(
  101. np.fft.rfft(x, n=n) / n,
  102. np.fft.rfft(x, n=n, norm="forward"), atol=1e-6)
  103. def test_irfft(self):
  104. x = random(30)
  105. assert_allclose(x, np.fft.irfft(np.fft.rfft(x)), atol=1e-6)
  106. assert_allclose(x, np.fft.irfft(np.fft.rfft(x, norm="backward"),
  107. norm="backward"), atol=1e-6)
  108. assert_allclose(x, np.fft.irfft(np.fft.rfft(x, norm="ortho"),
  109. norm="ortho"), atol=1e-6)
  110. assert_allclose(x, np.fft.irfft(np.fft.rfft(x, norm="forward"),
  111. norm="forward"), atol=1e-6)
  112. def test_rfft2(self):
  113. x = random((30, 20))
  114. assert_allclose(np.fft.fft2(x)[:, :11], np.fft.rfft2(x), atol=1e-6)
  115. assert_allclose(np.fft.rfft2(x),
  116. np.fft.rfft2(x, norm="backward"), atol=1e-6)
  117. assert_allclose(np.fft.rfft2(x) / np.sqrt(30 * 20),
  118. np.fft.rfft2(x, norm="ortho"), atol=1e-6)
  119. assert_allclose(np.fft.rfft2(x) / (30. * 20.),
  120. np.fft.rfft2(x, norm="forward"), atol=1e-6)
  121. def test_irfft2(self):
  122. x = random((30, 20))
  123. assert_allclose(x, np.fft.irfft2(np.fft.rfft2(x)), atol=1e-6)
  124. assert_allclose(x, np.fft.irfft2(np.fft.rfft2(x, norm="backward"),
  125. norm="backward"), atol=1e-6)
  126. assert_allclose(x, np.fft.irfft2(np.fft.rfft2(x, norm="ortho"),
  127. norm="ortho"), atol=1e-6)
  128. assert_allclose(x, np.fft.irfft2(np.fft.rfft2(x, norm="forward"),
  129. norm="forward"), atol=1e-6)
  130. def test_rfftn(self):
  131. x = random((30, 20, 10))
  132. assert_allclose(np.fft.fftn(x)[:, :, :6], np.fft.rfftn(x), atol=1e-6)
  133. assert_allclose(np.fft.rfftn(x),
  134. np.fft.rfftn(x, norm="backward"), atol=1e-6)
  135. assert_allclose(np.fft.rfftn(x) / np.sqrt(30 * 20 * 10),
  136. np.fft.rfftn(x, norm="ortho"), atol=1e-6)
  137. assert_allclose(np.fft.rfftn(x) / (30. * 20. * 10.),
  138. np.fft.rfftn(x, norm="forward"), atol=1e-6)
  139. def test_irfftn(self):
  140. x = random((30, 20, 10))
  141. assert_allclose(x, np.fft.irfftn(np.fft.rfftn(x)), atol=1e-6)
  142. assert_allclose(x, np.fft.irfftn(np.fft.rfftn(x, norm="backward"),
  143. norm="backward"), atol=1e-6)
  144. assert_allclose(x, np.fft.irfftn(np.fft.rfftn(x, norm="ortho"),
  145. norm="ortho"), atol=1e-6)
  146. assert_allclose(x, np.fft.irfftn(np.fft.rfftn(x, norm="forward"),
  147. norm="forward"), atol=1e-6)
  148. def test_hfft(self):
  149. x = random(14) + 1j*random(14)
  150. x_herm = np.concatenate((random(1), x, random(1)))
  151. x = np.concatenate((x_herm, x[::-1].conj()))
  152. assert_allclose(np.fft.fft(x), np.fft.hfft(x_herm), atol=1e-6)
  153. assert_allclose(np.fft.hfft(x_herm),
  154. np.fft.hfft(x_herm, norm="backward"), atol=1e-6)
  155. assert_allclose(np.fft.hfft(x_herm) / np.sqrt(30),
  156. np.fft.hfft(x_herm, norm="ortho"), atol=1e-6)
  157. assert_allclose(np.fft.hfft(x_herm) / 30.,
  158. np.fft.hfft(x_herm, norm="forward"), atol=1e-6)
  159. def test_ihfft(self):
  160. x = random(14) + 1j*random(14)
  161. x_herm = np.concatenate((random(1), x, random(1)))
  162. x = np.concatenate((x_herm, x[::-1].conj()))
  163. assert_allclose(x_herm, np.fft.ihfft(np.fft.hfft(x_herm)), atol=1e-6)
  164. assert_allclose(x_herm, np.fft.ihfft(np.fft.hfft(x_herm,
  165. norm="backward"), norm="backward"), atol=1e-6)
  166. assert_allclose(x_herm, np.fft.ihfft(np.fft.hfft(x_herm,
  167. norm="ortho"), norm="ortho"), atol=1e-6)
  168. assert_allclose(x_herm, np.fft.ihfft(np.fft.hfft(x_herm,
  169. norm="forward"), norm="forward"), atol=1e-6)
  170. @pytest.mark.parametrize("op", [np.fft.fftn, np.fft.ifftn,
  171. np.fft.rfftn, np.fft.irfftn])
  172. def test_axes(self, op):
  173. x = random((30, 20, 10))
  174. axes = [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
  175. for a in axes:
  176. op_tr = op(np.transpose(x, a))
  177. tr_op = np.transpose(op(x, axes=a), a)
  178. assert_allclose(op_tr, tr_op, atol=1e-6)
  179. def test_all_1d_norm_preserving(self):
  180. # verify that round-trip transforms are norm-preserving
  181. x = random(30)
  182. x_norm = np.linalg.norm(x)
  183. n = x.size * 2
  184. func_pairs = [(np.fft.fft, np.fft.ifft),
  185. (np.fft.rfft, np.fft.irfft),
  186. # hfft: order so the first function takes x.size samples
  187. # (necessary for comparison to x_norm above)
  188. (np.fft.ihfft, np.fft.hfft),
  189. ]
  190. for forw, back in func_pairs:
  191. for n in [x.size, 2*x.size]:
  192. for norm in [None, 'backward', 'ortho', 'forward']:
  193. tmp = forw(x, n=n, norm=norm)
  194. tmp = back(tmp, n=n, norm=norm)
  195. assert_allclose(x_norm,
  196. np.linalg.norm(tmp), atol=1e-6)
  197. @pytest.mark.parametrize("dtype", [np.half, np.single, np.double,
  198. np.longdouble])
  199. def test_dtypes(self, dtype):
  200. # make sure that all input precisions are accepted and internally
  201. # converted to 64bit
  202. x = random(30).astype(dtype)
  203. assert_allclose(np.fft.ifft(np.fft.fft(x)), x, atol=1e-6)
  204. assert_allclose(np.fft.irfft(np.fft.rfft(x)), x, atol=1e-6)
  205. @pytest.mark.parametrize(
  206. "dtype",
  207. [np.float32, np.float64, np.complex64, np.complex128])
  208. @pytest.mark.parametrize("order", ["F", 'non-contiguous'])
  209. @pytest.mark.parametrize(
  210. "fft",
  211. [np.fft.fft, np.fft.fft2, np.fft.fftn,
  212. np.fft.ifft, np.fft.ifft2, np.fft.ifftn])
  213. def test_fft_with_order(dtype, order, fft):
  214. # Check that FFT/IFFT produces identical results for C, Fortran and
  215. # non contiguous arrays
  216. rng = np.random.RandomState(42)
  217. X = rng.rand(8, 7, 13).astype(dtype, copy=False)
  218. # See discussion in pull/14178
  219. _tol = 8.0 * np.sqrt(np.log2(X.size)) * np.finfo(X.dtype).eps
  220. if order == 'F':
  221. Y = np.asfortranarray(X)
  222. else:
  223. # Make a non contiguous array
  224. Y = X[::-1]
  225. X = np.ascontiguousarray(X[::-1])
  226. if fft.__name__.endswith('fft'):
  227. for axis in range(3):
  228. X_res = fft(X, axis=axis)
  229. Y_res = fft(Y, axis=axis)
  230. assert_allclose(X_res, Y_res, atol=_tol, rtol=_tol)
  231. elif fft.__name__.endswith(('fft2', 'fftn')):
  232. axes = [(0, 1), (1, 2), (0, 2)]
  233. if fft.__name__.endswith('fftn'):
  234. axes.extend([(0,), (1,), (2,), None])
  235. for ax in axes:
  236. X_res = fft(X, axes=ax)
  237. Y_res = fft(Y, axes=ax)
  238. assert_allclose(X_res, Y_res, atol=_tol, rtol=_tol)
  239. else:
  240. raise ValueError()
  241. class TestFFTThreadSafe:
  242. threads = 16
  243. input_shape = (800, 200)
  244. def _test_mtsame(self, func, *args):
  245. def worker(args, q):
  246. q.put(func(*args))
  247. q = queue.Queue()
  248. expected = func(*args)
  249. # Spin off a bunch of threads to call the same function simultaneously
  250. t = [threading.Thread(target=worker, args=(args, q))
  251. for i in range(self.threads)]
  252. [x.start() for x in t]
  253. [x.join() for x in t]
  254. # Make sure all threads returned the correct value
  255. for i in range(self.threads):
  256. assert_array_equal(q.get(timeout=5), expected,
  257. 'Function returned wrong value in multithreaded context')
  258. def test_fft(self):
  259. a = np.ones(self.input_shape) * 1+0j
  260. self._test_mtsame(np.fft.fft, a)
  261. def test_ifft(self):
  262. a = np.ones(self.input_shape) * 1+0j
  263. self._test_mtsame(np.fft.ifft, a)
  264. def test_rfft(self):
  265. a = np.ones(self.input_shape)
  266. self._test_mtsame(np.fft.rfft, a)
  267. def test_irfft(self):
  268. a = np.ones(self.input_shape) * 1+0j
  269. self._test_mtsame(np.fft.irfft, a)