沿XArray的时间维度应用函数 [英] Apply function along time dimension of XArray
问题描述
我有一个图像堆栈存储在XArray数据数组中,其尺寸为时间x,y,我想沿每个像素的时间轴在其上应用自定义函数,以使输出为尺寸为x的单个图像, y.
I have an image stack stored in an XArray DataArray with dimensions time, x, y on which I'd like to apply a custom function along the time axis of each pixel such that the output is a single image of dimensions x,y.
我尝试过:apply_ufunc,但是函数失败,说明我需要先将数据加载到RAM中(即不能使用Dask数组).理想情况下,我想在内部将DataArray保留为Dask Array,因为不可能将整个堆栈加载到RAM中.确切的错误消息是:
I have tried: apply_ufunc but the function fails stating that I need to first load the data into RAM (i.e. cannot use a Dask Array). Ideally, I'd like to keep the DataArray as Dask Arrays internally as it isn't possible to load the entire stack into RAM. The exact error message is:
ValueError:apply_ufunc在参数上遇到了dask数组,但是尚未启用对dask数组的处理.设置
dask
参数或使用.load()
或.compute()
我的代码当前如下所示:
My code currently looks like this:
import numpy as np
import xarray as xr
import pandas as pd
def special_mean(x, drop_min=False):
s = np.sum(x)
n = len(x)
if drop_min:
s = s - x.min()
n -= 1
return s/n
times = pd.date_range('2019-01-01', '2019-01-10', name='time')
data = xr.DataArray(np.random.rand(10, 8, 8), dims=["time", "y", "x"], coords={'time': times})
data = data.chunk({'time':10, 'x':1, 'y':1})
res = xr.apply_ufunc(special_mean, data, input_core_dims=[["time"]], kwargs={'drop_min': True})
如果我确实使用.compute将数据加载到RAM中,那么我仍然会遇到以下错误:
If I do load the data into RAM using .compute then I still end up with an error which states:
ValueError:应用的函数返回的数据具有意外的维数:0与2,维数('y','x')
ValueError: applied function returned data with unexpected number of dimensions: 0 vs 2, for dimensions ('y', 'x')
我不确定我到底缺少什么/做错了什么.
I'm not sure entirely what I am missing/doing wrong.
推荐答案
def special_mean(x, drop_min=False):
s = np.sum(x)
n = len(x)
if drop_min:
s = s - x.min()
n -= 1
return s/n
times = pd.date_range('2019-01-01', '2019-01-10', name='time')
data = xr.DataArray(np.random.rand(10, 8, 8), dims=["time", "y", "x"], coords={'time': times})
data = data.chunk({'time':10, 'x':1, 'y':1})
res = xr.apply_ufunc(special_mean, data, input_core_dims=[["time"]], kwargs={'drop_min': True}, dask = 'allowed', vectorize = True)
上面的代码应该可以工作.
The code above should work.
这篇关于沿XArray的时间维度应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!