Premature broadcasting of coordinates in transforms creates slowdowns
See original GitHub issueDescription
In summary, when transforming coordinate frames we broadcast before transforming, which ought not to be necessary.
In my PR to add a topocentric frame I noticed some odd behaviour. Suppose obstime is a (N, 1) array of times and we have M targets. Below I create an alt-az frame intended to broadcast against each other:
import numpy as np
from astropy.time import Time
import astropy.units as u
from astropy.coordinates import EarthLocation, CIRS, AltAz, SkyCoord
from astropy.coordinates.tests.utils import randomly_sample_sphere
from astropy.coordinates.erfa_astrom import ErfaAstromInterpolator, erfa_astrom
times = Time('2020-08-21T00:00') + np.linspace(-3*u.hour, 3*u.hour, 1000)
location = EarthLocation.of_site('lapalma')
aa_frame = AltAz(obstime=times[:, np.newaxis], location=location)
# 1000 random locations on the sky
ra, dec, _ = randomly_sample_sphere(200)
coos = SkyCoord(ra, dec)
Now, if I convert this to AltAz it is extremely slow. But it’s much faster if I add CIRS as an intermediate step explicitly, even though CIRS is the intermediate frame between ICRS and AltAz…
coos.transform_to(aa_frame) # 18 seconds
coos.transform_to(CIRS()).transform_to(aa_frame) # 1 second
Note this isn’t like this in the current master, but it illustrates the issue, which I believe is this. If you run coos.transform_to(aa_frame)
the first step is to make a CIRS frame. The attributes of this frame are collected from the players in the transform, so it picks up the obstime from the AltAz frame and the positions from the ICRS frame, so the resulting CIRS frame is shape (N, M). This large frame is pushed through a CIRS->CIRS’ which causes the slowdown.
In the second instance the CIRS->CIRS’ transform is only for a coordinate of shape (M,) which is much faster.
Although this specific example doesn’t affect astropy master, it will if my PR is merged, and @mhvk points out it probably also slows down a single ICRS position -> AltAz at many times transform.
Additional context
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
Another possibility would be to add
obsgeoloc
andobsgeovel
as attributes to the AltAz frame. These could be derived from theEarthLocation
attribute when the frame is instantiated and passed directly to the CIRS frame instead of theEarthLocation
itself.In terms of where the time is being spent:
That transform calls
cirs_to_icrs
and thenicrs_to_cirs
, the bulk of the time is spent in the first call: