首页 > 解决方案 > 懒惰地交错/交织 Dask 数组

问题描述

我需要逐帧交错两个大型 HDF5 数据集,这些数据集表示来自显微镜测量的两个通道的视频帧。我认为 Dask 适合这项工作和下游流程。

这两个数组具有相同的形状和数据类型。基于此链接,我可以使用 NumPy 来处理小于内存的数组: 交织两个 numpy 数组

import numpy as np
# a numpy example of channel 1 data
ch1 = np.arange(1,5)[:,np.newaxis,np.newaxis]*np.ones((4,3,2))

# channel 2 has the same shape and dtype
ch2 = np.arange(10,50,10)[:,np.newaxis,np.newaxis]*np.ones((4,3,2))

# the interleaving starts with assigning a new array with douled size of the first dimension
ch1_2 = np.empty((2*ch1.shape[0],*ch1.shape[1:]), dtype=ch1.dtype)
# two assignments takes care of the interleaving 
ch1_2[0::2] = ch1
ch1_2[1::2] = ch2

不幸的是,它不适用于 Dask。

import dask.array as da
da_ch1 = da.from_array(ch1)
da_ch2 = da.from_array(ch2)
da_ch1_2 = da.empty((2*da_ch1.shape[0],*da_ch1.shape[1:]), dtype=da_ch1.dtype)
da_ch1_2[0::2] = da_ch1
da_ch1_2[1::2] = da_ch2

它失败了:“不支持 <class 'slice'> 的项目分配”。

任何人都可以帮助我使用与 Dask 兼容的替代方法吗?任何帮助,将不胜感激。

标签: pythonarraysdaskinterleave

解决方案


这是该问题的高级 Dask Array 解决方案:

da_ch1_2=da.rollaxis(da.stack((da_ch1,da_ch2)),axis=1).reshape((-1,*da_ch1.shape[1:]))

推荐阅读