def test_3d_aslist(): """ Verify that the blocks returned in the list format match the ones returned in the array format. """ orig_data = numpy.random.random( (6, 9, 16) ) blockshape = (2,3,4) final_shape = tuple(numpy.array(orig_data.shape) / blockshape) + blockshape assert final_shape == (3,3,4,2,3,4), final_shape array_view = blockwise_view( orig_data, blockshape ) block_list = blockwise_view( orig_data, blockshape, aslist=True ) assert (numpy.array(block_list) == array_view.reshape((-1,) + blockshape)).all()
def test_3d_aslist(): """ Verify that the blocks returned in the list format match the ones returned in the array format. """ orig_data = numpy.random.random((6, 9, 16)) blockshape = (2, 3, 4) final_shape = tuple(numpy.array(orig_data.shape) / blockshape) + blockshape assert final_shape == (3, 3, 4, 2, 3, 4), final_shape array_view = blockwise_view(orig_data, blockshape) block_list = blockwise_view(orig_data, blockshape, aslist=True) assert (numpy.array(block_list) == array_view.reshape((-1, ) + blockshape)).all()
def test_2d(): # Adapted from: # http://stackoverflow.com/a/8070716/162094 n = 4 m = 5 a = numpy.arange(1, n * m + 1).reshape(n, m) logger.debug("original data:\n{}".format(a)) sz = a.itemsize h, w = a.shape bh, bw = 2, 2 shape = (h // bh, w // bw, bh, bw) logger.debug("shape:{}".format(shape)) strides = sz * numpy.array([w * bh, bw, w, 1]) logger.debug("strides:{}".format(strides)) # This is our reference, copied straight from stackoverflow blocks = numpy.lib.stride_tricks.as_strided(a, shape=shape, strides=strides) logger.debug("reference blocks:\n{}".format(blocks)) view = blockwise_view(a, (bh, bw), require_aligned_blocks=False) logger.debug("blockwise_view:\n{}".format(view)) assert view.shape == blocks.shape assert (view == blocks).all() # Prove that this was a view, not a copy view[:] = 0 logger.debug("modified data:\n{}".format(a)) assert (a[0:4, 0:4] == 0).all(), "blockwise_view returned a copy, not a view!"
def test_3d(): """ Copy a 3D array block-by-block into a 6D array, and verify that the result matches blockwise_view() """ orig_data = numpy.random.random((6, 9, 16)) blockshape = (2, 3, 4) final_shape = tuple( numpy.array(orig_data.shape) // blockshape) + blockshape assert final_shape == (3, 3, 4, 2, 3, 4), final_shape blockwise_copy = numpy.zeros(final_shape) block_addresses = final_shape[0:3] for x in range(block_addresses[0]): for y in range(block_addresses[1]): for z in range(block_addresses[2]): block_start = numpy.array((x, y, z)) * blockshape block_stop = block_start + blockshape blockwise_copy[x, y, z] = orig_data[block_start[0]:block_stop[0], block_start[1]:block_stop[1], block_start[2]:block_stop[2]] view = blockwise_view(orig_data, blockshape) assert view.shape == blockwise_copy.shape assert (view == blockwise_copy).all() assert not view.flags["C_CONTIGUOUS"] c_order_copy = view.copy("C") assert c_order_copy.flags["C_CONTIGUOUS"]
def test_2d(): # Adapted from: # http://stackoverflow.com/a/8070716/162094 n=4 m=5 a = numpy.arange(1,n*m+1).reshape(n,m) logger.debug("original data:\n{}".format(a)) sz = a.itemsize h,w = a.shape bh,bw = 2,2 shape = (h/bh, w/bw, bh, bw) logger.debug("shape:{}".format(shape)) strides = sz*numpy.array([w*bh,bw,w,1]) logger.debug("strides:{}".format(strides)) # This is our reference, copied straight from stackoverflow blocks=numpy.lib.stride_tricks.as_strided(a, shape=shape, strides=strides) logger.debug("reference blocks:\n{}".format(blocks)) view = blockwise_view(a, (bh,bw), False) logger.debug("blockwise_view:\n{}".format(view)) assert view.shape == blocks.shape assert (view == blocks).all() # Prove that this was a view, not a copy view[:] = 0 logger.debug("modified data:\n{}".format(a)) assert (a[0:4, 0:4] == 0).all(), "blockwise_view returned a copy, not a view!"
def test_3d(): """ Copy a 3D array block-by-block into a 6D array, and verify that the result matches blockwise_view() """ orig_data = numpy.random.random( (6, 9, 16) ) blockshape = (2,3,4) final_shape = tuple(numpy.array(orig_data.shape) / blockshape) + blockshape assert final_shape == (3,3,4,2,3,4), final_shape blockwise_copy = numpy.zeros( final_shape ) block_addresses = final_shape[0:3] for x in range(block_addresses[0]): for y in range(block_addresses[1]): for z in range(block_addresses[2]): block_start = numpy.array((x,y,z)) * blockshape block_stop = block_start + blockshape blockwise_copy[x,y,z] = orig_data[ block_start[0] : block_stop[0], block_start[1] : block_stop[1], block_start[2] : block_stop[2] ] view = blockwise_view( orig_data, blockshape ) assert view.shape == blockwise_copy.shape assert (view == blockwise_copy).all() assert not view.flags['C_CONTIGUOUS'] c_order_copy = view.copy('C') assert c_order_copy.flags['C_CONTIGUOUS']
def execute(self, slot, subindex, roi, result): block_roi_start = roi.start // DVID_BLOCK_WIDTH block_roi_stop = (roi.stop + DVID_BLOCK_WIDTH - 1) // DVID_BLOCK_WIDTH block_slicing = roiToSlice(block_roi_start, block_roi_stop) if (numpy.array((roi.start, roi.stop)) % DVID_BLOCK_WIDTH).any(): # Create an array that is bigger than the result, but block-aligned. aligned_result_shape = (block_roi_stop - block_roi_start) * DVID_BLOCK_WIDTH aligned_result = numpy.ndarray(aligned_result_shape, numpy.uint8) else: aligned_result = result aligned_result_view = blockwise_view(aligned_result, 3 * (DVID_BLOCK_WIDTH, ), require_aligned_blocks=False) # broadcast 3d data into 6d view aligned_result_view[:] = self._dset[block_slicing][..., None, None, None] # If the result wasn't aligned, we couldn't broadcast directly to it. # Copy the data now. if aligned_result is not result: start = roi.start - (block_roi_start * DVID_BLOCK_WIDTH) stop = start + (roi.stop - roi.start) result[:] = aligned_result[roiToSlice(start, stop)]
def execute(self, slot, subindex, roi, result): block_roi_start = roi.start / DVID_BLOCK_WIDTH block_roi_stop = ( roi.stop + DVID_BLOCK_WIDTH-1 ) / DVID_BLOCK_WIDTH block_slicing = roiToSlice(block_roi_start, block_roi_stop) if (numpy.array( (roi.start, roi.stop) ) % DVID_BLOCK_WIDTH).any(): # Create an array that is bigger than the result, but block-aligned. aligned_result_shape = (block_roi_stop - block_roi_start) * DVID_BLOCK_WIDTH aligned_result = numpy.ndarray( aligned_result_shape, numpy.uint8 ) else: aligned_result = result aligned_result_view = blockwise_view(aligned_result, 3*(DVID_BLOCK_WIDTH,), require_aligned_blocks=False) if self._transpose_axes: dset_slicing = tuple(reversed(block_slicing)) # broadcast 3d data into 6d view aligned_result_view[:] = self._dset[dset_slicing].transpose()[..., None, None, None] else: # broadcast 3d data into 6d view aligned_result_view[:] = self._dset[block_slicing][..., None, None, None] # If the result wasn't aligned, we couldn't broadcast directly to it. # Copy the data now. if aligned_result is not result: start = roi.start - (block_roi_start*DVID_BLOCK_WIDTH) stop = start + (roi.stop - roi.start) result[:] = aligned_result[roiToSlice(start, stop)]
ret = inf.infer( inf.verboseVisitor() ) if ret.name != "NORMAL": raise RuntimeError("OpenGM inference failed with status: {}".format( ret.name )) mapping_index_array = inf.arg().astype(np.uint32) return mapping_index_array if __name__ == "__main__": import vigra from lazyflow.utility import blockwise_view # Superpixels are just (20,20,20) blocks, each with a unique value, 1-125 superpixels = np.zeros( (100,100,100), dtype=np.uint32 ) superpixel_block_view = blockwise_view( superpixels, (20,20,20) ) assert superpixel_block_view.shape == (5,5,5,20,20,20) superpixel_block_view[:] = np.arange(1, 126).reshape( (5,5,5) )[..., None, None, None] superpixels = superpixels[...,None] assert superpixels.min() == 1 assert superpixels.max() == 125 # Make 3 random probability classes probabilities = np.random.random( superpixels.shape[:-1] + (3,) ).astype( np.float32 ) probabilities = vigra.taggedView(probabilities, 'zyxc') superpixels = vigra.taggedView(superpixels, 'zyxc') from lazyflow.graph import Graph op = OpMulticut(graph=Graph())
if ret.name != "NORMAL": raise RuntimeError( "OpenGM inference failed with status: {}".format(ret.name)) mapping_index_array = inf.arg().astype(np.uint32) return mapping_index_array if __name__ == "__main__": import vigra from lazyflow.utility import blockwise_view # Superpixels are just (20,20,20) blocks, each with a unique value, 1-125 superpixels = np.zeros((100, 100, 100), dtype=np.uint32) superpixel_block_view = blockwise_view(superpixels, (20, 20, 20)) assert superpixel_block_view.shape == (5, 5, 5, 20, 20, 20) superpixel_block_view[:] = np.arange( 1, 126).reshape((5, 5, 5))[..., None, None, None] superpixels = superpixels[..., None] assert superpixels.min() == 1 assert superpixels.max() == 125 # Make 3 random probability classes probabilities = np.random.random( superpixels.shape[:-1] + (3,)).astype(np.float32) probabilities = vigra.taggedView(probabilities, 'zyxc') superpixels = vigra.taggedView(superpixels, 'zyxc')