-
Notifications
You must be signed in to change notification settings - Fork 28
ENH: Adding apply_along_axis
#871
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
prady0t
wants to merge
4
commits into
data-apis:main
Choose a base branch
from
prady0t:add-appl
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
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import array_api_extra._delegation as delegated_func | ||
| from array_api_extra import ( | ||
| angle, | ||
| apply_along_axis, | ||
| apply_where, | ||
| argpartition, | ||
| at, | ||
|
|
@@ -107,6 +108,95 @@ def test_all_contains_all_public_functions(): | |
| ) | ||
|
|
||
|
|
||
| class TestApplyAlongAxis: | ||
| @pytest.mark.xfail_xp_backend( | ||
| Backend.DASK, | ||
| reason="Dask apply_along_axis infers output shape differently", | ||
| strict=False, | ||
| ) | ||
| def test_simple(self, xp: ArrayNamespace): | ||
| x = xp.asarray([[1, 2, 3], [4, 5, 6]]) | ||
| actual = apply_along_axis( | ||
| lambda row: row + 1, | ||
| axis=1, | ||
| arr=x, | ||
| ) | ||
| expected = xp.asarray([[2, 3, 4], [5, 6, 7]]) | ||
| assert_equal(actual, expected) | ||
|
|
||
| @pytest.mark.xfail_xp_backend( | ||
| Backend.DASK, | ||
| reason="Dask implementation computes eagerly", | ||
| strict=False, | ||
| ) | ||
| @pytest.mark.xfail_xp_backend( | ||
| Backend.SPARSE, | ||
| reason="Sparse cannot stack scalar arrays with different fill values", | ||
| strict=False, | ||
| ) | ||
|
Comment on lines
+132
to
+136
Contributor
Author
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. test_scalar_output[sparse] - ValueError: This operation requires consistent fill-values, but argument 1 had a fill value of 15, which is different from a fill_value of 6 in the first argument. |
||
| def test_scalar_output(self, xp: ArrayNamespace): | ||
| x = xp.asarray([[1, 2, 3], [4, 5, 6]]) | ||
| actual = apply_along_axis( | ||
| xp.sum, | ||
| axis=1, | ||
| arr=x, | ||
| ) | ||
| expected = xp.asarray([6, 15]) | ||
| assert_equal(actual, expected) | ||
|
|
||
| @pytest.mark.xfail_xp_backend( | ||
| Backend.DASK, | ||
| reason="Dask implementation computes eagerly", | ||
| strict=False, | ||
| ) | ||
| def test_negative_axis(self, xp: ArrayNamespace): | ||
| x = xp.asarray([[1, 2, 3], [4, 5, 6]]) | ||
| actual = apply_along_axis( | ||
| lambda row: xp.flip(row, axis=0), | ||
| axis=-1, | ||
| arr=x, | ||
| ) | ||
| expected = xp.asarray([[3, 2, 1], [6, 5, 4]]) | ||
| assert_equal(actual, expected) | ||
|
|
||
| @pytest.mark.xfail_xp_backend( | ||
| Backend.SPARSE, | ||
| reason="Sparse cannot stack scalar arrays with different fill values", | ||
| strict=False, | ||
| ) | ||
| def test_3d_scalar_output(self, xp: ArrayNamespace): | ||
| arr_np = np.arange(24).reshape(2, 3, 4) | ||
| x = xp.asarray(arr_np) | ||
| expected = np.apply_along_axis(lambda a: np.sum(a), 1, arr_np) # noqa: PLW0108 | ||
| actual = apply_along_axis( | ||
| xp.sum, | ||
| axis=1, | ||
| arr=x, | ||
| ) | ||
| assert_equal(actual, xp.asarray(expected)) | ||
|
|
||
| def test_axis_out_of_bounds(self, xp: ArrayNamespace): | ||
| x = xp.asarray([[1, 2, 3], [4, 5, 6]]) | ||
| with pytest.raises(ValueError, match="out of bounds"): | ||
| _ = functions.apply_along_axis(lambda row: row, 2, x) | ||
| with pytest.raises(ValueError, match="out of bounds"): | ||
| _ = functions.apply_along_axis(lambda row: row, -3, x) | ||
|
|
||
| @pytest.mark.xfail_xp_backend( | ||
| Backend.SPARSE, | ||
| reason="Sparse cannot stack scalar arrays with different fill values", | ||
| strict=False, | ||
| ) | ||
| def test_non_array_output(self, xp: ArrayNamespace): | ||
| x = xp.asarray([[1, 2, 3], [4, 5, 6]]) | ||
| actual = functions.apply_along_axis( | ||
| lambda row: int(row[0]) + 1, # type: ignore[arg-type,return-value] # pyright: ignore[reportArgumentType] | ||
| 1, | ||
| x, | ||
| ) | ||
| assert_equal(actual, xp.asarray([2, 5])) | ||
|
|
||
|
|
||
| class TestApplyWhere: | ||
| @staticmethod | ||
| def f1(x: Array, y: Array | int = 10) -> Array: | ||
|
|
||
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.
test_simple[dask] - AssertionError: shapes do not match: (2, 1) != (2, 3)