简体   繁体   中英

Scipy: Argrelmax Find Maxmima along Dimension in 3d Array

I want to find relative local maxima in a 3D array (100,1000,1000) along the first dimension. I'm using the argrelmax function from scipy.signal

result = argrelmax(data, axis = 0, order = 20)

I can't really make sense of the output, I expected something like the relative maxima for each 1D slice through my data volume. Instead I get 3 tuples with 1653179 values. How can i relate them back to my original shape?

The return values of argrelmax are the array indices of the relative maxima. For example,

In [47]: np.random.seed(12345)

In [48]: x = np.random.randint(0, 10, size=(10, 3))

In [49]: x
Out[49]: 
array([[2, 5, 1],
       [4, 9, 5],
       [2, 1, 6],
       [1, 9, 7],
       [6, 0, 2],
       [9, 1, 2],
       [6, 7, 7],
       [7, 8, 7],
       [1, 7, 4],
       [0, 3, 5]])

In [50]: i, j = argrelmax(x, axis=0)

In [51]: i
Out[51]: array([1, 1, 3, 3, 5, 7, 7])

In [52]: j
Out[52]: array([0, 1, 1, 2, 0, 0, 1])

i contains the rows and j contains the columns of the relative maxima. Eg x[1, 0] holds the value 4 , which is a relative maximum in the first column, and x[1, 1] holds the value 9 , which is a relative maximum in the second column.

To process the local maxima column by column, you could do something like this:

In [56]: for col in range(x.shape[1]):
   ....:     mask = j == col
   ....:     print("Column:", col, " Position of local max:", i[mask])
   ....:     
Column: 0  Position of local max: [1 5 7]
Column: 1  Position of local max: [1 3 7]
Column: 2  Position of local max: [3]

The same applies to your 3D array. The following uses a much smaller 3D array as an example:

In [73]: np.random.seed(12345)

In [74]: data = np.random.randint(0, 10, size=(10, 3, 2))

In [75]: i, j, k = argrelmax(data, axis=0)

To get the positions of the relative maxima in the slice data[:, 0, 0] , you could do:

In [76]: mask00 = (j == 0) & (k == 0)

In [77]: i[mask00]
Out[77]: array([5, 8])

Check that those are the indices of the local maxima:

In [78]: data[:, 0, 0]
Out[78]: array([2, 2, 6, 6, 1, 7, 3, 0, 8, 7])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM