librosa / librosa

Python library for audio and music analysis
https://librosa.org/
ISC License
7.01k stars 956 forks source link

ValueError: zero-size array to reduction operation maximum which has no identity #931

Closed ashishpatel26 closed 5 years ago

ashishpatel26 commented 5 years ago

We have librosa version 0.6.3

ValueError                                Traceback (most recent call last)
 in ()
      1 idx = slice(*librosa.time_to_frames([30, 35], sr=sr))
      2 plt.figure(figsize=(12, 4))
----> 3 librosa.display.specshow(librosa.amplitude_to_db(S_full[:, idx], ref=np.max),
      4                          y_axis='log', x_axis='time', sr=sr)
      5 plt.colorbar()

2 frames
/usr/local/lib/python3.6/dist-packages/librosa/core/spectrum.py in amplitude_to_db(S, ref, amin, top_db)
    989     if six.callable(ref):
    990         # User supplied a function to calculate reference power
--> 991         ref_value = ref(magnitude)
    992     else:
    993         ref_value = np.abs(ref)

/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial)
   2503     """
   2504     return _wrapreduction(a, np.maximum, 'max', axis, None, out, keepdims=keepdims,
-> 2505                           initial=initial)
   2506 
   2507 

/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     84                 return reduction(axis=axis, out=out, **passkwargs)
     85 
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     87 
     88 

ValueError: zero-size array to reduction operation maximum which has no identity
lostanlen commented 5 years ago

What is S_full[:, idx].shape?

lostanlen commented 5 years ago

I just realized that this piece of code seems to come from our gallery of example scripts: plot_vocal_separation.py

https://github.com/librosa/librosa/blob/master/docs/examples/plot_vocal_separation.py#L45

I installed librosa 0.6.3 from a fresh conda environment and was unable to reproduce the error. Here's my install

Darwin-18.6.0-x86_64-i386-64bit
Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 14:38:56) 
[Clang 4.0.1 (tags/RELEASE_401/final)]
NumPy 1.17.0
SciPy 1.3.0
librosa 0.6.3

So the situation is two-fold: (1) either the error stems from using an old version of numpy, which i doubt;

(2) or the error stems from a modification of the script which causes S_full[:,idx] to be empty.

If i had to take a guess, i would expect that we're in situation (2), not (1).

Interestingly, just by looking at public repositories, i can see that the line

idx = slice(*librosa.time_to_frames([30, 35], sr=sr))

has been copy-pasted onto 30 different other scripts: https://github.com/search?p=1&q=idx+%3D+slice%28%2Alibrosa.time_to_frames%28%5B30%2C+35%5D%2C+sr%3Dsr%29%29&type=Code

TL;DR @ashishpatel26, perhaps you're running this code on an input signal that is less that 35 seconds long? In this case, make sure to adjust the [30, 35] time range accordingly.

lostanlen commented 5 years ago

Without news from OP @ashishpatel26, i'm going to close this issue. Feel free to re-open if necessary. Again, there's a >99% chance that the script above fails because it is run on an input signal that is shorter than 35 seconds. The solution is to adjust the time range.