|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see plotpy/LICENSE for details) |
| 5 | + |
| 6 | +"""Curve fitting parameter sliders: values outside the slider range, non-finite |
| 7 | +values and invalid inputs keep the fit dialog usable.""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import pytest |
| 13 | +from guidata.qthelpers import qt_app_context |
| 14 | + |
| 15 | +from plotpy.widgets.fit import FitDialog, FitParam, FitParamDataSet |
| 16 | + |
| 17 | + |
| 18 | +def _param( |
| 19 | + value: float = 0.5, vmin: float = 0.0, vmax: float = 1.0, logscale: bool = False |
| 20 | +) -> FitParam: |
| 21 | + """Return a fit parameter with its widgets, updated for the given value""" |
| 22 | + param = FitParam("p", 0.5, 0.0, 1.0, logscale=logscale) |
| 23 | + param.create_widgets(None, lambda: None) |
| 24 | + param.min, param.max, param.value = vmin, vmax, value |
| 25 | + param.update() |
| 26 | + return param |
| 27 | + |
| 28 | + |
| 29 | +def _has_range_warning(param: FitParam) -> bool: |
| 30 | + """Return True if the parameter shows the out-of-range warning""" |
| 31 | + return param.range_warning.isVisible() and bool(param.lineedit.toolTip()) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("logscale", [False, True]) |
| 35 | +@pytest.mark.parametrize("value", [1e15, 2.0, -1.0, -1e15]) |
| 36 | +def test_value_outside_range_clamps_slider(value: float, logscale: bool): |
| 37 | + """The slider stays at the nearest end and a warning is shown.""" |
| 38 | + with qt_app_context(exec_loop=False): |
| 39 | + param = _param(value, logscale=logscale) |
| 40 | + expected = param.steps - 1 if value > param.max else 0 |
| 41 | + assert param.slider.value() == expected |
| 42 | + assert not param.slider.signalsBlocked() |
| 43 | + assert param.value == value |
| 44 | + assert _has_range_warning(param) |
| 45 | + param.value = 0.5 |
| 46 | + param.update() |
| 47 | + assert not _has_range_warning(param) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "value, vmin, vmax", |
| 52 | + [ |
| 53 | + (np.nan, 0.0, 1.0), |
| 54 | + (np.inf, 0.0, 1.0), |
| 55 | + (-np.inf, 0.0, 1.0), |
| 56 | + (2.0, 1.0, 1.0), |
| 57 | + (np.float64(2.0), 1.0, 1.0), |
| 58 | + (1e308, -1e308, 1e308), |
| 59 | + ], |
| 60 | +) |
| 61 | +def test_undefined_slider_position(value: float, vmin: float, vmax: float): |
| 62 | + """Non-finite values and empty ranges leave the slider position unchanged.""" |
| 63 | + with qt_app_context(exec_loop=False): |
| 64 | + param = _param(0.5) |
| 65 | + position = param.slider.value() |
| 66 | + param.min, param.max, param.value = vmin, vmax, value |
| 67 | + param.update() |
| 68 | + assert param.slider.value() == position |
| 69 | + assert not param.slider.signalsBlocked() |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("logscale", [False, True]) |
| 73 | +def test_slider_mapping_round_trip(logscale: bool): |
| 74 | + """Slider position -> value -> slider position gives the same position.""" |
| 75 | + with qt_app_context(exec_loop=False): |
| 76 | + param = _param(0.5, 0.0, 10.0, logscale) |
| 77 | + last = param.steps - 1 |
| 78 | + for position in (0, 1, 1234, last - 1, last): |
| 79 | + param.slider_value_changed(position) |
| 80 | + param.update_slider_value() |
| 81 | + assert param.slider.value() == position |
| 82 | + param.value = param.min |
| 83 | + param.update_slider_value() |
| 84 | + assert param.slider.value() == 0 |
| 85 | + param.value = param.max |
| 86 | + param.update_slider_value() |
| 87 | + assert param.slider.value() == last |
| 88 | + |
| 89 | + |
| 90 | +def test_autofit_with_value_far_outside_range(): |
| 91 | + """All parameters are updated when the fit leaves a slider range.""" |
| 92 | + x = np.linspace(0.0, 1.0, 50) |
| 93 | + y = 1e6 * x + 3.0 |
| 94 | + |
| 95 | + def fitfunc(x: np.ndarray, params: list[float]) -> np.ndarray: |
| 96 | + a, b = params |
| 97 | + return a * x + b |
| 98 | + |
| 99 | + with qt_app_context(exec_loop=False): |
| 100 | + # The fitted slope is 1e6 times the slider range width: its slider position |
| 101 | + # used to exceed the 32-bit integer range accepted by `QSlider.setValue` |
| 102 | + params = [FitParam("a", 0.5, 0.0, 1.0), FitParam("b", 0.0, -5.0, 5.0)] |
| 103 | + dialog = FitDialog(edit=True, auto_fit=True) |
| 104 | + dialog.set_data(x, y, fitfunc, params) |
| 105 | + dialog.autofit() |
| 106 | + a, b = params |
| 107 | + assert a.value == pytest.approx(1e6) |
| 108 | + assert b.value == pytest.approx(3.0) |
| 109 | + for param in params: |
| 110 | + assert param.lineedit.text() == param.format % param.value |
| 111 | + assert not param.slider.signalsBlocked() |
| 112 | + assert a.slider.value() == a.steps - 1 |
| 113 | + assert _has_range_warning(a) |
| 114 | + assert not _has_range_warning(b) |
| 115 | + dialog.close() |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize("text", ["nan", "inf", "-inf", "abc", ""]) |
| 119 | +def test_line_edit_rejects_invalid_values(text: str): |
| 120 | + """Typing a non-numeric or non-finite value restores the previous value.""" |
| 121 | + with qt_app_context(exec_loop=False): |
| 122 | + param = _param(0.5) |
| 123 | + param.lineedit.setText(text) |
| 124 | + param.line_editing_finished() |
| 125 | + assert param.value == 0.5 |
| 126 | + assert param.lineedit.text() == param.format % 0.5 |
| 127 | + |
| 128 | + |
| 129 | +def test_line_edit_accepts_large_values(): |
| 130 | + """A large finite value is accepted, with the slider at its end.""" |
| 131 | + with qt_app_context(exec_loop=False): |
| 132 | + param = _param(0.5) |
| 133 | + param.lineedit.setText("1e300") |
| 134 | + param.line_editing_finished() |
| 135 | + assert param.value == 1e300 |
| 136 | + assert param.slider.value() == param.steps - 1 |
| 137 | + assert _has_range_warning(param) |
| 138 | + |
| 139 | + |
| 140 | +def test_steps_minimum(): |
| 141 | + """The parameter properties dialog requires at least two slider steps.""" |
| 142 | + assert not FitParamDataSet.steps.check_value(1) |
| 143 | + assert FitParamDataSet.steps.check_value(2) |
0 commit comments