Repository navigation
[BUG] Fix _pattern not set on older pickles #1465
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
Open
henrydingliu
wants to merge
15
commits into
casact:main
Choose a base branch
from
henrydingliu:bug/pattern_backup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9a43d58
fix _pattern breaking read_pickle
henrydingliu b07e740
ruff fix
henrydingliu f0dcf73
update folder
henrydingliu 5a9a448
Merge branch 'main' into bug/pattern_backup
henrydingliu 8e053d4
merge conflict fix
henrydingliu 020fd5b
Merge branch 'main' into bug/pattern_backup
henrydingliu 26f461e
equals and ruff
henrydingliu 01cf266
typo
henrydingliu 869d6b2
ruff fix
henrydingliu abe0205
ruff fixes
henrydingliu 329c990
more ruff
henrydingliu 6a0b08f
Merge branch 'main' into bug/pattern_backup
henrydingliu 1e01be8
bugbot fix
henrydingliu 888dd81
typo
henrydingliu 99f4cc4
more bugbot
henrydingliu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,8 @@ settings.json | |
|
|
||
| *.pkl | ||
|
|
||
| !chainladder/utils/data/*.pkl | ||
|
|
||
| # macOS related files | ||
| *.DS_Store | ||
| *Icon? | ||
|
|
||
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,263 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import dill | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| import chainladder as cl | ||
|
|
||
| if TYPE_CHECKING: | ||
| from chainladder import Triangle | ||
|
|
||
|
|
||
| class TestJSON: | ||
| """Test JSON IO""" | ||
|
|
||
| def test_triangle_json_io(self, clrd: Triangle) -> None: | ||
| """ | ||
| test JSON round trip for Triangle | ||
|
|
||
| Parameters | ||
| ---------- | ||
| clrd: Triangle | ||
| The CLRD sample data set fixture | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| clrd2 = cl.read_json(clrd.to_json(), array_backend=clrd.array_backend) | ||
| assert clrd.equals(clrd2) | ||
| assert clrd.index.equals(clrd2.index) | ||
| assert np.all(clrd.columns == clrd2.columns) | ||
| assert np.all(clrd.odims == clrd2.odims) | ||
| assert np.all(clrd.ddims == clrd2.ddims) | ||
| assert np.all(clrd.valuation == clrd2.valuation) | ||
|
|
||
| def test_json_for_val(self, raa: Triangle) -> None: | ||
| """ | ||
| test JSON round trip for val Triangle | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The CLRD sample data set fixture | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| x = raa.dev_to_val().to_json() | ||
| assert cl.read_json(x).equals(raa.dev_to_val()) | ||
|
|
||
| def test_estimator_json_io(self) -> None: | ||
| """ | ||
| test JSON round trip for Development estimator | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| assert ( | ||
| cl.read_json(cl.Development().to_json()).get_params() | ||
| == cl.Development().get_params() | ||
| ) | ||
|
|
||
| def test_pipeline_json_io(self) -> None: | ||
| """ | ||
| test JSON round trip for an estimator pipeline | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| pipe = cl.Pipeline( | ||
| steps=[("dev", cl.Development()), ("model", cl.BornhuetterFerguson())] | ||
| ) | ||
| pipe2 = cl.read_json(pipe.to_json()) | ||
| assert { | ||
| item[0]: item[1].get_params() for item in pipe.get_params()["steps"] | ||
| } == {item[0]: item[1].get_params() for item in pipe2.get_params()["steps"]} | ||
|
|
||
| def test_json_subtri(self, raa: Triangle, atol: float) -> None: | ||
| """ | ||
| test JSON round trip preserves sub-Triangles | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The CLRD sample data set fixture | ||
|
|
||
| atol: float | ||
| the absolute tolerance for this test | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| a = cl.read_json(cl.Chainladder().fit_predict(raa).to_json()).full_triangle_ | ||
| b = cl.Chainladder().fit_predict(raa).full_triangle_ | ||
| # since a and b are meant to be identical, (a - b) should be 0 everywhere | ||
| # 0's are turned to NaN's in Triangle arithmetic, resulting in an All-NaN | ||
| # warning when aggregating | ||
| with pytest.warns(RuntimeWarning, match="All-NaN"): | ||
| assert abs(a - b).max().max() < atol | ||
|
|
||
| def test_json_df(self, atol: float) -> None: | ||
| """ | ||
| test JSON round trip preserves sub-Triangles from Munich Adjustment | ||
|
|
||
| Parameters | ||
| ---------- | ||
| atol: float | ||
| the absolute tolerance for this test | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| x = cl.MunichAdjustment(paid_to_incurred=("paid", "incurred")).fit_transform( | ||
| cl.load_sample("mcl") | ||
| ) | ||
| assert abs(cl.read_json(x.to_json()).lambda_ - x.lambda_).sum() < atol | ||
|
|
||
|
|
||
| class TestPickle: | ||
| """Test JSON IO""" | ||
|
|
||
| def test_to_pickle_read_pickle(self, raa: Triangle) -> None: | ||
| """ | ||
| test JSON round trip for val Triangle | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The CLRD sample data set fixture | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| import os | ||
| import tempfile | ||
|
|
||
| dev = cl.Development(average="simple", n_periods=4).fit(raa) | ||
| fd, path = tempfile.mkstemp(suffix=".pkl") | ||
| os.close(fd) | ||
| try: | ||
| dev.to_pickle(path) | ||
| restored = cl.read_pickle(path) | ||
| assert restored.average == dev.average | ||
| assert restored.n_periods == dev.n_periods | ||
| np.testing.assert_array_almost_equal(restored.ldf_.values, dev.ldf_.values) | ||
| finally: | ||
| os.remove(path) | ||
|
|
||
| def test_read_pickle_triangle(self, raa: Triangle, tmp_path: Path) -> None: | ||
| """ | ||
| Create a triangle, dump a pickle of it, and then read it back in. The ingested | ||
| pickle should result in an equal copy of the triangle that was dumped. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The raa sample data set. | ||
| tmp_path: Path | ||
| The builtin pytest tmp_path fixture, provides a temporary path to dump the | ||
| pickle to. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
| pkl_path = tmp_path / "triangle.pkl" | ||
| with open(pkl_path, "wb") as f: | ||
| dill.dump(raa, f) | ||
| assert cl.read_pickle(str(pkl_path)).equals(raa) | ||
|
|
||
| def test_triangle_to_pickle( | ||
| self, raa: Triangle, clrd: Triangle, tmp_path: Path | ||
| ) -> None: | ||
| """ | ||
| Dump a pickle of a triangle and read it back in. The read-in triangle should | ||
| equal the one that was dumped. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The raa sample data set Triangle. | ||
| clrd: Triangle | ||
| The clrd sample data set Triangle. | ||
| tmp_path: Path | ||
| The builtin pytest tmp_path fixture, provides a temporary path to dump the | ||
| pickle to. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
| # Single-dimension case. | ||
| raa_path = tmp_path / "raa.pkl" | ||
| raa.to_pickle(str(raa_path)) | ||
| assert raa_path.is_file() | ||
| assert cl.read_pickle(str(raa_path)).equals(raa) | ||
|
|
||
| # Multidimensional case. | ||
| clrd_path = tmp_path / "clrd.pkl" | ||
| clrd.to_pickle(str(clrd_path)) | ||
| assert clrd_path.is_file() | ||
| assert cl.read_pickle(str(clrd_path)).equals(clrd) | ||
|
|
||
| def test_read_pickle_estimator(self, raa: Triangle, tmp_path: Path) -> None: | ||
| """ | ||
| Create an estimator, dump a pickle of it, and then read it back in. The ingested | ||
| pickle should result produce the same LDFs that the original estimator does. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The raa sample data set. | ||
| tmp_path: Path | ||
| The builtin pytest tmp_path fixture, provides a temporary path to dump the | ||
| pickle to. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
|
|
||
| pkl_path = tmp_path / "estimator.pkl" | ||
| dev = cl.Development().fit(raa) | ||
| with open(pkl_path, "wb") as f: | ||
| dill.dump(dev, f) | ||
| assert dev.ldf_.equals(cl.read_pickle(str(pkl_path)).ldf_) | ||
|
|
||
| def test_pickle_backward_compatibility(self, raa: Triangle) -> None: | ||
| """ | ||
| Tests backward compatibility with pickles from older versions | ||
|
|
||
| Parameters | ||
| ---------- | ||
| raa: Triangle | ||
| The raa sample data set fixture | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| data_dir: Path = Path(__file__).parent.parent.parent / "utils" / "data" | ||
| raa_pickles = list(data_dir.glob("raa*.pkl")) | ||
|
|
||
| for raa_pickle in raa_pickles: | ||
| test = cl.read_pickle(raa_pickle) | ||
| assert test.equals(raa) | ||
| assert repr(test) == repr(raa) | ||
| assert ( | ||
| cl.Development().fit(test).ldf_.equals(cl.Development().fit(raa).ldf_) | ||
| ) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.