2017-07-28 9 views
0

私はHDF5を初めて使用しており、MD5、サイズ、別のデータセットの3つの列を持つ複合型のデータセットを作成しようとしています。データセットのh5pyデータセットの作成方法

どうすればこの問題を解決できますか?

Iは、次のコードを試みた:私はエラーを取得

import h5py 
import numpy as np 

dbfile = h5py.File("test.h5",'w') 
dtype1 = h5py.Dataset('myset', (100,)) 
dtype2 = np.dtype([ 
    ('MD5', np.str_, 32), 
    ('size', "i8"), 
    ('timestep0', dtype1) 
    ]) 
records = dbfile.create_dateset('records', (4,), rec_type) 

を:

typeError: __init__() takes exactly 2 arguments (3 given) 

ラインを参照:

dtype1 = h5py.Dataset('myset', (100,)) 
+1

「h5py.Dataset()」コマンドは何をすべきでしょうか?その使用はどこに文書化されていますか? http://docs.h5py.org/en/latest/high/dataset.html#creating-datasets – hpaulj

+0

タイプデータセットを定義しようとしています。私はh5pyを仮定しました.Datasetはそれを行います。問題の一部は、ドキュメンテーションの中で私が見つけられないことです。 – user3394040

+0

私は分かりません。 'numpy'配列、または' HDF5'参照を使ってあなたがしようとしていることを示してください。 – hpaulj

答えて

0

h5py.Dataset('myset', (100,))直接datasetオブジェクトを作成しようとする(呼び出しますそれは__init__ですか?)しかし、リファレンスによると:

http://docs.h5py.org/en/latest/high/dataset.html#reference

class Dataset(identifier) 

Dataset objects are typically created via Group.create_dataset(), or by 
retrieving existing datasets from a file. Call this constructor to 
create a new Dataset bound to an existing DatasetID identifier. 

あなたは(私はまだ理解していない)、このようなオブジェクトを取得できたとしても、np.dtypeで仕事に行くのではありません。私はdatetime.datetimeオブジェクトに置き換える場合たとえば、結果がdtype='O'

In [503]: dtype2 = np.dtype([ 
    ...:  ('MD5', np.str_, 32), 
    ...:  ('size', "i8"), 
    ...:  ('timestep0', datetime.datetime) 
    ...:  ]) 

In [504]: dtype2 
Out[504]: dtype([('MD5', '<U32'), ('size', '<i8'), ('timestep0', 'O')]) 

numpyでdytes文字列、int型や山車などの定義されたタイプ、およびobject(ないリスト、辞書や他のPythonのクラス)です。

化合物のdtypeをh5pyに保存できますが、オブジェクトのdtypeは保存できません。 dtypeはnumpyオブジェクトdtypeにロードされますが、一般には他の方向には作用しません。

http://docs.h5py.org/en/latest/special.html#variable-length-strings

hdf5 can't write numpy array of object type

http://docs.h5py.org/en/latest/refs.html - 化合物DTYPE持つオブジェクト参考文献

In [7]: import h5py 
In [8]: f = h5py.File('wtihref.h5','w') 
In [9]: ds0 = f.create_dataset('dset0',np.arange(10)) 
In [10]: ds1 = f.create_dataset('dset1',np.arange(11)) 
In [11]: ds2 = f.create_dataset('dset2',np.arange(12)) 
In [12]: ds2.ref 
Out[12]: <HDF5 object reference> 
In [13]: ref_dtype = h5py.special_dtype(ref=h5py.Reference) 
In [14]: ref_dtype 
Out[14]: dtype('O') 
In [16]: rds = f.create_dataset('refdset', (5,), dtype=ref_dtype) 
In [17]: rds[:3]=[ds0.ref, ds1.ref, ds2.ref] 
In [28]: [f[r] for r in rds[:3]] 
Out[28]: 
[<HDF5 dataset "dset0": shape (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), type "<f4">, 
<HDF5 dataset "dset1": shape (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), type "<f4">, 
<HDF5 dataset "dset2": shape (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), type "<f4">] 

In [55]: dt2 = np.dtype([('x',int),('y','S12'),('z',ref_dtype)]) 
In [56]: rds1 = f.create_dataset('refdtype', (5,), dtype=dt2) 
In [72]: rds1[0]=(0,b'ONE',ds0.ref) 
In [75]: rds1[1]=(1,b'two',ds1.ref) 
In [76]: rds1[2]=(2,b'three',ds2.ref) 
In [82]: rds1[:3] 
Out[82]: 
array([(0, b'ONE', <HDF5 object reference>), 
     (1, b'two', <HDF5 object reference>), 
     (2, b'three', <HDF5 object reference>)], 
     dtype=[('x', '<i4'), ('y', 'S12'), ('z', 'O')]) 
In [83]: f[rds1[0]['z']] 
Out[83]: <HDF5 dataset "dset0": shape (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), type "<f4"> 

h5pyはのmetadataプロパティを使用リファレンスに情報を格納するための

In [84]: ref_dtype.metadata 
Out[84]: mappingproxy({'ref': h5py.h5r.Reference}) 
In [85]: dt2.fields['z'] 
Out[85]: (dtype('O'), 16) 
In [86]: dt2.fields['z'][0].metadata 
Out[86]: mappingproxy({'ref': h5py.h5r.Reference}) 
関連する問題