def fold_array(array, axis=1):
''' Folds an array in half over the given axis and averages.
Args:
array (`numpy.ndarray`): 2d array to fold.
axis (`int`): axis to fold over.
Returns
numpy.ndarray: new array.
'''
xs, ys = array.shape
if axis is 1:
xh = xs // 2
left_chunk = array[:, :xh]
right_chunk = array[:, xh:]
folded_array = np.concatenate((right_chunk[:, :, np.newaxis],
np.flip(np.flip(left_chunk, axis=1),
axis=0)[:, :, np.newaxis]),
axis=2)
else:
yh = ys // 2
top_chunk = array[:yh, :]
bottom_chunk = array[yh:, :]
folded_array = np.concatonate((bottom_chunk[:, :, np.newaxis],
np.flip(np.flip(top_chunk, axis=1),
axis=0)[:, :, np.newaxis]),
axis=2)
return folded_array.mean(axis=2)
评论列表
文章目录