将多个HDF5文件的数据集合并到虚拟数据集中

时间:2017-06-02 13:52:11

标签: python hdf5 h5py

我有几个HDF5文件,每个文件包含相同的两个数据集datalabels。这些数据集是多维数组,第一个维度对于两者都是相同的。

我想将HDF5文件合并到一个文件中,我认为最好的方法是创建一个虚拟数据集,[h5py reference],[HDF5 tutorial in C++]。但是,我没有在Python和h5py中找到任何示例。

是否有虚拟数据集的替代品,或者您是否知道使用h5py的任何示例?

3 个答案:

答案 0 :(得分:0)

尝试 gdal虚拟格式

答案 1 :(得分:0)

有人试过了。示例在这里,但不幸的是我无法使它工作,而且它似乎在语法上是不正确的。 https://github.com/aaron-parsons/h5py/blob/1e467f6db3df23688e90f44bde7558bde7173a5b/docs/vds.rst#using-the-vds-feature-from-h5py

f = h5py.File("VDS.h5", 'w', libver='latest')
file_names_to_concatenate = ['1.h5', '2.h5', '3.h5', '4.h5', '5.h5']
entry_key = 'data' # where the data is inside of the source files.
sh = h5.File(file_names_to_concatenate[0],'r')[entry_key].shape # get the first ones shape.

TGT = h5.VirtualTarget(outfile, outkey, shape=(len(file_names_to_concatenate, ) + sh)

for i in range(num_projections):
    VSRC = h5.VirtualSource(file_names_to_concatenate[i]), entry_key, shape=sh)
    VM = h5.VirtualMap(VSRC[:,:,:], TGT[i:(i+1):1,:,:,:],dtype=np.float)
    VMlist.append(VM)

d = f.create_virtual_dataset(VMlist=VMlist,fillvalue=0)
f.close()

答案 2 :(得分:0)

这是一个古老的问题,但无论如何...

虚拟数据集仅在h5py v2.9中完全出现(2018年12月20日)

他们有创建虚拟数据集的以下示例: https://github.com/h5py/h5py/blob/master/examples/vds_simple.py

我还做了一些实验,以连接示例创建的数据集。 这样只会创建一维数组。

import h5py
import numpy as np

file_names_to_concatenate = ['1.h5', '2.h5', '3.h5', '4.h5']
entry_key = 'data' # where the data is inside of the source files.

sources = []
total_length = 0
for i, filename in enumerate(file_names_to_concatenate):
    with h5py.File(file_names_to_concatenate[i], 'r') as activeData:
        vsource = h5py.VirtualSource(activeData[entry_key])
        total_length += vsource.shape[0]
        sources.append(vsource)

layout = h5py.VirtualLayout(shape=(total_length,),
                            dtype=np.float)

offset = 0
for vsource in sources:
    length = vsource.shape[0]
    layout[offset : offset + length] = vsource
    offset += length

with h5py.File("VDS_con.h5", 'w', libver='latest') as f:
    f.create_virtual_dataset(entry_key, layout, fillvalue=0)