Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError (assert linscale >= 1.0) on matplotlib < 1.4 #2

Closed
psycho23 opened this issue May 20, 2017 · 7 comments
Closed

AssertionError (assert linscale >= 1.0) on matplotlib < 1.4 #2

psycho23 opened this issue May 20, 2017 · 7 comments

Comments

@psycho23
Copy link

psycho23 commented May 20, 2017

On Linux Mint 17.3

python2 tryit.py blah.wav 
tryit.py:97: RuntimeWarning: divide by zero encountered in log10
  y = (20.0 * np.log10(y)).clip(-120)
Traceback (most recent call last):
  File "tryit.py", line 145, in <module>
    sys.exit(main(*sys.argv[1:]))
  File "tryit.py", line 124, in main
    plt.yscale('symlog', linthreshy=100, linscaley=0.25)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1522, in yscale
    ax.set_yscale(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2801, in set_yscale
    self.yaxis._set_scale(value, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 693, in _set_scale
    self._scale = mscale.scale_factory(value, self, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/scale.py", line 504, in scale_factory
    return _scale_mapping[scale](axis, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/scale.py", line 449, in \__init __
    assert linscale >= 1.0
AssertionError
python3 tryit.py blah.wav 
tryit.py:97: RuntimeWarning: divide by zero encountered in log10
  y = (20.0 * np.log10(y)).clip(-120)
Traceback (most recent call last):
  File "tryit.py", line 145, in <module>
    sys.exit(main(*sys.argv[1:]))
  File "tryit.py", line 103, in main
    X = X[window_size / overlap:]
TypeError: slice indices must be integers or None or have an \__index__ method
file blah.wav 
blah.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 22050 Hz
@tmick0
Copy link
Owner

tmick0 commented May 20, 2017

It appears that the reason for the crash is not the division by zero. Numpy will actually give a result of -inf, which is then 'fixed' by the call to clip. For example:

>>> x = np.array([0, 0, 0])
>>> y = np.log10(x)
__main__:1: RuntimeWarning: divide by zero encountered in log10
>>> y
array([-inf, -inf, -inf])
>>> y.clip(-120)
array([-120., -120., -120.])

The crashes you are seeing are actually related to different errors: In the Python 2 case, it appears to be an assertion error related to plot scaling, and in the Python 3 case it is due to the altered behavior of division from Python 2.

I believe I have fixed the error related to Python 3 in commit 57d6730. I am unable to replicate the other error with any wav file I have on hand. Please send the clip you are using as to assist me in debugging.

@tmick0
Copy link
Owner

tmick0 commented May 20, 2017

Since the first error you observed appears to be related to the use of the symlog scale, you may want to see if it persists when you choose linear scaling:

python spectrogram.py input.wav 1024 linear

The ability to choice between linear/log is added in commit a1f6434.

@psycho23
Copy link
Author

Just tried the latest master.

Works with both versions of python (2 and 3):
python spectrogram.py input.wav 1024 linear
Fails with both versions of python (2 and 3):
python spectrogram.py input.wav

$python2 spectrogram.py blah.wav
spectrogram.py:73: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)
Traceback (most recent call last):
File "spectrogram.py", line 110, in
sys.exit(main(*sys.argv[1:]))
File "spectrogram.py", line 89, in main
plt.yscale('symlog', linthreshy=100, linscaley=0.25)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1522, in yscale
ax.set_yscale(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2801, in set_yscale
self.yaxis._set_scale(value, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 693, in _set_scale
self._scale = mscale.scale_factory(value, self, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/scale.py", line 504, in scale_factory
return _scale_mapping[scale](axis, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/scale.py", line 449, in init
assert linscale >= 1.0
AssertionError

#SUCCESS.
$python2 spectrogram.py blah.wav 1024 linear
spectrogram.py:73: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)

$python3 spectrogram.py blah.wav
spectrogram.py:73: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)
Traceback (most recent call last):
File "spectrogram.py", line 110, in
sys.exit(main(*sys.argv[1:]))
File "spectrogram.py", line 89, in main
plt.yscale('symlog', linthreshy=100, linscaley=0.25)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 1522, in yscale
ax.set_yscale(*args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 2803, in set_yscale
self.yaxis._set_scale(value, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 694, in _set_scale
self._scale = mscale.scale_factory(value, self, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/scale.py", line 504, in scale_factory
return _scale_mapping[scale](axis, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/scale.py", line 449, in init
assert linscale >= 1.0
AssertionError

#SUCCESS
$python3 spectrogram.py blah.wav 1024 linear
spectrogram.py:73: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)

python2 --version
Python 2.7.6
python3 --version
Python 3.4.3
dpkg -l | grep matplotlib
ii python-matplotlib 1.3.1-1ubuntu5 amd64 Python based plotting system in a style similar to Matlab
ii python-matplotlib-data 1.3.1-1ubuntu5 all Python based plotting system (data package)
ii python3-matplotlib 1.3.1-1ubuntu5 amd64 Python based plotting system in a style similar to Matlab (Python 3)
dpkg -l | grep numpy
ii python-numpy 1:1.8.2-0ubuntu0.1 amd64 Numerical Python adds a fast array facility to the Python language
ii python3-numpy 1:1.8.2-0ubuntu0.1 amd64 Fast array facility to the Python 3 language

I'll find a way to get you blah.wav
I don't want to post here publicly because it's from Diablo II and therefore Copyrighted (the wav was directly ripped from d2sfx.mpq, which is in the Diablo II install CD). I'm 94.2% sure the sound was created in 1998 with Soundforge as stated in one of the sound files (.wav) I extracted from one of the Diablo II mpq files that I saw in some program somewhere.

@tmick0
Copy link
Owner

tmick0 commented May 23, 2017

I think I figured it out... I had purposely set linscaley=0.25 to reduce the displayed size of the linearly-scaled portion of the spectrum, however this is only allowed since matplotlib 1.4. You appear to be on 1.3, and I was testing with 1.5. It is not related to the specific input file you are using. You can set linscaley=1 on spectrogram.py:89 if you need compatibility with old matplotlib.

Refer to matplotlib/matplotlib#2288

@psycho23
Copy link
Author

Fixed!

python3 spectrogram.py blah.wav
spectrogram.py:73: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)
python2 spectrogram.py blah.wav
spectrogram.py:73: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)
diff spectrogram*
89c89
< plt.yscale('symlog', linthreshy=100, linscaley=1)
---
> plt.yscale('symlog', linthreshy=100, linscaley=0.25)

Is it best to refer to people that do have < matplotlib 1.4 to just come here (ie. this solution post #2) for the solution?
And you have my permission to rename this spectrogram's issue's name, which is also the beginning of the title of this page. The "divide by zero" thrown RuntimeWarning is still persistent, but the real problem was the crash caused by using an older python2 and python3 matplotlib 1.3.* library/module/package/a-bunch-of-python-code/ASCII-files/filesystem-malaygna/obligata/sanka/demois.

@tmick0 tmick0 changed the title Divide by zero. AssertionError (assert linscale >= 1.0) on matplotlib < 1.4 May 23, 2017
@tmick0
Copy link
Owner

tmick0 commented May 23, 2017

I can probably instrument the script to detect old matplotlib and set an appropriate linscaley (and probably throw a warning) in that case

@psycho23
Copy link
Author

psycho23 commented May 23, 2017

I detect there is a 13.89% this will help you @le1ca

$python2 spectrogram_possible.py blah.wav
spectrogram_fixed.py:74: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)
spectrogram_fixed.py:94: UserWarning: You are using matplotlib 1.3.* (and not >= 1.4.0). Therefore linscaley must equal 1, not 0.25
warnings.warn('You are using matplotlib 1.3.* (and not >= 1.4.0). Therefore linscaley must equal 1, not 0.25')

$python3 spectrogram_possible.py blah.wav
spectrogram_fixed.py:74: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)
spectrogram_fixed.py:94: UserWarning: You are using matplotlib 1.3.* (and not >= 1.4.0). Therefore linscaley must equal 1, not 0.25
warnings.warn('You are using matplotlib 1.3.* (and not >= 1.4.0). Therefore linscaley must equal 1, not 0.25')

$python2 spectrogram_possible.py blah.wav 1024 linear
spectrogram_fixed.py:74: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)

$python3 spectrogram_possible.py blah.wav 1024 linear
spectrogram_fixed.py:74: RuntimeWarning: divide by zero encountered in log10
Y = (20.0 * np.log10(Y)).clip(-120)

diff spectrogram.py spectrogram_possible.py
https://gist.github.com/psycho23/8d495f8b31c05fec6c0cbb6e5c5c40fb

tmick0 added a commit that referenced this issue Jul 8, 2017
@tmick0 tmick0 closed this as completed Jul 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants