首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建数据集的h5py数据集

如何创建数据集的h5py数据集
EN

Stack Overflow用户
提问于 2017-07-29 07:19:34
回答 1查看 4.7K关注 0票数 0

我是HDF5新手,我正在尝试创建一个复合类型的数据集,其中包含三列: MD5、size、another dataset。

我如何才能做到这一点?

我尝试了以下代码:

代码语言:javascript
复制
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)

我得到了错误:

代码语言:javascript
复制
typeError: __init__() takes exactly 2 arguments (3 given)

关于这一行:

代码语言:javascript
复制
dtype1 = h5py.Dataset('myset', (100,))
EN

回答 1

Stack Overflow用户

发布于 2017-07-29 08:45:22

h5py.Dataset('myset', (100,))尝试直接创建dataset对象(调用它的__init__?)。但根据参考文献:

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

代码语言:javascript
复制
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'

代码语言:javascript
复制
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')])

在Python中有定义的类型,如字符串、整型和浮点型,以及object (不是list、dict或其他numpy类)。

我可以将复合数据类型保存为h5py,但不能保存对象数据类型。有一个加载到numpy对象数据类型中的h5py数据类型,但它通常不会从另一个方向工作。

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 -对象引用

代码语言:javascript
复制
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">]

使用复合数据类型

代码语言:javascript
复制
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使用dtypemetadata属性来存储有关引用的信息:

代码语言:javascript
复制
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})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45383662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档