Skip to content

Constructing a SphericalRepresentation object with a negative distance is impossible #8091

Closed

Description

@Cadair

The explicit kwarg in the error dosen't seem to work.

In [2]: from astropy.coordinates import Distance, SphericalRepresentation; import astropy.units as u                                                                                                                                 
In [3]: SphericalRepresentation(10*u.deg, 10*u.deg, Distance(-100*u.m, allow_negative=True))    
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/Git/astropy/astropy/coordinates/representation.py in __init__(self, lon, lat, distance, differentials, copy)
   1569             try:
-> 1570                 self._distance = Distance(self._distance, copy=False)
   1571             except ValueError as e:

~/Git/astropy/astropy/coordinates/distances.py in __new__(cls, value, unit, z, cosmology, distmod, parallax, dtype, copy, order, subok, ndmin, allow_negative)
    183         if not allow_negative and np.any(distance.value < 0):
--> 184             raise ValueError("Distance must be >= 0.  Use the argument "
    185                              "'allow_negative=True' to allow negative values.")

ValueError: Distance must be >= 0.  Use the argument 'allow_negative=True' to allow negative values.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-3-bc5d4ae3542b> in <module>
----> 1 SphericalRepresentation(10*u.deg, 10*u.deg, Distance(-100*u.m, allow_negative=True))

~/Git/astropy/astropy/coordinates/representation.py in __init__(self, lon, lat, distance, differentials, copy)
   1571             except ValueError as e:
   1572                 if e.args[0].startswith('Distance must be >= 0'):
-> 1573                     raise ValueError("Distance must be >= 0. To allow negative "
   1574                                      "distance values, you must explicitly pass"
   1575                                      " in a `Distance` object with the the "

ValueError: Distance must be >= 0. To allow negative distance values, you must explicitly pass in a `Distance` object with the the argument 'allow_negative=True'.

Activity

  1. Cadair commented on Nov 8, 2018

    @Cadair
    MemberAuthor

    @adrn @eteq This is actually only an issue in SunPy's test suite (which was using unphysical numbers) but as far as I can tell it is actually a real bug that should be fixed anyway.

  2. mhvk commented on Nov 9, 2018

    @mhvk
    Contributor

    Yes, that's a bug: in the initializer, it should skip the conversion to a Distance if the distance is already of that class, ie.,

            if self._distance.unit.physical_type == 'length':
                try:
                    self._distance = Distance(self._distance, copy=False)
    

    Should have

    if not isinstance(self._distance, Distance) and self._distance.unit.physical_type == 'length':
    
  3. mhvk commented on Nov 9, 2018

    @mhvk
    Contributor

    p.s. Though frankly I'd prefer it if we moved this use of distance out of the representation class altogether, and instead did it inside frames.

  4. added this to the v4.0 milestone on Jul 18, 2019
  5. adrn commented on Oct 23, 2019

    @adrn
    Member

    @mhvk the quick fix you proposed doesn't work because the negative distance still hits this line. Could you say more about your thoughts on moving Distance out of the SphericalRepresentation class? So you would propose allowing any distance value in SphericalRepresentation by enforcing that it is a pure quantity, and only using Distance in the frame classes?

  6. pllim commented on Oct 23, 2019

    @pllim
    Member

    @adrn , you could use the permalink, you know? 😉

    attrs = [self.attr_classes[component](attr, copy=copy)

    Ref: https://help.github.com/en/github/managing-files-in-a-repository/getting-permanent-links-to-files

  7. mhvk commented on Oct 23, 2019

    @mhvk
    Contributor

    @adrn - yes, indeed, I would just use Quantity in SphericalRepresentation, since it can be used with other units than distance (outside of coordinates). And then instead use Distance inside frames.

  8. removed this from the v4.0 milestone on Nov 14, 2019
  9. astrojuanlu commented on Dec 20, 2020

    @astrojuanlu
    Member

    This was fixed in #11113:

    In [1]: from astropy.coordinates import Distance, SphericalRepresentation; import astropy.units as u
    
    In [2]: SphericalRepresentation(10*u.deg, 10*u.deg, Distance(-100*u.m, allow_negative=True))
    Out[2]: 
    <SphericalRepresentation (lon, lat, distance) in (deg, deg, m)
        (10., 10., -100.)>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions