[英]Can't access returned h5py object instance
I have a very weird issue here.我这里有一个很奇怪的问题。 I have 2 functions: one which reads an HDF5 file created using h5py and one which creates a new HDF5 file which concatenates the content returned by the former function.
我有 2 个函数:一个读取使用 h5py 创建的 HDF5 文件,另一个创建一个新的 HDF5 文件,该文件连接前一个 function 返回的内容。
def read_file(filename):
with h5py.File(filename+".hdf5",'r') as hf:
group1 = hf.get('group1')
group1 = hf.get('group2')
dataset1 = hf.get('dataset1')
dataset2 = hf.get('dataset2')
print group1.attrs['w'] # Works here
return dataset1, dataset2, group1, group1
And the create file function并创建文件 function
def create_chunk(start_index, end_index):
for i in range(start_index, end_index):
if i == start_index:
mergedhf = h5py.File("output.hdf5",'w')
mergedhf.create_dataset("dataset1",dtype='float64')
mergedhf.create_dataset("dataset2",dtype='float64')
g1 = mergedhf.create_group('group1')
g2 = mergedhf.create_group('group2')
rd1,rd2,rg1,rg2 = read_file(filename)
print rg1.attrs['w'] #gives me <Closed HDF5 group> message
g1.attrs['w'] = "content"
g1.attrs['x'] = "content"
g2.attrs['y'] = "content"
g2.attrs['z'] = "content"
print g1.attrs['w'] # Works Here
return mergedhf.get('dataset1'), mergedhf.get('dataset2'), g1, g2
def calling_function():
wd1, wd2, wg1, wg2 = create_chunk(start_index, end_index)
print wg1.attrs['w'] #Works here as well
Now the problem is, the dataset and the properties from the new file created and represented by wd1, wd2, wg1 and wg2 can be accessed by me and I can access the attribute data but i cant do the same for which I have read and returned the value for.现在的问题是,我可以访问由 wd1、wd2、wg1 和 wg2 创建和表示的新文件中的数据集和属性,我可以访问属性数据,但我无法执行我已阅读并返回的内容的价值。
Can anyone help me fetch the values of the dataset and group when I have returned the reference to the calling function?当我返回对调用 function 的引用时,任何人都可以帮我获取数据集和组的值吗?
The problem is in read_file
, this line:问题出在
read_file
中,这一行:
with h5py.File(filename+".hdf5",'r') as hf:
This closes hf
at the end of the with
block, ie when read_file
returns.这将在
with
块的末尾关闭hf
,即当read_file
返回时。 When this happens, the datasets and groups also get closed and you can no longer access them.发生这种情况时,数据集和组也会关闭,您将无法再访问它们。
There are (at least) two ways to fix this.有(至少)两种方法可以解决这个问题。 Firstly, you can open the file like you do in
create_chunk
:首先,您可以像在
create_chunk
中一样打开文件:
hf = h5py.File(filename+".hdf5", 'r')
and keep the reference to hf
around as long as you need it, before closing it:并在关闭它之前保留对
hf
的引用,只要你需要它:
hf.close()
The other way is to copy the data from the datasets in read_file
and return those instead:另一种方法是从
read_file
中的数据集中复制数据并返回这些数据:
dataset1 = hf.get('dataset1')[:]
dataset2 = hf.get('dataset2')[:]
Note that you can't do this with the groups.请注意,您不能对组执行此操作。 The file needs to be open for as long as you need to do things with the groups.
只要您需要对组执行操作,文件就需要打开。
The problem is in read_file, this line:
with h5py.File(filename+".hdf5",'r') as hf:
This closes hf at the end of the with block, ie when read_file returns.问题出在 read_file 中,这一行:
with h5py.File(filename+".hdf5",'r') as hf:
这会在 with 块的末尾关闭 hf,即当 read_file 返回时。 When this happens, the datasets and groups also get closed and you can no longer access them.发生这种情况时,数据集和组也会关闭,您将无法再访问它们。
For those who come across this and are reading a scalar dataset make sure to index using [()]
:对于那些遇到这种情况并正在阅读标量数据集的人,请确保使用
[()]
进行索引:
scalar_dataset1 = hf['scalar_dataset1'][()]
I had a similar issue as OP resulting in a return value of <closed hdf5 dataset>
.我遇到了与 OP 类似的问题,导致返回值
<closed hdf5 dataset>
。 However, I would get a ValueError when attempting to slice my scalar dataset with [:]
.但是,在尝试使用
[:]
对我的标量数据集进行切片时,我会得到一个ValueError 。
"ValueError: Illegal slicing argument for scalar dataspace"
Indexing with [()]
along with @Yossarian's answer helped solve my problem.使用
[()]
进行索引以及@Yossarian 的回答有助于解决我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.