-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Moving function quantity_allclose #7235 #7252
Changes from 8 commits
6b26606
77d278d
f69fb8c
82a8d21
5882cb4
ba9774e
a573d23
2e0f6d3
ac6ead4
e377e93
26d5224
e7f38f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,12 @@ | |
This module provides the tools used to internally run the astropy test suite | ||
from the installed astropy. It makes use of the `pytest` testing framework. | ||
""" | ||
|
||
import os | ||
import sys | ||
import types | ||
import pickle | ||
import warnings | ||
import functools | ||
|
||
import pytest | ||
|
||
try: | ||
|
@@ -21,6 +19,7 @@ | |
except ImportError: | ||
pass | ||
|
||
from ..units import allclose as quantity_allclose # noqa | ||
from ..utils.exceptions import (AstropyDeprecationWarning, | ||
AstropyPendingDeprecationWarning) | ||
|
||
|
@@ -30,7 +29,7 @@ | |
|
||
__all__ = ['raises', 'enable_deprecations_as_exceptions', 'remote_data', | ||
'treat_deprecations_as_exceptions', 'catch_warnings', | ||
'assert_follows_unicode_guidelines', 'quantity_allclose', | ||
'assert_follows_unicode_guidelines', | ||
'assert_quantity_allclose', 'check_pickling_recovery', | ||
'pickle_protocol', 'generic_recursive_equality_test'] | ||
|
||
|
@@ -461,52 +460,8 @@ def assert_quantity_allclose(actual, desired, rtol=1.e-7, atol=None, | |
:func:`numpy.testing.assert_allclose`. | ||
""" | ||
import numpy as np | ||
from astropy.units.quantity import _unquantify_allclose_arguments | ||
|
||
np.testing.assert_allclose(*_unquantify_allclose_arguments(actual, desired, | ||
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. Instead of using a private function from astropy.units, why not just do:
? 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. 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. Ops. I only saw Tom's comment. Sorry. I'll fix this shortly. |
||
rtol, atol), | ||
**kwargs) | ||
|
||
|
||
def quantity_allclose(a, b, rtol=1.e-5, atol=None, **kwargs): | ||
""" | ||
Returns True if two arrays are element-wise equal within a tolerance. | ||
|
||
This is a :class:`~astropy.units.Quantity`-aware version of | ||
:func:`numpy.allclose`. | ||
""" | ||
import numpy as np | ||
return np.allclose(*_unquantify_allclose_arguments(a, b, rtol, atol), | ||
**kwargs) | ||
|
||
|
||
def _unquantify_allclose_arguments(actual, desired, rtol, atol): | ||
from .. import units as u | ||
|
||
actual = u.Quantity(actual, subok=True, copy=False) | ||
|
||
desired = u.Quantity(desired, subok=True, copy=False) | ||
try: | ||
desired = desired.to(actual.unit) | ||
except u.UnitsError: | ||
raise u.UnitsError("Units for 'desired' ({0}) and 'actual' ({1}) " | ||
"are not convertible" | ||
.format(desired.unit, actual.unit)) | ||
|
||
if atol is None: | ||
# by default, we assume an absolute tolerance of 0 | ||
atol = u.Quantity(0) | ||
else: | ||
atol = u.Quantity(atol, subok=True, copy=False) | ||
try: | ||
atol = atol.to(actual.unit) | ||
except u.UnitsError: | ||
raise u.UnitsError("Units for 'atol' ({0}) and 'actual' ({1}) " | ||
"are not convertible" | ||
.format(atol.unit, actual.unit)) | ||
|
||
rtol = u.Quantity(rtol, subok=True, copy=False) | ||
try: | ||
rtol = rtol.to(u.dimensionless_unscaled) | ||
except Exception: | ||
raise u.UnitsError("`rtol` should be dimensionless") | ||
|
||
return actual.value, desired.value, rtol.value, atol.value |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
check_output) | ||
|
||
__all__ = ["Quantity", "SpecificTypeQuantity", | ||
"QuantityInfoBase", "QuantityInfo"] | ||
"QuantityInfoBase", "QuantityInfo", "allclose", "isclose"] | ||
|
||
|
||
# We don't want to run doctests in the docstrings we inherit from Numpy | ||
|
@@ -1722,3 +1722,61 @@ def _set_unit(self, unit): | |
", so cannot set it to '{0}'.".format(unit))) | ||
|
||
super()._set_unit(unit) | ||
|
||
|
||
def isclose(a, b, rtol=1.e-5, atol=None, **kwargs): | ||
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 now definitely is getting in the terrain of making more sense as a separate module/file! 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. Yeah, let's defer moving it to separate module in a separate PR. |
||
""" | ||
Notes | ||
----- | ||
Returns True if two arrays are element-wise equal within a tolerance. | ||
|
||
This is a :class:`~astropy.units.Quantity`-aware version of | ||
:func:`numpy.isclose`. | ||
""" | ||
return np.isclose(*_unquantify_allclose_arguments(a, b, rtol, atol), | ||
**kwargs) | ||
|
||
|
||
def allclose(a, b, rtol=1.e-5, atol=None, **kwargs): | ||
""" | ||
Notes | ||
----- | ||
Returns True if two arrays are element-wise equal within a tolerance. | ||
|
||
This is a :class:`~astropy.units.Quantity`-aware version of | ||
:func:`numpy.allclose`. | ||
""" | ||
return np.allclose(*_unquantify_allclose_arguments(a, b, rtol, atol), | ||
**kwargs) | ||
|
||
|
||
def _unquantify_allclose_arguments(actual, desired, rtol, atol): | ||
actual = Quantity(actual, subok=True, copy=False) | ||
|
||
desired = Quantity(desired, subok=True, copy=False) | ||
try: | ||
desired = desired.to(actual.unit) | ||
except UnitsError: | ||
raise UnitsError("Units for 'desired' ({0}) and 'actual' ({1}) " | ||
"are not convertible" | ||
.format(desired.unit, actual.unit)) | ||
|
||
if atol is None: | ||
# by default, we assume an absolute tolerance of 0 | ||
atol = Quantity(0) | ||
else: | ||
atol = Quantity(atol, subok=True, copy=False) | ||
try: | ||
atol = atol.to(actual.unit) | ||
except UnitsError: | ||
raise UnitsError("Units for 'atol' ({0}) and 'actual' ({1}) " | ||
"are not convertible" | ||
.format(atol.unit, actual.unit)) | ||
|
||
rtol = Quantity(rtol, subok=True, copy=False) | ||
try: | ||
rtol = rtol.to(dimensionless_unscaled) | ||
except Exception: | ||
raise UnitsError("`rtol` should be dimensionless") | ||
|
||
return actual.value, desired.value, rtol.value, atol.value |
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.
Can we do this on a bugfix branch? While importing with
*
is bad practice, this removal is an API change for those who do that.