You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Constructing a SphericalRepresentation object with a negative distance is impossible #8091
The explicit kwarg in the error dosen't seem to work.
In [2]: fromastropy.coordinatesimportDistance, SphericalRepresentation; importastropy.unitsasuIn [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'.
@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.
@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?
@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.
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.)>
Activity
Cadair commentedon Nov 8, 2018
@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.
mhvk commentedon Nov 9, 2018
Yes, that's a bug: in the initializer, it should skip the conversion to a
Distanceif thedistanceis already of that class, ie.,Should have
mhvk commentedon Nov 9, 2018
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.
adrn commentedon Oct 23, 2019
@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
Distanceout of theSphericalRepresentationclass? So you would propose allowing any distance value inSphericalRepresentationby enforcing that it is a pure quantity, and only usingDistancein the frame classes?pllim commentedon Oct 23, 2019
@adrn , you could use the permalink, you know? 😉
astropy/astropy/coordinates/representation.py
Line 140 in b96c6f3
Ref: https://help.github.com/en/github/managing-files-in-a-repository/getting-permanent-links-to-files
mhvk commentedon Oct 23, 2019
@adrn - yes, indeed, I would just use
QuantityinSphericalRepresentation, since it can be used with other units than distance (outside of coordinates). And then instead useDistanceinside frames.astrojuanlu commentedon Dec 20, 2020
This was fixed in #11113: