I have an array of points (called points), consisting of ~30000 x,y, and z values. I also have a separate array of points (called vertices), about ~40000 x,y, and z values. The latter array indexes the lower-front-left corners of some cubes of side length size. I would like to find out which points reside in which cubes, and how many points reside in each cube. I wrote a loop to do this, which works like this:
for i in xrange(len(vertices)):
cube=((vertices[i,0]<= points[:,0]) &
(points[:,0]<(vertices[i,0]+size)) &
(vertices[i,1]<= points[:,1]) &
(points[:,1] < (vertices[i,1]+size)) &
(vertices[i,2]<= points[:,2]) &
(points[:,2] < (vertices[i,2]+size))
)
numpoints[i]=len(points[cube])
(The loop is order the individual cubes, and the "cube" creates a boolean array of indices.) I then store points[cube] somewhere, but this isn't what's slowing me down; it's the creating of "cube=".
I would like to speed this loop up (it takes tens of seconds to complete on a macbook pro). I tried rewriting the "cube=" part in C, as follows:
for i in xrange(len(vertices)):
cube=zeros(pp, dtype=bool)
code="""
for (int j=0; j<pp; ++j){
if (vertices(i,0)<= points(j,0))
if (points(j,0) < (vertices(i,0)+size))
if (vertices(i,1)<= points(j,1))
if (points(j,1) < (vertices(i,1)+size))
if (vertices(i,2)<= points(j,2))
if (points(j,2) < (vertices(i,2)+size))
cube(j)=1;
}
return_val = 1;"""
weave.inline(code,
['vertices', 'points','size','pp','cube', 'i'])
numpoints[i]=len(points[cube])
This sped it up by a bit more than a factor of two. Rewriting both loops in C actually made it only slightly faster than the original numpy-only version, because of the frequent references to the array objects necessary to keep track of which points are in which cubes. I suspect it's possible to do this much more rapidly, and that I'm missing something. Can anyone suggest how to speed this up?? I'm new to numpy/python, and thanks in advance.