-
Notifications
You must be signed in to change notification settings - Fork 10
ENH: new function isclose
#113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55208b0
ENH: new functions `isclose` and `allclose`
crusaderky 5f54c92
Remove allclose
crusaderky c3861a7
Test none shapes
crusaderky 33b37df
tweaks
lucascolley 542da61
lockfile
lucascolley 42da174
lint
lucascolley 38e88b5
code review
crusaderky 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
cov | ||
create_diagonal | ||
expand_dims | ||
isclose | ||
kron | ||
nunique | ||
pad | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,11 +4,13 @@ | |
Note that this is private API; don't expect it to be stable. | ||
""" | ||
|
||
import math | ||
from types import ModuleType | ||
|
||
from ._utils._compat import ( | ||
array_namespace, | ||
is_cupy_namespace, | ||
is_dask_namespace, | ||
is_pydata_sparse_namespace, | ||
is_torch_namespace, | ||
) | ||
|
@@ -40,8 +42,16 @@ def _check_ns_shape_dtype( | |
msg = f"namespaces do not match: {actual_xp} != f{desired_xp}" | ||
assert actual_xp == desired_xp, msg | ||
|
||
msg = f"shapes do not match: {actual.shape} != f{desired.shape}" | ||
assert actual.shape == desired.shape, msg | ||
actual_shape = actual.shape | ||
desired_shape = desired.shape | ||
if is_dask_namespace(desired_xp): | ||
if any(math.isnan(i) for i in actual_shape): | ||
actual_shape = actual.compute().shape | ||
if any(math.isnan(i) for i in desired_shape): | ||
desired_shape = desired.compute().shape | ||
|
||
msg = f"shapes do not match: {actual_shape} != f{desired_shape}" | ||
assert actual_shape == desired_shape, msg | ||
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. This will have to be replicated on the scipy PR for dask support |
||
|
||
msg = f"dtypes do not match: {actual.dtype} != {desired.dtype}" | ||
assert actual.dtype == desired.dtype, msg | ||
|
@@ -61,6 +71,11 @@ def xp_assert_equal(actual: Array, desired: Array, err_msg: str = "") -> None: | |
The expected array (typically hardcoded). | ||
err_msg : str, optional | ||
Error message to display on failure. | ||
|
||
See Also | ||
-------- | ||
xp_assert_close : Similar function for inexact equality checks. | ||
numpy.testing.assert_array_equal : Similar function for NumPy arrays. | ||
""" | ||
xp = _check_ns_shape_dtype(actual, desired) | ||
|
||
|
@@ -112,6 +127,16 @@ def xp_assert_close( | |
Absolute tolerance. Default: 0. | ||
err_msg : str, optional | ||
Error message to display on failure. | ||
|
||
See Also | ||
-------- | ||
xp_assert_equal : Similar function for exact equality checks. | ||
isclose : Public function for checking closeness. | ||
numpy.testing.assert_allclose : Similar function for NumPy arrays. | ||
|
||
Notes | ||
----- | ||
The default `atol` and `rtol` differ from `xp.all(xpx.isclose(a, b))`. | ||
""" | ||
xp = _check_ns_shape_dtype(actual, desired) | ||
|
||
|
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.
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.
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.
On eager backends, this is less performant than
but it supports backends with NaN shapes like Dask.
Both jax.jit and dask with non-NaN shape should elide the comparison away.