2
我正在嘗試使用h5py
模塊使用python slice對象訪問HDF5文件中的數據。我把這個例子放在一起,表明它可以與numpy
數組一起使用,但不能和h5py
一起使用。使用h5py讀取HDF5文件時使用python slice對象?
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]
這樣的分片?