gpt4 book ai didi

python - 将 hdf5 文件组合成单个数据集

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:15 25 4
gpt4 key购买 nike

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

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

我还尝试使用命令行工具 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'

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

最佳答案

虽然您不能明确地将行附加到 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()

关于python - 将 hdf5 文件组合成单个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961586/

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