python - 懒惰地交错/交织 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 兼容的替代方法吗?任何帮助,将不胜感激。
解决方案
这是该问题的高级 Dask Array 解决方案:
da_ch1_2=da.rollaxis(da.stack((da_ch1,da_ch2)),axis=1).reshape((-1,*da_ch1.shape[1:]))
推荐阅读
- python-3.x - 网页抓取时“[]”作为输出是什么意思
- swift - swift if条件有多个案例
- c# - 适用于 .Net Core 的 Microsoft 服务点
- openssl - 输出的 bin 文件比纯文本 bin 文件大 16 字节;当我们使用 opnessl AES ECB 命令进行测试时
- django - dDjango Tempalte TemplateSyntaxError
- maven - Maven 在编译阶段转到 https://repo.maven.apache.org/maven2/,甚至不同的 repo 也被定义为“中央”
- android - 如何在 onAvitivityResult 中使用存储库或视图模型方法
- java - Beanio:如何验证一个字段,取决于另一个字段值?
- wordpress - 在 WooCommerce 中更改添加到购物车按钮的 HTML
- scala - 在尝试在 PySpark 中使用 SCALA UDF 时,尽管提供了 --jars:错误:pyspark.sql.utils.AnalysisException:u'Can not load class