Tuesday, 12 January 2021

How to perform a "windowed" operation on a dask array

I have a Xarray that has 3 dimensions (time, x and y) - they are basically a stack of images. I want to operate in a "windowed" manner on pairs of images.

Basically take a pair, say the first two "time" images, and call a function on a 5x5 window over them. In numpy I would do this by simply slicing appropriately and computing my metric by first forming "patches":

params = []
for i in range(0,patch1.shape[0],1):
    for j in range(0,patch1.shape[1],1):
        window1 = np.copy(imga[i:i+N,j:j+N]).flatten()
        window2 = np.copy(imgb[i:i+N,j:j+N]).flatten()
        params.append((window1, window2))

Here N is the window size, 5 for instance. Then computing my metric:

def f(param):
    return metric_function(*param)

with Pool(4) as p:
    r = list(tqdm.tqdm(p.imap(f,params), total=len(params)))

However, I have difficultly translating this to dask and I would like some help. My first intuition is to use the map_overlap function, but I don't think I fully understand how to, especially because the output is of a different dimension from the input; i.e. the output would only be the center pixel of the whole NxN patch.



from How to perform a "windowed" operation on a dask array

No comments:

Post a Comment