Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/scipy/ndimage/fourier.py : 16%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) 2003-2005 Peter J. Verveer
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6#
7# 1. Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#
10# 2. Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following
12# disclaimer in the documentation and/or other materials provided
13# with the distribution.
14#
15# 3. The name of the author may not be used to endorse or promote
16# products derived from this software without specific prior
17# written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31import numpy
32from numpy.core.multiarray import normalize_axis_index
33from . import _ni_support
34from . import _nd_image
36__all__ = ['fourier_gaussian', 'fourier_uniform', 'fourier_ellipsoid',
37 'fourier_shift']
40def _get_output_fourier(output, input):
41 if output is None:
42 if input.dtype.type in [numpy.complex64, numpy.complex128,
43 numpy.float32]:
44 output = numpy.zeros(input.shape, dtype=input.dtype)
45 else:
46 output = numpy.zeros(input.shape, dtype=numpy.float64)
47 elif type(output) is type:
48 if output not in [numpy.complex64, numpy.complex128,
49 numpy.float32, numpy.float64]:
50 raise RuntimeError("output type not supported")
51 output = numpy.zeros(input.shape, dtype=output)
52 elif output.shape != input.shape:
53 raise RuntimeError("output shape not correct")
54 return output
57def _get_output_fourier_complex(output, input):
58 if output is None:
59 if input.dtype.type in [numpy.complex64, numpy.complex128]:
60 output = numpy.zeros(input.shape, dtype=input.dtype)
61 else:
62 output = numpy.zeros(input.shape, dtype=numpy.complex128)
63 elif type(output) is type:
64 if output not in [numpy.complex64, numpy.complex128]:
65 raise RuntimeError("output type not supported")
66 output = numpy.zeros(input.shape, dtype=output)
67 elif output.shape != input.shape:
68 raise RuntimeError("output shape not correct")
69 return output
72def fourier_gaussian(input, sigma, n=-1, axis=-1, output=None):
73 """
74 Multidimensional Gaussian fourier filter.
76 The array is multiplied with the fourier transform of a Gaussian
77 kernel.
79 Parameters
80 ----------
81 input : array_like
82 The input array.
83 sigma : float or sequence
84 The sigma of the Gaussian kernel. If a float, `sigma` is the same for
85 all axes. If a sequence, `sigma` has to contain one value for each
86 axis.
87 n : int, optional
88 If `n` is negative (default), then the input is assumed to be the
89 result of a complex fft.
90 If `n` is larger than or equal to zero, the input is assumed to be the
91 result of a real fft, and `n` gives the length of the array before
92 transformation along the real transform direction.
93 axis : int, optional
94 The axis of the real transform.
95 output : ndarray, optional
96 If given, the result of filtering the input is placed in this array.
97 None is returned in this case.
99 Returns
100 -------
101 fourier_gaussian : ndarray
102 The filtered input.
104 Examples
105 --------
106 >>> from scipy import ndimage, misc
107 >>> import numpy.fft
108 >>> import matplotlib.pyplot as plt
109 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
110 >>> plt.gray() # show the filtered result in grayscale
111 >>> ascent = misc.ascent()
112 >>> input_ = numpy.fft.fft2(ascent)
113 >>> result = ndimage.fourier_gaussian(input_, sigma=4)
114 >>> result = numpy.fft.ifft2(result)
115 >>> ax1.imshow(ascent)
116 >>> ax2.imshow(result.real) # the imaginary part is an artifact
117 >>> plt.show()
118 """
119 input = numpy.asarray(input)
120 output = _get_output_fourier(output, input)
121 axis = normalize_axis_index(axis, input.ndim)
122 sigmas = _ni_support._normalize_sequence(sigma, input.ndim)
123 sigmas = numpy.asarray(sigmas, dtype=numpy.float64)
124 if not sigmas.flags.contiguous:
125 sigmas = sigmas.copy()
127 _nd_image.fourier_filter(input, sigmas, n, axis, output, 0)
128 return output
131def fourier_uniform(input, size, n=-1, axis=-1, output=None):
132 """
133 Multidimensional uniform fourier filter.
135 The array is multiplied with the Fourier transform of a box of given
136 size.
138 Parameters
139 ----------
140 input : array_like
141 The input array.
142 size : float or sequence
143 The size of the box used for filtering.
144 If a float, `size` is the same for all axes. If a sequence, `size` has
145 to contain one value for each axis.
146 n : int, optional
147 If `n` is negative (default), then the input is assumed to be the
148 result of a complex fft.
149 If `n` is larger than or equal to zero, the input is assumed to be the
150 result of a real fft, and `n` gives the length of the array before
151 transformation along the real transform direction.
152 axis : int, optional
153 The axis of the real transform.
154 output : ndarray, optional
155 If given, the result of filtering the input is placed in this array.
156 None is returned in this case.
158 Returns
159 -------
160 fourier_uniform : ndarray
161 The filtered input.
163 Examples
164 --------
165 >>> from scipy import ndimage, misc
166 >>> import numpy.fft
167 >>> import matplotlib.pyplot as plt
168 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
169 >>> plt.gray() # show the filtered result in grayscale
170 >>> ascent = misc.ascent()
171 >>> input_ = numpy.fft.fft2(ascent)
172 >>> result = ndimage.fourier_uniform(input_, size=20)
173 >>> result = numpy.fft.ifft2(result)
174 >>> ax1.imshow(ascent)
175 >>> ax2.imshow(result.real) # the imaginary part is an artifact
176 >>> plt.show()
177 """
178 input = numpy.asarray(input)
179 output = _get_output_fourier(output, input)
180 axis = normalize_axis_index(axis, input.ndim)
181 sizes = _ni_support._normalize_sequence(size, input.ndim)
182 sizes = numpy.asarray(sizes, dtype=numpy.float64)
183 if not sizes.flags.contiguous:
184 sizes = sizes.copy()
185 _nd_image.fourier_filter(input, sizes, n, axis, output, 1)
186 return output
189def fourier_ellipsoid(input, size, n=-1, axis=-1, output=None):
190 """
191 Multidimensional ellipsoid Fourier filter.
193 The array is multiplied with the fourier transform of a ellipsoid of
194 given sizes.
196 Parameters
197 ----------
198 input : array_like
199 The input array.
200 size : float or sequence
201 The size of the box used for filtering.
202 If a float, `size` is the same for all axes. If a sequence, `size` has
203 to contain one value for each axis.
204 n : int, optional
205 If `n` is negative (default), then the input is assumed to be the
206 result of a complex fft.
207 If `n` is larger than or equal to zero, the input is assumed to be the
208 result of a real fft, and `n` gives the length of the array before
209 transformation along the real transform direction.
210 axis : int, optional
211 The axis of the real transform.
212 output : ndarray, optional
213 If given, the result of filtering the input is placed in this array.
214 None is returned in this case.
216 Returns
217 -------
218 fourier_ellipsoid : ndarray
219 The filtered input.
221 Notes
222 -----
223 This function is implemented for arrays of rank 1, 2, or 3.
225 Examples
226 --------
227 >>> from scipy import ndimage, misc
228 >>> import numpy.fft
229 >>> import matplotlib.pyplot as plt
230 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
231 >>> plt.gray() # show the filtered result in grayscale
232 >>> ascent = misc.ascent()
233 >>> input_ = numpy.fft.fft2(ascent)
234 >>> result = ndimage.fourier_ellipsoid(input_, size=20)
235 >>> result = numpy.fft.ifft2(result)
236 >>> ax1.imshow(ascent)
237 >>> ax2.imshow(result.real) # the imaginary part is an artifact
238 >>> plt.show()
239 """
240 input = numpy.asarray(input)
241 output = _get_output_fourier(output, input)
242 axis = normalize_axis_index(axis, input.ndim)
243 sizes = _ni_support._normalize_sequence(size, input.ndim)
244 sizes = numpy.asarray(sizes, dtype=numpy.float64)
245 if not sizes.flags.contiguous:
246 sizes = sizes.copy()
247 _nd_image.fourier_filter(input, sizes, n, axis, output, 2)
248 return output
251def fourier_shift(input, shift, n=-1, axis=-1, output=None):
252 """
253 Multidimensional Fourier shift filter.
255 The array is multiplied with the Fourier transform of a shift operation.
257 Parameters
258 ----------
259 input : array_like
260 The input array.
261 shift : float or sequence
262 The size of the box used for filtering.
263 If a float, `shift` is the same for all axes. If a sequence, `shift`
264 has to contain one value for each axis.
265 n : int, optional
266 If `n` is negative (default), then the input is assumed to be the
267 result of a complex fft.
268 If `n` is larger than or equal to zero, the input is assumed to be the
269 result of a real fft, and `n` gives the length of the array before
270 transformation along the real transform direction.
271 axis : int, optional
272 The axis of the real transform.
273 output : ndarray, optional
274 If given, the result of shifting the input is placed in this array.
275 None is returned in this case.
277 Returns
278 -------
279 fourier_shift : ndarray
280 The shifted input.
282 Examples
283 --------
284 >>> from scipy import ndimage, misc
285 >>> import matplotlib.pyplot as plt
286 >>> import numpy.fft
287 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
288 >>> plt.gray() # show the filtered result in grayscale
289 >>> ascent = misc.ascent()
290 >>> input_ = numpy.fft.fft2(ascent)
291 >>> result = ndimage.fourier_shift(input_, shift=200)
292 >>> result = numpy.fft.ifft2(result)
293 >>> ax1.imshow(ascent)
294 >>> ax2.imshow(result.real) # the imaginary part is an artifact
295 >>> plt.show()
296 """
297 input = numpy.asarray(input)
298 output = _get_output_fourier_complex(output, input)
299 axis = normalize_axis_index(axis, input.ndim)
300 shifts = _ni_support._normalize_sequence(shift, input.ndim)
301 shifts = numpy.asarray(shifts, dtype=numpy.float64)
302 if not shifts.flags.contiguous:
303 shifts = shifts.copy()
304 _nd_image.fourier_shift(input, shifts, n, axis, output)
305 return output