diff --git a/src/pyrecest/smoothers/sliding_window_manifold_mean_smoother.py b/src/pyrecest/smoothers/sliding_window_manifold_mean_smoother.py index 1ad9c2a946..cefaf149d0 100644 --- a/src/pyrecest/smoothers/sliding_window_manifold_mean_smoother.py +++ b/src/pyrecest/smoothers/sliding_window_manifold_mean_smoother.py @@ -6,8 +6,9 @@ from functools import partial from operator import index as operator_index +from pyrecest.backend import all as backend_all from pyrecest.backend import any as backend_any -from pyrecest.backend import asarray, concatenate, ndim, stack, sum +from pyrecest.backend import asarray, concatenate, isfinite, ndim, stack, sum from pyrecest.distributions import ( AbstractHypercylindricalDistribution, AbstractHyperhemisphericalDistribution, @@ -92,6 +93,8 @@ def __init__( self.window_weights = asarray(window_weights).reshape(-1) if self.window_weights.shape[0] != self.window_size: raise ValueError("window_weights must have length window_size.") + if not bool(backend_all(isfinite(self.window_weights))): + raise ValueError("window_weights must be finite.") if backend_any(self.window_weights < 0): raise ValueError("window_weights must be non-negative.") if sum(self.window_weights) <= 0: diff --git a/tests/smoothers/test_sliding_window_manifold_mean_smoother_nonfinite_weights.py b/tests/smoothers/test_sliding_window_manifold_mean_smoother_nonfinite_weights.py new file mode 100644 index 0000000000..3be7fc452e --- /dev/null +++ b/tests/smoothers/test_sliding_window_manifold_mean_smoother_nonfinite_weights.py @@ -0,0 +1,26 @@ +import unittest + +import numpy as np +from pyrecest.backend import array +from pyrecest.smoothers import SlidingWindowManifoldMeanSmoother + + +class SlidingWindowManifoldMeanSmootherNonfiniteWeightsTest(unittest.TestCase): + def test_rejects_nonfinite_window_weights(self): + invalid_weight_vectors = ( + array([1.0, np.nan, 1.0]), + array([1.0, np.inf, 1.0]), + array([1.0, -np.inf, 1.0]), + ) + + for window_weights in invalid_weight_vectors: + with self.subTest(window_weights=window_weights): + with self.assertRaisesRegex(ValueError, "finite"): + SlidingWindowManifoldMeanSmoother( + window_size=3, + window_weights=window_weights, + ) + + +if __name__ == "__main__": + unittest.main()