繁体   English   中英

从多个 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 个单独的组到一个数据集。

你在这里 go; 将值从 file1 中的 3 个数据集复制到 file2 中的单个数据集的简单示例。 我包括了一些测试来验证兼容的 dtype 和 shape。 创建 file1 的代码包含在顶部。 代码中的注释应该解释这个过程。 我有另一篇文章展示了在 2 个 HDF5 文件之间复制数据的多种方法。 请参阅这篇文章:如何合并多个.h5 文件?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM