PIA-Group / BioSPPy

Biosignal Processing in Python
Other
573 stars 274 forks source link

Uncaught ValueError in basic_scr() method #47

Closed Tomatenbiss closed 3 years ago

Tomatenbiss commented 5 years ago

When using the basic_scr() method of the EDA library I faced an uncaught ValueError for some of my signals. The error message is the following:

a = np.array([np.max(signal[i1[i]:i3[i]]) for i in range(li)])
File "/usr/local/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 2320, in amax out=out, **kwargs)
File "/usr/local/lib/python3.6/site-packages/numpy/core/_methods.py", line 26, in _amax return umr_maximum(a, axis, None, out, keepdims)
ValueError: zero-size array to reduction operation maximum which has no identity

I tracked down the error to line 167 that contains

a = np.array([np.max(signal[i1[i]:i3[i]]) for i in range(li)])

More precisely the error occurs when signal[i1[i]:i3[i]] has a length of zero (which occurred a couple of times in almost half of my datasets). I figured this error is thrown when np.max() is called on an empty list. A possible solution would be to replace this line with the following:

a = np.array([np.max(signal[i1[i]:i3[i]]) for i in range(li) if len(signal[i1[i]:i3[i]]) > 0])

While analyzing this issue I recognized that the above list comprehension is supposed to yield the amplitudes of the peaks. But what it actually does is to yield the maximum value of the original signal in the given interval. This could be solved by yielding the difference between min and max value in the given interval. To combine both of my proposals the following line might be used:

a = np.array([np.max(signal[i1[i]:i3[i]]) - np.min(signal[i1[i]:i3[i]]) for i in range(li) if len(signal[i1[i]:i3[i]]) > 0])

afonsocraposo commented 3 years ago

Fixed with commit https://github.com/PIA-Group/BioSPPy/commit/709c6a121cfc3ea2181eb29f7c89049552cb5802.