8

I read the scipy docs for the function here : scipy.ndimage.uniform_filter1d. However, when I tried using it, I couldn't wrap around my head on it's working. I read the docs, ran the example over there in the Python Shell, used my own example but still no progress. For eg:

>>> from scipy.ndimage import uniform_filter1d
>>> uniform_filter1d([2, 8, 0, 4, 1, 9, 9, 0], size=3)
array([4, 3, 4, 1, 4, 6, 6, 3])
>>> uniform_filter1d([1, 2, 3, 4, 5, 6, 7, 8], size=3)
array([1, 2, 3, 4, 5, 6, 7, 7])

When I saw the output of the second array, it felt like the function retained most of the array's elements. However in the second example it felt like barring 4 & 1 all the other elements in the output array were completely new.

Thus I would like you to help me understand the working and the use of this function.

1 Answer 1

19

What this filter does is, according to size, to take the arithmetic average of each pixel with its neighbor. Size is the size of the sub-array to calculate arithmetic average. The standard for pixels without enough neighbors is to reflect. Let us go its process:

uniform_filter1d([1,2,3,4,5,6], size=3)

[1,2,3,4,5,6] # index 0, Reflect 1 : [1,1,2] -> average: 4/3 = 1
[1,2,3,4,5,6] # index 1, [1,2,3] -> average: 6/3 = 2
[1,2,3,4,5,6] # index 2, [2,3,4] -> average: 9/3 = 3
[1,2,3,4,5,6] # index 3, [3,4,5] -> average: 12/3 = 4
[1,2,3,4,5,6] # index 4, [4,5,6] -> average: 15/3 = 5
[1,2,3,4,5,6] # index 5, Reflect 6 : [5,6,6] -> average: 17/3 = 5

Result: [1,2,3,4,5,5]
11
  • 3
    really great explanation. the documentation for this function is really inadequate and doesn't help. your explanation did the trick for me. Feb 21, 2020 at 12:29
  • 1
    FYI you can turn this code into a super fast running-sum (aka windowed-sum, aka convolve with a rectangle of that has size-n and magnitude 1) by multiplying the input data by size-n! really useful/helpful. Feb 21, 2020 at 12:30
  • 1
    @OzgurBagci uniform_filter1d() does a running mean so you have to adjust for that scaling. and it also has non-intuitive params so for example do (1.) uniform_filter1d(data*size, size=size, mode='constant', cval=0.0, origin=-(size//2) (2.) you can test against np.convolve(data, np.ones(size), mode='valid') Feb 25, 2020 at 20:50
  • 1
    it's an important tool because it is about 20-30x faster than np.convolve() Feb 26, 2020 at 16:20
  • 1
    Tx, Ozgur. You helped with the mining of the algorithm. Never the less is helpful to say that the point being calculated is placed in window/2. The filter (mean of the window) is done half the window to the left and the other half to the right of the point. if you have uniform_filter1d([2, 8, 0, 4, 1, 9, 9, 0], size=4) you operate over [8, 2, 2, 8, 0 4, 1, 9, 0, 0, 9]. Another interesting beaver is with np.nan. If you have uniform_filter1d([2, 8, 0, 4, np.nan, 9, 9, 0], size=4), you will get all nans after you find the nan independently of the np.nan be in the window or not.
    – OLGOW
    May 9, 2022 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.