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

BUG: ZeroDivision Error in peak_finding_utils.pyx #20720

Open
Jerry-Ma opened this issue May 16, 2024 · 0 comments · May be fixed by #20721
Open

BUG: ZeroDivision Error in peak_finding_utils.pyx #20720

Jerry-Ma opened this issue May 16, 2024 · 0 comments · May be fixed by #20721
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.signal

Comments

@Jerry-Ma
Copy link

Describe your issue.

The peak_widths function encounters ZeroDivisionError when trying to interpolate the peak half max indices.

Upon examining the code, the fix should be rather simple:

diff --git a/scipy/signal/_peak_finding_utils.pyx b/scipy/signal/_peak_finding_utils.pyx
index 997272cc6..ddbb7e3d7 100644
--- a/scipy/signal/_peak_finding_utils.pyx
+++ b/scipy/signal/_peak_finding_utils.pyx
@@ -352,7 +352,8 @@ def _peak_widths(const np.float64_t[::1] x not None,
             left_ip = <np.float64_t>i
             if x[i] < height:
                 # Interpolate if true intersection height is between samples
-                left_ip += (height - x[i]) / (x[i + 1] - x[i])
+                if x[i + 1] != x[i]:
+                    left_ip += (height - x[i]) / (x[i + 1] - x[i])

             # Find intersection point on right side
             i = peak
@@ -361,7 +362,8 @@ def _peak_widths(const np.float64_t[::1] x not None,
             right_ip = <np.float64_t>i
             if  x[i] < height:
                 # Interpolate if true intersection height is between samples
-                right_ip -= (height - x[i]) / (x[i - 1] - x[i])
+                if x[i + 1] != x[i]:
+                    right_ip -= (height - x[i]) / (x[i - 1] - x[i])

             widths[p] = right_ip - left_ip
             if widths[p] == 0:

I can prepare a pull request if this all make sense. Thank you!

Reproducing Code Example

This only happens when the half max index has the same value as the next one in the data. I'll add an example later.

Error message

 
    return _peak_widths(x, peaks, rel_height, *prominence_data)
           │            │  │      │            └ (<Column name='height' dtype='float64' length=484>
           │            │  │      │              14.042502665214634
           │            │  │      │              43.689094482618046
           │            │  │      │              15.566300369975462
           │            │  │      │              29.13583374295080...
           │            │  │      └ 0.5
           │            │  └ array([   172,   2668,   5952,   6550,   6675,   6736,   8486,   9324,
           │            │            11327,  13169,  13830,  14804,  16110,  16784,...
           │            └ array([0., 0., 0., ..., 0., 0., 0.])
           └ <built-in function _peak_widths>
  File "_peak_finding_utils.pyx", line 355, in scipy.signal._peak_finding_utils._peak_widths
ZeroDivisionError: float division
``` ```
    return _peak_widths(x, peaks, rel_height, *prominence_data)
           │            │  │      │            └ (<Column name='height' dtype='float64' length=484>
           │            │  │      │              14.042502665214634
           │            │  │      │              43.689094482618046
           │            │  │      │              15.566300369975462
           │            │  │      │              29.13583374295080...
           │            │  │      └ 0.5
           │            │  └ array([   172,   2668,   5952,   6550,   6675,   6736,   8486,   9324,
           │            │            11327,  13169,  13830,  14804,  16110,  16784,...
           │            └ array([0., 0., 0., ..., 0., 0., 0.])
           └ <built-in function _peak_widths>
  File "_peak_finding_utils.pyx", line 355, in scipy.signal._peak_finding_utils._peak_widths
ZeroDivisionError: float division


### SciPy/NumPy/Python version and system information

```shell
/bin/sh: 1: /tmp/pip-build-env-wyk103a3/overlay/bin/meson: not found
ninja: error: rebuilding 'build.ninja': subcommand failed
1.14.0.dev0+0.f2d4775 1.26.4 sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/include/x86_64-linux-gnu/openblas-pthread/
    lib directory: /usr/lib/x86_64-linux-gnu/openblas-pthread/
    name: openblas
    openblas configuration: USE_64BITINT= DYNAMIC_ARCH=1 DYNAMIC_OLDER=1 NO_CBLAS=
      NO_LAPACK= NO_LAPACKE=1 NO_AFFINITY=1 USE_OPENMP=0 generic MAX_THREADS=64
    pc file directory: /usr/lib/x86_64-linux-gnu/pkgconfig
    version: 0.3.20
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/include/x86_64-linux-gnu/openblas-pthread/
    lib directory: /usr/lib/x86_64-linux-gnu/openblas-pthread/
    name: openblas
    openblas configuration: USE_64BITINT= DYNAMIC_ARCH=1 DYNAMIC_OLDER=1 NO_CBLAS=
      NO_LAPACK= NO_LAPACKE=1 NO_AFFINITY=1 USE_OPENMP=0 generic MAX_THREADS=64
    pc file directory: /usr/lib/x86_64-linux-gnu/pkgconfig
    version: 0.3.20
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.12.0
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 11.4.0
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 11.4.0
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.10
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 11.4.0
  pythran:
    include directory: ../../../../../../../../tmp/pip-build-env-wyk103a3/overlay/lib/python3.10/site-packages/pythran
    version: 0.15.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /home/ma/.pyenv/versions/3.10.6_sys/envs/tolteca_v2/bin/python3.10
  version: '3.10'
@Jerry-Ma Jerry-Ma added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label May 16, 2024
@Jerry-Ma Jerry-Ma linked a pull request May 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.signal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants