3

h5pyを使用しています。HDF5 ファイル内に、文字列 (column1) と region_reference (column2) の複合データセットが必要です。このために、文字列と参照のnumpy dtypeを定義しようとしています。

しかし、これ以前でも、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.
4

1 に答える 1

1

私は同じ問題を経験し、h5py で問題を作成しました (ただし、おそらくこれは 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 でこのコードを見た後、このようなエラーは、HDF をまったく使用せずに単純な numpy で簡単に再現できます。

objarr = np.array(123, dtype=np.dtype('O'))
objarr.astype(np.dtype([('field', np.dtype('O'))]))

ここの 2 行目では、まったく同じエラーが発生します。一方、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'))]))

これで、少なくともこれが発生する理由と回避方法についてのアイデアが得られました:)これ以上のことに興味がある場合は、言及された問題に従って開発者の回答を確認してください(現在、それらのどれもありません)。

于 2014-07-27T13:54:04.997 に答える