Skip to content

Commit 5631ab9

Browse files
committed
Ensure Longitude wrapping and Latitude limits work
1 parent e7fee84 commit 5631ab9

5 files changed

Lines changed: 61 additions & 8 deletions

File tree

‎astropy/coordinates/angles.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,17 @@ def _validate_angles(self, angles=None):
553553
if angles.unit is u.deg:
554554
limit = 90
555555
elif angles.unit is u.rad:
556-
limit = self.dtype.type(0.5 * np.pi)
556+
limit = 0.5 * np.pi
557557
else:
558558
limit = u.degree.to(angles.unit, 90.0)
559559

560-
invalid_angles = np.any(angles.value < -limit) or np.any(angles.value > limit)
560+
# Ensure ndim>=1 so that comparison is done using the angle dtype.
561+
# Otherwise, e.g., np.array(np.pi/2, 'f4') > np.pi/2 will yield True.
562+
# (This feels like a bug -- see https://github.com/numpy/numpy/issues/23247)
563+
# Note that we should avoid using `angles.dtype` directly since for
564+
# structured arrays like Distribution this will be `void`.
565+
angles_view = angles.view(np.ndarray)[np.newaxis]
566+
invalid_angles = np.any(angles_view < -limit) or np.any(angles_view > limit)
561567
if invalid_angles:
562568
raise ValueError(
563569
"Latitude angle(s) must be within -90 deg <= angle <= 90 deg, "

‎astropy/uncertainty/core.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ def view(self, dtype=None, type=None):
311311

312312
# Override __getitem__ so that 'samples' is returned as the sample class.
313313
def __getitem__(self, item):
314+
if isinstance(item, Distribution):
315+
# Required for in-place operations like dist[dist < 0] += 360.
316+
return self.distribution[item.distribution]
314317
result = super().__getitem__(item)
315318
if item == "samples":
316319
# Here, we need to avoid our own redefinition of view.
@@ -320,6 +323,13 @@ def __getitem__(self, item):
320323
else:
321324
return result
322325

326+
def __setitem__(self, item, value):
327+
if isinstance(item, Distribution):
328+
# Support operations like dist[dist < 0] = 0.
329+
self.distribution[item.distribution] = value
330+
else:
331+
super().__setitem__(item, value)
332+
323333
# Override __eq__ and __ne__ to pass on directly to the ufunc since
324334
# otherwise comparisons with non-distributions do not work (but
325335
# deferring if other defines __array_ufunc__ = None -- see

‎astropy/uncertainty/tests/test_containers.py‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,62 @@
88
import astropy.units as u
99
from astropy.coordinates import Angle, Latitude, Longitude
1010
from astropy.uncertainty import Distribution
11-
from astropy.utils import NumpyRNGContext
1211

1312

14-
@pytest.mark.parametrize("angle_cls", [Angle, Longitude, Latitude])
1513
class TestAngles:
1614
@classmethod
1715
def setup_class(cls):
18-
with NumpyRNGContext(12345):
19-
cls.a = np.random.normal(10, [[0.2], [1.5], [4], [1]], size=(4, 10))
16+
cls.a = np.arange(27.0).reshape(3, 9)
2017
cls.d = Distribution(cls.a)
2118
cls.q = cls.a << u.deg
2219
cls.dq = Distribution(cls.q)
2320

21+
@pytest.mark.parametrize("angle_cls", [Angle, Longitude, Latitude])
2422
def test_as_input_for_angle(self, angle_cls):
2523
da = angle_cls(self.dq)
2624
assert isinstance(da, angle_cls)
2725
assert isinstance(da, Distribution)
2826
assert_array_equal(da.distribution, angle_cls(self.q))
2927

28+
@pytest.mark.parametrize("angle_cls", [Angle, Longitude, Latitude])
3029
def test_using_angle_as_input(self, angle_cls):
3130
a = angle_cls(self.q)
3231
da = Distribution(a)
3332
assert isinstance(da, angle_cls)
3433
assert isinstance(da, Distribution)
3534

35+
# Parametrize the unit to check the various branches in Latitude._validate_angles
36+
@pytest.mark.parametrize("dtype", ["f8", "f4"])
37+
@pytest.mark.parametrize(
38+
"value", [90 * u.deg, np.pi / 2 * u.radian, 90 * 60 * u.arcmin]
39+
)
40+
def test_at_limit_for_latitude(self, value, dtype):
41+
q = u.Quantity(value, dtype=dtype).reshape(1)
42+
qd = Distribution(q)
43+
ld = Latitude(qd)
44+
assert_array_equal(ld.distribution, Latitude(q))
45+
46+
# Parametrize the unit in case Longitude._wrap_at becomes unit-dependent.
47+
@pytest.mark.parametrize("dtype", ["f8", "f4"])
48+
@pytest.mark.parametrize(
49+
"value", [360 * u.deg, 2 * np.pi * u.radian, 360 * 60 * u.arcmin]
50+
)
51+
def test_at_wrap_angle_for_longitude(self, value, dtype):
52+
q = u.Quantity(value, dtype=dtype).reshape(1)
53+
qd = Distribution(q)
54+
ld = Longitude(qd)
55+
assert_array_equal(ld.distribution, Longitude(q))
56+
assert np.all(ld.distribution == 0)
57+
58+
@pytest.mark.parametrize("angle_cls", [Longitude, Latitude])
3659
def test_operation_gives_correct_subclass(self, angle_cls):
3760
# Lon and Lat always fall back to Angle
3861
da = angle_cls(self.dq)
3962
da2 = da + da
4063
assert isinstance(da, Angle)
4164
assert isinstance(da, Distribution)
4265

66+
@pytest.mark.parametrize("angle_cls", [Longitude, Latitude])
4367
def test_pdfmean_gives_correct_subclass(self, angle_cls):
4468
# Lon and Lat always fall back to Angle
4569
da = angle_cls(self.dq)

‎astropy/uncertainty/tests/test_distribution.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,15 @@ def test_distribution_can_be_compared_to_non_distribution(self, op):
501501
def test_distribution_comparison_defers_correctly(self, op):
502502
result = op(self.d, self.override)
503503
assert result == op.__name__
504+
505+
506+
class TestSetItemWithSelection:
507+
def test_setitem(self):
508+
d = Distribution([90.0, 30.0, 0.0])
509+
d[d > 50] = 0.0
510+
assert_array_equal(d, Distribution([0.0, 30.0, 0.0]))
511+
512+
def test_inplace_operation(self):
513+
d = Distribution([90.0, 30.0, 0.0])
514+
d[d > 50] *= -1.0
515+
assert_array_equal(d, Distribution([-90.0, 30.0, 0.0]))
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Ensure that ``Distribution`` can be compared with ``==`` and ``!=``
2-
with regular arrays or scalars. This also ensures that the ``Angle`` subclass
3-
``Longitude`` can be initialized with a ``Distribution``.
2+
with regular arrays or scalars, and that inplace operations like
3+
``dist[dist<0] *= -1`` work. This also ensures that the ``Angle`` subclasses
4+
``Longitude`` and ``Latitude`` can be initialized with a ``Distribution``.

0 commit comments

Comments
 (0)