微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在不使用 dask 的情况下分块加载 xarray DataArray

如何解决在不使用 dask 的情况下分块加载 xarray DataArray

是否可以将 DataArray 的部分块(存储为单个 netcdf 文件)从磁盘加载到内存中(即不立即加载整个数据阵列)但使用 dask-dataarrays?

问题是我使用 dask 作为我的集群调度程序来提交作业,并且在这些作业中 - 我想将数据数组从磁盘中分页分页到内存中。不幸的是,dask 不喜欢嵌套的 dask 调度程序,因此尝试按照 da = xr.open_datarray( file,chunks={'time':1000} ) 不起作用(导致 dask 抛出嵌套的守护进程错误)。

理想情况下,我想做这样的事情 - 无需将整个数据数组加载到内存中,而只将相关部分加载:

da = xr.open_datarray( my_file )  # lazy open the file
for t in range( 0,len( da ),1000 ) :
    da_actual = da[t:t+1000].load() # materialize the data into memory
    # do some compute with da_actual

有关如何实现这一目标的任何指示/想法将不胜感激

解决方法

delayed 包装它可能会有所帮助:

import dask

@dask.delayed
def custom_array_func(my_file):
    da = xr.open_datarray( my_file )  # lazy open the file
        for t in range( 0,len( da ),1000 ) :
            da_actual = da[t:t+1000].load() # materialize the data into memory
            # do some compute with da_actual
    return final_result # or can return None if nothing is needed

[computed_results] = dask.compute([custom_array_func(my_file) for my_file in list_of_files])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。