已知问题¶

已知缺陷¶

一些操作会使数量失去单位¶

>>> import astropy.units as u
>>> import numpy as np
>>> q = u.Quantity(np.arange(10.), u.m)
>>> np.dot(q,q)
285.0
>>> np.hstack((q,q))
<Quantity [0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 0., 1., 2., 3., 4., 5.,
6., 7., 8., 9.] (Unit not initialised)>


>>> ratio = (3600 * u.s) / (1 * u.h)
>>> ratio
<Quantity 3600. s / h>
>>> np.array(ratio)
array(3600.)
>>> np.array([ratio])
array([1.])


>>> q.dot(q)
<Quantity 285. m2>

>>> np.array(ratio.to(u.dimensionless_unscaled))
array(1.)

>>> u.Quantity([q, q]).flatten()
<Quantity [0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 0., 1., 2., 3., 4., 5.,
6., 7., 8., 9.] m>


>>> a = np.ones(4)
>>> a[2:3] = 2*u.kg
>>> a
array([1., 1., 2., 1.])

>>> a = np.ones(4)
>>> a[2:3] = 1*u.cm/u.m
>>> a
array([1., 1., 1., 1.])


>>> a = np.ones(4)
>>> a[2] = 1*u.cm/u.m
>>> a
array([1.  , 1.  , 0.01, 1.  ])

>>> a = np.ones(4)
>>> a[2:3] = [1*u.cm/u.m]
>>> a
array([1.  , 1.  , 0.01, 1.  ])


>>> a = np.ones(4)
>>> a[2] = 1*u.cm
Traceback (most recent call last):
...
TypeError: only dimensionless scalar quantities can be converted to Python scalars


Numpy数组创建函数不能用于初始化数量¶

>>> my_quantity = u.Quantity(1, u.m)
>>> np.full(10, my_quantity)
Traceback (most recent call last):
...
UnitConversionError: 'm' (length) and '' (dimensionless) are not convertible


>>> np.full(10, 1) << u.m
<Quantity [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.] m>


广播时数量会失去单位¶

>>> q = u.Quantity(np.arange(10.), u.m)
>>> b = np.broadcast_to(q, (2, len(q)))
>>> b
array([[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])
>>> b2 = np.broadcast_to(q, (2, len(q)), subok=True)
>>> b2
<Quantity [[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]] m>


>>> a = np.array(q)
>>> a
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> a2 = np.array(q, subok=True)
>>> a2
<Quantity [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.] m>


数量浮动比较np.isclose公司失败¶

$|a - b| \le (a_\textrm{tol} + r_\textrm{tol} \times |b|)$

>>> from astropy import units as u, constants as const
>>> import numpy as np
>>> np.isclose(500 * u.km/u.s, 300 * u.km / u.s)
Traceback (most recent call last):
...
UnitConversionError: Can only apply 'add' function to dimensionless quantities when other argument is not a quantity (unless the latter is all zero/infinity/nan)


>>> np.isclose(500 * u.km/u.s, 300 * u.km / u.s, atol=1e-8 * u.mm / u.s)
False


数量np.linspace公司NumPy 1.10故障¶

linspace 在使用NumPy 1.10.0到1.10.5时，由于NumPy中的错误，无法正确处理数量。解决方案是升级到NumPy 1.10.6或更高版本，在这个版本中，bug得到了修复。

numpy.int64 不分解输入 Quantity 物体¶

Python 的 int() 通过 __index__ 虽然 numpy.int64numpy.int_ 不可穿过 __index__ . 这意味着上游修复 numpy is required in order for  astropy.units 要控制在这些函数中分解输入：

>>> np.int64((15 * u.km) / (15 * u.imperial.foot))
1
>>> np.int_((15 * u.km) / (15 * u.imperial.foot))
1
>>> int((15 * u.km) / (15 * u.imperial.foot))
3280


构建/安装/测试问题¶

MacOS X和Linux中的区域设置错误¶

...
ValueError: unknown locale: UTF-8


...
stderr = stderr.decode(stdio_encoding)
TypeError: decode() argument 1 must be str, not None


export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"


\$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"


在IPython中运行测试时日志记录测试失败¶

When running the Astropy tests using astropy.test() in an IPython interpreter, some of the tests in the astropy/tests/test_logger.py might fail depending on the version of IPython or other factors. This is due to mutually incompatible behaviors in IPython and pytest, and is not due to a problem with the test itself or the feature being tested.

某些docstring无法在IPython<0.13.2中显示¶

In [1]: import astropy.units as u

In [2]: u.Angstrom?
Out[2]: ERROR: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in
position 184: ordinal not in range(128) [IPython.core.page]


import sys
sys.setdefaultencoding('utf-8')