sot / chandra_aca

Chandra Aspect Camera Tools
https://sot.github.io/chandra_aca
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Performance improvements for planets #138

Closed taldcroft closed 10 months ago

taldcroft commented 1 year ago

Description

This is part of the changes to kadi, chandra_aca, ska_sun, Quaternion, and Chandra.Maneuver to make kadi command states faster.

This change is to use convert_time_format in place of CxoTime or DateTime. Some slight re-ordering of code logic was needed but the changes should be fairly understandable.

This PR also avoids using astropy units in this context. While units are nice and elegant, regular floats / np.arrays are much faster.

Interface impacts

Testing

Unit tests

Independent check of unit tests by Jean

Functional tests

Functional performance improvements

About a factor of two for these cases.

Setup:

>>> from chandra_aca.planets import get_planet_eci, get_planet_angular_sep
>>> from cxotime import CxoTime
>>> times = CxoTime("2023:001").secs + np.linspace(0, 1000, 100)

Flight:

>>> %timeit dat = get_planet_eci("mars", times)
823 µs ± 15.5 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
>>> %timeit  get_planet_angular_sep("mars", time=times, ra=0, dec=1)
860 µs ± 4.5 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

PR:

>>> %timeit get_planet_eci("mars", times)
414 µs ± 2.94 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
>>> %timeit  get_planet_angular_sep("mars", time=times, ra=0, dec=1)
430 µs ± 1.27 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

convert_time_format() reference

For reference, here are timing comparisons showing that convert_time_format is indeed faster than DateTime for common cases:

In [1]: from Chandra.Time import DateTime

In [2]: from cxotime import convert_time_format, CxoTime

In [3]: %timeit DateTime(100.0).jd
9.26 µs ± 84.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [4]: timeit convert_time_format(100.0, "jd")
4.54 µs ± 27 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [5]: %timeit DateTime("2023:001:01:01:01.000").jd
16.4 µs ± 76.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [6]: %timeit convert_time_format("2023:001:01:01:01.000", "jd")
10.1 µs ± 1.3 µs per loop (mean ± std. dev. of 7 runs, 100,000 loops each)