我使用h5py。我想在我的HDF5文件中有一个字符串(column1)和regional_reference(column2)的复合数据集。为此,我试图定义一个numpy dtype的String和Reference。
但即使在此之前,我也未能定义hdf5区域引用的numpy dtype数组。
##map_h5py.py
import h5py
import numpy as np
h = h5py.File('testing_mapping.h5', 'a')
cell_names = ['cell0', 'cell1', 'cell2', 'cell3']
dummy_data = np.random.rand(4,20)
##create random data
dset = h.create_dataset('/data/Vm', data=dummy_data, dtype='float32')
#declare a data type
sp_type = np.dtype([('ref',h5py.special_dtype(ref=h5py.RegionReference))])
##this works - 1
refs_list = []
for ii in range(dset.shape[0]):
refs_list.append(dset.regionref[ii])
h.create_dataset('/map/Vm_list', data=refs_list, dtype=h5py.special_dtype(ref=h5py.RegionReference))
##this doesn't - 2
ref_dset = h.create_dataset('/map/Vm_pre', shape=(dset.shape[0],), dtype=sp_type)
for ii in range(dset.shape[0]):
ref_dset[ii] = dset.regionref[ii]
# #this doesn't - 3
ref_numpy = np.zeros(dset.shape[0], dtype=sp_type)
for ii in range(dset.shape[0]):
ref_numpy[ii] = dset.regionref[ii]
h.create_dataset('/map/Vm_post', data=ref_numpy, dtype=sp_type)
h.close()
2和3的错误如下,
ref_numpy[ii] = dset.regionref[ii]
ValueError: Setting void-array with object members using buffer.
答案 0 :(得分:1)
我遇到了同样的问题,并在h5py创建了一个issue(但是,这可能会被发送到numpy - 见下文)。在这里,我将复制该问题的重要信息,以便了解为什么会发生这种情况以及如何克服它。
这是一个问题的最小例子,它表明明显的分配方法不起作用:
with h5py.File('tst.hdf5', mode='w') as f:
ds1 = f.create_dataset('ds1', shape=(1,), dtype=np.dtype([('objfield', h5py.special_dtype(ref=h5py.Reference))]))
ds2 = f.create_dataset('ds2', shape=(), dtype=np.int)
# All these lines raise ValueError:
# ds1[0, 'objfield'] = ds2.ref
# ds1[0, 'objfield'] = (ds2.ref,)
# ds1[0, 'objfield'] = np.array(ds2.ref)
# ds1[0, 'objfield'] = np.array((ds2.ref,))
# ds1[0, 'objfield'] = np.array((ds2.ref,), dtype=h5py.special_dtype(ref=h5py.Reference))
# ds1[0, 'objfield'] = np.array(ds2.ref, dtype=np.dtype([('objfield', h5py.special_dtype(ref=h5py.Reference))]))
# Only this one works:
ds1[0, 'objfield'] = np.array((ds2.ref,), dtype=np.dtype([('objfield', h5py.special_dtype(ref=h5py.Reference))]))
这里的最后一行是我创建的解决方法。
当抛出错误时,会抛出这段代码:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-63893646daac> in <module>()
4
5 # All these lines raise ValueError:
----> 6 ds1[0, 'objfield'] = ds2.ref
7 # ds1[0, 'objfield'] = (ds2.ref,)
8 # ds1[0, 'objfield'] = np.array(ds2.ref)
/usr/local/lib/python2.7/dist-packages/h5py/_hl/dataset.pyc in __setitem__(self, args, val)
506 val = numpy.asarray(val, dtype=dtype, order='C')
507 if cast_compound:
--> 508 val = val.astype(numpy.dtype([(names[0], dtype)]))
509 else:
510 val = numpy.asarray(val, order='C')
ValueError: Setting void-array with object members using buffer.
在dataset.py中查看此代码之后,可以轻松地使用简单的numpy重现这样的错误,而不使用HDF:
objarr = np.array(123, dtype=np.dtype('O'))
objarr.astype(np.dtype([('field', np.dtype('O'))]))
这里的第二行引发完全相同的错误。另一方面,与HDF示例类似,此代码有效:
objarr = np.array((123, ), dtype=np.dtype([('field', np.dtype('O'))]))
objarr = np.asarray(objarr, dtype=np.dtype('O'))
objarr = objarr.astype(np.dtype([('field', np.dtype('O'))]))
所以,现在你至少知道为什么会这样,以及如何解决它:)如果你对此不感兴趣,请按照提到的issue开发人员的答案(现在有没有一个。)