Repository navigation
[FEAT] Check Development parameter values in fit and read_json
#1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
henrydingliu
merged 11 commits into
casact:main
from
scks1:feat-validate-params-1569-solo
Nov 16, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b3b0f4b
[FEAT] Check Development parameter values in fit and read_json
scks1 d420695
[FIX] Call only chainladder's _validate_params in read_json
scks1 1f99121
[TST] Cover read_json with a dict assumption
scks1 28bcb04
[FIX] Rename the parameter check hook to _check_param_values
scks1 fa0d67a
[FIX] Keep n_periods=0 as an alias for -1
scks1 23d13b0
[DOCS] Drop the numeric average option from Development's docstring
scks1 c6cba5f
[FIX] Declare Development's allowed string values as Literal aliases
scks1 5d1fbca
[CHORE] Move _assumption_values to Common next to _validate_assumption
scks1 2782716
[CHORE] Drop the empty parameter check hook from MethodBase
scks1 8f91795
[CHORE] Keep the parameter check on Development only
scks1 a9def71
[TST] Start test_io.py for development estimator JSON tests
scks1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a |
||
| """ | ||
| 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): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
somewhere in this module or development/base.py, there's also checking for length of the parameters. do you think that should be in scope for this method? (doesn't have to be this PR)