使用您的 dtype,我可以创建一个数组:
In [37]: np.array([_],dtype=SPECIAL_TYPE)
Out[37]:
array([ (array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]], dtype=uint8), 1, 'a', 1, 1, list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), 1)],
dtype=[('arr', 'O'), ('int1', 'u1'), ('str', 'O'), ('int2', 'u1'), ('int3', 'u1'), ('list', 'O'), ('int4', 'u1')])
但是尝试用它创建 dataset,即使是 1d,我也会从解释器中退出:
In [38]: f=h5py.File('vlentest.h5','w')
In [39]: db = f.create_dataset('db',(10,), dtype=SPECIAL_TYPE)
In [40]: db[:]
Segmentation fault (core dumped)
有两个问题 - vlen 是否适用于二维数组,是否适用于复合 dtype?您正在推动二维数组中 dtype 中的多个 vlen 的界限。
您是否看过在复合 dtype 中使用 vlen 的文档或示例?
注意 h5py 如何在 numpy 中实现 vlen - 它定义了这些字段为 'O' 对象 dtype。它将指针存储在数组中,而不是可变长度对象本身。通常对象 dtype 数组不能用h5py 保存。但是这些字段必须添加一些注释,h5py 使用这些注释将指针转换为HDF5 接受的那种结构。
Storing string datasets in hdf5 with unicode 探索如何存储 vlen str。
Storing multidimensional variable length array with h5py
实验,用小事陈述
In [14]: f = h5py.File('temp.h5')
In [15]: db1 = f.create_dataset('db1',(5,), dtype=dt1)
In [16]: db2 = f.create_dataset('db2',(5,), dtype=dt2)
In [17]: db1[:]
Out[17]:
array([('',), ('',), ('',), ('',), ('',)],
dtype=[('str', 'O')])
In [18]: db2[:]
Out[18]:
array([('', 0), ('', 0), ('', 0), ('', 0), ('', 0)],
dtype=[('str', 'O'), ('int4', '<i4')])
设置一些db1 值:
In [24]: db1[0]=('a',)
In [25]: db1[1]=('ab',)
In [26]: db1[:]
Out[26]:
array([('a',), ('ab',), ('',), ('',), ('',)],
dtype=[('str', 'O')])
db2 的工作方式相同:
In [30]: db2[0]=('abc',10)
In [31]: db2[1]=('abcde',6)
In [32]: db2[:]
Out[32]:
array([('abc', 10), ('abcde', 6), ('', 0), ('', 0), ('', 0)],
dtype=[('str', 'O'), ('int4', '<i4')])
2 vlen 字符串也可以:
In [34]: dt3 = np.dtype([("str1", h5py.special_dtype(vlen=str)),("str2", h5py.special_dtype(vlen=str))])
In [35]: db3 = f.create_dataset('db3',(3,), dtype=dt3)
In [36]: db3[:]
Out[36]:
array([('', ''), ('', ''), ('', '')],
dtype=[('str1', 'O'), ('str2', 'O')])
In [37]: db3[0] = ('abc','defg')
In [38]: db3[1] = ('abcd','de')
In [39]: db3[:]
Out[39]:
array([('abc', 'defg'), ('abcd', 'de'), ('', '')],
dtype=[('str1', 'O'), ('str2', 'O')])
还有一个数组 vlen
In [41]: dt4 = np.dtype([("str1", h5py.special_dtype(vlen=str)),("list", h5py.special_dtype(vlen=np.int))])
In [42]: dt4
Out[42]: dtype([('str1', 'O'), ('list', 'O')])
In [43]: db4 = f.create_dataset('db4',(3,), dtype=dt4)
In [47]: db4[0]=('abcdef',np.arange(5))
In [48]: db4[1]=('abc',np.arange(3))
In [49]: db4[:]
Out[49]:
array([('abcdef', array([0, 1, 2, 3, 4])), ('abc', array([0, 1, 2])),
('', array([], dtype=int32))],
dtype=[('str1', 'O'), ('list', 'O')])
但我不能使用list
In [50]: db4[2]=('abc',[1,2,3,4])
--------------------------------------------------------------------------
AttributeError: 'list' object has no attribute 'dtype'
h5py 保存数组,而不是列表。显然,这也适用于这些嵌套值。 http://docs.h5py.org/en/latest/special.html 有使用列表设置vlen 的示例,但它首先转换为数组。
如果我尝试保存 2d 数组,它只会写入 1d
In [59]: db4[2]=('abc',np.ones((2,2),int))
In [60]: db4[:]
Out[60]:
array([('abcdef', array([0, 1, 2, 3, 4])), ('abc', array([0, 1, 2])),
('abc', array([1, 1]))],
dtype=[('str1', 'O'), ('list', 'O')])
此 dtype 有效:
In [21]: dt1 = np.dtype([("str1", h5py.special_dtype(vlen=str)),('f1',int),("list", h5py.special_dtype(vlen=np.int))])
这是核心转储
In [30]: dt1 = np.dtype([("f0", h5py.special_dtype(vlen=np.uint8)),('f1',int),("f2", h5py.special_dtype(vlen=np.int))])
这是 vlen uint8 问题,还是 vlen be first 的问题?