在python中将多个H5合并到一个H5文件中

问题描述 投票:0回答:1

我是Python编码新手。我想将 2 个 h5 文件中的数据合并到一个主 H5 文件中。 在图片中我提到了从哪里复制到哪个文件。 enter image description here 实际上,我不想迭代每个数据集并在主文件中创建相同的数据集。我只想获取 SRRXX/SRR630/* 所有组并将其添加到主文件中。

#read the mainfile dataset
        with h5py.File(main_h5_path, 'r') as h5_main_file_obj:
            # return if H5 doesn't contain any data
            if len(h5_main_file_obj.keys()) == 0:
                return
            main_file_timestamp_dtset_obj = h5_main_file_obj['/' + 'SRR6XX' + '/' + 'SRR630']

            for file in h5_files:
                with h5py.File(file, 'r') as h5_sub_file_obj:
                    # return if H5 doesn't contain any data
                    if len(h5_sub_file_obj.keys()) == 0:
                        continue
                    sub_file_timestamp_dtset_obj = h5_sub_file_obj['/' + 'SRR6XX' + '/' + 'SRR630']
                    # h5_main_file_obj.create_dataset(sub_file_timestamp_dtset_obj)
                    for ts_key in sub_file_timestamp_dtset_obj.keys():
                        print('ts_key', ts_key)
                        each_ts_ds = h5_sub_file_obj['/' + 'SRR6XX' + '/' + 'SRR630' + '/' + str(ts_key) + '/']
                        h5_main_file_obj.create_dataset(each_ts_ds)


    except (IOError, OSError, Exception) as e:
        print(f"Error occurred during H5 merging: {e}")
        return -1
    return 0

并获得例外。也尝试过 create_group() ,但同样的问题。

enter image description here

python hdf5 h5py
1个回答
0
投票

如果我理解,您想要将组'/SRR6XX/SRR630'下的

组名称
从源文件复制到主(目标)文件。如果是这样,请执行以下更改才能使其正常工作:

  1. 主(目标)文件必须以附加模式打开才能添加新对象。
  2. 循环中的
  3. ts_key
    是对象名称(而不是对象)。使用 .items() 获取名称和对象(或仅按名称引用对象)。
  4. 您正在根级别的主(目标)文件中创建新对象。 您需要修改以引用适当的组对象(
    main_file_timestamp_dtset_obj
    )

修改后的代码如下:

def a_function:

  with h5py.File(main_h5_path, 'a') as h5_main_file_obj: # need Append mode to add groups
    # return if H5 doesn't contain any data
    if len(h5_main_file_obj.keys()) == 0:
        return
    main_file_timestamp_dtset_obj = h5_main_file_obj['/SRR6XX/SRR630']

    for file in h5_files:
        with h5py.File(file, 'r') as h5_sub_file_obj:
            # return if H5 doesn't contain any data
            if len(h5_sub_file_obj.keys()) == 0:
                continue
            sub_file_timestamp_dtset_obj = h5_sub_file_obj['/SRR6XX/SRR630']
            # h5_main_file_obj.create_dataset(sub_file_timestamp_dtset_obj)
            for ts_key in sub_file_timestamp_dtset_obj.keys():
                print('ts_key:', ts_key)
                main_file_timestamp_dtset_obj.create_group(ts_key)

我编写了另一个更紧凑的解决方案,并在复制之前检查源对象是否为组。见下文。另一项需要考虑的检查:在创建已存在的组之前,主(目标)文件中的组名称。

def my_function():
      
    with h5py.File(main_h5_path, mode='a') as h5ft:
        if len(h5ft.keys()) == 0:
            return
        for h5_source in h5_files:
            with h5py.File(h5_source,'r') as h5fs:
                if len(h5ft.keys()) == 0:
                    return
                for grp_name, h5_obj in h5fs['SRR6XX/SRR630'].items(): 
                    if isinstance(h5_obj,h5py.Group):
                        h5ft['SRR6XX/SRR630'].create_group(grp_name) 
© www.soinside.com 2019 - 2024. All rights reserved.