-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Open
Description
Description
Pickling a WCS with SIP coefficients results in a WCS with small numerical difference in the coefficients.
There may be more than 1 issue here. Since:
- during pickling the wcs hdulist is written to a
BytesIO(losing precision as noted https://docs.astropy.org/en/stable/io/fits/appendix/faq.html#why-am-i-losing-precision-when-i-assign-floating-point-values-in-the-header) - during WCS.init if provided a header the header is converted to a string and then converted back before reading the SIP keywords
Expected behavior
No loss of precision.
How to Reproduce
import numpy as np
from astropy.wcs import WCS, Sip
import pickle
rng = np.random.default_rng(42)
wcs = WCS(naxis=2)
wcs.wcs.crval = [251.29, 57.58]
wcs.wcs.cdelt = [1, 1]
wcs.wcs.crpix = [507, 507]
wcs.wcs.pc = np.array([[7.7e-6, 3.3e-5], [3.7e-5, -6.8e-6]])
wcs._naxis = [1014, 1014]
wcs.wcs.ctype = ["RA---TAN-SIP", "DEC--TAN-SIP"]
# Generate random SIP coefficients
a = rng.uniform(low=-1e-5, high=1e-5, size=(5, 5))
b = rng.uniform(low=-1e-5, high=1e-5, size=(5, 5))
# Assign SIP coefficients
wcs.sip = Sip(a, b, None, None, wcs.wcs.crpix)
wcs.wcs.set()
pwcs = pickle.loads(pickle.dumps(wcs))
np.testing.assert_equal(wcs.sip.a, pwcs.sip.a)Fails with:
Arrays are not equal
Mismatched elements: 23 / 25 (92%)
Max absolute difference: 9.41396049e-06
Max relative difference: 4.45193698e-14
x: array([[ 5.479121e-06, -1.222431e-06, 7.171958e-06, 3.947361e-06,
-8.116453e-06],
[ 9.512447e-06, 5.222794e-06, 5.721286e-06, -7.437727e-06,...
y: array([[ 5.479121e-06, -1.222431e-06, 7.171958e-06, 3.947361e-06,
-8.116453e-06],
[ 9.512447e-06, 5.222794e-06, 5.721286e-06, -7.437727e-06,...
Versions
## -- End pasted text --
macOS-14.5-arm64-arm-64bit
Python 3.10.6 (main, Oct 3 2022, 15:35:33) [Clang 14.0.0 (clang-1400.0.29.102)]
astropy 6.1.2
Numpy 1.24.0
pyerfa 2.0.1.4
Scipy 1.13.1
Matplotlib not installed
Reactions are currently unavailable