test_fourier.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import numpy
  2. from numpy import fft
  3. from numpy.testing import (assert_almost_equal, assert_array_almost_equal,
  4. assert_equal)
  5. import pytest
  6. from scipy import ndimage
  7. class TestNdimageFourier:
  8. @pytest.mark.parametrize('shape', [(32, 16), (31, 15), (1, 10)])
  9. @pytest.mark.parametrize('dtype, dec',
  10. [(numpy.float32, 6), (numpy.float64, 14)])
  11. def test_fourier_gaussian_real01(self, shape, dtype, dec):
  12. a = numpy.zeros(shape, dtype)
  13. a[0, 0] = 1.0
  14. a = fft.rfft(a, shape[0], 0)
  15. a = fft.fft(a, shape[1], 1)
  16. a = ndimage.fourier_gaussian(a, [5.0, 2.5], shape[0], 0)
  17. a = fft.ifft(a, shape[1], 1)
  18. a = fft.irfft(a, shape[0], 0)
  19. assert_almost_equal(ndimage.sum(a), 1, decimal=dec)
  20. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  21. @pytest.mark.parametrize('dtype, dec',
  22. [(numpy.complex64, 6), (numpy.complex128, 14)])
  23. def test_fourier_gaussian_complex01(self, shape, dtype, dec):
  24. a = numpy.zeros(shape, dtype)
  25. a[0, 0] = 1.0
  26. a = fft.fft(a, shape[0], 0)
  27. a = fft.fft(a, shape[1], 1)
  28. a = ndimage.fourier_gaussian(a, [5.0, 2.5], -1, 0)
  29. a = fft.ifft(a, shape[1], 1)
  30. a = fft.ifft(a, shape[0], 0)
  31. assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
  32. @pytest.mark.parametrize('shape', [(32, 16), (31, 15), (1, 10)])
  33. @pytest.mark.parametrize('dtype, dec',
  34. [(numpy.float32, 6), (numpy.float64, 14)])
  35. def test_fourier_uniform_real01(self, shape, dtype, dec):
  36. a = numpy.zeros(shape, dtype)
  37. a[0, 0] = 1.0
  38. a = fft.rfft(a, shape[0], 0)
  39. a = fft.fft(a, shape[1], 1)
  40. a = ndimage.fourier_uniform(a, [5.0, 2.5], shape[0], 0)
  41. a = fft.ifft(a, shape[1], 1)
  42. a = fft.irfft(a, shape[0], 0)
  43. assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec)
  44. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  45. @pytest.mark.parametrize('dtype, dec',
  46. [(numpy.complex64, 6), (numpy.complex128, 14)])
  47. def test_fourier_uniform_complex01(self, shape, dtype, dec):
  48. a = numpy.zeros(shape, dtype)
  49. a[0, 0] = 1.0
  50. a = fft.fft(a, shape[0], 0)
  51. a = fft.fft(a, shape[1], 1)
  52. a = ndimage.fourier_uniform(a, [5.0, 2.5], -1, 0)
  53. a = fft.ifft(a, shape[1], 1)
  54. a = fft.ifft(a, shape[0], 0)
  55. assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
  56. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  57. @pytest.mark.parametrize('dtype, dec',
  58. [(numpy.float32, 4), (numpy.float64, 11)])
  59. def test_fourier_shift_real01(self, shape, dtype, dec):
  60. expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
  61. expected.shape = shape
  62. a = fft.rfft(expected, shape[0], 0)
  63. a = fft.fft(a, shape[1], 1)
  64. a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
  65. a = fft.ifft(a, shape[1], 1)
  66. a = fft.irfft(a, shape[0], 0)
  67. assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1],
  68. decimal=dec)
  69. assert_array_almost_equal(a.imag, numpy.zeros(shape),
  70. decimal=dec)
  71. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  72. @pytest.mark.parametrize('dtype, dec',
  73. [(numpy.complex64, 6), (numpy.complex128, 11)])
  74. def test_fourier_shift_complex01(self, shape, dtype, dec):
  75. expected = numpy.arange(shape[0] * shape[1], dtype=dtype)
  76. expected.shape = shape
  77. a = fft.fft(expected, shape[0], 0)
  78. a = fft.fft(a, shape[1], 1)
  79. a = ndimage.fourier_shift(a, [1, 1], -1, 0)
  80. a = fft.ifft(a, shape[1], 1)
  81. a = fft.ifft(a, shape[0], 0)
  82. assert_array_almost_equal(a.real[1:, 1:], expected[:-1, :-1],
  83. decimal=dec)
  84. assert_array_almost_equal(a.imag, numpy.zeros(shape),
  85. decimal=dec)
  86. @pytest.mark.parametrize('shape', [(32, 16), (31, 15), (1, 10)])
  87. @pytest.mark.parametrize('dtype, dec',
  88. [(numpy.float32, 5), (numpy.float64, 14)])
  89. def test_fourier_ellipsoid_real01(self, shape, dtype, dec):
  90. a = numpy.zeros(shape, dtype)
  91. a[0, 0] = 1.0
  92. a = fft.rfft(a, shape[0], 0)
  93. a = fft.fft(a, shape[1], 1)
  94. a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
  95. shape[0], 0)
  96. a = fft.ifft(a, shape[1], 1)
  97. a = fft.irfft(a, shape[0], 0)
  98. assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec)
  99. @pytest.mark.parametrize('shape', [(32, 16), (31, 15)])
  100. @pytest.mark.parametrize('dtype, dec',
  101. [(numpy.complex64, 5), (numpy.complex128, 14)])
  102. def test_fourier_ellipsoid_complex01(self, shape, dtype, dec):
  103. a = numpy.zeros(shape, dtype)
  104. a[0, 0] = 1.0
  105. a = fft.fft(a, shape[0], 0)
  106. a = fft.fft(a, shape[1], 1)
  107. a = ndimage.fourier_ellipsoid(a, [5.0, 2.5], -1, 0)
  108. a = fft.ifft(a, shape[1], 1)
  109. a = fft.ifft(a, shape[0], 0)
  110. assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
  111. def test_fourier_ellipsoid_unimplemented_ndim(self):
  112. # arrays with ndim > 3 raise NotImplementedError
  113. x = numpy.ones((4, 6, 8, 10), dtype=numpy.complex128)
  114. with pytest.raises(NotImplementedError):
  115. a = ndimage.fourier_ellipsoid(x, 3)
  116. def test_fourier_ellipsoid_1d_complex(self):
  117. # expected result of 1d ellipsoid is the same as for fourier_uniform
  118. for shape in [(32, ), (31, )]:
  119. for type_, dec in zip([numpy.complex64, numpy.complex128],
  120. [5, 14]):
  121. x = numpy.ones(shape, dtype=type_)
  122. a = ndimage.fourier_ellipsoid(x, 5, -1, 0)
  123. b = ndimage.fourier_uniform(x, 5, -1, 0)
  124. assert_array_almost_equal(a, b, decimal=dec)
  125. @pytest.mark.parametrize('shape', [(0, ), (0, 10), (10, 0)])
  126. @pytest.mark.parametrize('dtype',
  127. [numpy.float32, numpy.float64,
  128. numpy.complex64, numpy.complex128])
  129. @pytest.mark.parametrize('test_func',
  130. [ndimage.fourier_ellipsoid,
  131. ndimage.fourier_gaussian,
  132. ndimage.fourier_uniform])
  133. def test_fourier_zero_length_dims(self, shape, dtype, test_func):
  134. a = numpy.ones(shape, dtype)
  135. b = test_func(a, 3)
  136. assert_equal(a, b)