-
Notifications
You must be signed in to change notification settings - Fork 21
ENH: add nanmin
#804
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?
ENH: add nanmin
#804
Changes from all commits
6709082
214a7a3
9bd01fc
c6f41f1
23a1dd7
236f11d
d98cfa5
4f261f7
7822f6c
d46313c
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| isin | ||
| kron | ||
| nan_to_num | ||
| nanmin | ||
| nunique | ||
| one_hot | ||
| pad | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| isin, | ||
| kron, | ||
| nan_to_num, | ||
| nanmin, | ||
| nunique, | ||
| one_hot, | ||
| pad, | ||
|
|
@@ -2203,3 +2204,84 @@ def test_xp(self, xp: ModuleType): | |
| res = unravel_index(indices, shape, xp=xp) | ||
| for res_arr, exp_arr in zip(res, expected, strict=True): | ||
| assert_equal(res_arr, exp_arr) | ||
|
|
||
|
|
||
| class TestNanMin: | ||
|
OmarManzoor marked this conversation as resolved.
|
||
| def test_simple(self, xp: ModuleType): | ||
| a = xp.asarray([[1, 2], [3, xp.nan]]) | ||
|
|
||
| # with the default `axis=None` a single scalar is returned | ||
| res = nanmin(a) | ||
| expected = 1.0 | ||
| assert res == expected | ||
|
|
||
| res = nanmin(a, axis=0) | ||
| expected = xp.asarray([1.0, 2.0]) | ||
| assert_equal(res, expected) | ||
|
|
||
| res = nanmin(a, axis=1) | ||
| expected = xp.asarray([1.0, 3.0]) | ||
| assert_equal(res, expected) | ||
|
|
||
| def test_bigger(self, xp: ModuleType): | ||
| a = xp.asarray( | ||
| [ | ||
| [1, xp.nan, 4, 5], | ||
| [xp.nan, -2, xp.nan, -4], | ||
| [2, 1, 3, xp.nan], | ||
| ] | ||
| ) | ||
|
|
||
| res = nanmin(a, axis=0) | ||
| expected = xp.asarray([1.0, -2.0, 3.0, -4.0]) | ||
| assert_equal(res, expected) | ||
|
|
||
| res = nanmin(a, axis=1) | ||
| expected = xp.asarray([1.0, -4.0, 1.0]) | ||
| assert_equal(res, expected) | ||
|
|
||
| def test_with_infinity(self, xp: ModuleType): | ||
| a = xp.asarray([0.1, 1.0, xp.nan, xp.inf]) | ||
| res = nanmin(a) | ||
| expected = 0.1 | ||
| assert res == expected | ||
|
|
||
| a = xp.asarray([0.1, 1.0, xp.nan, -xp.inf]) | ||
| res = nanmin(a) | ||
| expected = -xp.inf | ||
| assert res == expected | ||
|
|
||
| def test_scalar(self, xp: ModuleType): | ||
| a = xp.asarray(1.0) | ||
| assert nanmin(a) == 1.0 | ||
|
|
||
| @pytest.mark.filterwarnings("ignore:.*All-NaN slice*.:RuntimeWarning") | ||
| def test_all_nan_slice_2d(self, xp: ModuleType): | ||
| a = xp.asarray( | ||
| [ | ||
| [xp.nan, 5.0], | ||
| [xp.nan, 2.0], | ||
| ] | ||
| ) | ||
|
|
||
| res = nanmin(a, axis=0, xp=xp) | ||
| expected = xp.asarray([xp.nan, 2.0]) | ||
| assert_equal(res, expected) | ||
|
|
||
| @pytest.mark.skip_xp_backend( | ||
| Backend.TORCH, reason="torch.nanmin does not support tensors on meta device" | ||
| ) | ||
|
Comment on lines
+2271
to
+2273
Member
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. can we change this reason to point to the part of torch we are using that is lacking support? E.g. (if this is accurate) "
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. Done! I think this seems better. |
||
| @pytest.mark.parametrize("axis", [None, 0, 1]) | ||
| def test_device(self, axis: int | None, xp: ModuleType, device: Device): | ||
| a = xp.asarray([[4, xp.nan, 1], [2, 5, xp.nan]], device=device) | ||
| res = nanmin(a, axis=axis) | ||
| assert get_device(res) == device | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("axis", "expected_list"), [(0, [2.0, 3.0, 1.0]), (1, [1.0, 2.0])] | ||
| ) | ||
| def test_xp(self, axis: int | None, expected_list: list[float], xp: ModuleType): | ||
| a = xp.asarray([[4, xp.nan, 1], [2, 3, xp.nan]]) | ||
| res = nanmin(a, axis=axis, xp=xp) | ||
| expected = xp.asarray(expected_list) | ||
| assert_equal(res, expected) | ||
Uh oh!
There was an error while loading. Please reload this page.