Skip to content
Browse files

ENH: expose control truncation of ndimage.gaussian_filter().

This change gives the user control over the size at which the Gaussian filter is truncated,
with default behavior to truncate at 4 standard deviations (the previous fixed value).
  • Loading branch information...
1 parent 659017f commit 482ec0de768c563f0d550cf9a44e33f3af1e528b @thouis committed Jun 5, 2012
Showing with 19 additions and 6 deletions.
  1. +11 −6 scipy/ndimage/filters.py
  2. +8 −0 scipy/ndimage/tests/test_filters.py
View
17 scipy/ndimage/filters.py
@@ -168,7 +168,7 @@ def convolve1d(input, weights, axis = -1, output = None, mode = "reflect",
@docfiller
def gaussian_filter1d(input, sigma, axis = -1, order = 0, output = None,
- mode = "reflect", cval = 0.0):
+ mode = "reflect", cval = 0.0, truncate = 4.0):
"""One-dimensional Gaussian filter.
Parameters
@@ -185,13 +185,15 @@ def gaussian_filter1d(input, sigma, axis = -1, order = 0, output = None,
%(output)s
%(mode)s
%(cval)s
+ truncate : float
+ Truncate the filter at this many standard deviations.
+ Default is 4.0.
"""
if order not in range(4):
raise ValueError('Order outside 0..3 not implemented')
sd = float(sigma)
- # make the length of the filter equal to 4 times the standard
- # deviations:
- lw = int(4.0 * sd + 0.5)
+ # make the radius of the filter equal to truncate standard deviations
+ lw = int(truncate * sd + 0.5)
weights = [0.0] * (2 * lw + 1)
weights[lw] = 1.0
sum = 1.0
@@ -232,7 +234,7 @@ def gaussian_filter1d(input, sigma, axis = -1, order = 0, output = None,
@docfiller
def gaussian_filter(input, sigma, order = 0, output = None,
- mode = "reflect", cval = 0.0):
+ mode = "reflect", cval = 0.0, truncate = 4.0):
"""Multi-dimensional Gaussian filter.
Parameters
@@ -253,6 +255,9 @@ def gaussian_filter(input, sigma, order = 0, output = None,
%(output)s
%(mode)s
%(cval)s
+ truncate : float
+ Truncate the filter at this many standard deviations.
+ Default is 4.0.
Notes
-----
@@ -275,7 +280,7 @@ def gaussian_filter(input, sigma, order = 0, output = None,
if len(axes) > 0:
for axis, sigma, order in axes:
gaussian_filter1d(input, sigma, axis, order, output,
- mode, cval)
+ mode, cval, truncate)
input = output
else:
output[...] = input[...]
View
8 scipy/ndimage/tests/test_filters.py
@@ -52,3 +52,11 @@ def test_valid_origins():
# Just check this raises an error instead of silently accepting or
# segfaulting.
assert_raises(ValueError, filter, data, 3, origin=2)
+
+def test_gaussian_truncate():
+ """Test that Gaussian filters can be truncated at different widths."""
+ arr = np.zeros((100, 100), np.float)
+ arr[50, 50] = 1
+ num_zeros_2 = (sndi.gaussian_filter(arr, 5, truncate=2) > 0).sum()
+ num_zeros_5 = (sndi.gaussian_filter(arr, 5, truncate=5) > 0).sum()
+ assert (num_zeros_5 > num_zeros_2 * 2)

0 comments on commit 482ec0d

Please sign in to comment.
Something went wrong with that request. Please try again.