-
Notifications
You must be signed in to change notification settings - Fork 102
REFACTOR: Reorganize type hierarchy of sparse.py. Move array-level fu… #739
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
genedan
wants to merge
5
commits into
main
Choose a base branch
from
#737-sparse-hierarchy
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
5 commits
Select commit
Hold shift + click to select a range
f65af55
REFACTOR: Reorganize type hierarchy of sparse.py. Move array-level fu…
genedan a33f181
FIX: Apply bugbot fixes.
genedan 41bf857
FIX: Apply bugbot fixes.
genedan e48a28e
FIX: Apply bugbot fixes.
genedan 0715793
TEST: Add unit tests to chainladder.utils.sparse to cover missing lines.
genedan 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
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,110 @@ | ||
| import numpy as np | ||
|
|
||
| from chainladder.utils.sparse import ( | ||
| array, | ||
| floor, | ||
| COO, | ||
| where | ||
| ) | ||
|
|
||
|
|
||
| def test_array_from_list_default_fill_value() -> None: | ||
| """ | ||
| Tests chainladder.utils.sparse.array() when no fill value is provided. | ||
| Checks whether the default nan is filled. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
| result: COO = array([1.0, 2.0, 3.0]) | ||
| assert isinstance(result, COO) | ||
| assert np.isnan(result.fill_value) | ||
|
|
||
|
|
||
| def test_array_from_list_explicit_fill_value() -> None: | ||
| """ | ||
| Tests chainladder.utils.sparse.array() when a fill value of 0 is provided. | ||
| Checks whether the 0 is filled. | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
||
| """ | ||
| result: COO = array([1, 2, 3], fill_value=0) | ||
| assert isinstance(result, COO) | ||
| assert result.fill_value == 0 | ||
|
|
||
|
|
||
| def test_array_from_coo_default_fill_value() -> None: | ||
| """ | ||
| Tests chainladder.utils.sparse.array() when initializing from a sparse array with a default fill value. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
| coo = COO.from_numpy(np.array([1.0, 2.0, 3.0])) | ||
| result: COO = array(coo) | ||
| assert isinstance(result, COO) | ||
| assert np.isnan(result.fill_value) | ||
|
|
||
|
|
||
| def test_array_from_coo_explicit_fill_value() -> None: | ||
| """ | ||
| Tests chainladder.utils.sparse.array() when initializing from a sparse array with an explicit fill value. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
| coo = COO.from_numpy(np.array([1, 2, 3])) | ||
| result: COO = array(coo, fill_value=0) | ||
| assert isinstance(result, COO) | ||
| assert result.fill_value == 0 | ||
|
|
||
|
|
||
| def test_where_selects_from_two_arrays() -> None: | ||
| """ | ||
| Tests element-wise where across sparse arrays. Calls np.where on each element triplet | ||
| (cond[i], a[i], b[i]) - returning a[i] where the condition is True and b[i] where it's False. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| a: COO = array([1.0, 2.0, 3.0]) | ||
| b: COO = array([10.0, 20.0, 30.0]) | ||
| cond: COO = array([True, False, True]) | ||
| result: COO = where(cond, a, b) | ||
| assert isinstance(result, COO) | ||
| np.testing.assert_array_equal(result.todense(), [1.0, 20.0, 3.0]) | ||
|
|
||
|
|
||
| def test_floor_rounds_down() -> None: | ||
| """ | ||
| Checks floor function rounding down with positive and negative floats. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| a: COO = array([1.2, 2.7, -0.3]) | ||
| result: COO = floor(a) | ||
| np.testing.assert_array_equal(result.todense(), [1.0, 2.0, -1.0]) | ||
|
|
||
|
|
||
| def test_floor_mutates_in_place() -> None: | ||
| """ | ||
| Checks in-place mutation of floor function. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| a = array([1.2, 2.7, -0.3]) | ||
| result: COO = floor(a) | ||
| assert result is a |
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
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.