[Numpy-discussion] looking for code advice
Gordon Wrigley
gordon@tolomea....
Wed Sep 29 07:25:04 CDT 2010
Hi
First the disclaimer: This is my first numpy experience, so I have next to
no idea what I'm doing.
I've muddled through and managed to put together some code for my current
problem and now that I have it going I'd like to hear any comments people
may have on both my solution and other ways of approaching the problem.
I have two goals here, I'd like to make the process run faster and I'd like
to broaden my understanding of numpy as I can see from my brief use of it
that it is a remarkably powerful tool.
Now to the problem at hand. I find this difficult to explain but will try as
best I can.
The best word I have for the process is decimation. The input and output are
both 3 dimensional arrays of uint8's. The output is half the size of the
input along each dimension. Each cell [x,y,z] in the output corresponds to
the 2x2x2 block [2*x:2*x+2, 2*y:2*y+2, 2*z:2*z+2] in the input. The tricky
bit is in how the correspondence works. If all the cells in the input block
have the same value then the cell in the output block will also have that
value. Otherwise the output cell will have the value MIXED.
Here is my current solution, from my limited testing it seems to produce the
result I'm after.
def decimate(data_in):
in_x, in_y, in_z = data_in.shape
out_x = in_x / 2
out_y = in_y / 2
out_z = in_z / 2
out_shape = out_x, out_y, out_z
out_size = product(out_shape)
# figure out which chunks are homogeneous
reshaped_array = data_in.reshape(out_x, 2, out_y, 2, out_z,
2).transpose(0,2,4,1,3,5).reshape(out_x, out_y, out_z, 8)
min_array = numpy.amin(reshaped_array, axis=3)
max_array = numpy.amax(reshaped_array, axis=3)
equal_array = numpy.equal(min_array, max_array)
# select the actual value for the homogeneous chunks and MIXED for the
heterogeneous
decimated_array = data_in[::2,::2,::2]
mixed_array = numpy.tile(MIXED, out_size).reshape(out_shape)
data_out = numpy.where(equal_array, decimated_array, mixed_array)
return data_out
For the curious this is will be used to build a voxel octtree for a 3d
graphics application. The final setup will be more complicated, this is the
minimum that will let me get up and running.
Regards
Gordon
P.S. congrats on numpy, it is a very impressive tool, I've only scraped the
surface and it's already impressed me several times over.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100929/10b61c94/attachment.html
More information about the NumPy-Discussion
mailing list