Skip to content

Commit 00e64a9

Browse files
lpsingermhvk
andauthored
Broadcast frames over both data and attributes (#15121)
Fixes #5982, fixes #8812, fixes #11184, fixes #12965, fixes #14383. - Broadcast data and attributes together. - If frame data are non-scalar, then broadcast them to the full shape of the frame. Scalar attributes remain scalar, though. Co-authored-by: Marten van Kerkwijk <[email protected]>
1 parent 9742126 commit 00e64a9

5 files changed

Lines changed: 220 additions & 57 deletions

File tree

‎astropy/coordinates/baseframe.py‎

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,12 @@ def __init__(
307307
self._representation = self._infer_representation(
308308
representation_type, differential_type
309309
)
310-
self._data = self._infer_data(args, copy, kwargs) # possibly None.
310+
data = self._infer_data(args, copy, kwargs) # possibly None.
311311

312-
# Set frame attributes, if any
312+
shapes = [] if data is None else [data.shape]
313313

314+
# Set frame attributes, if any.
315+
# Keep track of their shapes, but do not broadcast them yet.
314316
values = {}
315317
for fnm, fdefault in self.get_frame_attr_defaults().items():
316318
# Read-only frame attributes are defined as FrameAttribute
@@ -320,9 +322,9 @@ def __init__(
320322
if fnm in kwargs:
321323
value = kwargs.pop(fnm)
322324
setattr(self, "_" + fnm, value)
323-
# Validate attribute by getting it. If the instance has data,
324-
# this also checks its shape is OK. If not, we do it below.
325-
values[fnm] = getattr(self, fnm)
325+
# Validate attribute by getting it.
326+
values[fnm] = value = getattr(self, fnm)
327+
shapes.append(getattr(value, "shape", ()))
326328
else:
327329
setattr(self, "_" + fnm, fdefault)
328330
self._attr_names_with_defaults.append(fnm)
@@ -333,39 +335,25 @@ def __init__(
333335
f"keywords: {list(kwargs)}"
334336
)
335337

336-
# We do ``is None`` because self._data might evaluate to false for
337-
# empty arrays or data == 0
338-
if self._data is None:
339-
# No data: we still need to check that any non-scalar attributes
340-
# have consistent shapes. Collect them for all attributes with
341-
# size > 1 (which should be array-like and thus have a shape).
342-
shapes = {
343-
fnm: value.shape
344-
for fnm, value in values.items()
345-
if getattr(value, "shape", ())
346-
}
347-
if shapes:
348-
if len(shapes) > 1:
349-
try:
350-
self._no_data_shape = check_broadcast(*shapes.values())
351-
except ValueError as err:
352-
raise ValueError(
353-
f"non-scalar attributes with inconsistent shapes: {shapes}"
354-
) from err
355-
356-
# Above, we checked that it is possible to broadcast all
357-
# shapes. By getting and thus validating the attributes,
358-
# we verify that the attributes can in fact be broadcast.
359-
for fnm in shapes:
360-
getattr(self, fnm)
361-
else:
362-
self._no_data_shape = shapes.popitem()[1]
363-
364-
else:
365-
self._no_data_shape = ()
338+
# Determine the overall shape of the frame.
339+
try:
340+
self._shape = check_broadcast(*shapes)
341+
except ValueError as err:
342+
raise ValueError(
343+
f"non-scalar data and/or attributes with inconsistent shapes: {shapes}"
344+
) from err
345+
346+
# Broadcast the data if necessary and set it
347+
if data is not None and data.shape != self._shape:
348+
data = data._apply(np.broadcast_to, shape=self._shape, subok=True)
349+
self._data = data
350+
# Broadcast the attributes if necessary by getting them again
351+
# (we now know the shapes will be OK).
352+
for key in values:
353+
getattr(self, key)
366354

367355
# The logic of this block is not related to the previous one
368-
if self._data is not None:
356+
if self.has_data:
369357
# This makes the cache keys backwards-compatible, but also adds
370358
# support for having differentials attached to the frame data
371359
# representation object.
@@ -721,7 +709,7 @@ def has_data(self):
721709

722710
@property
723711
def shape(self):
724-
return self.data.shape if self.has_data else self._no_data_shape
712+
return self._shape
725713

726714
# We have to override the ShapedLikeNDArray definitions, since our shape
727715
# does not have to be that of the data.
@@ -1609,6 +1597,7 @@ def apply_method(value):
16091597
new._representation = self._representation.copy()
16101598
new._attr_names_with_defaults = self._attr_names_with_defaults.copy()
16111599

1600+
new_shape = ()
16121601
for attr in self.frame_attributes:
16131602
_attr = "_" + attr
16141603
if attr in self._attr_names_with_defaults:
@@ -1617,6 +1606,7 @@ def apply_method(value):
16171606
value = getattr(self, _attr)
16181607
if getattr(value, "shape", ()):
16191608
value = apply_method(value)
1609+
new_shape = new_shape or value.shape
16201610
elif method == "copy" or method == "flatten":
16211611
# flatten should copy also for a single element array, but
16221612
# we cannot use it directly for array scalars, since it
@@ -1627,22 +1617,11 @@ def apply_method(value):
16271617

16281618
if self.has_data:
16291619
new._data = apply_method(self.data)
1620+
new_shape = new_shape or new._data.shape
16301621
else:
16311622
new._data = None
1632-
shapes = [
1633-
getattr(new, "_" + attr).shape
1634-
for attr in new.frame_attributes
1635-
if (
1636-
attr not in new._attr_names_with_defaults
1637-
and getattr(getattr(new, "_" + attr), "shape", ())
1638-
)
1639-
]
1640-
if shapes:
1641-
new._no_data_shape = (
1642-
check_broadcast(*shapes) if len(shapes) > 1 else shapes[0]
1643-
)
1644-
else:
1645-
new._no_data_shape = ()
1623+
1624+
new._shape = new_shape
16461625

16471626
return new
16481627

‎astropy/coordinates/tests/test_frames.py‎

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,16 @@ def test_time_inputs():
753753
c = FK4(1 * u.deg, 2 * u.deg, obstime="hello")
754754
assert "Invalid time input" in str(err.value)
755755

756-
# A vector time should work if the shapes match, but we don't automatically
757-
# broadcast the basic data (just like time).
758-
FK4([1, 2] * u.deg, [2, 3] * u.deg, obstime=["J2000", "J2001"])
759-
with pytest.raises(ValueError) as err:
760-
FK4(1 * u.deg, 2 * u.deg, obstime=["J2000", "J2001"])
761-
assert "shape" in str(err.value)
756+
# A vector time should work if the shapes match, and we automatically
757+
# broadcast the basic data.
758+
c = FK4([1, 2] * u.deg, [2, 3] * u.deg, obstime=["J2000", "J2001"])
759+
assert c.shape == (2,)
760+
c = FK4(1 * u.deg, 2 * u.deg, obstime=["J2000", "J2001"])
761+
assert c.shape == (2,)
762+
763+
# If the shapes are not broadcastable, then we should raise an exception.
764+
with pytest.raises(ValueError, match="inconsistent shapes"):
765+
FK4([1, 2, 3] * u.deg, [4, 5, 6] * u.deg, obstime=["J2000", "J2001"])
762766

763767

764768
def test_is_frame_attr_default():
@@ -1676,3 +1680,50 @@ def test_frame_coord_comparison():
16761680
error_msg = "Can only compare SkyCoord to Frame with data"
16771681
with pytest.raises(ValueError, match=error_msg):
16781682
frame == coord # noqa: B015
1683+
1684+
1685+
@pytest.mark.parametrize(
1686+
["s1", "s2"],
1687+
(
1688+
((1,), (1,)),
1689+
((2,), (1,)),
1690+
((1,), (2,)),
1691+
((2,), (2,)),
1692+
((2, 1), (1,)),
1693+
((1,), (2, 1)),
1694+
((2, 1), (1, 3)),
1695+
),
1696+
)
1697+
def test_altaz_broadcast(s1, s2):
1698+
"""Note: Regression test for #5982"""
1699+
where = EarthLocation.from_geodetic(lat=45 * u.deg, lon=30 * u.deg, height=0 * u.m)
1700+
time = Time(np.full(s1, 58000.0), format="mjd")
1701+
angle = np.full(s2, 45.0) * u.deg
1702+
result = AltAz(alt=angle, az=angle, obstime=time, location=where)
1703+
assert result.shape == np.broadcast_shapes(s1, s2)
1704+
1705+
1706+
def test_transform_altaz_array_obstime():
1707+
"""Note: Regression test for #12965"""
1708+
obstime = Time("2010-01-01T00:00:00")
1709+
location = EarthLocation(0 * u.deg, 0 * u.deg, 0 * u.m)
1710+
1711+
frame1 = AltAz(location=location, obstime=obstime)
1712+
coord1 = SkyCoord(alt=80 * u.deg, az=0 * u.deg, frame=frame1)
1713+
1714+
obstimes = obstime + np.linspace(0, 15, 50) * u.min
1715+
frame2 = AltAz(location=location, obstime=obstimes)
1716+
coord2 = SkyCoord(alt=coord1.alt, az=coord1.az, frame=frame2)
1717+
assert np.all(coord2.alt == 80 * u.deg)
1718+
assert np.all(coord2.az == 0 * u.deg)
1719+
assert coord2.shape == (50,)
1720+
1721+
# test transformation to ICRS works
1722+
assert len(coord2.icrs) == 50
1723+
1724+
1725+
def test_spherical_offsets_by_broadcast():
1726+
"""Note: Regression test for #14383"""
1727+
assert SkyCoord(
1728+
ra=np.array([123, 134, 145]), dec=np.array([45, 56, 67]), unit=u.deg
1729+
).spherical_offsets_by(2 * u.deg, 2 * u.deg).shape == (3,)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Support Numpy broadcasting over frame data and attributes. See
2+
:ref:`whatsnew-6.0-broadcasting-frame-attributes`.

‎docs/coordinates/performance.inc.rst‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ coordinate::
1515

1616
>>> coord = SkyCoord(ra_array, dec_array, unit='deg') # doctest: +SKIP
1717

18+
Frame attributes can be arrays too, as long as the coordinate data and all of
19+
the frame attributes have shapes that are compatible according to
20+
:doc:`Numpy broadcasting rules <numpy:user/basics.broadcasting>`:
21+
22+
23+
.. testsetup::
24+
25+
>>> from astropy.coordinates import FK4
26+
>>> from astropy import units as u
27+
28+
::
29+
30+
>>> coord = FK4(1 * u.deg, 2 * u.deg, obstime=["J2000", "J2001"])
31+
>>> coord.shape
32+
(2,)
33+
1834
In addition, looping over a |SkyCoord| object can be slow. If you need to
1935
transform the coordinates to a different frame, it is much faster to transform a
2036
single |SkyCoord| with arrays of values as opposed to looping over the
@@ -54,6 +70,61 @@ properties::
5470
EXAMPLE END
5571
5672

73+
Broadcasting Over Frame Data and Attributes
74+
-------------------------------------------
75+
76+
..
77+
EXAMPLE START
78+
Broadcasting Over Frame Data and Attributes
79+
80+
Frames in `astropy.coordinates` support
81+
:doc:`Numpy broadcasting rules <numpy:user/basics.broadcasting>` over both
82+
frame data and frame attributes. This makes it easy and fast to do positional
83+
astronomy calculations and transformations on sweeps of parameters.
84+
85+
Where this really shines is doing fast observability calculations over arrays.
86+
The following example constructs an `~astropy.coordinates.EarthLocation` array
87+
of length :samp:`{L}`, a `~astropy.coordinates.SkyCoord` array of length
88+
:samp:`{M}`, and a `~astropy.time.Time` array of length :samp:`N`. It uses
89+
Numpy broadcasting rules to evaluate a boolean array of shape
90+
:samp:`({L}, {M}, {N})` that is `True` for those observing locations, times,
91+
and sky coordinates, for which the target is above an altitude limit::
92+
93+
>>> from astropy.coordinates import EarthLocation, AltAz, SkyCoord
94+
>>> from astropy.coordinates.angles import uniform_spherical_random_surface
95+
>>> from astropy.time import Time
96+
>>> from astropy import units as u
97+
>>> import numpy as np
98+
99+
>>> L = 25
100+
>>> M = 100
101+
>>> N = 50
102+
103+
>>> # Earth locations of length L
104+
>>> c = uniform_spherical_random_surface(L)
105+
>>> locations = EarthLocation.from_geodetic(c.lon, c.lat)
106+
107+
>>> # Celestial coordinates of length M
108+
>>> coords = SkyCoord(uniform_spherical_random_surface(M))
109+
110+
>>> # Observation times of length N
111+
>>> obstimes = Time('2023-08-04') + np.linspace(0, 24, N) * u.hour
112+
113+
>>> # AltAz coordinates of shape (L, M, N)
114+
>>> frame = AltAz(
115+
... location=locations[:, np.newaxis, np.newaxis],
116+
... obstime=obstimes[np.newaxis, np.newaxis, :])
117+
>>> altaz = coords[np.newaxis, :, np.newaxis].transform_to(frame) # doctest: +REMOTE_DATA
118+
119+
>>> min_altitude = 30 * u.deg
120+
>>> is_above_altitude_limit = (altaz.alt > min_altitude) # doctest: +REMOTE_DATA
121+
>>> is_above_altitude_limit.shape # doctest: +REMOTE_DATA
122+
(25, 100, 50)
123+
124+
..
125+
EXAMPLE END
126+
127+
57128
Improving Performance for Arrays of ``obstime``
58129
-----------------------------------------------
59130

‎docs/whatsnew/6.0.rst‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ In particular, this release includes:
1414

1515
* :ref:`whatsnew-6.0-geodetic-representation-geometry`
1616
* :ref:`whatsnew-6.0-planetary-wcs-bodyfixed`
17+
* :ref:`whatsnew-6.0-broadcasting-frame-attributes`
1718
* :ref:`whatsnew-6.0-cosmology-latex-export`
1819
* :ref:`whatsnew-6.0-iers-data`
1920

@@ -74,6 +75,65 @@ See :ref:`creating_planetary_wcs` for an example.
7475
Planetary images or spectral cube WCS description can be manipulated using the
7576
:mod:`~astropy.wcs` module.
7677

78+
79+
.. _whatsnew-6.0-broadcasting-frame-attributes:
80+
81+
Support for Numpy broadcasting over frame data and attributes
82+
=============================================================
83+
84+
Frames in `astropy.coordinates` now support
85+
:doc:`Numpy broadcasting rules <numpy:user/basics.broadcasting>` over both
86+
frame data and frame attributes. Previously, broadcasting was only supported
87+
over framed data. This makes it much easier and faster to do positional
88+
astronomy calculations and transformations on sweeps of parameters.
89+
90+
For example, the user can now create frame objects with scalar data but vector
91+
frame attributes, such as::
92+
93+
from astropy.coordinates import FK4
94+
from astropy import units as u
95+
96+
FK4(1 * u.deg, 2 * u.deg, obstime=["J2000", "J2001"])
97+
98+
Where this really shines is doing fast observability calculations over arrays.
99+
The following example constructs an `~astropy.coordinates.EarthLocation` array
100+
of length :samp:`{L}`, a `~astropy.coordinates.SkyCoord` array of length
101+
:samp:`{M}`, and a `~astropy.time.Time` array of length :samp:`N`. It uses
102+
Numpy broadcasting rules to evaluate a boolean array of shape
103+
:samp:`({L}, {M}, {N})` that is `True` for those observing locations, times,
104+
and sky coordinates, for which the target is above an altitude limit::
105+
106+
>>> from astropy.coordinates import EarthLocation, AltAz, SkyCoord
107+
>>> from astropy.coordinates.angles import uniform_spherical_random_surface
108+
>>> from astropy.time import Time
109+
>>> from astropy import units as u
110+
>>> import numpy as np
111+
112+
>>> L = 25
113+
>>> M = 100
114+
>>> N = 50
115+
116+
>>> # Earth locations of length L
117+
>>> c = uniform_spherical_random_surface(L)
118+
>>> locations = EarthLocation.from_geodetic(c.lon, c.lat)
119+
120+
>>> # Celestial coordinates of length M
121+
>>> coords = SkyCoord(uniform_spherical_random_surface(M))
122+
123+
>>> # Observation times of length N
124+
>>> obstimes = Time('2023-08-04') + np.linspace(0, 24, N) * u.hour
125+
126+
>>> # AltAz coordinates of shape (L, M, N)
127+
>>> frame = AltAz(
128+
... location=locations[:, np.newaxis, np.newaxis],
129+
... obstime=obstimes[np.newaxis, np.newaxis, :])
130+
>>> altaz = coords[np.newaxis, :, np.newaxis].transform_to(frame) # doctest: +REMOTE_DATA
131+
132+
>>> min_altitude = 30 * u.deg
133+
>>> is_above_altitude_limit = (altaz.alt > min_altitude) # doctest: +REMOTE_DATA
134+
>>> is_above_altitude_limit.shape # doctest: +REMOTE_DATA
135+
(25, 100, 50)
136+
77137
.. _whatsnew-6.0-cosmology-latex-export:
78138

79139
Updates to `~astropy.cosmology`

0 commit comments

Comments
 (0)