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

np.testing.assert_equal does not work with quantity lists #1272

Closed
edmundsj opened this issue Mar 23, 2021 · 9 comments · Fixed by #1421
Closed

np.testing.assert_equal does not work with quantity lists #1272

edmundsj opened this issue Mar 23, 2021 · 9 comments · Fixed by #1421

Comments

@edmundsj
Copy link

When creating a list of quantities from a numpy array, numpy's built-in test methods for array comparison no longer work. This is breaking a lot of my test suite now that I am trying to incorporate pint's unit capabilities into my packages. Is there an easy way around this, or am I just going to have to write a wrapper function and swap it out for numpy's?

import pint
import numpy as np


ureg = pint.UnitRegistry()
data = ureg.mV * np.array([1, 2, 3])
np.testing.assert_equal(data, data) # This fails

@andrewgsavage
Copy link
Collaborator

There's some helper functions in the testsuite

from pint.testsuite import helpers
helpers.assert_quantity_equal(x, y)

@hgrecco
Copy link
Owner

hgrecco commented Mar 23, 2021

I was also starting to look into this. Any idea what triggered this?

@edmundsj
Copy link
Author

edmundsj commented Mar 23, 2021

It looks like numpy fails to recognize the object as array-like, and so doesn't invoke all() appropriately. I see there has been discussion of this for a while (#1128). The error I get is below:

if not (desired == actual):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@hgrecco
Copy link
Owner

hgrecco commented Mar 23, 2021

Yes, but you mentioned that is working before. That is what surprised me.

@keewis
Copy link
Contributor

keewis commented Mar 23, 2021

looking at the code of assert_equal it seems that there is a special case for ndarray (using isinstance), for which assert_array_equal is used. If we directly use numpy.testing.assert_array_equal that casts to numpy using asarray.

I think for testing it would be good to expose pint.testsuite.helpers.assert_quantity_equal as pint.testing.assert_equal.

@edmundsj
Copy link
Author

Yes, but you mentioned that is working before. That is what surprised me.

Ah! Apologies, not working with pint, but working with regular arrays of dimensionless numbers.

@hgrecco
Copy link
Owner

hgrecco commented Mar 23, 2021

Ahh, ok. Then as mentioned by @andrewgsavage and @keewis mentions we should expose and document assert_quantity_equal (and assert_quantity_almost_equal) which work like the corresponding numpy counterparts but deal with unit conversions.

@keewis
Copy link
Contributor

keewis commented Mar 23, 2021

following numpy.testing, assert_quantity_almost_equal should be exposed as pint.testing.assert_allclose

@hgrecco
Copy link
Owner

hgrecco commented Mar 23, 2021

True (see https://github.com/numpy/numpy/blob/v1.20.0/numpy/testing/_private/utils.py#L936 )

My recomendation is tha we double check the implementation of assert_quantity_almost_equal so tha it follows the semantics of assert_allclose.
If yes, then we expose assert_quantity_almost_equal as assert_allclose
If no, then we define assert_allclose to match the semantics of numpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants