Skip to content

Commit 55da6b4

Browse files
neutrinocerosmhvk
andauthored
BUG: support numpy's unwrap ufunc (#20132)
Co-authored-by: Marten H. van Kerkwijk <[email protected]>
1 parent a888e4d commit 55da6b4

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎astropy/units/quantity_helper/helpers.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
NUMPY_LT_2_2,
2020
NUMPY_LT_2_3,
2121
NUMPY_LT_2_5,
22+
NUMPY_LT_2_6,
2223
)
2324

2425
from . import UFUNC_HELPERS, UNSUPPORTED_UFUNCS
@@ -368,6 +369,22 @@ def helper_clip(f, unit1, unit2, unit3):
368369
return converters, result_unit
369370

370371

372+
def helper_unwrap(f, unit1, unit2, unit3):
373+
"""Support the private numpy ufunc np._core.umath._unwrap.
374+
375+
This ufunc is used internally in `~numpy.unwrap`, and like for clip, the
376+
first array is the primary one, and discont and period should simply be
377+
converted to its unit.
378+
379+
A tricky part is that `~numpy.unwrap` has dimensionless defaults for
380+
discont and period, which are simply passed on. Rather than deal with
381+
these here, we just continue to override `~numpy.unwrap` in
382+
``astropy.units.quantity_helpers.function_helpers``.
383+
384+
"""
385+
return helper_clip(f, unit1, unit2, unit3)
386+
387+
371388
# list of ufuncs:
372389
# https://numpy.org/doc/stable/reference/ufuncs.html#available-ufuncs
373390

@@ -582,4 +599,8 @@ def helper_clip(f, unit1, unit2, unit3):
582599
if isinstance(getattr(np_umath, "clip", None), np.ufunc):
583600
UFUNC_HELPERS[np_umath.clip] = helper_clip
584601

602+
if not NUMPY_LT_2_6:
603+
# See docstring of helper_unwrap about this private numpy ufunc.
604+
UFUNC_HELPERS[np_umath._unwrap] = helper_unwrap
605+
585606
del ufunc

‎astropy/units/tests/test_quantity_ufuncs.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from astropy.units import quantity_helper as qh
1818
from astropy.units.quantity_helper.converters import UfuncHelpers
1919
from astropy.units.quantity_helper.helpers import helper_sqrt
20-
from astropy.utils.compat.numpycompat import NUMPY_LT_2_3
20+
from astropy.utils.compat.numpycompat import NUMPY_LT_2_3, NUMPY_LT_2_6
2121
from astropy.utils.compat.optional_deps import HAS_SCIPY
2222

2323

@@ -1222,6 +1222,35 @@ def test_clip_errors(self):
12221222
self.clip(q, 0.0, 1.0)
12231223

12241224

1225+
@pytest.mark.skipif(NUMPY_LT_2_6, reason="no _unwrap ufunc available")
1226+
class TestUnwrap:
1227+
"""Test the np._core.umath._unwrap ufunc.
1228+
1229+
In numpy, this is hidden behind `~numpy.unwrap`, a function that inserts
1230+
dimensionless defaults for discont and period that are a bit strange to
1231+
deal with (unless one wanted an implicitly ``u.dimensionless_angles()``.
1232+
So, we continue to override the function, which means that in practice
1233+
this ufunc is never called with Quantity. But we support it for
1234+
completeness.
1235+
1236+
"""
1237+
1238+
@classmethod
1239+
def setup_class(cls):
1240+
cls.unwrap = np_umath._unwrap
1241+
1242+
def test_unwrap_simple(self):
1243+
a = [10.0, 90.0, 440.0] * u.deg
1244+
discont = np.pi * u.rad
1245+
period = 1 * u.cycle
1246+
got = self.unwrap(a, discont, period)
1247+
assert got.unit == a.unit
1248+
exp_value = self.unwrap(
1249+
a.value, discont.to_value(a.unit), period.to_value(a.unit)
1250+
)
1251+
assert_array_equal(got.value, exp_value)
1252+
1253+
12251254
class TestUfuncAt:
12261255
"""Test that 'at' method for ufuncs (calculates in-place at given indices)
12271256

‎astropy/utils/compat/numpycompat.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"NUMPY_LT_2_4",
2222
"NUMPY_LT_2_4_1",
2323
"NUMPY_LT_2_5",
24+
"NUMPY_LT_2_6",
2425
"chararray",
2526
"get_chararray",
2627
]
@@ -34,6 +35,7 @@
3435
NUMPY_LT_2_4 = not minversion(np, "2.4.0.dev0")
3536
NUMPY_LT_2_4_1 = not minversion(np, "2.4.1.dev0")
3637
NUMPY_LT_2_5 = not minversion(np, "2.5.0.dev0")
38+
NUMPY_LT_2_6 = not minversion(np, "2.6.0.dev0")
3739

3840

3941
def __getattr__(attr):

0 commit comments

Comments
 (0)