我有许多hdf5文件,每个文件都有一个单独的数据集。这些数据集太大了,无法保存在RAM中。 我想将这些文件合并成一个文件,其中包含所有单独的数据集(即不是将数据集连接成一个数据集)。
一种方法是创建一个hdf5文件,然后逐个复制数据集。 这将是缓冲复制,因此速度较慢且复杂。
有没有更简单的方法?看起来应该有,因为实质上只是创建一个容器文件。
我正在使用python/h5py。
我有许多hdf5文件,每个文件都有一个单独的数据集。这些数据集太大了,无法保存在RAM中。 我想将这些文件合并成一个文件,其中包含所有单独的数据集(即不是将数据集连接成一个数据集)。
一种方法是创建一个hdf5文件,然后逐个复制数据集。 这将是缓冲复制,因此速度较慢且复杂。
有没有更简单的方法?看起来应该有,因为实质上只是创建一个容器文件。
我正在使用python/h5py。
实际上,这是HDF5的用例之一。如果您只想从单个文件中访问所有数据集,并且不关心它们在磁盘上的实际存储方式,您可以使用外部链接。来自HDF5网站:
外部链接允许组将另一个HDF5文件中的对象包含在内,并使库能够访问这些对象,就好像它们在当前文件中一样。通过这种方式,组似乎直接包含了数据集、命名数据类型,甚至是实际上位于不同文件中的组。该功能通过一系列函数实现,这些函数创建和管理链接,定义和检索到外部对象的路径,并解释链接名称:
myfile = h5py.File('foo.hdf5','a')
myfile['ext link'] = h5py.ExternalLink("otherfile.hdf5", "/path/to/resource")
注意:打开myfile
文件时,如果它是现有文件,则应使用'a'
打开。如果您使用'w'
打开它,则会擦除其内容。otherfile.hdf5
有多快,但操作所有数据集将是透明的 - 也就是说,h5py将看到所有数据集都存在于foo.hdf5
中。'w'
打开文件时会出现这种情况。我认为正确答案应该是 myfile = h5py.File('foo.hdf5', 'a')
。 - Yamaneko一个解决方案是使用 h5py
接口来调用 HDF5 API 的低级别 H5Ocopy
函数,特别是使用 h5py.h5o.copy
函数:
In [1]: import h5py as h5
In [2]: hf1 = h5.File("f1.h5")
In [3]: hf2 = h5.File("f2.h5")
In [4]: hf1.create_dataset("val", data=35)
Out[4]: <HDF5 dataset "val": shape (), type "<i8">
In [5]: hf1.create_group("g1")
Out[5]: <HDF5 group "/g1" (0 members)>
In [6]: hf1.get("g1").create_dataset("val2", data="Thing")
Out[6]: <HDF5 dataset "val2": shape (), type "|O8">
In [7]: hf1.flush()
In [8]: h5.h5o.copy(hf1.id, "g1", hf2.id, "newg1")
In [9]: h5.h5o.copy(hf1.id, "val", hf2.id, "newval")
In [10]: hf2.values()
Out[10]: [<HDF5 group "/newg1" (1 members)>, <HDF5 dataset "newval": shape (), type "<i8">]
In [11]: hf2.get("newval").value
Out[11]: 35
In [12]: hf2.get("newg1").values()
Out[12]: [<HDF5 dataset "val2": shape (), type "|O8">]
In [13]: hf2.get("newg1").get("val2").value
Out[13]: 'Thing'
以上内容是在Debian Wheezy基本安装的前提下,使用h5py
版本2.0.1-2+b1
和iPython版本0.13.1-2+deb7u1
及Python版本2.7.3-4+deb7u1
生成的。在执行上述操作之前,文件f1.h5
和f2.h5
不存在。 请注意,根据salotz的建议,对于Python 3,数据集/组名称需要为 bytes
(例如 b"val"
),而不是 str
。
[7]
命令中的hf1.flush()
非常关键,因为低级接口似乎总是从存储在磁盘上的.h5
文件版本中读取,而不是从内存中缓存读取。通过使用hf1.get("g1").id
提供该组的ID,可以将数据集复制到/从未在File
根目录下的组中。
请注意,如果已在目标位置中存在指定名称的对象,则h5py.h5o.copy
将失败并引发异常(无覆盖)。
h5py
2.7.1和Python 3.6.5,需要将字符串转换为字节,因此请将h5.h5o.copy(hf1.id, "g1", hf2.id, "newg1")
替换为h5.h5o.copy(hf1.id, b"g1", hf2.id, b"newg1")
。 - salotz我通常使用ipython和h5copy工具结合使用,与纯python解决方案相比速度更快。安装h5copy后即可使用。
#PLESE NOTE THIS IS IPYTHON CONSOLE CODE NOT PURE PYTHON
import h5py
#for every dataset Dn.h5 you want to merge to Output.h5
f = h5py.File('D1.h5','r+') #file to be merged
h5_keys = f.keys() #get the keys (You can remove the keys you don't use)
f.close() #close the file
for i in h5_keys:
!h5copy -i 'D1.h5' -o 'Output.h5' -s {i} -d {i}
为了完全自动化该过程,假设您正在工作的文件夹中存储了要合并的文件:
import os
d_names = os.listdir(os.getcwd())
d_struct = {} #Here we will store the database structure
for i in d_names:
f = h5py.File(i,'r+')
d_struct[i] = f.keys()
f.close()
# A) empty all the groups in the new .h5 file
for i in d_names:
for j in d_struct[i]:
!h5copy -i '{i}' -o 'output.h5' -s {j} -d {j}
如果您想要在输出文件output.h5中保留先前的数据集,您需要首先使用标志-p
创建组:
# B) Create a new group in the output.h5 file for every input.h5 file
for i in d_names:
dataset = d_struct[i][0]
newgroup = '%s/%s' %(i[:-3],dataset)
!h5copy -i '{i}' -o 'output.h5' -s {dataset} -d {newgroup} -p
for j in d_struct[i][1:]:
newgroup = '%s/%s' %(i[:-3],j)
!h5copy -i '{i}' -o 'output.h5' -s {j} -d {newgroup}
import h5py
import os
d_names = os.listdir(os.getcwd())
d_struct = {} #Here we will store the database structure
for i in d_names:
f = h5py.File(i,'r+')
d_struct[i] = f.keys()
f.close()
for i in d_names:
for j in d_struct[i]:
os.system('h5copy -i %s -o output.h5 -s %s -d %s' % (i, j, j))