gpt4 book ai didi

python - 使用 h5py 读取 HDF5 文件时使用 python 切片对象?

转载 作者:行者123 更新时间:2023-11-28 22:52:22 27 4
gpt4 key购买 nike

我正在尝试使用 python slice 对象通过 h5py 模块访问 HDF5 文件中的数据。我把这个例子放在一起是为了表明它适用于 numpy 数组,但不适用于 h5py

import h5py
import numpy as np

slice_obj = [slice(None,3,None), slice(2,5,None)]

test_array = np.ones((3,5))
print test_array[0:3,2:5]
print test_array[slice_obj]

f = h5py.File("testing.hdf5","w")
f['data'] = test_array
f.close()

f = h5py.File("testing.hdf5","r")
test2 = f['data'][0:3,2:5]
print test2
test2 = f['data'][slice_obj]
print test2
f.close()

这给出了以下输出:

[[ 1.  1.  1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]
Traceback (most recent call last):
File "slice.py", line 17, in <module>
test2 = f['data'][slice_obj]
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/h5py/_hl/dataset.py", line 439, in __getitem__
self.id.read(mspace, fspace, arr, mtype)
File "h5d.pyx", line 179, in h5py.h5d.DatasetID.read (h5py/h5d.c:2479)
File "_proxy.pyx", line 118, in h5py._proxy.dset_rw (h5py/_proxy.c:1300)
File "_proxy.pyx", line 84, in h5py._proxy.H5PY_H5Dread (h5py/_proxy.c:1051)
IOError: can't read data (Dataset: Read failed)

有谁知道 h5py 是否无法做到这一点?如果不是,是否有另一种方法在 h5py 中切片,使用对象或变量,而不是像 f['data'][0:3,2: 这样显式键入切片: 5] 在我的例子中?

最佳答案

玩弄你的例子:

test2 = f['data']
print test2
print test2.shape
print test2[0:3,2:5]
print test2[slice(None,3,None),slice(2,5,None)] # ok
print test2[slice_obj[0],slice_obj[1]] # ok
print test2[tuple(slice_obj)] # ok
print test2[[slice(None,3,None),slice(2,5,None)]] # fail
print f['data'][tuple(slice_obj)] 3 ok

所以看起来 h5py 数组可以使用切片,但不能将列表拆分为其元素。但它确实需要一个元组。我的猜测是 getitem 的实现方式略有不同。

您正在做高级索引。 numpy 文档说:

Advanced indexing is triggered when the selection object, obj,... a tuple with at least one sequence object.... when the selection object is not a tuple, it will be referred to as if it had been promoted to a 1-tuple, which will be called the selection tuple.

h5py 可能不会将此提升为元组。否则它似乎可以很好地进行高级索引。

关于python - 使用 h5py 读取 HDF5 文件时使用 python 切片对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20523230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com