我使用 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.