-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Preparatory refactor: create ak_almost_equal._impl * Adding ak.array_equal Includes a very minimal test case. Needs more. * Fixing bug in nplike, array_equal The NaN values were not compared correctly. Also adding several tests of ak.array_equal. Also firing a minor issue in ak.array_equal. * Fixing array_equal docstring * Update src/awkward/operations/ak_almost_equal.py Use is operator to compare classes Co-authored-by: Jim Pivarski <jpivarski@users.noreply.github.com> * Remove defaults from args to ak_almost_equal._impl * Fixing more bugs * Possible fix for old numpy * style: pre-commit fixes --------- Co-authored-by: Jim Pivarski <jpivarski@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6eb8627
commit e15518f
Showing
5 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE | ||
|
||
from __future__ import annotations | ||
|
||
import awkward as ak | ||
from awkward._dispatch import high_level_function | ||
|
||
__all__ = ("array_equal",) | ||
|
||
|
||
@high_level_function() | ||
def array_equal( | ||
a1, | ||
a2, | ||
equal_nan: bool = False, | ||
dtype_exact: bool = True, | ||
same_content_types: bool = True, | ||
check_parameters: bool = True, | ||
check_regular: bool = True, | ||
): | ||
""" | ||
True if two arrays have the same shape and elements, False otherwise. | ||
Args: | ||
a1: Array-like data (anything #ak.to_layout recognizes). | ||
a2: Array-like data (anything #ak.to_layout recognizes). | ||
equal_nan: bool (default=False) | ||
Whether to count NaN values as equal to each other. | ||
dtype_exact: bool (default=True) whether the dtypes must be exactly the same, or just the | ||
same family. | ||
same_content_types: bool (default=True) | ||
Whether to require all content classes to match | ||
check_parameters: bool (default=True) whether to compare parameters. | ||
check_regular: bool (default=True) whether to consider ragged and regular dimensions as | ||
unequal. | ||
TypeTracer arrays are not supported, as there is very little information to | ||
be compared. | ||
""" | ||
# Dispatch | ||
yield a1, a2 | ||
|
||
return ak.operations.ak_almost_equal._impl( | ||
a1, | ||
a2, | ||
rtol=0.0, | ||
atol=0.0, | ||
dtype_exact=dtype_exact, | ||
check_parameters=check_parameters, | ||
check_regular=check_regular, | ||
exact_eq=True, | ||
same_content_types=same_content_types and check_regular, | ||
equal_nan=equal_nan, | ||
) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE | ||
|
||
from __future__ import annotations | ||
|
||
import numpy as np | ||
|
||
import awkward as ak | ||
from awkward.contents import NumpyArray | ||
from awkward.index import Index64 | ||
|
||
|
||
def test_array_equal_simple(): | ||
assert ak.array_equal( | ||
ak.Array([[1, 2], [], [3, 4, 5]]), | ||
ak.Array([[1, 2], [], [3, 4, 5]]), | ||
) | ||
|
||
|
||
def test_array_equal_mixed_dtype(): | ||
assert not ak.array_equal( | ||
ak.Array(np.array([1.5, 2.0, 3.25], dtype=np.float32)), | ||
ak.Array(np.array([1.5, 2.0, 3.25], dtype=np.float64)), | ||
) | ||
assert ak.array_equal( | ||
ak.Array(np.array([1.5, 2.0, 3.25], dtype=np.float32)), | ||
ak.Array(np.array([1.5, 2.0, 3.25], dtype=np.float64)), | ||
dtype_exact=False, | ||
) | ||
|
||
|
||
def test_array_equal_on_listoffsets(): | ||
a1 = ak.contents.ListOffsetArray( | ||
Index64(np.array([0, 3, 3, 5])), NumpyArray(np.arange(5) * 1.5) | ||
) | ||
a2 = ak.contents.ListOffsetArray( | ||
Index64(np.array([0, 3, 3, 5])), | ||
NumpyArray(np.arange(10) * 1.5), # Longer array content than a1 | ||
) | ||
assert ak.array_equal(a1, a2) | ||
|
||
|
||
def test_array_equal_mixed_content_type(): | ||
a1 = ak.Array([[1, 2, 3], [4, 5, 6], [7, 8]]) | ||
a1r = ak.to_regular(a1[:2]) | ||
assert not ak.array_equal(a1[:2], a1r) | ||
assert ak.array_equal(a1[:2], a1r, check_regular=False) | ||
assert not ak.array_equal(a1, a1r, check_regular=False) | ||
|
||
assert ak.array_equal( | ||
a1, a1.layout | ||
) # high-level automatically converted to layout in pre-check | ||
|
||
a2_np = ak.contents.NumpyArray(np.arange(3)) | ||
a2_ia = ak.contents.IndexedArray( | ||
Index64(np.array([0, 1, 2])), NumpyArray(np.arange(3)) | ||
) | ||
assert ak.array_equal(a2_np, a2_ia, same_content_types=False) | ||
|
||
|
||
def test_array_equal_opion_types(): | ||
a1 = ak.Array([1, 2, None, 4]) | ||
a2 = ak.concatenate([ak.Array([1, 2]), ak.Array([None, 4])]) | ||
assert ak.array_equal(a1, a2) | ||
|
||
a3 = a1.layout.to_ByteMaskedArray(valid_when=True) | ||
assert not ak.array_equal(a1, a3, same_content_types=True) | ||
assert ak.array_equal(a1, a3, same_content_types=False) | ||
assert not ak.array_equal( | ||
a1, ak.Array([1, 2, 3, 4]), same_content_types=False, dtype_exact=False | ||
) | ||
|
||
|
||
def test_array_equal_nan(): | ||
a = ak.Array([1.0, 2.5, np.nan]) | ||
nplike = a.layout.backend.nplike | ||
assert not nplike.array_equal(a.layout.data, a.layout.data) | ||
assert nplike.array_equal(a.layout.data, a.layout.data, equal_nan=True) | ||
assert not ak.array_equal(a, a) | ||
assert ak.array_equal(a, a, equal_nan=True) | ||
|
||
|
||
def test_array_equal_with_params(): | ||
a1 = NumpyArray( | ||
np.array([1, 2, 3], dtype=np.uint32), parameters={"foo": {"bar": "baz"}} | ||
) | ||
a2 = NumpyArray( | ||
np.array([1, 2, 3], dtype=np.uint32), parameters={"foo": {"bar": "Not so fast"}} | ||
) | ||
assert not ak.array_equal(a1, a2) | ||
assert ak.array_equal(a1, a2, check_parameters=False) |