【问题标题】:Conversion from (xarray) dask.array to numpy array is very slow从 (xarray) dask.array 到 numpy 数组的转换非常慢
【发布时间】:2017-05-18 01:10:38
【问题描述】:

我最近开始使用 xarray。这是一个非常有用的工具。但是我遇到了一些小问题,我相信这很容易解决。我的问题是,我想从多维(时间、纬度、经度)dask.array 中为给定的纬度和经度值选择时间序列。使用 .sel 切片效果非常好且速度快,但是当我尝试使用 np.array 选项获取实际值时,它需要很长时间。 以下效果非常好:

enter code here
% time y = ENS_MEAN.prec.sel(latitude=20, longitude=20)
% time ENS_MEAN.prec.sel(latitude=20, longitude=20)

Output:

CPU times: user 10.7 ms, sys: 908 µs, total: 11.6 ms
Wall time: 10.6 ms
CPU times: user 2.94 ms, sys: 0 ns, total: 2.94 ms
Wall time: 2.83 ms



Out[42]:
<xarray.DataArray 'prec' (time: 29)>
dask.array<getitem..., shape=(29,), dtype=float64, chunksize=(29,)>
Coordinates:
longitude  float32 20.0
latitude   float32 20.0
* time       (time) datetime64[ns] 1982-05-01 1983-05-01 1984-05-01 ...

但是,当我尝试以 numpy 数组格式(见下文)获取实际值时,转换最多需要 2 分钟。我想知道这个问题是否与块大小有关?

%time np.array(y)

CPU times: user 2min 12s, sys: 47.1 s, total: 2min 59s
Wall time: 2min 20s

/home/......./anaconda3/lib/python3.5/site-
packages/dask/array/numpy_compat.py:45: RuntimeWarning: invalid value 
encountered in true_divide
x = np.divide(x1, x2, out)

Out[41]:
array([-0.00881837, -0.02694129,  0.03033962,  0.01635965, -0.01392146,
   -0.03904842, -0.00269604, -0.00114008,  0.0051511 , -0.02376757,
   -0.01574946, -0.01025411, -0.01544669, -0.02065624, -0.02342096,
   -0.01664323,  0.08460527,  0.04862781, -0.0035033 , -0.00721429,
   -0.00995117,  0.0263697 , -0.00358022,  0.00376811, -0.01527904,
   -0.00548013,  0.03295138, -0.01114444,  0.02648388])

非常感谢您回答我的问题。

【问题讨论】:

    标签: python python-xarray


    【解决方案1】:

    在这种情况下,在您调用 np.array() 之前,实际上不会计算任何内容——在此之前只构建抽象计算图。

    当你从磁盘加载数据时,一个简单的方法应该是设置一个更小的块大小,例如,ds = xarray.open_dataset(..., chunks={'latitude': 1, 'longitude': 1})。 Dask 应该优化索引操作,但我们最近遇到了一些问题—— 请参阅this GitHub issue 进行讨论。

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 1970-01-01
      • 2011-08-11
      • 2016-01-16
      • 2015-02-23
      • 2017-01-08
      • 2015-10-15
      • 2021-09-12
      • 2018-05-04
      相关资源
      最近更新 更多