Documentation link: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.maximum_filter1d.html#r4df4f294a847-2
The example given in the documentation is,
>>> maximum_filter1d([2, 8, 0, 4, 1, 9, 9, 0,1], size=3)
array([8, 8, 8, 4, 9, 9, 9, 9, 1])
My understanding of the function is that it performs a sliding window over the element with the window size of k [ For the given example it's 3]
Which implies,
1st window = max[2,8,0] => 8
2nd window = max[8,0,4] => 8
3th window = max[0,4,1] => 4
4th window = max[4,1,9] => 9
5th window = max[1,9,9] => 9
6th window = max[9,9,0] => 9
7th window = max[9,0,1] => 9
8th window = max[0,1,2] => 0 [Not sure, how to group element on edges, but from the documentation, it uses "reflect" mode by default, which I'm not sure how it changes the original array]
To sum up the questions,
maximum_filter1d
parameter?maximum_filter1d
work?If possible, provide an explanation with an example!
Thanks in advance!
Have a great day!
BOUNDARY CONDITION
Let us take this example to elaborate the boundary condition:
>>> input_arr = [1,0,6,1,9]
>>> output_arr = maximum_filter1d(input_array, size=3)
array([1, 6, 6, 9, 9])
What you explained earlier is correct. However, you were unsure about the boundary. It does something very simple called as 'reflect'. Let me explain that:
Let's assume the filter is at the start, i.e., the center of the filter window is the first element in the input array. So therefore it is input_arr[0] = 1
. Now we have the left boundary, which means there is nothing to the left of input_arr[0]
. However, the window is of size = 3. Therefore we have one value to the right of 1
, which is 0
.
Let's call this array([1,0])
. Now reflecting this along the value 1
is
reflected_array = array([0,1])
. However, the window is of size 3, Therefore the filter would make it array([0,1,0])
. Therefore, actually the first filter window would be
1st window = max[0,1,0] => 1
Similarly, such reflection would work for the last window. In this case,
Last window = max[1,9,1] => 9
The remaining windows you already know, how it works.
ORIGIN CONDITION
Let's take the same example. We know that the default value of origin is 0 which would result in the same as before:
>>> input_arr = [1,0,6,1,9]
>>> output_arr = maximum_filter1d(input_array, size=3, origin=0)
array([1, 6, 6, 9, 9])
In our case, origin can take three possible values because the filter size is 3. The known default case for origin=0
works as I explained above. The other possible values are origin=-1
, which shifts the window to the right and origin=1
, which shifts the window to left.
Let's look at that with example with a new input array (changed for clarity) for the origin=-1
case.
>>> input_arr = [1,0,2,6,9]
>>> output_arr = maximum_filter1d(input_array, size=3, origin=-1)
array([2, 6, 9, 9, 9])
In this case, the boundary is reflected with input_arr[1] = 0
. i.e., array([0,2])
reflects to array([2,0])
. Therefore, the 2 goes before the first window.
1st window = max[2,1,0] => 2 for window centre at input_arr[0] = 1
2nd window = max[0,2,6] => 6 for window centre at input_arr[1] = 0
Take a note that in the 2nd window, the center of the input array shifted by 1 to the right.
A similar logic can be thought of for origin=1
REMAINING
cval : This just allows you to fill the value before the beginning. In this case it will not use, this special boundary condition. You can put any value and your filter will compare it with that for the boundaries.
mode: We saw that the default is 'reflect'. You can change this boundary condition by changing the mode.
SUGGESTION
Try out different cases for different scenarios yourself to consolidate this concept further. I tried my best to explain as much as I can. I think you will learn more when you try out more cases yourself and play around with values.