gpt4 book ai didi

python - 合并 hdf5 文件

转载 作者:IT老高 更新时间:2023-10-28 22:14:09 44 4
gpt4 key购买 nike

我有许多 hdf5 文件,每个文件都有一个数据集。数据集太大而无法保存在 RAM 中。我想将这些文件组合成一个单独包含所有数据集的文件(即 not 将数据集连接成一个数据集)。

一种方法是创建一个 hdf5 文件,然后一个一个地复制数据集。这将是缓慢而复杂的,因为它需要缓冲副本。

有没有更简单的方法来做到这一点?似乎应该有,因为它本质上只是创建一个容器文件。

我正在使用 python/h5py。

最佳答案

这实际上是 HDF5 的用例之一。如果您只想能够从单个文件访问所有数据集,而不关心它们实际上是如何存储在磁盘上的,您可以使用 external links .来自 HDF5 website :

External links allow a group to include objects in another HDF5 file and enable the library to access those objects as if they are in the current file. In this manner, a group may appear to directly contain datasets, named datatypes, and even groups that are actually in a different file. This feature is implemented via a suite of functions that create and manage the links, define and retrieve paths to external objects, and interpret link names:

Here's how to do it in h5py :

myfile = h5py.File('foo.hdf5','a')
myfile['ext link'] = h5py.ExternalLink("otherfile.hdf5", "/path/to/resource")

注意:打开myfile时,如果是现有文件,应使用'a'打开。如果你用 'w' 打开它,它会清除它的内容。

这比将所有数据集复制到新文件中要快得多。我不知道对 otherfile.hdf5 的访问速度有多快,但是对所有数据集的操作将是透明的 - 也就是说,h5py 会将所有数据集视为驻留在 foo.hdf5 .

关于python - 合并 hdf5 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18492273/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com