Q:

Optimize numpy array maniplation for ray tracing

I've got some code to do basic ray tracing in a map to determine if a ray is hitting a wall or not. 
[Edit]: The size of y_coords and x_coords is generally 18x1000 (corresponding points).  self.map is 800x800
def ray_trace(self, x, y, x_coords, y_coords):
    ray_distances = []
    resolution = self.parameters['resolution']
    for i in range(x_coords.shape[0]):
        distance = 0

        # filter x and y coords to stay within map regions
        ray_range = np.bitwise_and(x_coords[i]<799,y_coords[i]<799)

        # determine ending index where the ray stops
        len_ray = ray_range[ray_range==True].shape[0]

        # zip up the x and y coords
        ray_coords = np.c_[x_coords[i,0:len_ray], y_coords[i,0:len_ray]]

        # look up all the coordinates in the map and find where the map is
        # less than or equal to zero (this is a wall)
        ray_values, = np.where(self.map[tuple(ray_coords. T)] <= 0)

        # some special exceptions
        if not ray_values.shape[0]:
            if not len(ray_coords):
                end_of_ray = np.array([x/resolution, y/resolution])
            else:
                end_of_ray = ray_coords[len(ray_values)]
        else:
            # get the end of the ray
            end_of_ray = ray_coords[ray_values.item(0)]

        # find the distance from the originating point
        distance = math.sqrt((end_of_ray.item(0) - x/resolution)**2 + 
                             (end_of_ray.item(1) - y/resolution)**2)

        ray_distances.append(distance)
    return ray_distances

I'm having an issue in the np.c_ and np.where lines - I profiled them and those lines with kernprof.py and they are taking an extremely long time (especially np.c_, which takes up 50% of the time).  Does anyone have any ideas on how to optimize this? 

A:

You really don't need to play with indices so much.  Advanced indexing means that you can index with two equal-sized coordinate arrays, without needing to combine them into coordinates first. 
coord_mask = (x_coords < 799) & (y_coords < 799)
for i in xrange(len(coord_mask)):
    distance = 0
    row_mask = coord_mask[i]
    row_x = x_coords[i, row_mask]
    row_y = y_coords[i, row_mask]
    mapvals = self.map[row_x, row_y] # advanced indexing
    ray_values, = (mapvals <= 0).nonzero()
    ...

