Skip to content

Creating a PhysicsSphericalRepresentation with NaNs raises RuntimeWarning #11558

Closed

Description

@nstarman

Description

This is not exactly a bug, but inconsistent behavior between the different spherical representations. For example, the following code will raise a RuntimeWarning (and thus error in pytest) if the representation is PhysicsSpherical, but not Spherical.

from astropy.coordinates import PhysicsSphericalRepresentation
from astropy.coordinates.matrix_utilities import rotation_matrix
import astropy.units as u
import numpy as np

s1 = PhysicsSphericalRepresentation(
    phi=[1, 2] * u.deg, theta=[3, 4] * u.deg, r=[5, np.nan] * u.kpc)
matrix = rotation_matrix(-10, "z", u.deg)

# THIS PART RAISES THE WARNING
expected = (s1.to_cartesian().transform(matrix).represent_as(PhysicsSphericalRepresentation))

The reason for this is in the initialization of PhysicsSphericalRepresentation

    if np.any(self._theta < 0.*u.deg) or np.any(self._theta > 180.*u.deg):

I use this example because it fails the test suite in #11444. @mhvk @adrn

Which is not NaN-"protected".

Expected behavior

A NaN-"protected" check of the angular domain.

Actual behavior

Raises RuntimeWarning.

Steps to Reproduce

See decription

Perhaps a simpler example is

from astropy.coordinates import PhysicsSphericalRepresentation
import astropy.units as u
s1 = PhysicsSphericalRepresentation(phi=[1, 2] * u.deg, theta=[3, np.nan] * u.deg)

System Details

macOS-10.16-x86_64-i386-64bit
Python 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 07:56:27)
[Clang 9.0.1 ]
Numpy 1.18.4
astropy 4.3.dev884+g8c8fe91a6.d20210413
Scipy 1.4.1
Matplotlib 3.4.1

Activity

  1. mhvk commented on Apr 14, 2021

    @mhvk
    Contributor

    the problem is actually a line above, in .wrap_at, and the simpler example should be

    s1 = PhysicsSphericalRepresentation(phi=[1, np.nan] * u.deg, theta=[3, 4] * u.deg, r=[1, 1])
    

    the basic warning is raised by

    np.mod(np.nan, 360)
    
  2. mhvk commented on Apr 14, 2021

    @mhvk
    Contributor

    Oddly, in Longitude._wrap_internal there is an with np.errstate(invalid='ignore') for this, but not in Angle.wrap_at.

  3. mhvk commented on Apr 15, 2021

    @mhvk
    Contributor

    As discussed in #11568, on numpy < 1.19, it is not just the wrapping that causes a warning, but also the comparison of the inclination (as noted on top).

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