Skip to content

spherical_offsets_to gives error with array: AttributeError: 'Quantity' object has no 'hypot' member #5163

Closed

Description

@richardgmcmahon

I have followed the example at:

http://docs.astropy.org/en/stable/coordinates/matchsep.html#matching-catalogs

my example/test is:

import astropy
print('astropy.__version__:', astropy.__version__)

from astropy.table import Table
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.coordinates import match_coordinates_sky

from astroquery.irsa import Irsa
from astroquery.sdss import SDSS

c = SkyCoord(ra=ra1*u.degree, dec=dec1*u.degree, distance=distance1*u.kpc)  
catalog = SkyCoord(ra=ra2*u.degree, dec=dec2*u.degree, distance=distance2*u.kpc)  
idx, d2d, d3d = c.match_to_catalog_3d(catalog)  

matches = catalog[idx]  
dra, ddec = c.spherical_offsets_to(matches) 

no specfic catalogue is used. In my example I use astroquery as shown below:

region_center = SkyCoord(9.81625*u.deg, 0.88806*u.deg, frame='icrs')
allwise_irsa = Irsa.query_region(coordinates=region_center, radius=4*u.arcmin,
                            catalog='wise_allwise_p3as_psd')
print('Number of ALLWISE sources:', len(allwise_irsa))

sdss = SDSS.query_region(coordinates=region_center, radius=4*u.arcmin,
                         spectro=False,
                         photoobj_fields=['ra','dec','u','g','r','i','z'])

ra = allwise_irsa['ra']
dec = allwise_irsa['dec']
radec_allwise = SkyCoord(ra, dec,unit=(u.degree, u.degree), frame='icrs')

ra = sdss['ra']
dec = sdss['dec']
radec_sdss = SkyCoord(ra, dec,unit=(u.degree, u.degree), frame='icrs')

idx, d2d, d3d = match_coordinates_sky(radec_allwise, radec_sdss)

idx, d2d, d3d = radec_allwise.match_to_catalog_sky(radec_sdss)
print('Number of xmatch sources:', len(idx))

matches = radec_sdss[idx]
print('Test single source')
print(matches[0])
print(radec_allwise[0])
dra, ddec = radec_allwise[0].spherical_offsets_to(matches[0])
print('dra, ddec:', dra.arcsec, ddec.arcsec)

print('Test array')
dra, ddec = radec_allwise.spherical_offsets_to(matches)

I am using astropy 1.2.1

Traceback (most recent call last):
  File "spherical_offsets_to_test.py", line 55, in <module>
    dra, ddec = radec_allwise.spherical_offsets_to(matches)
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/coordinates/sky_coordinate.py", line 747, in spherical_offsets_to
    acoord = tocoord.transform_to(aframe)
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/coordinates/sky_coordinate.py", line 403, in transform_to
    new_coord = trans(self.frame, generic_frame)
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/coordinates/transformations.py", line 915, in __call__
    curr_coord = t(curr_coord, curr_toframe)
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/coordinates/transformations.py", line 833, in __call__
    newrep = newrep.represent_as(fromcoord.data.__class__)
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/coordinates/representation.py", line 90, in represent_as
    return other_class.from_cartesian(self.to_cartesian())
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/coordinates/representation.py", line 421, in from_cartesian
    s = np.hypot(cart.x, cart.y)
  File "/home/rgm/anaconda/lib/python2.7/site-packages/astropy/units/quantity.py", line 753, in __getattr__
    attr))
AttributeError: 'Quantity' object has no 'hypot' member`

Activity

  1. mhvk commented on Jul 8, 2016

    @mhvk
    Contributor

    This is most bizarre. Is it possible to make a simple example without astroquery? Also, what numpy version are you using?

  2. richardgmcmahon commented on Jul 8, 2016

    @richardgmcmahon
    Author

    OK, I have used raw numpy and get the same error: see below; i have added a test using the arrays for separation and position_angle and theu seem to work; so the problem is specific to spherical_offsets_to.

    I have poked around in the traceback but it is not obviuos to me what is causing the error: AttributeError: 'Quantity' object has no 'hypot' member

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    """
    http://docs.astropy.org/en/stable/coordinates/matchsep.html#matching-catalogs
    
    """
    
    import numpy as np
    
    import astropy
    print('astropy.__version__:', astropy.__version__)
    
    from astropy import units as u
    from astropy.coordinates import SkyCoord
    from astropy.coordinates import match_coordinates_sky
    
    def test1():
        """
    
        """
        ra_center = 180.0
        dec_center = -60.0
    
        width = 1.0
        ra_width = width * np.cos(np.deg2rad(dec_center))
        dec_width = width
        npoints = 10
        ra1 = np.random.uniform(ra_center - ra_width / 2.0,
                                ra_center + ra_width / 2.0,
                                npoints)
    
        dec1 = np.random.uniform(dec_center - dec_width / 2.0,
                                 dec_center + dec_width / 2.0,
                                 npoints)
    
        ra2 = np.random.uniform(ra_center - ra_width / 2.0,
                                ra_center + ra_width / 2.0,
                                npoints)
    
        dec2 = np.random.uniform(dec_center - dec_width / 2.0,
                                 dec_center + dec_width / 2.0,
                                 npoints)
        print('ra1 range:', np.min(ra1), np.max(ra1))
        print('dec1 range:', np.min(dec1), np.max(dec1))
        print('ra2 range:', np.min(ra2), np.max(ra2))
        print('dec2 range:', np.min(dec2), np.max(dec2))
    
        c = SkyCoord(ra=ra1 * u.degree, dec=dec1 * u.degree)
        catalog = SkyCoord(ra=ra2 * u.degree, dec=dec2 * u.degree)
    
        idx, d2d, d3d = c.match_to_catalog_sky(catalog)
        matches = catalog[idx]
    
        sep = c.separation(matches)
        print('Separation range (arcsecs):', np.min(sep.arcsec), np.max(sep.arcsec))
    
        pa = c.position_angle(matches)
        print('PA range (degrees):', np.min(pa.degree), np.max(pa.degree))
    
        dra, ddec = c.spherical_offsets_to(matches)
    
    if __name__ == "__main__":
        """
    
        """
    
        test1()
  3. mhvk commented on Aug 11, 2016

    @mhvk
    Contributor

    @richardgmcmahon - I've been slow here, but trying to simplify your example further, I see that spherical_offsets_to fails for any non-scalar SkyCoord:

    from astropy.coordinates import SkyCoord
    sc = SkyCoord('12h00m00s', '+10d00m00s')
    sc.spherical_offsets_to(sc)             
    # (<Angle 2.5057879273476366e-14 deg>, <Angle 0.0 deg>)
    sc = SkyCoord(['12h00m00s'], ['+10d00m00s'])
    sc.spherical_offsets_to(sc)                 
    # AttributeError: 'Quantity' object has no 'hypot' member
    

    The problem is that a wrong transformation matrix is created, full of Quantity objects. It definitely is higher up than a problem with Quantity...

    Still investigating further...

  4. mhvk commented on Aug 11, 2016

    @mhvk
    Contributor

    OK, this actually happens to be solved already in current master (perhaps somewhat inadvertently...), due to my removal of the use of numpy matrices (#5104). This will be in astropy 1.3, but I don't know whether we now should consider back-porting it to 1.2 (or even 1.0?). @eteq, @astrofrog?

  5. added and removed on Aug 11, 2016
  6. mhvk commented on Aug 11, 2016

    @mhvk
    Contributor

    p.s. At the very least we should include some array-shaped test cases, so that we don't regress on this...

  7. eteq commented on Nov 29, 2016

    @eteq
    Member

    Given how close 1.3 is, I think we can probably call this closed if it's fixed for 1.3? spherical_offsets_to is not in 1.0.x, so no need to port it there...

  8. added this to the v1.3.0 milestone on Nov 29, 2016
  9. eteq commented on Nov 29, 2016

    @eteq
    Member

    That is, the actual underlying issue. I created #5163 to implement the regression test @mhvk suggested above.

  10. added
    Close?Tell stale bot that this issue/PR is stale
    on Nov 29, 2016
  11. added a commit that references this issue on Nov 29, 2016
  12. eteq commented on Nov 29, 2016

    @eteq
    Member

    With #5519 in and passing, I think this is OK to close, so I will do so. But @richardgmcmahon, if you're still seeing the error in the lastest master, feel free to re-open this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClose?Tell stale bot that this issue/PR is stalecoordinates

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions