[AstroPy] why the warning about Converting Quantity?
Erik Tollerud
erik.tollerud@gmail....
Fri Mar 22 09:57:35 CDT 2013
Hello Jonathan et al.,
I think some confusion here stems from the fact that ``AU`` is a bit
of a special constant - in most cases, you're probably better off
using the AU *unit*, rather than the constant. That is, if you were
doing something like
arrinau = arr * constants.au
That gives exactly the warning you mentioned, whereas
arrinau = arr * units.au
won't give out a warning, because it creates a `Quantity` object with
the units `AU`, rather than a regular array without any units.
Does that make sense, or am I still misunderstanding how you're using
this? If the latter, can you give an actual example of the line that
actually produces this warning? That'll make it easier to understand
exactly what you're suggesting.
On Thu, Mar 21, 2013 at 3:22 PM, Jonathan Slavin
<jslavin@cfa.harvard.edu> wrote:
> Hi Adrian,
>
> Yes, as I mentioned farther down in my post, the warnings are generated
> when I use it in an expression -- with numpy arrays. It'd be nice to
> have a less cumbersome way of suppressing those warnings (yes it's just
> one line, but not so easy to remember). In fact, I would expect to use
> the constants mostly in expressions with numpy arrays, variables, etc.
> that don't have units, so my preference would be that by default those
> warnings are turned off -- at least for the constants. The constants
> are useful for their values alone, though clearly the value of the units
> module is to associate units with values. Maybe a simple method like
> value.asfloat() could be added that would allow conversion from a units
> object to a numpy float.
>
> Jon
>
> On Thu, 2013-03-21 at 15:05 -0400, Adrian Price-Whelan wrote:
>> Hey Jonathan --
>>
>> Are you sure that's the point in the code that's producing the
>> warning? What operations are you doing with the AU object? When you
>> use the .to() method, it still returns a Quantity object -- *not* the
>> value of the original object in those units. If you then stick AU into
>> any numpy function, it will work but will just extract the value from
>> the object (hence the warning).
>>
>> See, for example:
>>
>> >>> import astropy.units as u
>> >>> np.sqrt(15*u.km)
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> 3.872983346207417
>>
>> To turn this off, you'll want to set:
>> >>> u.quantity.WARN_IMPLICIT_NUMERIC_CONVERSION.set(False)
>> >>> np.sqrt(15*u.km)
>> 3.872983346207417
>>
>> Note that this is documented in the astropy.units.quantity section of
>> the astropy documentation:
>> http://docs.astropy.org/en/latest/units/quantity.html#converting-to-python-or-numpy-types
>>
>> Thanks,
>> Adrian
>>
>> On Mar 21, 2013, at 2:20 PM, Jonathan Slavin wrote:
>>
>> > Hi,
>> >
>> > I have just used the constants module in astropy for the first time and
>> > got 6 warnings from my code as a result:
>> >
>> > import astropy.constants as const
>> > AU = const.au.to('km')
>> >
>> > leads to
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> >
>> > It seems that I get the warning every place I use the value of AU. Do I
>> > have to turn off warnings to prevent this? Is there some other way to
>> > deal with this?
>> >
>> > Jon
>> > --
>> > ______________________________________________________________
>> > Jonathan D. Slavin Harvard-Smithsonian CfA
>> > jslavin@cfa.harvard.edu 60 Garden Street, MS 83
>> > phone: (617) 496-7981 Cambridge, MA 02138-1516
>> > cell: (781) 363-0035 USA
>> > ______________________________________________________________
>> >
>> > _______________________________________________
>> > AstroPy mailing list
>> > AstroPy@scipy.org
>> > http://mail.scipy.org/mailman/listinfo/astropy
>>
>> --
>> Adrian Price-Whelan
>> Department of Astronomy
>> Columbia University
>>
>>
>>
>
> --
> ______________________________________________________________
> Jonathan D. Slavin Harvard-Smithsonian CfA
> jslavin@cfa.harvard.edu 60 Garden Street, MS 83
> phone: (617) 496-7981 Cambridge, MA 02138-1516
> cell: (781) 363-0035 USA
> ______________________________________________________________
>
> _______________________________________________
> AstroPy mailing list
> AstroPy@scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
--
Erik
On Thu, Mar 21, 2013 at 3:22 PM, Jonathan Slavin
<jslavin@cfa.harvard.edu> wrote:
> Hi Adrian,
>
> Yes, as I mentioned farther down in my post, the warnings are generated
> when I use it in an expression -- with numpy arrays. It'd be nice to
> have a less cumbersome way of suppressing those warnings (yes it's just
> one line, but not so easy to remember). In fact, I would expect to use
> the constants mostly in expressions with numpy arrays, variables, etc.
> that don't have units, so my preference would be that by default those
> warnings are turned off -- at least for the constants. The constants
> are useful for their values alone, though clearly the value of the units
> module is to associate units with values. Maybe a simple method like
> value.asfloat() could be added that would allow conversion from a units
> object to a numpy float.
>
> Jon
>
> On Thu, 2013-03-21 at 15:05 -0400, Adrian Price-Whelan wrote:
>> Hey Jonathan --
>>
>> Are you sure that's the point in the code that's producing the
>> warning? What operations are you doing with the AU object? When you
>> use the .to() method, it still returns a Quantity object -- *not* the
>> value of the original object in those units. If you then stick AU into
>> any numpy function, it will work but will just extract the value from
>> the object (hence the warning).
>>
>> See, for example:
>>
>> >>> import astropy.units as u
>> >>> np.sqrt(15*u.km)
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> 3.872983346207417
>>
>> To turn this off, you'll want to set:
>> >>> u.quantity.WARN_IMPLICIT_NUMERIC_CONVERSION.set(False)
>> >>> np.sqrt(15*u.km)
>> 3.872983346207417
>>
>> Note that this is documented in the astropy.units.quantity section of
>> the astropy documentation:
>> http://docs.astropy.org/en/latest/units/quantity.html#converting-to-python-or-numpy-types
>>
>> Thanks,
>> Adrian
>>
>> On Mar 21, 2013, at 2:20 PM, Jonathan Slavin wrote:
>>
>> > Hi,
>> >
>> > I have just used the constants module in astropy for the first time and
>> > got 6 warnings from my code as a result:
>> >
>> > import astropy.constants as const
>> > AU = const.au.to('km')
>> >
>> > leads to
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> > WARNING: Converting Quantity object in units 'km' to a Numpy array
>> > [astropy.units.quantity]
>> >
>> > It seems that I get the warning every place I use the value of AU. Do I
>> > have to turn off warnings to prevent this? Is there some other way to
>> > deal with this?
>> >
>> > Jon
>> > --
>> > ______________________________________________________________
>> > Jonathan D. Slavin Harvard-Smithsonian CfA
>> > jslavin@cfa.harvard.edu 60 Garden Street, MS 83
>> > phone: (617) 496-7981 Cambridge, MA 02138-1516
>> > cell: (781) 363-0035 USA
>> > ______________________________________________________________
>> >
>> > _______________________________________________
>> > AstroPy mailing list
>> > AstroPy@scipy.org
>> > http://mail.scipy.org/mailman/listinfo/astropy
>>
>> --
>> Adrian Price-Whelan
>> Department of Astronomy
>> Columbia University
>>
>>
>>
>
> --
> ______________________________________________________________
> Jonathan D. Slavin Harvard-Smithsonian CfA
> jslavin@cfa.harvard.edu 60 Garden Street, MS 83
> phone: (617) 496-7981 Cambridge, MA 02138-1516
> cell: (781) 363-0035 USA
> ______________________________________________________________
>
> _______________________________________________
> AstroPy mailing list
> AstroPy@scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
--
Erik Tollerud
More information about the AstroPy
mailing list