[AstroPy] Bug in units decompose()?

John Quinn john.quinn at ucd.ie
Tue Jul 1 16:14:16 EDT 2014


Thanks for the responses. 

I think I can use to()  to convert to other units and that will do what I want.

I was confused after reading the documentation at:
http://docs.astropy.org/en/stable/units/decomposing_and_composing.html#reducing-a-unit-to-its-irreducible-parts
and found that:
> u.J.compose()
[Unit("J"), Unit("1e+07 erg"), Unit("4.58743e+17 Ry"), Unit("6.24151e+18 eV")]

> u.Ry.decompose(bases=[u.m, u.N]) 
2.17987e-18 m N

> u.eV.decompose(bases=[u.m, u.N])
1.60218e-19 m N

but:

> u.erg.decompose(bases=[u.m, u.N])
gives an error. 

Similarly, converting to J sometimes works and sometimes doesn't, and I was very surprised that decomposing
something in J to J gave an error.  

Regards,

John





On 1 Jul 2014, at 18:43, Erik Bray <embray at stsci.edu> wrote:

> If I understand correctly this is because u.J is already defined in terms of 
> kilograms, meters, and seconds (actually Newtons and meters).  You could 
> redefine J as a new unit, and then redefine those other units relative to the new J.
> 
> But I'm not entirely clear exactly what the intent is here.  I do agree the 
> error is a little confusing. But not as much if you know that Joule is already a 
> compound unit, and it's trying to recursively decompose the units that comprise 
> it in terms of Joules, hence the confusion.
> 
> Erik
> 
> On 07/01/2014 11:18 AM, John Quinn wrote:
>> Hello,
>> 
>> I am currently evaluating iPython Notebook as a replacement for MATHCAD in our
>> teaching laboratories
>> and am particularly interested in using the units and constants packages of
>> astropy.
>> 
>> I have encountered an apparent bug using the units decompose function and have
>> reduced it to the
>> following example:
>> 
>>> from astropy import units as u
>>> u.J.decompose(bases=[u.J])
>> 
>> which produces the errors given below.
>> 
>> Similarly, I cannot decompose a unit consisting of 1 u.N * u.m into u.J, but
>> strangely u.Ry.decompose(bases=[u.J]) works fine.
>> 
>> Am I missing something obvious or is this a bug?
>> 
>> I am using astropy 0.3.2 with python 3.4.1 on Mac OS X  (anaconda). I see the
>> same on the Linux and Windows
>> versions and with Python 2.7.7.
>> 
>> Thanks,
>> 
>> John
>> 
>> ---------------------------------------------------------------------------
>> UnitsError                                 Traceback (most recent call last)
>> <ipython-input-54-3aa084c7cc08>  in<module>()
>> ----> 1  u.J.decompose(bases=[u.J])
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  indecompose(self, bases)
>>    1837
>>    1838      def  decompose(self,  bases=set()):
>> -> 1839          return  self._represents.decompose(bases=bases)
>>    1840      decompose.__doc__=  UnitBase.decompose.__doc__
>>    1841
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  indecompose(self, bases)
>>    1999
>>    2000          x = CompositeUnit(self.scale, self.bases, self.powers, decompose=True,
>> -> 2001                            decompose_bases=bases)
>>    2002          if  len(bases)  ==  0:
>>    2003              self._decomposed_cache=  x
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  in__init__(self, scale, bases, powers, decompose, decompose_bases, _error_check)
>>    1897          self._powers=  powers
>>    1898          self._decomposed_cache=  None
>> -> 1899          self._expand_and_gather(decompose=decompose,  bases=decompose_bases)
>>    1900
>>    1901      def  __repr__(self):
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  in_expand_and_gather(self, decompose, bases)
>>    1959          for  b,  pin  zip(self.bases,  self.powers):
>>    1960              if  decomposeand  bnot  in  bases:
>> -> 1961                  b  =  b.decompose(bases=bases)
>>    1962
>>    1963              if  isinstance(b,  CompositeUnit):
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  indecompose(self, bases)
>>    1837
>>    1838      def  decompose(self,  bases=set()):
>> -> 1839          return  self._represents.decompose(bases=bases)
>>    1840      decompose.__doc__=  UnitBase.decompose.__doc__
>>    1841
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  indecompose(self, bases)
>>    1999
>>    2000          x = CompositeUnit(self.scale, self.bases, self.powers, decompose=True,
>> -> 2001                            decompose_bases=bases)
>>    2002          if  len(bases)  ==  0:
>>    2003              self._decomposed_cache=  x
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  in__init__(self, scale, bases, powers, decompose, decompose_bases, _error_check)
>>    1897          self._powers=  powers
>>    1898          self._decomposed_cache=  None
>> -> 1899          self._expand_and_gather(decompose=decompose,  bases=decompose_bases)
>>    1900
>>    1901      def  __repr__(self):
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  in_expand_and_gather(self, decompose, bases)
>>    1959          for  b,  pin  zip(self.bases,  self.powers):
>>    1960              if  decomposeand  bnot  in  bases:
>> -> 1961                  b  =  b.decompose(bases=bases)
>>    1962
>>    1963              if  isinstance(b,  CompositeUnit):
>> 
>> /Users/quinn/anaconda/envs/py34/lib/python3.4/site-packages/astropy/units/core.py  indecompose(self, bases)
>>    1573              raise UnitsError(
>>    1574                  "Unit {0} can not be decomposed into the requested"
>> -> 1575                  "bases".format(self))
>>    1576
>>    1577          return  self
>> 
>> UnitsError: Unit kg can not be decomposed into the requested bases
>> 
>> 
>> 
>> 
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy at scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
>> 
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy

Dr. John Quinn
School of Physics
Science Center North
University College Dublin
Belfield, Dublin 4
Ireland

T: +353-1-7162278
F: +353-1-2837275
john.quinn at ucd.ie



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20140701/d20581a1/attachment.html>


More information about the AstroPy mailing list