在 py3 中使用 h5py 打开后,使用 h5py py2 制作的 hdf5 损坏

hdf5 made with h5py py2 corrupted after opening with h5py in py3

问题

我在 python 2.7.

中使用 h5py 创建了一个文件

这些步骤会导致损坏:

  1. 我使用 scp 从合作者那里下载了一份新副本。整整286MB.

  2. 我用 hdfview 打开它来检查它是否可读。这会正确显示所有数据集和组。

  3. 我退出 hdfview。

  4. 重复步骤 2 和 3 以确保 hdfview 没有损坏文件。

  5. 我打开 ipython 3.6 并且,

    import h5py f = h5py.File(filename,'r') g = f['/sol000']#one group that should be there

我得到KeyError: "Unable to open object (Object 'sol000' doesn't exist)"

  1. f.close()然后退出ipython。我再次用 hdfview 打开它,整个结构都消失了。该文件现在是 4KB。

我可以在 python 2 hdf5 中打开文件并访问所有数据集,但我的代码必须使用 python 3。

系统

在 Fedora 24 64 位、python 2.7、hdf5 2.7.0

上创建的文件

系统试图在 Fedora 25 64 位 python 3.6、h5py 2.7.0

上读取它

显示的最少代码应该可以工作

在系统 1 上:

import h5py
import numpy as np
f = h5py.File("file.hdf5","w")
f.create_dataset("/sol000/data",(100,100),dtype=float)
f["/sol000/data"] = np.zeros([100,100],dtype=float)
f.close()

在系统 2 上:执行步骤 1-4。

import h5py
f = h5py.File("file.hdf5","r")
f.visit(lambda *x:print(x))
#(sol000/data,)
f.close()

解决方案是强制执行 libver=earliest。 IE。以下代码用于打开文件:

import h5py
f = f.File("file.hdf5","r",libver="earliest")

我发现 h5py 文档中可能存在不一致之处。 It claims

The “earliest” option means that HDF5 will make a best effort to be backwards compatible.

The default is “earliest”.

如果它仅在我明确设置时才有效,则这不可能是真的。 事实证明,我的合作者使用旧版本的 hdf5 C 库创建了可损坏的文件。