Skip to content Skip to sidebar Skip to footer

Simple Calculation In Scipy: Minima

import numpy as np from scipy import signal data = np.array([[[*3, 2, 1, np.nan, np.nan], [22, 1, 1, 4, 4], [4, 2, 3, 3, 4], [1, 1, 4, 1,

Solution 1:

NOTE: My python is on the fritz right now, so I haven't been able to test this. Let me know if I've got something wrong.

Your data, when reshaped to (5,25), is all increasing along axis 0. This means that there are NO relative minima per argrelmin(data,axis=0) unless you specify mode='wrap'. In that case, the first element of each vector would be the minimum. However, this is not necessarily true when all elements in a vector are NaNs (which does occur in your dataset) or if the last element is a NaN. Since argrelmin uses np.less for comparison (which returns False for all comparisons with NaNs), I'm guessing you will find no minima in those rows regardless of how you modify the function call (although I haven't tried it).

To summarize: the reason argrelmin returns an empty array is because there are no relative minima along the first axis of your dataset. Also note that you are NOT guaranteed to have 25 minima values in your dataset unless can make some pretty specific assumptions (e.g., data is increasing, using wrap, and no nans).

This is also why you are receiving 27 elements back when applying argrelmin along axis 1 - there are 27 relative minima.

e.g.,

regular:

3., 2., 1., nan, nan, 22., 1., 1., 4., 4., 4., 2., 3., 3., 4., 1., 1., 4., 1., 5., 2., 4., 5., 2., 1.

with wrap:

3., 2., 1., nan, nan, 22., 1., 1., 4., 4., 4., 2., 3., 3., 4., 1., 1., 4., 1., 5., 2., 4., 5., 2., 1.

Note: It looks like you could make your own comparitor to handle NaNs and pass that to argrelextrema, but you'd still have to deal with all NaN cases, etc.

Solution 2:

Did you check what signal.argrelmin() actually returned for your (reshaped) data? You need to choose the axis properly. For a simple example,

data = np.array([[19, 11, 14, 14, 10],[19,21,12,14,11]])
scipy.signal.argrelmin(data)

returns

(array([], dtype=float64), array([], dtype=float64))

which then gives the IndexError you reference.

Post a Comment for "Simple Calculation In Scipy: Minima"