-
Notifications
You must be signed in to change notification settings - Fork 26
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,79 @@ | |
| ) | ||
|
|
||
|
|
||
| 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( | ||
| np.sum, | ||
|
Check failure on line 171 in tests/test_funcs.py
|
||
| 1, | ||
| arr_np, | ||
| None, | ||
| ) | ||
| actual = apply_along_axis( | ||
| xp.sum, | ||
| axis=1, | ||
| arr=x, | ||
| ) | ||
| assert_equal(actual, xp.asarray(expected)) | ||
|
|
||
|
|
||
| class TestApplyWhere: | ||
| @staticmethod | ||
| def f1(x: Array, y: Array | int = 10) -> Array: | ||
|
|
||
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)