简体   繁体   English

h5py 如何将数据集移动到新组 - 奇怪的错误

[英]h5py how to move dataset to a new group - strange error

I have an hdf5 file that has a dataset 'foo' located at the root.我有一个 hdf5 文件,它的数据集“foo”位于根目录。 I would like to move it into a new group 'bar'.我想将它移到一个新的组“栏”中。 I have RTFM and attempted the following:我有RTFM并尝试了以下操作:

with h5py.File(fname, 'a') as h5file:
    newgroup = h5file.create_group('bar')
    h5file.move('foo', 'bar/foo')

This appears to work fine.这似乎工作正常。 Now I try to actually verify that it worked现在我尝试实际验证它是否有效

with h5py.File(fname, 'r') as h5file:
    print(h5file['bar']['foo'].shape)

This actually prints the correct shape of the dataset.这实际上打印了数据集的正确形状。 But immediately afterwards it throws an exception KeyError: "Unable to open object (object 'foo' doesn't exist)" .但紧接着它抛出异常KeyError: "Unable to open object (object 'foo' doesn't exist)" Or, more precisely,或者,更准确地说,

/opt/anaconda3/envs/py36qt5/lib/python3.6/site-packages/h5py/_hl/group.py in __getitem__(self, name)
    165                 raise ValueError("Invalid HDF5 object reference")
    166         else:
--> 167             oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
    168 
    169         otype = h5i.get_type(oid)

Any idea what is going wrong?知道出了什么问题吗? h5py.__version__ = 2.7.1

EDIT :编辑

Here's an even more idiotic observation.这是一个更愚蠢的观察。 The following code works as expected and does not throw the above exception.以下代码按预期工作,不会抛出上述异常。

with h5py.File(fname, 'r') as h5file:
    for key in h5file['bar'].keys():
        print(key, h5file['bar'][key].shape)

I am at a complete loss...我完全不知所措......

I am running h5py 2.10 , and this code segment works fine.我正在运行h5py 2.10 ,这段代码运行正常。
I don't know why it doesn't work for you.我不知道为什么它对你不起作用。

import h5py
import numpy as np

fname='SO_64558399.h5'

with h5py.File(fname, 'w') as h5file:
    arr = np.arange(100).reshape(10,10)
    h5file.create_dataset('foo',data=arr)
    
with h5py.File(fname, 'a') as h5file:
    newgroup = h5file.create_group('bar')
    h5file.move('foo', 'bar/foo')
    
with h5py.File(fname, 'r') as h5file:
    print(h5file['bar']['foo'].shape)
    for key in h5file['bar'].keys():
        print(key, h5file['bar'][key].shape)

The printed output is:打印出来的 output 是:

(10, 10)
foo (10, 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM