question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FloatingPointError: underflow encountered in square

See original GitHub issue

It only happens in some audios, but it occurs frequently. And it happens not only in just one function, but many functions like librosa.beat.beat_track() and librosa.feature.mfcc(). Below is an example of librosa.beat.beat_track().

---> 14 tempo, base_beats = librosa.beat.beat_track(y=base, sr=44100)
File \anaconda\lib\site-packages\librosa\util\decorators.py:88, in deprecate_positional_args.<locals>._inner_deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     86 extra_args = len(args) - len(all_args)
     87 if extra_args <= 0:
---> 88     return f(*args, **kwargs)
     90 # extra_args > 0
     91 args_msg = [
     92     "{}={}".format(name, arg)
     93     for name, arg in zip(kwonly_args[:extra_args], args[-extra_args:])
     94 ]

File \anaconda\lib\site-packages\librosa\beat.py:181, in beat_track(y, sr, onset_envelope, hop_length, start_bpm, tightness, trim, bpm, prior, units)
    172     bpm = tempo(
    173         onset_envelope=onset_envelope,
    174         sr=sr,
   (...)
    177         prior=prior,
    178     )[0]
    180 # Then, run the tracker
--> 181 beats = __beat_tracker(onset_envelope, bpm, float(sr) / hop_length, tightness, trim)
    183 if units == "frames":
    184     pass

File \anaconda\lib\site-packages\librosa\beat.py:599, in __beat_tracker(onset_envelope, bpm, fft_res, tightness, trim)
    596 beats = np.array(beats[::-1], dtype=int)
    598 # Discard spurious trailing beats
--> 599 beats = __trim_beats(localscore, beats, trim)
    601 return beats

File \anaconda\lib\site-packages\librosa\beat.py:682, in __trim_beats(localscore, beats, trim)
    679 smooth_boe = scipy.signal.convolve(localscore[beats], scipy.signal.hann(5), "same")
    681 if trim:
--> 682     threshold = 0.5 * ((smooth_boe ** 2).mean() ** 0.5)
    683 else:
    684     threshold = 0.0

FloatingPointError: underflow encountered in square

version: 0.9.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
weWillGetTherecommented, May 22, 2022

I’ve been unable to replicate this so far. I’m attempting the following:

import numpy as np
x = np.float32(1)
y = x.copy()
for n in range(0, -2048, -1):
    y = y / 2
    print(f"{n:4}\t{y:g}\t{y**2:g}")

which is designed to underflow float32 precision. It does not ever trigger the exception above. Here’s my configuration:

In [11]: librosa.show_versions()
INSTALLED VERSIONS
------------------
python: 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:25:59) 
[GCC 10.3.0]

librosa: 0.9.1

audioread: 2.1.9
numpy: 1.21.6
scipy: 1.8.0
sklearn: 1.1.0
joblib: 1.1.0
decorator: 5.1.1
soundfile: 0.10.3
resampy: 0.2.2
numba: 0.55.1
pooch: v1.6.0
packaging: 21.3

numpydoc: 1.2.1
sphinx: 4.5.0
sphinx_rtd_theme: 1.0.0
sphinx_multiversion: 0.2.4
sphinx_gallery: 0.10.1
mir_eval: 0.6
ipython: None
sphinxcontrib-svg2pdfconverter: None
pytest: 7.1.2
pytest-mpl: None
pytest-cov: None
matplotlib: 3.5.2
samplerate: 0.1.0
soxr: 0.2.3
contextlib2: installed, no version number available
presets: 0.1.3
In [9]: np.show_config()
blas_info:
    libraries = ['cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    include_dirs = ['/home/bmcfee/miniconda/envs/py39/include']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
    libraries = ['cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    include_dirs = ['/home/bmcfee/miniconda/envs/py39/include']
    language = c
lapack_info:
    libraries = ['lapack', 'blas', 'lapack', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    language = f77
lapack_opt_info:
    libraries = ['lapack', 'blas', 'lapack', 'blas', 'cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    language = c
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/bmcfee/miniconda/envs/py39/include']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3
    found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL

@weWillGetThere one thing that might be relevant here is the numpy error configuration. Can you run in your environment and report the result? For my installation, I have:np.geterr()

In [12]: np.geterr()
Out[12]: {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}

My guess is that your configuration might have . If my guess is correct, then there’s nothing really for us to do here. The underflow is expected in this case, and ought to be harmless, so you could (temporarily) do (or warn, or whatever) to avoid this issue.'under': 'raise'``np.seterr(under='ignore')

I’ve changed my numpy configuration and now it works. Thank you!

1reaction
bmcfeecommented, May 21, 2022

I’ve been unable to replicate this so far. I’m attempting the following:

import numpy as np
x = np.float32(1)
y = x.copy()
for n in range(0, -2048, -1):
    y = y / 2
    print(f"{n:4}\t{y:g}\t{y**2:g}")

which is designed to underflow float32 precision. It does not ever trigger the exception above. Here’s my configuration:

In [11]: librosa.show_versions()
INSTALLED VERSIONS
------------------
python: 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:25:59) 
[GCC 10.3.0]

librosa: 0.9.1

audioread: 2.1.9
numpy: 1.21.6
scipy: 1.8.0
sklearn: 1.1.0
joblib: 1.1.0
decorator: 5.1.1
soundfile: 0.10.3
resampy: 0.2.2
numba: 0.55.1
pooch: v1.6.0
packaging: 21.3

numpydoc: 1.2.1
sphinx: 4.5.0
sphinx_rtd_theme: 1.0.0
sphinx_multiversion: 0.2.4
sphinx_gallery: 0.10.1
mir_eval: 0.6
ipython: None
sphinxcontrib-svg2pdfconverter: None
pytest: 7.1.2
pytest-mpl: None
pytest-cov: None
matplotlib: 3.5.2
samplerate: 0.1.0
soxr: 0.2.3
contextlib2: installed, no version number available
presets: 0.1.3
In [9]: np.show_config()
blas_info:
    libraries = ['cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    include_dirs = ['/home/bmcfee/miniconda/envs/py39/include']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
    libraries = ['cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    include_dirs = ['/home/bmcfee/miniconda/envs/py39/include']
    language = c
lapack_info:
    libraries = ['lapack', 'blas', 'lapack', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    language = f77
lapack_opt_info:
    libraries = ['lapack', 'blas', 'lapack', 'blas', 'cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/bmcfee/miniconda/envs/py39/lib']
    language = c
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/bmcfee/miniconda/envs/py39/include']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3
    found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL

@weWillGetThere one thing that might be relevant here is the numpy error configuration. Can you run np.geterr() in your environment and report the result? For my installation, I have:

In [12]: np.geterr()
Out[12]: {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}

My guess is that your configuration might have 'under': 'raise'. If my guess is correct, then there’s nothing really for us to do here. The underflow is expected in this case, and ought to be harmless, so you could (temporarily) do np.seterr(under='ignore') (or warn, or whatever) to avoid this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy error: underflow encountered in exp - Stack Overflow
FloatingPointError : underflow encountered in exp. The array I'm trying to define is as follows: time = np.arange(length) window ...
Read more >
What is the significance of underflow during parameter update ...
Underflow is happening because the result of that calculation is a number of smaller absolute value than your computer can actually ...
Read more >
Log Scale: FloatingPointError: underflow encountered in ...
When plotting data beforehand, everything works fine. This is an issue for me, because I'm trying to plot multiple subplots with shared y...
Read more >
numpy.seterr — NumPy v1.25.dev0 Manual
Underflow: result so close to zero that some precision was lost. ... line 1, in <module> FloatingPointError: overflow encountered in scalar multiply.
Read more >
How to Fix: RuntimeWarning: Overflow encountered in exp
This warning occurs while using the NumPy library's exp() function upon using on a value that is too large.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found