question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

match_to_catalog_sky should raise a nicer error when the match or catalog coordinates have nan values

See original GitHub issue

I believe this is related to this SO question filed a few months ago.

Basically, match_to_catalog_sky() is throwing an IndexError and I can’t figure out why. I’m using Python 3.7.3 and astropy 3.1.2.

Here’s the code to reproduce it (data files):

from astropy.io import ascii
from astropy.coordinates import SkyCoord
from astropy import units as u

d1 = ascii.read('c1.dat')
ra_obs, dec_obs = d1['ra'], d1['dec']
d2 = ascii.read('c2.dat')
ra_qry, dec_qry = d2['ra'], d2['dec']

c1 = SkyCoord(ra_obs, dec_obs, unit=(u.degree, u.degree))
c2 = SkyCoord(ra_qry, dec_qry, unit=(u.degree, u.degree))

idx, d2d, d3d = c1.match_to_catalog_sky(c2)

The full traceback is:

$ python test.py
/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/angles.py:642: RuntimeWarning: invalid value encountered in less
  if np.any(self_angle < wrap_angle_floor) or np.any(self_angle >= wrap_angle):
/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/angles.py:642: RuntimeWarning: invalid value encountered in greater_equal
  if np.any(self_angle < wrap_angle_floor) or np.any(self_angle >= wrap_angle):
/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/angles.py:529: RuntimeWarning: invalid value encountered in less
  if np.any(angles.value < lower) or np.any(angles.value > upper):
/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/angles.py:529: RuntimeWarning: invalid value encountered in greater
  if np.any(angles.value < lower) or np.any(angles.value > upper):
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    idx, d2d, d3d = c1.match_to_catalog_sky(c2)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/sky_coordinate.py", line 1045, in match_to_catalog_sky
    storekdtree='_kdtree_sky')
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/matching.py", line 154, in match_coordinates_sky
    idx, sep2d, sep3d = match_coordinates_3d(newmatch_u, newcat_u, nthneighbor, storekdtree)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/matching.py", line 83, in match_coordinates_3d
    sep2d = catalogcoord[idx].separation(matchcoord)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/utils/misc.py", line 940, in __getitem__
    return self._apply('__getitem__', item)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/baseframe.py", line 1476, in _apply
    new._data = apply_method(self.data)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/baseframe.py", line 1447, in apply_method
    return value._apply(method, *args, **kwargs)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/representation.py", line 780, in _apply
    rep = super()._apply(method, *args, **kwargs)
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/coordinates/representation.py", line 263, in _apply
    apply_method(getattr(self, component)))
  File "/home/gabriel/miniconda3/envs/py3/lib/python3.7/site-packages/astropy/units/quantity.py", line 1035, in __getitem__
    out = super().__getitem__(key)
IndexError: index 22670 is out of bounds for axis 0 with size 22670

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
smohcommented, Jul 14, 2019

I think it’s because you have nan entries in c2. Not sure if this was also the cause for the SO question but it seems to work fine once you clean nans in c2.

clean = (~np.isnan(c2.ra) & ~np.isnan(c2.dec))
print(len(c2), clean.sum(), len(c2)-clean.sum())
c2_clean = c2[clean]
22670 12660 10010
idx, d2d, d3d = c1.match_to_catalog_sky(c2_clean)

The nan entries seems to completely mess up KDTree built from them making it return dist=inf and idx=(lenth of catalogcoord) for any query. Not sure if this was handled in previous versions of astropy.

0reactions
smohcommented, Jul 14, 2019

(Oops, just saw @adrn mentioned _3d methods already)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catalog Matching Using Astropy.Coordinates - ADocLib
I'm using Python 3.7.3 and astropy 3.1.2.should raise a nicer error when the match or catalog coordinates have nan values #8985. matchtocatalogsky ...
Read more >
SkyCoord — Astropy v5.2
High-level object providing a flexible interface for celestial coordinate representation, manipulation, and transformation between systems. The SkyCoord class ...
Read more >
1. A short course in PyVO 2. Prerequisites 3. What's the VO
This course will introduce you to the primary concepts of PyVO, an astropy-affiliated package for accessing Virtual Observatory services from Python.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found