diff --git a/chainladder/core/common.py b/chainladder/core/common.py index 8b73648b8..d84fad6f9 100644 --- a/chainladder/core/common.py +++ b/chainladder/core/common.py @@ -435,3 +435,22 @@ def _validate_assumption( if axis == 2: arr = arr[None, None, :, None] return arr + + def _assumption_values(self, value, X: TriangleLike | None, axis: int = 3) -> list: + """ + Return the individual values of an assumption that can be checked. + + A scalar gives itself and a list, tuple, set or array gives its + elements. A dict or callable gives the values it resolves to against + ``X`` along ``axis``, or nothing when ``X`` is ``None``. + """ + if isinstance(value, dict) or callable(value): + if X is None: + return [] + values = self._validate_assumption(X, value, axis=axis).flatten() + elif isinstance(value, (list, tuple, set, np.ndarray)): + values = np.asarray(list(value) if isinstance(value, set) else value) + values = values.flatten() + else: + return [value] + return [v.item() if isinstance(v, np.generic) else v for v in values] diff --git a/chainladder/development/development.py b/chainladder/development/development.py index f78a10ec2..e67e08575 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -16,6 +16,7 @@ Literal, # Self, # Make use of this once Python 3.10 is deprecated. TYPE_CHECKING, + get_args, ) if TYPE_CHECKING: @@ -24,6 +25,18 @@ from pandas import Series from types import ModuleType +# Allowed values of Development's string parameters. The annotations in +# Development.__init__ use them, and _check_param_values reads them with +# get_args. +Average = Literal["volume", "simple", "regression", "geometric"] +SigmaInterpolation = Literal["log-linear", "mack"] + + +def _one_of(values: tuple) -> str: + """Return ``values`` as "'a', 'b' or 'c'" for an error message.""" + quoted = [repr(v) for v in values] + return ", ".join(quoted[:-1]) + " or " + quoted[-1] + class Development(DevelopmentBase): """ @@ -37,11 +50,9 @@ class Development(DevelopmentBase): Setting n_periods to 1 does not allow enough degrees of freedom to support calculation of all regression statistics; only LDFs would be calculated. - average: literal (or list of literals), or float, optional (default = 'volume') + average: literal (or list of literals), optional (default = 'volume') type of averaging to use for ldf average calculation. - Options include 'volume', 'simple', 'regression', and 'geometric'. If numeric values are supplied, - then (2-average) in the style of Zehnwirth & Barnett is used - for the exponent of the regression weights. + Options include 'volume', 'simple', 'regression', and 'geometric'. sigma_interpolation: string optional (default = 'log-linear') Options include 'log-linear' and 'mack' drop: tuple or list of tuples @@ -328,8 +339,8 @@ class Development(DevelopmentBase): def __init__( self, n_periods: int = -1, - average: Literal["volume", "simple", "regression", "geometric"] = "volume", - sigma_interpolation: Literal["log-linear", "mack"] = "log-linear", + average: Average = "volume", + sigma_interpolation: SigmaInterpolation = "log-linear", drop: tuple | list[tuple] | None = None, drop_high: bool | int | list[bool] | list[int] | None = None, drop_low: bool | int | list[bool] | list[int] | None = None, @@ -356,6 +367,39 @@ def __init__( # Undeclared until fitted attributes - scikit-learn convention. self.average_: np.ndarray + def _check_param_values(self, X: TriangleLike | None = None) -> None: + """ + Check ``average``, ``n_periods`` and ``sigma_interpolation`` and raise + a ``ValueError`` naming the first one with a value ``fit`` cannot use. + + ``average`` takes the values of ``Average``, ``n_periods`` takes -1 or + a positive integer (0 is kept as an alias for -1), and + ``sigma_interpolation`` takes the values of ``SigmaInterpolation``. A + list is checked value by value. ``fit`` calls the method with the + triangle, so a dict or callable is checked once resolved against it; + ``read_json`` calls it with no triangle and skips those. + """ + averages = get_args(Average) + for value in self._assumption_values(self.average, X): + if not (isinstance(value, str) and value in averages): + raise ValueError(f"average must be {_one_of(averages)}, got {value!r}.") + for value in self._assumption_values(self.n_periods, X): + if ( + isinstance(value, (bool, np.bool_)) + or not isinstance(value, (int, np.integer)) + or not (value == -1 or value >= 0) + ): + raise ValueError( + "n_periods must be -1 or a positive integer (0 is read as " + f"-1), got {value!r}." + ) + interpolations = get_args(SigmaInterpolation) + if self.sigma_interpolation not in interpolations: + raise ValueError( + f"sigma_interpolation must be {_one_of(interpolations)}, " + f"got {self.sigma_interpolation!r}." + ) + def fit(self, X: TriangleLike, y: None = None, sample_weight: None = None): """ Fit the model with X. @@ -375,6 +419,7 @@ def fit(self, X: TriangleLike, y: None = None, sample_weight: None = None): Returns the instance itself. """ + self._check_param_values(X) # Triangle must be cumulative and in "development" mode. obj: TriangleLike = self._set_fit_groups(X).incr_to_cum().val_to_dev().copy() xp: ModuleType = obj.get_array_module() diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index 4e3086a1a..c6e8b78b9 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1090,3 +1090,45 @@ def test_pct_reported_requires_ldf(raa): _ = raa.pct_reported_ with pytest.raises(AttributeError): _ = raa.pct_unreported_ + + +@pytest.mark.parametrize( + "params, name", + [ + ({"average": "bogus"}, "average"), + ({"average": 1.5}, "average"), + ({"average": ["volume"] * 8 + ["bogus"]}, "average"), + ({"average": lambda age: "bogus" if age == 12 else "volume"}, "average"), + ({"n_periods": -5}, "n_periods"), + ({"n_periods": 2.5}, "n_periods"), + ({"n_periods": True}, "n_periods"), + ({"sigma_interpolation": "bogus"}, "sigma_interpolation"), + ], +) +def test_fit_rejects_invalid_params(raa, params, name): + """ + Check that ``fit`` raises a ``ValueError`` naming a parameter whose value + it cannot use, instead of failing later or fitting silently (#1569). + """ + with pytest.raises(ValueError, match=name): + cl.Development(**params).fit(raa) + + +def test_n_periods_zero_fits_as_all_periods(raa): + """Check that ``n_periods=0`` still fits as ``n_periods=-1`` (#1606).""" + zero = cl.Development(n_periods=0).fit(raa) + all_periods = cl.Development(n_periods=-1).fit(raa) + assert np.allclose(zero.ldf_.values, all_periods.ldf_.values) + assert np.allclose(zero.sigma_.values, all_periods.sigma_.values) + + +def test_valid_params_pass_validation(raa): + """Check that lists, dicts and callables of valid values still fit.""" + cl.Development( + average=["volume", "simple"] * 4 + ["regression"], + n_periods={age: -1 if age < 60 else 3 for age in raa.development}, + sigma_interpolation="mack", + ).fit(raa) + cl.Development(average=lambda age: "geometric", n_periods=[-1] * 9).fit(raa) + dev = cl.Development(n_periods=4, average="simple") + assert cl.read_json(dev.to_json()).get_params() == dev.get_params() diff --git a/chainladder/development/tests/test_io.py b/chainladder/development/tests/test_io.py new file mode 100644 index 000000000..bb20d8d0d --- /dev/null +++ b/chainladder/development/tests/test_io.py @@ -0,0 +1,41 @@ +"""Tests for saving and loading development estimators with JSON.""" + +import json + +import pytest + +import chainladder as cl + + +@pytest.mark.parametrize( + "params, name", + [({"average": "bogus"}, "average"), ({"n_periods": -5}, "n_periods")], +) +def test_read_json_rejects_invalid_params(params, name): + """Check that ``read_json`` checks the parameter values it loads (#1569).""" + json_str = json.dumps({"params": params, "__class__": "Development"}) + with pytest.raises(ValueError, match=name): + cl.read_json(json_str) + with pytest.raises(ValueError, match=name): + cl.read_json(json.dumps([{"name": "dev", **json.loads(json_str)}])) + + +@pytest.mark.parametrize("name", ["Trend", "BerquistSherman", "ParallelogramOLF"]) +def test_read_json_loads_estimators_without_the_hook(name): + """ + Check that ``read_json`` still loads estimators that do not define + ``_check_param_values``. + """ + estimator = getattr(cl, name)() + assert cl.read_json(estimator.to_json()).get_params() == estimator.get_params() + + +def test_read_json_leaves_dict_params_to_fit(): + """ + Check that ``read_json`` loads a dict assumption without a triangle to + resolve it against, leaving its values to be checked in ``fit``. + """ + dev = cl.Development(n_periods={12: 2, 24: -1}) + loaded = cl.read_json(dev.to_json()) + assert isinstance(loaded, cl.Development) + assert list(loaded.n_periods.values()) == [2, -1] diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index e581face7..1e92d9b59 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -393,13 +393,11 @@ def read_json(json_str, array_backend=None): array_backend = options.ARRAY_BACKEND json_dict = json.loads(json_str) if type(json_dict) is list: - import chainladder as cl - return Pipeline( steps=[ ( item["name"], - cl.__dict__[item["__class__"]]().set_params(**item["params"]), + _estimator_from_json(item["__class__"], item["params"]), ) for item in json_dict ] @@ -444,9 +442,21 @@ def read_json(json_str, array_backend=None): else: return tri else: - import chainladder as cl + return _estimator_from_json(json_dict["__class__"], json_dict["params"]) + + +def _estimator_from_json(class_name, params): + """ + Build the chainladder estimator named ``class_name`` with ``params`` and + check the parameter values it can check without a triangle. + """ + import chainladder as cl - return cl.__dict__[json_dict["__class__"]]().set_params(**json_dict["params"]) + estimator = cl.__dict__[class_name]().set_params(**params) + # Only estimators with parameter checks define the hook. + if hasattr(estimator, "_check_param_values"): + estimator._check_param_values() + return estimator def _origin_periods(index, grain):