convolve
和
hstack
. 我想用dask创建一个等价的函数,但是到目前为止我尝试的各种方法都没有成功。
我要做的是计算数组n个数的“滑动和”,除非其中任何一个数是n an,在这种情况下,和也应该是NaN。结果的(n-1)元素也应该是NaN,因为不假定有环绕/后伸。
例如:
input vector: [3, 4, 6, 2, 1, 3, 5, np.NaN, 8, 5, 6]
n: 3
result: [NaN, NaN, 13, 12, 9, 6, 9, NaN, NaN, NaN, 19]
或
input vector: [1, 5, 7, 2, 3, 4, 9, 6, 3, 8]
n: 4
result: [NaN, NaN, NaN, 15, 17, 16, 18, 22, 22, 26]
def sum_to_scale(values, scale):
# don't bother if the number of values to sum is 1 (will result in duplicate array)
if scale == 1:
return values
# get the valid sliding summations with 1D convolution
sliding_sums = np.convolve(values, np.ones(scale), mode="valid")
# pad the first (n - 1) elements of the array with NaN values
return np.hstack(([np.NaN] * (scale - 1), sliding_sums))
如何使用dask数组API(和/或
dask_image.ndfilters