[AstroPy] Problem with astropy 03 and quantities
thomas.robitaille at gmail.com
Mon Nov 25 06:43:02 EST 2013
On 25 November 2013 11:14, Yannick Roehlly <yannick.roehlly at lam.fr> wrote:
> Hi Thomas,
> Thanks for the answer.
> Le Mon, 25 Nov 2013 10:47:37 +0100,
> Thomas Robitaille <thomas.robitaille at gmail.com> a écrit :
>> First, regarding the ``copy`` issue, could you provide some code to
>> replicate this? ``copy`` should work with Quantites:
>> In : np.copy(np.array([1,2,3]) * u.m)
>> Out: array([1, 2, 3])
> np.copy should accept an array-like but here it fails if it's an array
> of quantities.
> In : np.copy([x * u.m for x in xrange(10)])
> ERROR:astropy:ValueError: setting an array element with a sequence.
> ERROR: ValueError: setting an array element with a sequence.
> ValueError Traceback (most recent call
> last) <ipython-input-16-dd73be22bb28> in <module>() ----> 1 np.copy([x
> * u.m for x in xrange(10)])
> /usr/lib/pymodules/python2.7/numpy/lib/function_base.pyc in copy(a,
> order) 828
> 829 """
> --> 830 return array(a, order=order, copy=True)
> 832 # Basic operations
> ValueError: setting an array element with a sequence.
Ah - in this case, are you the one making the array of quantity
objects, or is that coming from astropy.cosmology? We should
definitely never return an array of quantities, but rather a quantity
> I understand that this is not a bug as everything works as expected and
> as numpy can't manage a Quantity datatype.
>> To adapt your code, you simply need to add ``.value`` to the quantity
>> to get the underlying Numpy array if you want to drop the units.
> Yes, but that means the code will not work with prior version of
> astropy. I'd better change it to have a numpy.array instead of a list
> at the point the copy fails.
I would suggest using a try...except in your code for this case, so
that you can maintain backward-compatibility.
>> There is a performance penalty in using Quantity in 0.3, but already
>> in 0.3.1 there will be significant optimizations, and given that
>> ``Quantity`` is widely used in Astropy, you can expect more work on
>> optimizations in future.
> My question was not for the astropy internals but for the computation
> on arrays. Thanks to the pointer Kyle gave me, I've seen that some
> optimizations made by Alex consist in splitting the value from the
> quantity, making the computations and joining them back.
>> By the way, there *is* an entry in the changelog:
> I've read it ;-) but I meant a warning, as it can break some code. But
> I admit I'm in a specific case.
To some extent, all of the things in the API changes section might
break code - for 0.3 the list is unfortunately quite long, but as Neil
pointed out, we want to make sure we minimize this in future.
> One can't proceed from the informal to the formal by formal means.
> AstroPy mailing list
> AstroPy at scipy.org
More information about the AstroPy