Perform [(x,y,v),...] -> ndarray conversion using complex numbers

Factor-of-2 speedup on code that currently takes ~20% of the runtime --
turns out cache is good stuff.
This commit is contained in:
jan 2016-07-16 18:32:11 -07:00
parent caf51d72ba
commit d418ff149d

View File

@ -155,8 +155,11 @@ def raster(poly_xy: numpy.ndarray,
cover = diff(poly[:, 1], axis=0)[non_edge] / diff(grid_y)[y_sub] cover = diff(poly[:, 1], axis=0)[non_edge] / diff(grid_y)[y_sub]
area = (endpoint_avg[non_edge, 0] - grid_x[x_sub]) * cover / diff(grid_x)[x_sub] area = (endpoint_avg[non_edge, 0] - grid_x[x_sub]) * cover / diff(grid_x)[x_sub]
poly_grid = sparse.coo_matrix((-area, (x_sub, y_sub)), shape=num_xy_px).toarray() # Use coo_matrix(...).toarray() to efficiently convert from (x, y, v) pairs to ndarrays.
cover_grid = sparse.coo_matrix((cover, (x_sub, y_sub)), shape=num_xy_px).toarray() # We can use v = (-area + 1j * cover) followed with calls to numpy.real() and numpy.imag() to
poly_grid = poly_grid + cover_grid.cumsum(axis=0) # improve performance (Otherwise we'd have to call coo_matrix() twice. It's really inefficient
# because it involves lots of random memory access, unlike real() and imag()).
poly_grid = sparse.coo_matrix((-area + 1j * cover, (x_sub, y_sub)), shape=num_xy_px).toarray()
result_grid = numpy.real(poly_grid) + numpy.imag(poly_grid).cumsum(axis=0)
return poly_grid return result_grid