0

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,

  • What does origin, cval, mode mean in the maximum_filter1d parameter?
  • How does the maximum_filter1d work?

If possible, provide an explanation with an example!

Thanks in advance!

Have a great day!

Sanjay S
  • 13
  • 3
  • The example looks it uses a window centered on the element. The first and last are truncated, (2,8) and (0,1) – hpaulj Mar 25 '21 at 19:57
  • @hpaulj not exactly! It's reflecting the inside value. For example at the beginning (2,8) reflected to (8,2) and as the window is 3, max(8,2,8) = 8, at the end (0,1) reflected to (1,0). Therefore max(0,1,0) = 1. I'll post it in the answer with better explanation – Shaan Mar 25 '21 at 20:05

1 Answers1

0

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.

Shaan
  • 71
  • 6
  • How does the filter position, when the window size is even? @Shaan – Sanjay S Mar 26 '21 at 06:41
  • Also how about when the window_size is greater than the matrix_size?@Shaan – Sanjay S Mar 26 '21 at 08:29
  • For both your questions, it has one answer. The filter by default (origin=0) is starts always with the center value. For ex: window_size=4, at the first window. It will keep 2 elements outside and 2 elements inside for comparison. In this case, it will also reflect the first element of the input array. Then it moves by one step. The 2nd window now has 1 element outside and 3 elements inside. As I mentioned it to you, you need to try out different cases if you want to know how it works even more. The key to understanding is just trying out possible cases and getting your hands dirty to do so. – Shaan Mar 26 '21 at 09:25
  • Ahh okay!, Thanks, @Shaan! – Sanjay S Mar 29 '21 at 08:48