如何在HDf5文件中聚合50个数据集



我有一个HDF5文件,其中有两个组,每个组包含50个相同类型的4D numpy数组数据集。我想将每组中的所有50个数据集合并为一个数据集。换句话说,我想要的不是2x50个数据集,而是2x1个数据集。我怎样才能做到这一点?该文件的大小为18.4 Gb。我是处理大型数据集的新手。我和h5py一起在python上工作。

谢谢!

看看这个答案:如何组合多个.h5文件-方法3b:将所有数据合并到1个可调整数据集。它描述了一种将数据从多个HDF5文件复制到单个数据集的方法。你想做一些类似的事情。唯一的区别是您所有的数据集都在一个HDF5文件中。

你没有说你想如何堆叠4D阵列。在我的第一个答案中,我将它们沿着轴=3堆叠。正如我在评论中所指出的,我更容易(更干净(将合并的数据集创建为5d数组,并沿第5轴(轴=4(堆叠数据。我喜欢这个有两个原因:代码更简单/更容易理解,2(轴=4表示一个唯一的数据集(而不是在轴=3上切片(更直观(对我来说(。

我写了一个独立的例子来演示这个过程。首先,它创建一些数据并关闭文件。然后它重新打开文件(只读(,并为复制的数据集创建一个新文件。它在第一个文件中的组和数据集上循环,并将数据复制到第二个文件中合并的数据集。5D示例是第一个,下面是我最初的4D示例。

注意:这是一个简单的例子,适用于您的具体案例。如果你正在编写一个通用的解决方案,它应该在盲目合并数据之前检查一致的形状和数据类型(我不这么做(。

创建示例数据的代码(2组,每组5个数据集(:

import h5py
import numpy as np
# Create a simple H5 file with 2 groups and 5 datasets (shape=a0,a1,a2,a3)
with h5py.File('SO_69937402_2x5.h5','w') as h5f1:

a0,a1,a2,a3 = 100,20,20,10
grp1 = h5f1.create_group('group1')
for ds in range(1,6):
arr = np.random.random(a0*a1*a2*a3).reshape(a0,a1,a2,a3)
grp1.create_dataset(f'dset_{ds:02d}',data=arr)
grp2 = h5f1.create_group('group2')
for ds in range(1,6):
arr = np.random.random(a0*a1*a2*a3).reshape(a0,a1,a2,a3)
grp2.create_dataset(f'dset_{ds:02d}',data=arr)        

合并数据的代码(2组,每组1个5D数据集——我的偏好(:

with h5py.File('SO_69937402_2x5.h5','r') as h5f1, 
h5py.File('SO_69937402_2x1_5d.h5','w') as h5f2:

# loop on groups in existing file (h5f1)
for grp in h5f1.keys():
# Create group in h5f2 if it doesn't exist
print('working on group:',grp)
h5f2.require_group(grp)
# Loop on datasets in group
ds_cnt = len(h5f1[grp].keys())
for i,ds in enumerate(h5f1[grp].keys()):
print('working on dataset:',ds)
if 'merged_ds' not in h5f2[grp].keys():
# If dataset doesn't exist in group, create it
# Set maxshape so dataset is resizable
ds_shape = h5f1[grp][ds].shape
merge_ds = h5f2[grp].create_dataset('merged_ds',dtype=h5f1[grp][ds].dtype,
shape=(ds_shape+(ds_cnt,)), maxshape=(ds_shape+(None,)) )
# Now add data to the merged dataset 
merge_ds[:,:,:,:,i] = h5f1[grp][ds]

合并数据的代码(2组,每组1个4D数据集(:

with h5py.File('SO_69937402_2x5.h5','r') as h5f1, 
h5py.File('SO_69937402_2x1_4d.h5','w') as h5f2:

# loop on groups in existing file (h5f1)
for grp in h5f1.keys():
# Create group in h5f2 if it doesn't exist
print('working on group:',grp)
h5f2.require_group(grp)
# Loop on datasets in group
for ds in h5f1[grp].keys():
print('working on dataset:',ds)
if 'merged_ds' not in h5f2[grp].keys():
# if dataset doesn't exist in group, create it
# Set maxshape so dataset is resizable
ds_shape = h5f1[grp][ds].shape
merge_ds = h5f2[grp].create_dataset('merged_ds',data=h5f1[grp][ds],
maxshape=[ds_shape[0],ds_shape[1],ds_shape[2],None])       
else:
# otherwise, resize the merged dataset to hold new values
ds1_shape = h5f1[grp][ds].shape
ds2_shape = merge_ds.shape
merge_ds.resize(ds1_shape[3]+ds2_shape[3],axis=3)
merge_ds[ :,:,:, ds2_shape[3]:ds2_shape[3]+ds1_shape[3] ] = h5f1[grp][ds]

最新更新