test_fourier.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import math
  2. import numpy as np
  3. from scipy._lib._array_api import (
  4. xp_assert_equal,
  5. assert_array_almost_equal,
  6. assert_almost_equal,
  7. is_cupy,
  8. make_xp_test_case,
  9. make_xp_pytest_param,
  10. )
  11. import pytest
  12. from scipy import ndimage
  13. skip_xp_backends = pytest.mark.skip_xp_backends
  14. class TestNdimageFourier:
  15. @pytest.mark.parametrize('shape', [(32, 16), (31, 15), (1, 10)])
  16. @pytest.mark.parametrize('dtype, dec', [("float32", 6), ("float64", 14)])
  17. @make_xp_test_case(ndimage.fourier_gaussian)
  18. def test_fourier_gaussian_real01(self, shape, dtype, dec, xp):
  19. fft = getattr(xp, 'fft')
  20. a = np.zeros(shape, dtype=dtype)
  21. a[0, 0] = 1.0
  22. a = xp.asarray(a)
  23. a = fft.rfft(a, n=shape[0], axis=0)
  24. a = fft.fft(a, n=shape[1], axis=1)
  25. a = ndimage.fourier_gaussian(a, [5.0, 2.5], shape[0], 0)
  26. a = fft.ifft(a, n=shape[1], axis=1)
  27. a = fft.irfft(a, n=shape[0], axis=0)
  28. assert_almost_equal(ndimage.sum(a), xp.asarray(1), decimal=dec,
  29. check_0d=False)
  30. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  31. @pytest.mark.parametrize('dtype, dec', [("complex64", 6), ("complex128", 14)])
  32. @make_xp_test_case(ndimage.fourier_gaussian)
  33. def test_fourier_gaussian_complex01(self, shape, dtype, dec, xp):
  34. fft = getattr(xp, 'fft')
  35. a = np.zeros(shape, dtype=dtype)
  36. a[0, 0] = 1.0
  37. a = xp.asarray(a)
  38. a = fft.fft(a, n=shape[0], axis=0)
  39. a = fft.fft(a, n=shape[1], axis=1)
  40. a = ndimage.fourier_gaussian(a, [5.0, 2.5], -1, 0)
  41. a = fft.ifft(a, n=shape[1], axis=1)
  42. a = fft.ifft(a, n=shape[0], axis=0)
  43. assert_almost_equal(ndimage.sum(xp.real(a)), xp.asarray(1.0), decimal=dec,
  44. check_0d=False)
  45. @pytest.mark.parametrize('shape', [(32, 16), (31, 15), (1, 10)])
  46. @pytest.mark.parametrize('dtype, dec', [("float32", 6), ("float64", 14)])
  47. @make_xp_test_case(ndimage.fourier_uniform)
  48. def test_fourier_uniform_real01(self, shape, dtype, dec, xp):
  49. fft = getattr(xp, 'fft')
  50. a = np.zeros(shape, dtype=dtype)
  51. a[0, 0] = 1.0
  52. a = xp.asarray(a)
  53. a = fft.rfft(a, n=shape[0], axis=0)
  54. a = fft.fft(a, n=shape[1], axis=1)
  55. a = ndimage.fourier_uniform(a, [5.0, 2.5], shape[0], 0)
  56. a = fft.ifft(a, n=shape[1], axis=1)
  57. a = fft.irfft(a, n=shape[0], axis=0)
  58. assert_almost_equal(ndimage.sum(a), xp.asarray(1.0), decimal=dec,
  59. check_0d=False)
  60. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  61. @pytest.mark.parametrize('dtype, dec', [("complex64", 6), ("complex128", 14)])
  62. @make_xp_test_case(ndimage.fourier_uniform)
  63. def test_fourier_uniform_complex01(self, shape, dtype, dec, xp):
  64. fft = getattr(xp, 'fft')
  65. a = np.zeros(shape, dtype=dtype)
  66. a[0, 0] = 1.0
  67. a = xp.asarray(a)
  68. a = fft.fft(a, n=shape[0], axis=0)
  69. a = fft.fft(a, n=shape[1], axis=1)
  70. a = ndimage.fourier_uniform(a, [5.0, 2.5], -1, 0)
  71. a = fft.ifft(a, n=shape[1], axis=1)
  72. a = fft.ifft(a, n=shape[0], axis=0)
  73. assert_almost_equal(ndimage.sum(xp.real(a)), xp.asarray(1.0), decimal=dec,
  74. check_0d=False)
  75. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  76. @pytest.mark.parametrize('dtype, dec', [("float32", 4), ("float64", 11)])
  77. @make_xp_test_case(ndimage.fourier_shift)
  78. def test_fourier_shift_real01(self, shape, dtype, dec, xp):
  79. fft = getattr(xp, 'fft')
  80. expected = np.arange(shape[0] * shape[1], dtype=dtype).reshape(shape)
  81. expected = xp.asarray(expected)
  82. a = fft.rfft(expected, n=shape[0], axis=0)
  83. a = fft.fft(a, n=shape[1], axis=1)
  84. a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
  85. a = fft.ifft(a, n=shape[1], axis=1)
  86. a = fft.irfft(a, n=shape[0], axis=0)
  87. assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1], decimal=dec)
  88. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  89. @pytest.mark.parametrize('dtype, dec', [("complex64", 4), ("complex128", 11)])
  90. @make_xp_test_case(ndimage.fourier_shift)
  91. def test_fourier_shift_complex01(self, shape, dtype, dec, xp):
  92. fft = getattr(xp, 'fft')
  93. expected = np.arange(shape[0] * shape[1], dtype=dtype).reshape(shape)
  94. expected = xp.asarray(expected)
  95. a = fft.fft(expected, n=shape[0], axis=0)
  96. a = fft.fft(a, n=shape[1], axis=1)
  97. a = ndimage.fourier_shift(a, [1, 1], -1, 0)
  98. a = fft.ifft(a, n=shape[1], axis=1)
  99. a = fft.ifft(a, n=shape[0], axis=0)
  100. assert_array_almost_equal(xp.real(a)[1:, 1:], expected[:-1, :-1], decimal=dec)
  101. assert_array_almost_equal(xp.imag(a), xp.zeros(shape), decimal=dec)
  102. @pytest.mark.parametrize('shape', [(32, 16), (31, 15), (1, 10)])
  103. @pytest.mark.parametrize('dtype, dec', [("float32", 5), ("float64", 14)])
  104. @make_xp_test_case(ndimage.fourier_ellipsoid)
  105. def test_fourier_ellipsoid_real01(self, shape, dtype, dec, xp):
  106. fft = getattr(xp, 'fft')
  107. a = np.zeros(shape, dtype=dtype)
  108. a[0, 0] = 1.0
  109. a = xp.asarray(a)
  110. a = fft.rfft(a, n=shape[0], axis=0)
  111. a = fft.fft(a, n=shape[1], axis=1)
  112. a = ndimage.fourier_ellipsoid(a, [5.0, 2.5], shape[0], 0)
  113. a = fft.ifft(a, n=shape[1], axis=1)
  114. a = fft.irfft(a, n=shape[0], axis=0)
  115. assert_almost_equal(ndimage.sum(a), xp.asarray(1.0), decimal=dec,
  116. check_0d=False)
  117. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  118. @pytest.mark.parametrize('dtype, dec', [("complex64", 5), ("complex128", 14)])
  119. @make_xp_test_case(ndimage.fourier_ellipsoid)
  120. def test_fourier_ellipsoid_complex01(self, shape, dtype, dec, xp):
  121. fft = getattr(xp, 'fft')
  122. a = np.zeros(shape, dtype=dtype)
  123. a[0, 0] = 1.0
  124. a = xp.asarray(a)
  125. a = fft.fft(a, n=shape[0], axis=0)
  126. a = fft.fft(a, n=shape[1], axis=1)
  127. a = ndimage.fourier_ellipsoid(a, [5.0, 2.5], -1, 0)
  128. a = fft.ifft(a, n=shape[1], axis=1)
  129. a = fft.ifft(a, n=shape[0], axis=0)
  130. assert_almost_equal(ndimage.sum(xp.real(a)), xp.asarray(1.0), decimal=dec,
  131. check_0d=False)
  132. @make_xp_test_case(ndimage.fourier_ellipsoid)
  133. def test_fourier_ellipsoid_unimplemented_ndim(self, xp):
  134. # arrays with ndim > 3 raise NotImplementedError
  135. x = xp.ones((4, 6, 8, 10), dtype=xp.complex128)
  136. with pytest.raises(NotImplementedError):
  137. ndimage.fourier_ellipsoid(x, 3)
  138. @make_xp_test_case(ndimage.fourier_ellipsoid)
  139. def test_fourier_ellipsoid_1d_complex(self, xp):
  140. # expected result of 1d ellipsoid is the same as for fourier_uniform
  141. for shape in [(32, ), (31, )]:
  142. for type_, dec in zip([xp.complex64, xp.complex128], [5, 14]):
  143. x = xp.ones(shape, dtype=type_)
  144. a = ndimage.fourier_ellipsoid(x, 5, -1, 0)
  145. b = ndimage.fourier_uniform(x, 5, -1, 0)
  146. assert_array_almost_equal(a, b, decimal=dec)
  147. @pytest.mark.parametrize('shape', [(0, ), (0, 10), (10, 0)])
  148. @pytest.mark.parametrize('dtype', ["float32", "float64",
  149. "complex64", "complex128"])
  150. @pytest.mark.parametrize('test_func',
  151. [make_xp_pytest_param(ndimage.fourier_ellipsoid),
  152. make_xp_pytest_param(ndimage.fourier_gaussian),
  153. make_xp_pytest_param(ndimage.fourier_uniform)])
  154. def test_fourier_zero_length_dims(self, shape, dtype, test_func, xp):
  155. if (
  156. is_cupy(xp)
  157. and test_func.__name__ == "fourier_ellipsoid"
  158. and math.prod(shape) == 0
  159. ):
  160. pytest.xfail("CuPy's fourier_ellipsoid does not accept size==0 arrays")
  161. dtype = getattr(xp, dtype)
  162. a = xp.ones(shape, dtype=dtype)
  163. b = test_func(a, 3)
  164. xp_assert_equal(a, b)