简体   繁体   English

Xarray 多维滚动构造

[英]Xarray rolling construct in multiple dimension

What is the best way to have xarray rolling construct for a multidimensional rolling window?为多维滚动 window 设置 xarray 滚动构造的最佳方法是什么? Here is a numpy example:这是一个 numpy 示例:

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.我想知道如何将 xarray 用于相同目的。 For example, doing same operation as above using xarray:例如,使用 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.据我了解, xr.rolling 不允许多维。 Please let me know if there are other ways to do this type of operation.请让我知道是否有其他方法可以进行此类操作。

Thanks谢谢

xr.rolling now accepts multiple dimensions. xr.rolling 现在接受多个维度。 You have to provide a dict mapping (or keywords based ) to rolling.construct.您必须为 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您的 numpy 示例采用 windows 中心,它不是 xr.rolling 的默认值,因此您必须明确提供center=True

Following code give same result as your numpy code:以下代码给出与 numpy 代码相同的结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM