【问题标题】:Combine datasets of multiple HDF5 files into a virtual dataset将多个 HDF5 文件的数据集组合成一个虚拟数据集
【发布时间】:2017-11-03 23:08:29
【问题描述】:

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

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

有没有虚拟数据集的替代品,或者你知道任何使用 h5py 的例子吗?

【问题讨论】:

    标签: python hdf5 h5py


    【解决方案1】:

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

    虚拟数据集刚刚完全出现在 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)
    

    【讨论】:

      【解决方案2】:

      试试gdal虚拟格式

      【讨论】:

      • GDAL 似乎不支持 HDF5
      • @sappjw 原版不支持,编译时使用 libhdf5 dll 会很有用:gdal.org/frmt_hdf5.html
      【解决方案3】:

      有人试过了。示例在这里,但不幸的是我无法让它工作,而且它似乎在语法上不正确。 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()
      

      【讨论】:

      • 感谢您的回答,但它不在 h5py 的正式版本中,我无法让它与该分支一起使用。你试过了吗?它对你有用吗?
      • 对不起,我试过但失败了。相应地更新了我的答案。
      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 2016-01-02
      • 2017-10-02
      • 2012-03-26
      • 2018-03-18
      • 2015-09-07
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多