[AstroPy] why the warning about Converting Quantity?

Erik Bray embray@stsci....
Fri Mar 22 12:54:41 CDT 2013


Hi Jon,

Since nobody else mentioned this I'll chime in.  You asked "it'd be nice to have 
a less cumbersome way of suppressing those warnings".  The 
u.quantity.WARN_IMPLICIT_NUMERIC_CONVERSION flag that Adrian mentioned is a 
configuration option which can be set permanently in your astropy.cfg file.  See 
http://astropy.readthedocs.org/en/latest/configs/index.html#using-config 
(although admittedly this is non-obvious; perhaps there's still room for 
improvement in the configuration docs for end-users).

In short, find your ~/.astropy/config/astropy.cfg and add

[units.quantity]
warn_implicit_numeric_conversion = False

to permanently disable this warning.  But remember, that places the onus on you 
to be careful about units when converting to types that don't carry the units 
with them (such as Numpy arrays).

Erik

On 03/21/2013 03:22 PM, Jonathan Slavin 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
>>
>>
>>
>



More information about the AstroPy mailing list