【问题标题】:Combined hdf5 files into single dataset将 hdf5 文件合并为单个数据集
【发布时间】:2016-01-02 21:01:36
【问题描述】:

我有很多 hdf5 文件,每个文件都有一个数据集。我想将它们组合成一个数据集,其中数据都在同一个卷中(每个文件都是一个图像,我想要一个大的延时图像)。

我编写了一个 python 脚本来将数据提取为一个 numpy 数组,存储它们,然后尝试将其写入一个新的 h5 文件。但是,这种方法行不通,因为合并后的数据使用的内存超过了我拥有的 32 GB RAM。

我也尝试过使用命令行工具 h5copy。

h5copy -i file1.h5 -o combined.h5 -s '/dataset' -d '/new_data/t1'
h5copy -i file2.h5 -o combined.h5 -s '/dataset' -d '/new_data/t2'

这可行,但它会在新文件中生成许多数据集,而不是将所有数据集串联起来。

【问题讨论】:

    标签: python numpy hdf5 h5py hdf


    【解决方案1】:

    尽管您无法将行显式附加到 hdf5 数据集,但您可以在创建数据集时使用 maxshape 关键字来发挥您的优势,从而允许您“调整”数据集的大小以适应新数据。 (见http://docs.h5py.org/en/latest/faq.html#appending-data-to-a-dataset

    假设您的数据集的列数始终相同,您的代码最终会看起来像这样:

    import h5py
    
    output_file = h5py.File('your_output_file.h5', 'w')
    
    #keep track of the total number of rows
    total_rows = 0
    
    for n, f in enumerate(file_list):
      your_data = <get your data from f>
      total_rows = total_rows + your_data.shape[0]
      total_columns = your_data.shape[1]
    
      if n == 0:
        #first file; create the dummy dataset with no max shape
        create_dataset = output_file.create_dataset("Name", (total_rows, total_columns), maxshape=(None, None))
        #fill the first section of the dataset
        create_dataset[:,:] = your_data
        where_to_start_appending = total_rows
    
      else:
        #resize the dataset to accomodate the new data
        create_dataset.resize(total_rows, axis=0)
        create_dataset[where_to_start_appending:total_rows, :] = your_data
        where_to_start_appending = total_rows
    
    output_file.close()
    

    【讨论】:

    • 什么是
    • 这将是您从每个文件中获取数据所需执行的任何命令或步骤,并且取决于文件的类型。例如,如果您正在处理 HDF5 文件列表,则需要使用 h5py.File 创建一个文件对象,然后使用类似 file_object["dataset_name"][slice] 从文件中读取数据
    猜你喜欢
    • 2012-03-26
    • 2019-08-07
    • 2017-11-03
    • 2011-05-20
    • 2018-07-17
    • 2021-12-24
    • 2015-11-28
    • 2021-07-05
    • 2013-10-07
    相关资源
    最近更新 更多