Skip to content

Commit 50e88af

Browse files
committed
Fix signal.ricker error as part of CW_SSIM
1 parent ee7c7f6 commit 50e88af

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/unraphael/dash/image_clustering.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,34 @@ def calculate_cw_ssim_similarity(i1: np.ndarray, i2: np.ndarray) -> float:
565565
sig1 = i1_gray.flatten()
566566
sig2 = i2_gray.flatten()
567567

568+
# Define custom ricker wavelet function as fallback
569+
def custom_ricker(points, a):
570+
"""
571+
Return a Ricker wavelet (Mexican hat wavelet) of length 'points' with parameter 'a'.
572+
573+
This is a custom implementation that can be used when scipy.signal.ricker
574+
or scipy.signal.windows.ricker are not available.
575+
"""
576+
A = 2 / (np.sqrt(3 * a) * np.pi**0.25)
577+
wsq = a**2
578+
vec = np.arange(0, points) - (points - 1.0) / 2
579+
xsq = vec**2
580+
mod = (1.0 - xsq / wsq)
581+
gauss = np.exp(-xsq / (2 * wsq))
582+
return A * mod * gauss
583+
568584
# Use the correct wavelet function (handle both old and new SciPy versions)
569585
try:
570586
# Try new SciPy version (windows module)
571587
from scipy.signal.windows import ricker
572588
wavelet = ricker
573589
except ImportError:
574-
# Fall back to old SciPy version
575-
wavelet = signal.ricker
590+
try:
591+
# Fall back to old SciPy version
592+
wavelet = signal.ricker
593+
except AttributeError:
594+
# Fall back to custom implementation if neither is available
595+
wavelet = custom_ricker
576596

577597
# Set width parameter for the wavelet transform
578598
widths = np.arange(1, 30)

0 commit comments

Comments
 (0)