简体   繁体   English

使用 xarray 滚动的有限差分

[英]Finite difference using xarray rolling

My goal is to compute a derivative of a moving window of a multidimensional dataset along a given dimension, where the dataset is stored as Xarray DataArray or DataSet .我的目标是计算沿给定维度的多维数据集的移动 window 的导数,其中数据集存储为 Xarray DataArrayDataSet

In the simplest case, given a 2D array I would like to compute a moving difference across multiple entries in one dimension, eg:在最简单的情况下,给定一个二维数组,我想计算一维中多个条目的移动差异,例如:

data = np.kron(np.linspace(0,1,10), np.linspace(1,4,6) ).reshape(10,6)
T=3
reducedArray = np.zeros_like(data)
for i in range(data.shape[1]):
    if i < T:
        reducedArray[:,i] = data[:,i] - data[:,0]
    else:
        reducedArray[:,i] = data[:,i] - data[:,i-T]

where the if i <T condition ensures that input and output contain proper values (ie, no nan s) and are of identical shape.其中if i <T条件确保 input 和 output 包含正确的值(即没有nan )并且具有相同的形状。

Xarray's diff aims to perform a finite-difference approximation of a given derivative order using nearest-neighbours, so it is not suitable here, hence the question: Xarray 的diff旨在使用最近邻对给定的导数阶进行有限差分逼近,因此在这里不适合,因此问题是:

Is it possible to perform this operation using Xarray functions only?是否可以仅使用 Xarray 函数执行此操作?

The rolling weighted average example appears to be something similar, but still too distinct due to the usage of NumPy routines. 滚动加权平均示例看起来很相似,但由于使用了 NumPy 例程,因此仍然过于不同。 I've been thinking that something along the lines of the following should work:我一直在想,以下内容应该可行:

xr2DDataArray = xr.DataArray(
    data,
    dims=('x','y'),
    coords={'x':np.linspace(0,1,10), 'y':np.linspace(1,4,6)}
)
r = xr2DDataArray.rolling(x=T,min_periods=2)
r.reduce( redFn )

I am struggling with the definition of redFn here,though.不过,我在这里为redFn的定义苦苦挣扎。

Caveat The actual dataset to which the operation is to be applied will have a size of ~10GiB, so a solution that does not blow up the memory requirements will be highly appreciated!警告要应用该操作的实际数据集的大小约为 10GiB,因此我们将高度赞赏不会破坏 memory 要求的解决方案!

I have never used xarray , so maybe I am mistaken, but I think you can get the result you want avoiding using loops and conditionals.我从来没有使用xarray ,所以也许我弄错了,但我认为你可以得到你想要避免使用循环和条件的结果。 This is at least twice faster than your example for numpy arrays:这比numpy arrays 的示例至少快两倍:

data = np.kron(np.linspace(0,1,10), np.linspace(1,4,6)).reshape(10,6)
reducedArray = np.empty_like(data)
reducedArray[:, T:] = data[:, T:] - data[:, :-T]
reducedArray[:, :T] = data[:, :T] - data[:, 0, np.newaxis]

I imagine the improvement will be higher when using DataArray s.我想使用DataArray时改进会更高。 It does not use xarray functions but neither depends on numpy functions.它不使用xarray函数,但也不依赖于numpy函数。 I am confident that translating this to xarray will be straightforward, I know that it works if there are no coords , but once you include them, you get an error because of the coords mismatch ( coords of data[:, T:] and of data[:, :-T] are different).我相信将其转换为xarray会很简单,我知道如果没有coords ,它会起作用,但是一旦包含它们,由于coords不匹配( coords of data[:, T:]和 of data[:, :-T]不同)。 Sadly, I can't do better now.可悲的是,我现在不能做得更好。

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

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