Skip to content

gh-1076: Use array comparison functions from array-api-extra#1083

Draft
paddyroddy wants to merge 24 commits into
mainfrom
paddy/issue-1076
Draft

gh-1076: Use array comparison functions from array-api-extra#1083
paddyroddy wants to merge 24 commits into
mainfrom
paddy/issue-1076

Conversation

@paddyroddy
Copy link
Copy Markdown
Member

@paddyroddy paddyroddy commented May 19, 2026

Description

As can be seen in scipy/scipy#25143 and scikit-learn/scikit-learn#34019, array-api-extra now provides array comparison methods. This means we should be able to remove our custom Compare helper class.

Closes: #1076

Checks

  • Is your code passing linting?
  • Is your code passing tests?
  • Have you added additional tests (if required)?
  • Have you modified/extended the documentation (if required)?
  • Have you added a one-liner changelog entry above (if required)?

@paddyroddy paddyroddy self-assigned this May 19, 2026
@paddyroddy paddyroddy added enhancement New feature or request dependencies Pull requests that update a dependency file array-api Work is related to the Array API testing Work is related to testing labels May 19, 2026
@paddyroddy paddyroddy marked this pull request as ready for review May 21, 2026 14:21
@paddyroddy paddyroddy marked this pull request as draft May 21, 2026 14:53
Comment on lines -12 to -13
from tests.fixtures.helper_classes import Compare

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and everywhere else, we can call xp_assert... inside the class itself, so compare.assert_allclose works but internally calls xp_assert_close. This would help avoid changing lines everywhere.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but the Compare class was always a hack so I'd rather to the big changes once

old = healpix.ang2pix(healpix_inputs.nside, thetas, phis, lonlat=lonlat)
new = hp.ang2pix(healpix_inputs.nside, thetas, phis, lonlat=lonlat, xp=xp)
compare.assert_array_equal(old, new)
xp_assert_equal(xp.asarray(old), new)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and everywhere else, instead of changing it everywhere, you can modify the compare class functions first to convert a Python scalar or list into an array, then call the array api extra function with these inputs. See here for example (done in this SciPy PR):

def _convert_scalar_to_array(x, xp):
    if isinstance(x, (list, tuple)) or type(x) in (
        int,
        float,
        complex,
        bool,
    ):
        return xp.asarray(x)
    return x

def xp_assert_close(actual, desired, *, rtol=None, atol=0, check_dtype=True,
                    check_shape=True, check_0d=False, err_msg='', xp=None):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel if we did this then we could have just stuck with our initial implementation

Copy link
Copy Markdown

@lucascolley lucascolley May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the advantage of converting array-likes to arrays is that you gain the strictness of checking that the return namespace is what you expect

assert j is None
assert a.shape == (0,)
compare.assert_allclose(xpb.asarray(s), 1.0)
xp_assert_close(s, xpb.ones_like(s))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check_shape=False parameter should be an alternative here

old = healpix.ang2pix(healpix_inputs.nside, thetas, phis, lonlat=lonlat)
new = hp.ang2pix(healpix_inputs.nside, thetas, phis, lonlat=lonlat, xp=xp)
compare.assert_array_equal(old, new)
xp_assert_equal(xp.asarray(old), new)
Copy link
Copy Markdown

@lucascolley lucascolley May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the advantage of converting array-likes to arrays is that you gain the strictness of checking that the return namespace is what you expect

@lucascolley
Copy link
Copy Markdown

public API is now available on array-api-extra main and documented at https://data-apis.org/array-api-extra/api-testing.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array-api Work is related to the Array API dependencies Pull requests that update a dependency file enhancement New feature or request testing Work is related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backend-agnostic Compare

3 participants