<meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div>Hi</div><div><br></div><div>First the disclaimer: This is my first numpy experience, so I have next to no idea what I'm doing.</div>
<div><br></div><div>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.</div>
<div><br></div><div>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.</div>
<div><br></div><div>Now to the problem at hand. I find this difficult to explain but will try as best I can.</div><div>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.</div>
<div><br></div><div>Here is my current solution, from my limited testing it seems to produce the result I'm after.</div><div><br></div><div><div>def decimate(data_in):</div><div>    in_x, in_y, in_z = data_in.shape</div>
<div>    out_x = in_x / 2</div><div>    out_y = in_y / 2</div><div>    out_z = in_z / 2</div><div>    out_shape = out_x, out_y, out_z</div><div>    out_size = product(out_shape)</div><div><br></div><div>    # figure out which chunks are homogeneous</div>
<div>    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)</div><div>    min_array = numpy.amin(reshaped_array, axis=3)</div><div>    max_array = numpy.amax(reshaped_array, axis=3)</div>
<div>    equal_array = numpy.equal(min_array, max_array)</div><div><br></div><div>    # select the actual value for the homogeneous chunks and MIXED for the heterogeneous</div><div>    decimated_array = data_in[::2,::2,::2]</div>
<div>    mixed_array = numpy.tile(MIXED, out_size).reshape(out_shape)</div><div>    data_out = numpy.where(equal_array, decimated_array, mixed_array)</div><div><br></div><div>    return data_out</div></div><div><br></div>
<div>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.</div><div><br></div><div>
Regards</div><div>Gordon</div><div><br></div><div>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.</div><div><br></div></span>