【问题标题】:retriving data saved under HDF5 group as Carray检索保存在 HDF5 组下的数据作为数组
【发布时间】:2017-12-09 06:00:48
【问题描述】:

我是 HDF5 文件格式的新手,我有一个以 HDF5 格式保存的数据(图像)。图像保存在名为“数据”的组下,该组位于根组下的 Carrays 下。我想做的是检索保存的图像的一部分。例如前400或类似的东西。以下是我所做的。

 h5f = h5py.File('images.h5f', 'r')
 image_grp= h5f['/data/']  #the image group (data) is opened
 print(image_grp[0:400])

但我收到以下错误

Traceback (most recent call last):
File "fgf.py", line 32, in <module>
print(image_grp[0:40])
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
    (/feedstock_root/build_artefacts/h5py_1496410723014/work/h5py-2.7.0/h5py/_objects.c:2846)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
    (/feedstock_root/build_artefacts/h5py_1496410723014/work/h5py
    2.7.0/h5py/_objects.c:2804)
File "/..../python2.7/site-packages/h5py/_hl/group.py", line 169, in
    __getitem__oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "/..../python2.7/site-packages/h5py/_hl/base.py", line 133, in _e name = name.encode('ascii')
AttributeError: 'slice' object has no attribute 'encode'

我不确定为什么会出现此错误,但我什至不确定是否可以对保存为单独数据集的图像进行切片。

【问题讨论】:

  • 请提供更多信息。您的组数据中是否有多个数据集,或者您的组实际上是一个数据集?如果您的组“数据”中有多个数据集,则必须先检索数据集名称,然后遍历数据集以获取数据。对于初学者来说,先用 hdfview 检查文件也很舒服。 support.hdfgroup.org/products/java/release/download.html
  • 谢谢@max9111 是的,实际上我的组“数据”包含多个数据集,在我发布这个问题后,我尝试迭代数据集名称以获取数据并且它可以工作,尽管它很慢...... .!
  • 您能否提供代码显示您的具体操作?慢 200-400/秒或更低是什么意思?数组的大小是否相同,如果是,为什么它们实际上以这种方式存储?

标签: python-2.7 hdf5


【解决方案1】:

昨天我遇到了类似的错误,并编写了一小段代码来获取我想要的 h5py 文件片段。

import h5py

def h5py_slice(h5py_file, begin_index, end_index):
    slice_list = []
    with h5py.File(h5py_file, 'r') as f:
        for i in range(begin_index, end_index):
            slice_list.append(f[str(i)][...])
    return slice_list

它可以像这样使用

the_desired_slice_list = h5py_slice('images.h5f', 0, 400)

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但它是搜索'slice' object has no attribute 'encode'时的第一个命中,并且没有解决方案。

    发生错误是因为“组”是一个没有encoding 属性的group。您正在寻找 dataset 元素。

    您需要找到/知道包含dataset 的项目的密钥。

    一个建议是列出组中的所有键,然后猜测它是哪一个:

    print(list(image_grp.keys()))
    

    这将为您提供组中的密钥。 一个常见的情况是第一个元素是图像,所以你可以这样做:

    image_grp= h5f['/data/']
    image= image_grp(image_grp.keys[0])
    print(image[0:400])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2021-02-17
      相关资源
      最近更新 更多