Q regarding equality tests with wcs_world2pix()

Hello. I am writing some unit tests for a bit of software I’m working on, and one of the functions I’ve written uses astropy.wcs to create image headers. I’ve written a simple unit test to validate the output, but it’s failing for reasons I can’t quite understand. Here is some sample code to reproduce the problem:

from astropy.wcs import WCS

pxScale = 1/3600.
wcs_dict = {'CTYPE1': 'RA---TAN',
                    'CTYPE2': 'DEC--TAN',
                    'CRVAL1': 180.,
                    'CRVAL2': 0.,
                    'CRPIX1': 250,
                    'CRPIX2': 300,
                    'CD1_1': -pxScale,
                    'CD1_2': 0.0,
                    'CD2_1': 0.0,
                    'CD2_2': pxScale}
w = WCS(wcs_dict)

cen = w.wcs_world2pix(180., 0., 1)

assert cen[0] == 250.
assert cen[1] == 300.

The first assertion succeeds for me, but the second fails. cen[1] here seems to be exactly the same data type as cen[0] (numpy.ndarray), so I can’t imagine why the first would succeed but the second wouldn’t.

Does anyone have a good explanation for this?

You are comparing floating point values:

In [3]: cen[1] - 300.
Out[3]: -1.261923898709938e-11

You should use numpy.isclose or similar to do the comparison.

1 Like

So it was that simple. I suppose I got confused by the fact that assert cen[0] == 250. worked, then (also with any other number for the x origin). In all of my tests, it was only ever the y-axis that was failing.

Thanks for clearing that up!