def extract_patches(arr, patch_shape=8, extraction_step=1):
 arr_ndim = arr.ndim
 if isinstance(patch_shape, numbers.Number):
  patch_shape = tuple(([patch_shape] * arr_ndim))
 if isinstance(extraction_step, numbers.Number):
  extraction_step = tuple(([extraction_step] * arr_ndim))
 patch_strides = arr.strides
 slices = [slice(None, None, st) for st in extraction_step]
 indexing_strides = arr[slices].strides
 patch_indices_shape = (((np.array(arr.shape) - np.array(patch_shape)) // np.array(extraction_step)) + 1)
 shape = tuple((list(patch_indices_shape) + list(patch_shape)))
 strides = tuple((list(indexing_strides) + list(patch_strides)))
 patches = as_strided(arr, shape=shape, strides=strides)
 return patches

def extract_patches ( arr , patch_shape = 8 , extraction_step = 1 ) : arr_ndim = arr . ndim if isinstance ( patch_shape , numbers . Number ) : patch_shape = tuple ( ( [ patch_shape ] * arr_ndim ) ) if isinstance ( extraction_step , numbers . Number ) : extraction_step = tuple ( ( [ extraction_step ] * arr_ndim ) ) patch_strides = arr . strides slices = [ slice ( None , None , st ) for st in extraction_step ] indexing_strides = arr [ slices ] . strides patch_indices_shape = ( ( ( np . array ( arr . shape ) - np . array ( patch_shape ) ) // np . array ( extraction_step ) ) + 1 ) shape = tuple ( ( list ( patch_indices_shape ) + list ( patch_shape ) ) ) strides = tuple ( ( list ( indexing_strides ) + list ( patch_strides ) ) ) patches = as_strided ( arr , shape = shape , strides = strides ) return patches
extracts patches of any n-dimensional array in place using strides .