當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python dask.array.Array.map_overlap用法及代碼示例


用法:

Array.map_overlap(func, depth, boundary=None, trim=True, **kwargs)

將函數映射到具有一些重疊的數組塊上

我們在數組的塊之間共享相鄰區域,然後映射一個函數,然後修剪掉相鄰的條帶。

請注意,此函數會在計算前嘗試自動確定輸出數組類型,如果您希望該函數在對 0-d 數組進行操作時不會成功,請參閱map_blocks 中的meta 關鍵字參數。

參數

func: function

應用於每個擴展塊的函數

depth: int, tuple, or dict

每個塊應與其鄰居共享的元素數量如果是元組或字典,那麽每個軸可能不同

boundary: str, tuple, dict

如何處理邊界。值包括‘reflect’, ‘periodic’, ‘nearest’, ‘none’,或任何常量值,如 0 或 np.nan

trim: bool

調用 map 函數後是否從每個塊中修剪 depth 元素。如果您的映射函數已經為您執行此操作,請將其設置為 False

**kwargs:

map_blocks 中有效的其他關鍵字參數。

例子

>>> import dask.array as da
>>> x = np.array([1, 1, 2, 3, 3, 3, 2, 1, 1])
>>> x = da.from_array(x, chunks=5)
>>> def derivative(x):
...     return x - np.roll(x, 1)
>>> y = x.map_overlap(derivative, depth=1, boundary=0)
>>> y.compute()
array([ 1,  0,  1,  1,  0,  0, -1, -1,  0])
>>> import dask.array as da
>>> x = np.arange(16).reshape((4, 4))
>>> d = da.from_array(x, chunks=(2, 2))
>>> y = d.map_overlap(lambda x: x + x.size, depth=1, boundary='reflect')
>>> y.compute()
array([[16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])
>>> func = lambda x: x + x.size
>>> depth = {0: 1, 1: 1}
>>> boundary = {0: 'reflect', 1: 'none'}
>>> d.map_overlap(func, depth, boundary).compute()  
array([[12,  13,  14,  15],
       [16,  17,  18,  19],
       [20,  21,  22,  23],
       [24,  25,  26,  27]])
>>> x = np.arange(16).reshape((4, 4))
>>> d = da.from_array(x, chunks=(2, 2))
>>> y = d.map_overlap(lambda x: x + x[2], depth=1, boundary='reflect', meta=np.array(()))
>>> y
dask.array<_trim, shape=(4, 4), dtype=float64, chunksize=(2, 2), chunktype=numpy.ndarray>
>>> y.compute()
array([[ 4,  6,  8, 10],
       [ 8, 10, 12, 14],
       [20, 22, 24, 26],
       [24, 26, 28, 30]])
>>> import cupy  
>>> x = cupy.arange(16).reshape((4, 4))  
>>> d = da.from_array(x, chunks=(2, 2))  
>>> y = d.map_overlap(lambda x: x + x[2], depth=1, boundary='reflect', meta=cupy.array(()))  
>>> y  
dask.array<_trim, shape=(4, 4), dtype=float64, chunksize=(2, 2), chunktype=cupy.ndarray>
>>> y.compute()  
array([[ 4,  6,  8, 10],
       [ 8, 10, 12, 14],
       [20, 22, 24, 26],
       [24, 26, 28, 30]])

相關用法


注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask.array.Array.map_overlap。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。