diff --git a/glue/sample/src/sinter/_probability_util.py b/glue/sample/src/sinter/_probability_util.py index 116f09759..f6d07fc80 100644 --- a/glue/sample/src/sinter/_probability_util.py +++ b/glue/sample/src/sinter/_probability_util.py @@ -130,9 +130,16 @@ def binary_intercept(*, func: Callable[[float], float], start_x: float, step: fl start_y = func(start_x) if abs(start_y - target_y) <= atol: return start_x - while (func(start_x + step) >= target_y) == (start_y >= target_y): + step = float(step) + while True: + try: + cond = (func(start_x + step) >= target_y) == (start_y >= target_y) + except OverflowError: + raise ValueError("Failed.") + if not cond: + break step *= 2 - if np.isinf(step) or step == 0: + if math.isinf(step) or step == 0: raise ValueError("Failed.") xs = [start_x, start_x + step] min_x = min(xs) @@ -252,6 +259,8 @@ def fit_line_y_at_x(*, ... ) sinter.Fit(low=14.47247314453125, best=16.0, high=17.52752685546875) """ + if max_extra_squared_error < 0: + raise ValueError(f"max_extra_squared_error={max_extra_squared_error} < 0") # Local import to reduce initial cost of importing sinter. from scipy.stats import linregress @@ -307,6 +316,8 @@ def fit_line_slope(*, ... ) sinter.Fit(low=1.2928924560546875, best=2.0, high=2.7071075439453125) """ + if max_extra_squared_error < 0: + raise ValueError(f"max_extra_squared_error={max_extra_squared_error} < 0") # Local import to reduce initial cost of importing sinter. from scipy.stats import linregress diff --git a/glue/sample/src/sinter/_probability_util_test.py b/glue/sample/src/sinter/_probability_util_test.py index 2afe4f6e6..a6fcac28c 100644 --- a/glue/sample/src/sinter/_probability_util_test.py +++ b/glue/sample/src/sinter/_probability_util_test.py @@ -98,6 +98,10 @@ def test_binary_intercept(): t = binary_intercept(func=lambda x: -x**2, start_x=0, step=-1, target_y=-82.3, atol=0.2) assert t < 0 and abs(t**2 - 82.3) <= 0.2 + with pytest.raises(ValueError, match="Failed"): + binary_intercept(func=lambda x: x**2, start_x=5, step=1, target_y=-10, atol=0.01) + with pytest.raises(ValueError, match="Failed"): + binary_intercept(func=lambda x: -x**2, start_x=5, step=1, target_y=10, atol=0.01) def test_fit_y_at_x(): fit = fit_line_y_at_x( @@ -118,6 +122,11 @@ def test_fit_slope(): np.testing.assert_allclose(fit.best, 4) assert 3 < fit.low < 3.5 < fit.best < 4.5 < fit.high < 5 + with pytest.raises(ValueError, match='max_extra_squared_error'): + fit_line_y_at_x(xs=[1, 2, 3], ys=[1, 5, 9], target_x=100, max_extra_squared_error=-1) + with pytest.raises(ValueError, match='max_extra_squared_error'): + fit_line_slope(xs=[1, 2, 3], ys=[1, 5, 9], max_extra_squared_error=-1) + def test_fit_binomial_shrink_towards_half(): with pytest.raises(ValueError, match='max_likelihood_factor'):