我有许多hdf5文件,每个文件都有一个数据集。数据集太大而无法容纳在RAM中。我想将这些文件组合成一个单独包含所有数据集的文件(即不将数据集连接成单个数据集)。
一种方法是创建一个hdf5文件,然后逐个复制数据集。这将是缓慢而复杂的,因为它需要缓冲副本。
有更简单的方法吗?似乎应该有,因为它实际上只是创建一个容器文件。
我正在使用python / h5py。
一种解决方案是使用h5py
接口到HDF5 API的低级别H5Ocopy
function,特别是h5py.h5o.copy
function:
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'
以上是使用h5py
版本2.0.1-2+b1
和iPython版本0.13.1-2+deb7u1
在Python版本2.7.3-4+deb7u1
上从或多或少的Debilla Wheezy安装。在执行上述操作之前,文件f1.h5
和f2.h5
不存在。请注意,根据salotz,对于Python 3,数据集/组名称需要是bytes
(例如,b"val"
),而不是str
。
命令hf1.flush()
中的[7]
是至关重要的,因为低级接口显然总是从存储在磁盘上的.h5
文件的版本中提取,而不是缓存在内存中。通过使用例如File
提供该组的ID,可以实现向不在hf1.get("g1").id
根处的组复制数据集。
请注意,如果指定名称的对象已存在于目标位置,则h5py.h5o.copy
将失败并出现异常(无删除)。
这实际上是HDF5的使用案例之一。如果您只想从单个文件访问所有数据集,并且不关心它们实际存储在磁盘上的方式,则可以使用external links。来自HDF5 website:
外部链接允许组在另一个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
中。
我使用官方hdf5工具中的h5copy找到了一个非python解决方案。 h5copy可以将单个指定的数据集从hdf5文件复制到另一个现有的hdf5文件中。
如果有人找到基于python / h5py的解决方案,我会很高兴听到它。
我通常使用ipython和h5copy工具togheter,这比纯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}
为了对此进行更新,使用HDF5版本1.10提供了一个新功能,在此上下文中称为“虚拟数据集”可能很有用。 在这里,您可以找到一个简短的教程和一些解释:Virtual Datasets。 这里有关于该功能的更完整详细的说明和文档: Virtual Datasets extra doc。 在这里,h5py中的合并拉取请求将虚拟数据集API包含到h5py中: h5py Virtual Datasets PR但我不知道它是否已经在当前的h5py版本中可用或将在以后出现。
要使用Python(而不是IPython)和h5copy来合并HDF5文件,我们可以在GM's answer上构建:
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))