I have a large set of coordinates contained in one astrophilia coordinate object. I would like to apply a function to each coordinate in parallel and create an output array of the same shape, but this is slow.
(In my case, this function is a model that takes galacticocentric coordinates and produces "the brightness associated with this point in space".)
Illustration:
In [339]: type(data)
Out[339]: astropy.coordinates.builtin_frames.galactocentric.Galactocentric
In [340]: data.shape, data.size
Out[340]: ((21, 21, 31), 13671)
In [341]: data[0,0,0]
Out[341]:
<Galactocentric Coordinate (galcen_distance=8.3 kpc, galcen_ra=266d24m18.36s, galcen_dec=-28d56m10.23s, z_sun=27.0 pc, roll=0.0 deg): (rho, phi, z) in (kpc, deg, kpc)
( 8.29995608, 180., 0.027)>
In [342]: func = vectorize(lambda coord: 0)
In [343]: %time func(data).shape
CPU times: user 33.2 s, sys: 88.1 ms, total: 33.3 s
Wall time: 33.4 s
Out[343]: (21, 21, 31)
I suspect this is slow, because at each iteration, a new coordinate object is initialized before being passed to a vectorized function ( discussion ).
numpy , ( ).
.
? numpy , ?
!
:
from numpy import *
from astropy import units as u
from astropy.coordinates import Galactocentric
x = linspace(0, 1, 1e3)*u.pc
data = Galactocentric(x=x, y=0*u.pc, z=0*u.pc)
@vectorize
def func(coord):
'''ultimately in terms of coord.x, coord.y, coord.z...'''
return 0
func(data)