Skip to content

units: warning from numpy operation on Quantity arithmetic #3240

Closed

Description

@adrn

The following gives a warning:

from astropy import units as u
import numpy as np

a = 15 * u.kpc
b = 27 * u.pc

print np.arcsin((b/a))

but doing this does not:

print np.arcsin((b/a).decompose())

The warning is:

RuntimeWarning: invalid value encountered in arcsin
  print np.arcsin((b/a))

Activity

  1. anuraghota commented on Dec 19, 2014

    @anuraghota

    Because we already set the parameters,

    a = 15 * u.kpc
    b = 27 * u.pc
    
    print np.arcsin(b/a)
    

    will not give warning. (Note the extra brackets removed)
    instead of -

    print np.arcsin((b/a))
    

    Suppose we write

    print np.arcsin((27 * u.pc)/(15 * u.kpc))
    

    This will work good!

  2. mhvk commented on Dec 19, 2014

    @mhvk
    Contributor

    Careful -- the warning only appears once... it has nothing to do with double parentheses.

    Bizarre.

  3. mhvk commented on Dec 19, 2014

    @mhvk
    Contributor

    OK, I think I got it: the problem is that astropy calls np.arcsin twice, once with just the value of the quantity, and another time with the value properly scaled. So, since b/a=1.8 pc / kpc, it first calls np.arcsin(1.8) (which raises the warning and returns nan), then discards this result, and then recalculates np.arcsin(0.0018).

    With current numpy, this is actually unavoidable: we are allowed to prepare output arrays in __array_prepare__ and do corrections to the output in __array_wrap__ but we cannot take into account the units of the inputs before the actual calculation (so, we recalculate in __array_wrap__ if needed). This will only change with numpy 1.10 and its __numpy_ufunc__ method (see #2583).

    But this issue suggests we should silence numpy warnings for the initial calculation. Not completely sure if this can easily be done safely (i.e., ensuring that a mistimed ctrl-C doesn't mess up things). @mdboom, @embray?

  4. embray commented on Dec 19, 2014

    @embray
    Member

    Yeah, that's tricky--we could silence the warning in __array_prepare__ and restore it later, but a lot could go wrong there. That said, the worst that can happen is the warning would be permanently silenced for the rest of the user's session. Other things could be done to try to avoid this like registering a signal handler and such but it's all probably more effort than it's worth. I'd lean toward just documenting this as a known issue for Numpy < 1.10.

  5. adrn commented on Jul 3, 2016

    @adrn
    MemberAuthor

    I still see this warning in the current Astropy master with Numpy 1.11.1 -- from what I understand in reading #2583 , this is because the __numpy_ufunc__ stuff got pushed to Numpy 1.12?

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