首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python HDF5属性

Python HDF5属性
EN

Stack Overflow用户
提问于 2020-12-28 15:58:41
回答 1查看 339关注 0票数 0

我正在尝试将度量属性保存在HDF5文件中。我花了很多时间处理格式化文件,在这些文件中,单个属性条目中似乎有一组数据类型不同的属性。

例如,对于我的文件,命令

代码语言:javascript
运行
复制
f = h5py.File('test.data','r+')
f['Measurement/Surface'].attrs['X Converter']

产生

代码语言:javascript
运行
复制
array([(b'LateralCat', b'Pixels', array([0.        , 2.00097752, 0.        , 0.        ]))],
      dtype=[('Category', 'O'), ('BaseUnit', 'O'), ('Parameters', 'O')])

在这里,前两个条目是字符串,第三个条目是数组。现在,如果我试图将值保存到另一个文件中:

代码语言:javascript
运行
复制
f1 = h5py.File('test_output.data','r+')
f1['Measurement/Surface'].attrs.create('X Converter',[(b'LateralCat', b'Pixels', np.array([0.        , 2.00097752, 0.        , 0.        ]))])

我知道这个错误:

跟踪(最近一次调用):文件"",第1行,在F1‘度量衡/表面’.atts.create(‘X转换器’,[(b‘’LateralCat‘,b’‘Pixels’,np.array(0 )。,2.00097752,0。,0。)])文件"C:\WinPython\WinPython-64bit-3.6.3.0Zero\python-3.6.3.amd64\lib\site-packages\h5py_hl\attrs.py",第171行,在create = h5t.py_create(original_dtype,logical=True)文件"h5py\h5t.pyx“中,第1611行,在h5py.h5t.py_create文件"h5py\h5t.pyx”中,第1633行,在h5py.h5t.py_create文件"h5py\h5t.pyx“中,第1688行,在h5py.h5t.py_create TypeError中: Object dtype dtype('O')没有本机HDF5等效

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-28 16:53:47

你不是在拯救同样的东西。原始的dtype是重要的。

代码语言:javascript
运行
复制
In [101]: [(b'LateralCat', b'Pixels', np.array([0.        , 2.00097752, 0.        ,
     ...:  0.        ]))]
Out[101]: 
[(b'LateralCat',
  b'Pixels',
  array([0.        , 2.00097752, 0.        , 0.        ]))]
In [102]: np.array(_)
<ipython-input-102-7a2cd91c32ca>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  np.array(_)
Out[102]: 
array([[b'LateralCat', b'Pixels',
        array([0.        , 2.00097752, 0.        , 0.        ])]],
      dtype=object)

In [104]: np.array([(b'LateralCat', b'Pixels', np.array([0.        , 2.00097752, 0.
     ...:         , 0.        ]))],
     ...:       dtype=[('Category', 'O'), ('BaseUnit', 'O'), ('Parameters', 'O')])
Out[104]: 
array([(b'LateralCat', b'Pixels', array([0.        , 2.00097752, 0.        , 0.        ]))],
      dtype=[('Category', 'O'), ('BaseUnit', 'O'), ('Parameters', 'O')])

In [105]: x = _
In [106]: x.dtype
Out[106]: dtype([('Category', 'O'), ('BaseUnit', 'O'), ('Parameters', 'O')])

In [108]: x['Category']
Out[108]: array([b'LateralCat'], dtype=object)
In [109]: x['BaseUnit']
Out[109]: array([b'Pixels'], dtype=object)
In [110]: x['Parameters']
Out[110]: 
array([array([0.        , 2.00097752, 0.        , 0.        ])],
      dtype=object)

尽管这并不能很好地解决这个问题,因为dtype仍然包含对象dtype字段。

代码语言:javascript
运行
复制
In [111]: import h5py
In [112]: f=h5py.File('test.h5','w')
In [113]: 
In [113]: g = f.create_group('test')
In [114]: g.attrs.create('converter',x)
Traceback (most recent call last):
...
TypeError: Object dtype dtype('O') has no native HDF5 equivalent

正如注释中所指出的,numpy对象dtype在写入h5py时是有问题的。你知道原始文件是如何创建的吗?可能存在一些格式或结构,h5py将其呈现为带有对象字段的复合dtype,但不能直接写。为了了解更多信息,我必须深入了解更多的文档(可能还有原始文件)。

https://docs.h5py.org/en/stable/special.html

我可以将这些数据写成一个更传统的结构化数组:

代码语言:javascript
运行
复制
In [120]: y=np.array([(b'LateralCat', b'Pixels', np.array([0.        , 2.00097752,
     ...: 0.        , 0.        ]))],
     ...:       dtype=[('Category', 'S20'), ('BaseUnit', 'S20'), ('Parameters', 'fl
     ...: oat',4)])
In [121]: y
Out[121]: 
array([(b'LateralCat', b'Pixels', [0.        , 2.00097752, 0.        , 0.        ])],
      dtype=[('Category', 'S20'), ('BaseUnit', 'S20'), ('Parameters', '<f8', (4,))])

In [122]: g.attrs.create('converter',y)
In [125]: g.attrs['converter']
Out[125]: 
array([(b'LateralCat', b'Pixels', [0.        , 2.00097752, 0.        , 0.        ])],
      dtype=[('Category', 'S20'), ('BaseUnit', 'S20'), ('Parameters', '<f8', (4,))])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65480393

复制
相关文章

相似问题

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