[AstroPy] Bug in units decompose()?
Erik Bray
embray at stsci.edu
Tue Jul 1 13:43:32 EDT 2014
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
>
More information about the AstroPy
mailing list