从多个 hdf5 组创建数据集

Creating a dataset from multiple hdf5 groups

从多个 hdf5 组创建数据集

组代码

np.array(hdf.get('all my groups'))

然后我添加了用于从组创建数据集的代码。

with h5py.File('/train.h5', 'w') as hdf:
hdf.create_dataset('train', data=one_T+two_T+three_T+four_T+five_T)

错误信息是

ValueError: operands could not be broadcast together with shapes(534456,4) (534456,14)

除了列长度不同外,每组中的数字都相同。 5 个独立的组到一个数据集。

给你;将值从 file1 中的 3 个数据集复制到 file2 中的单个数据集的简单示例。我包括了一些测试来验证兼容的数据类型和形状。创建 file1 的代码包含在顶部。代码中的注释应该解释这个过程。我还有另一个 post,显示了在 2 个 HDF5 文件之间复制数据的多种方法。看到这个post:How can I combine multiple .h5 file?

import h5py
import numpy as np
import sys

# Data for file1
arr1 = np.random.random(80).reshape(20,4)
arr2 = np.random.random(40).reshape(20,2)
arr3 = np.random.random(60).reshape(20,3)

#Create file1 with 3 datasets
with h5py.File('file1.h5','w') as h5f :
    h5f.create_dataset('ds_1',data=arr1)
    h5f.create_dataset('ds_2',data=arr2)
    h5f.create_dataset('ds_3',data=arr3)
 
# Open file1 for reading and file2 for writing
with h5py.File('file1.h5','r') as h5f1 , \
     h5py.File('file2.h5','w') as h5f2 :

# Loop over datasets in file1 and check data compatiblity         
    for i, ds in enumerate(h5f1.keys()) :
        if i == 0:
            ds_0 = ds
            ds_0_dtype = h5f1[ds].dtype
            n_rows = h5f1[ds].shape[0]
            n_cols = h5f1[ds].shape[1]
        else:
            if h5f1[ds].dtype != ds_0_dtype :
                print(f'Dset 0:{ds_0}: dtype:{ds_0_dtype}')
                print(f'Dset {i}:{ds}: dtype:{h5f1[ds].dtype}')
                sys.exit('Error: incompatible dataset dtypes')

            if h5f1[ds].shape[0] != n_rows :
                print(f'Dset 0:{ds_0}: shape[0]:{n_rows}')
                print(f'Dset {i}:{ds}: shape[0]:{h5f1[ds].shape[0]}')
                sys.exit('Error: incompatible dataset shape')

            n_cols += h5f1[ds].shape[1]
        prev_ds = ds    

# Create new empty dataset with appropriate dtype and size
# Using maxshape paramater to make resizable in the future
    h5f2.create_dataset('ds_123', dtype=ds_0_dtype, shape=(n_rows,n_cols), maxshape=(n_rows,None))
    
# Loop over datasets in file1, read data into xfer_arr, and write to file2        
    first = 0
    for ds in h5f1.keys() :
        xfer_arr = h5f1[ds][:]
        last = first + xfer_arr.shape[1]
        h5f2['ds_123'][:, first:last] = xfer_arr[:]
        first = last

这个答案解决了 OP 在对我的第一个答案的评论中的请求(一个例子是 ds_1 所有列,ds_2 前两列,ds_3第 4 和第 6 列,ds_4 所有列")。过程非常相似,但输入比第一个答案“稍微复杂一些”。因此,我使用了不同的方法来定义要复制的数据集名称和列。差异:

  • 第一个解决方案迭代来自“keys()”的数据集名称(完全复制每个数据集,附加到新文件中的数据集)。新数据集的大小是通过将所有数据集的大小相加计算得出的。
  • 第二种解决方案使用 2 个列表来定义 1) 数据集名称 (ds_list) 和 2) 要从每个数据集复制的关联列(col_list 是列表的一个)。新数据集的大小是通过对 col_list 中的列数求和来计算的。我使用“花式索引”来使用 col_list.
  • 提取列
  • 您决定如何执行此操作取决于您的数据。
  • 注意:为简单起见,我删除了 dtype 和 shape 测试。您应该包括这些以避免与“现实世界”问题相关的错误。

代码如下:

# Data for file1
arr1 = np.random.random(120).reshape(20,6)
arr2 = np.random.random(120).reshape(20,6)
arr3 = np.random.random(120).reshape(20,6)
arr4 = np.random.random(120).reshape(20,6)

# Create file1 with 4 datasets
with h5py.File('file1.h5','w') as h5f :
    h5f.create_dataset('ds_1',data=arr1)
    h5f.create_dataset('ds_2',data=arr2)
    h5f.create_dataset('ds_3',data=arr3)
    h5f.create_dataset('ds_4',data=arr4)
 
# Open file1 for reading and file2 for writing
with h5py.File('file1.h5','r') as h5f1 , \
     h5py.File('file2.h5','w') as h5f2 :

# Loop over datasets in file1 to get dtype and rows (should test compatibility)        
     for i, ds in enumerate(h5f1.keys()) :
        if i == 0:
            ds_0_dtype = h5f1[ds].dtype
            n_rows = h5f1[ds].shape[0]
            break

# Create new empty dataset with appropriate dtype and size
# Use maxshape parameter to make resizable in the future

    ds_list = ['ds_1','ds_2','ds_3','ds_4']
    col_list =[ [0,1,2,3,4,5], [0,1], [3,5], [0,1,2,3,4,5] ]
    n_cols = sum( [ len(c) for c in col_list])
    h5f2.create_dataset('combined', dtype=ds_0_dtype, shape=(n_rows,n_cols), maxshape=(n_rows,None))
    
# Loop over datasets in file1, read data into xfer_arr, and write to file2        
    first = 0  
    for ds, cols in zip(ds_list, col_list) :
        xfer_arr = h5f1[ds][:,cols]
        last = first + xfer_arr.shape[1]
        h5f2['combined'][:, first:last] = xfer_arr[:]
        first = last