Astropy, Numpy: Using a coordinate function is very slow

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              # Not that big, really
Out[340]: ((21, 21, 31), 13671)

In [341]: data[0,0,0]                        # An example of a single coordinate
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)  # Dummy function

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

# Generate lots of coordinates
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

# timeit
func(data)
+4
1

( - . ) numpy, , , numpy. :

coords_np = stack([coords.rho, coords.phi, coords.z]).value

( , , .value.)

(rho, phi, z) ,

>>> coords_np[:,0,0,0]
array([  <rho>,  <phi>,    <z>])

(rho, phi, z) -> x coords_np :

scalar_field = apply_along_axis(func, 0, coords_np)

func(coords) ( ), .


: , apply_along_axis , . , - lambda rho, phi, z: rho**2 + z**2, coords.rho**2 + coords.z**2, stack([coords.rho, coords.phi, coords.z]), . .

. .

+1
source

Source: https://habr.com/ru/post/1689767/


All Articles