Ress
Ress

Reputation: 780

Xarray rolling construct in multiple dimension

What is the best way to have xarray rolling construct for a multidimensional rolling window? Here is a numpy example:

import numpy as np
from numpy.lib.stride_tricks import as_strided

data = np.array(np.arange(6).reshape(2, 3),dtype="float64")


win_size = (
    3  # Size of the window (e.g. 3*3)
)
win_size_half = int(np.floor(win_size / 2))

# pad with nan to get correct window for the edges
data = np.pad(
    data,
    (win_size_half, win_size_half),
    "constant",
    constant_values=(np.nan),
)

sub_shape = (win_size, win_size)
view_shape = tuple(np.subtract(data.shape, sub_shape) + 1) + sub_shape
data_view = as_strided(
    data, view_shape, data.strides * 2
)
data_view = data_view.reshape((-1,) + sub_shape)

#Expected results
>>> data_view
array([[[nan, nan, nan],
        [nan,  0.,  1.],
        [nan,  3.,  4.]],

       [[nan, nan, nan],
        [ 0.,  1.,  2.],
        [ 3.,  4.,  5.]],

       [[nan, nan, nan],
        [ 1.,  2., nan],
        [ 4.,  5., nan]],

       [[nan,  0.,  1.],
        [nan,  3.,  4.],
        [nan, nan, nan]],

       [[ 0.,  1.,  2.],
        [ 3.,  4.,  5.],
        [nan, nan, nan]],

       [[ 1.,  2., nan],
        [ 4.,  5., nan],
        [nan, nan, nan]]])

I wonder how can use xarray for the same purpose. For example, doing same operation as above using xarray:

import xarray as xr

da =xr.DataArray(np.array(np.arange(6).reshape(2, 3),dtype="float64"),dims=("a","b"))

# And something like
rolling = da.rolling({"a":win_size,"b":win_size})

# producing same results as in numpy example
rolling.construct("window_dim")

To my understanding xr.rolling doesn't allow multiple dimensions. Please let me know if there are other ways to do this type of operation.

Thanks

Upvotes: 5

Views: 1302

Answers (1)

Olivier Archer
Olivier Archer

Reputation: 71

xr.rolling now accepts multiple dimensions. You have to provide a dict mapping (or keywords based ) to rolling.construct.

Your numpy example take the windows center, and it's not the default for xr.rolling, so you have to explicitly provide center=True

Following code give same result as your numpy code:

import xarray as xr
import numpy as np

da =xr.DataArray(np.array(np.arange(6).reshape(2, 3),dtype="float64"),dims=("a","b"))

rolling = da.rolling({"a":3,"b":3}, center=True)

# producing same results as in numpy example
da_roll = rolling.construct(a='ka',b='kb')
da_roll
Out[2]: 
<xarray.DataArray (a: 2, b: 3, ka: 3, kb: 3)>
array([[[[nan, nan, nan],
         [nan,  0.,  1.],
         [nan,  3.,  4.]],
        [[nan, nan, nan],
         [ 0.,  1.,  2.],
         [ 3.,  4.,  5.]],
        [[nan, nan, nan],
         [ 1.,  2., nan],
         [ 4.,  5., nan]]],
       [[[nan,  0.,  1.],
         [nan,  3.,  4.],
         [nan, nan, nan]],
        [[ 0.,  1.,  2.],
         [ 3.,  4.,  5.],
         [nan, nan, nan]],
        [[ 1.,  2., nan],
         [ 4.,  5., nan],
         [nan, nan, nan]]]])
Dimensions without coordinates: a, b, ka, kb

Upvotes: 5

Related Questions