Skip to content
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

fix: add ak.array_equal to NEP18 overrides, documentation, and add one more test #3225

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/reference/toctree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,12 @@
generated/ak.backend

.. toctree::
:caption: Approximation
:caption: Approximation and comparison

generated/ak.round
generated/ak.isclose
generated/ak.almost_equal
generated/ak.array_equal

.. toctree::
:caption: NumPy compatibility
Expand Down
18 changes: 9 additions & 9 deletions src/awkward/operations/ak_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from __future__ import annotations

from awkward._backends.dispatch import backend_of_obj
from awkward._backends.dispatch import backend_of
from awkward._backends.numpy import NumpyBackend
from awkward._behavior import behavior_of, get_array_class, get_record_class
from awkward._dispatch import high_level_function
from awkward._layout import ensure_same_backend
from awkward._nplikes.numpy_like import NumpyMetadata
from awkward._parameters import parameters_are_equal
from awkward.operations.ak_to_layout import to_layout
Expand Down Expand Up @@ -82,14 +83,13 @@ def _impl(
left_behavior = behavior_of(left)
right_behavior = behavior_of(right)

left_backend = backend_of_obj(left, default=cpu)
right_backend = backend_of_obj(right, default=cpu)
if left_backend is not right_backend:
return False
backend = left_backend

left_layout = to_layout(left, allow_record=False).to_packed()
right_layout = to_layout(right, allow_record=False).to_packed()
layouts = ensure_same_backend(
to_layout(left, allow_record=False),
to_layout(right, allow_record=False),
)
left_layout = layouts[0].to_packed()
right_layout = layouts[1].to_packed()
backend = backend_of(left_layout)

if not backend.nplike.known_data:
raise NotImplementedError(
Expand Down
1 change: 1 addition & 0 deletions src/awkward/operations/ak_array_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
__all__ = ("array_equal",)


@ak._connect.numpy.implements("array_equal")
@high_level_function()
def array_equal(
a1,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_1105_ak_aray_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ def test_array_equal_with_params():
)
assert not ak.array_equal(a1, a2)
assert ak.array_equal(a1, a2, check_parameters=False)


def test_array_equal_numpy_override():
assert np.array_equal(
ak.Array([[1, 2], [], [3, 4, 5]]),
ak.Array([[1, 2], [], [3, 4, 5]]),
)
5 changes: 4 additions & 1 deletion tests/test_2305_nep_18_lazy_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
def test_binary():
ak_array = ak.Array(np.arange(10, dtype="<u4"))
np_array = np.arange(10, dtype=">u4")
assert np.array_equal(ak_array, np_array)
with pytest.raises(TypeError):
# ak.array_equal now overrides np.array_equal, and requires
# both arrays to be valid within awkward.
assert np.array_equal(ak_array, np_array)


def test_different_backend():
Expand Down
9 changes: 5 additions & 4 deletions tests/test_2678_same_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import numpy as np # noqa: F401
import pytest # noqa: F401
import pytest

import awkward as ak

Expand All @@ -16,6 +16,7 @@ def test_where():


def test_almost_equal():
assert not ak.almost_equal(
[True, False, False], ak.to_backend([True, False, False], "typetracer")
)
with pytest.raises(NotImplementedError):
ak.almost_equal(
[True, False, False], ak.to_backend([True, False, False], "typetracer")
)
Loading