【问题标题】:merging several hdf5 files into one pytable将几个 hdf5 文件合并到一个 pytable 中
【发布时间】:2013-10-07 15:44:45
【问题描述】:

我有几个hdf5 文件,每个文件都具有相同的结构。我想通过某种方式合并hdf5 文件,从中创建一个pytable

我的意思是,如果 file1 中的数组大小为 x,而 file2 中的数组大小为 y,则 pytable 中的结果数组大小为 x+y,首先包含来自 file1 的所有条目,然后包含所有条目file2 中的条目。

【问题讨论】:

    标签: hdf5 pytables


    【解决方案1】:

    您希望如何执行此操作取决于您拥有的数据类型。数组和 CArray 具有静态大小,因此您需要预先分配数据空间。因此,您将执行以下操作:

    import tables as tb
    file1 = tb.open_file('/path/to/file1', 'r')
    file2 = tb.open_file('/path/to/file2', 'r')
    file3 = tb.open_file('/path/to/file3', 'r')
    x = file1.root.x
    y = file2.root.y
    
    z = file3.create_array('/', 'z', atom=x.atom, shape=(x.nrows + y.nrows,))
    z[:x.nrows] = x[:]
    z[x.nrows:] = y[:]
    

    但是,EArrays 和 Tables 是可扩展的。因此,您不需要预先分配大小,而是可以使用 copy_node() 和 append()。

    import tables as tb
    file1 = tb.open_file('/path/to/file1', 'r')
    file2 = tb.open_file('/path/to/file2', 'r')
    file3 = tb.open_file('/path/to/file3', 'r')
    x = file1.root.x
    y = file2.root.y
    
    z = file1.copy_node('/', name='x', newparent=file3.root, newname='z')
    z.append(y)
    

    【讨论】:

    • 这可能很明显,但我不清楚最后两行在做什么。 z 应该是组合输出文件吗?那两条线做同样的事情吗?是否可以在这里澄清变量命名和定义?
    • 正在写入的文件需要以追加(或写入)模式打开。所以打开file3时使用'a'而不是'r'
    • 为了让示例工作,我还必须将最后一行更改为:z.append(y[:])
    猜你喜欢
    • 2011-06-18
    • 2011-05-20
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2022-12-08
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多