将变长序列写入复合数组

时间:2021-04-21 09:10:59

标签: hdf5 h5py

我在 h5py 中使用复合数据类型,其中一些元素是可变长度数组。我找不到设置项目的方法。下面的 MWE 展示了 6 种不同的方法来做到这一点(顺序索引——无论如何都不能在 h5py 中工作,融合索引,对列/行进行读取-修改-提交),这两种方法都不起作用。

正确的方法是什么?为什么在将整数列表写入 public class MyClass { // ... public async Task<MyClass> Function1() { // code which uses `await` below // .... // end of this code return this; } public async Task<string> Function2() { // code which uses `await` below // .... // end of this code return "some text"; } } 列表时,h5py 会说 Cannot change data-type for object array

int32

1 个答案:

答案 0 :(得分:0)

在处理复合数据集时,我发现最好在单个语句中添加所有行数据。我调整了您的代码并展示了如何添加 3 行数据(每行长度不同)。注意我是如何:1)用元组定义数据行; 2) 用 np.array() 定义整数列表;和 3) 不要引用字段名称 ['a']

with h5py.File('test-vla.h5','w') as h5:
    dt=np.dtype([('a',h5py.vlen_dtype(np.dtype('int32')))])
    dset=h5.create_dataset('test',(5,),dtype=dt)
    print (dset.dtype, dset.shape)
    dset[0] = ( np.array([0,1,2]), )
    dset[1] = ( np.array([1,2,3,4]), )
    dset[2] = ( np.array([0,1,2,3,4]), )

有关详细信息,请查看 HDF 组论坛上HDF5 辅助工具/h5py下的这篇文章:
Compound datatype with int, float and array of floats

相关问题