-
Notifications
You must be signed in to change notification settings - Fork 29
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,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, | ||
| ) | ||
|
Comment on lines
+112
to
+116
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_simple[dask] - AssertionError: shapes do not match: (2, 1) != (2, 3)
Contributor
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. Do we know why this is? |
||
| 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.
Contributor
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. Do we know why this is? |
||
| 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: | ||
|
|
||
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.
Should this not have an
xpin the signature?