What's happening
The gabor and gabor_kernel functions crash with a
ZeroDivisionError when bandwidth=0 is passed, instead of a clear
error message. The docstring doesn't say anywhere that bandwidth
has to be a positive number.
How to reproduce it
import numpy as np
from skimage.filters import gabor
image = np.zeros((10, 10))
gabor(image, frequency=0.1, bandwidth=0)
What actually happens
Why it fails
- Internally,
gabor calls gabor_kernel, which calls a helper
function _sigma_prefactor(bandwidth)
- That helper computes:
1.0 / np.pi * sqrt(log(2) / 2.0) * (2.0**b + 1) / (2.0**b - 1)
- When
bandwidth is 0, 2.0**0 equals 1, which makes the bottom
part of that formula, (2.0**b - 1), come out to exactly 0
- This isn't a rare floating-point rounding issue — it's a guaranteed,
exact division by zero for this one input
- Nowhere in
gabor, gabor_kernel, or _sigma_prefactor is there
any check that stops bandwidth=0 from reaching this line
Evidence this is a real, reproducible bug
- Reproduced it myself with the minimal script above — the traceback
clearly shows the crash happening through gabor → gabor_kernel
→ _sigma_prefactor
- None of the existing doctest examples use
bandwidth=0 — they only
use the default (1) or 0.1
- None of the existing tests cover it either — the test for bandwidth
only checks 1 and 0.5
- The docstring for the
bandwidth parameter never mentions that it
needs to be strictly positive
Suggested fix
Add a simple validation check near the start of gabor_kernel (which
gabor also goes through), something like:
if bandwidth <= 0:
raise ValueError('bandwidth must be > 0')
Version info
scikit-image version: 0.25.2
numpy version: 2.1.3
Platform: Linux-6.6.122+-x86_64-with-glibc2.35
What's happening
The
gaborandgabor_kernelfunctions crash with aZeroDivisionErrorwhenbandwidth=0is passed, instead of a clearerror message. The docstring doesn't say anywhere that
bandwidthhas to be a positive number.
How to reproduce it
What actually happens
Why it fails
gaborcallsgabor_kernel, which calls a helperfunction
_sigma_prefactor(bandwidth)1.0 / np.pi * sqrt(log(2) / 2.0) * (2.0**b + 1) / (2.0**b - 1)bandwidthis0,2.0**0equals1, which makes the bottompart of that formula,
(2.0**b - 1), come out to exactly0exact division by zero for this one input
gabor,gabor_kernel, or_sigma_prefactoris thereany check that stops
bandwidth=0from reaching this lineEvidence this is a real, reproducible bug
clearly shows the crash happening through
gabor→gabor_kernel→
_sigma_prefactorbandwidth=0— they onlyuse the default (
1) or0.1only checks
1and0.5bandwidthparameter never mentions that itneeds to be strictly positive
Suggested fix
Add a simple validation check near the start of
gabor_kernel(whichgaboralso goes through), something like:Version info
scikit-image version: 0.25.2
numpy version: 2.1.3
Platform: Linux-6.6.122+-x86_64-with-glibc2.35