【问题标题】:Column stack and row stack with H5py to existing datasets使用 H5py 到现有数据集的列堆栈和行堆栈
【发布时间】:2013-07-02 13:18:08
【问题描述】:

我正在尝试使用 Python 将 HDF5 文件中的列堆栈和行堆栈数据与其他数据一起使用。我正在从相机记录图像并将它们保存到单个文件中。然后我希望能够生成一个将所有图像拼凑在一起的单个文件。因此,我希望能够在一个新文件中创建一个数据集,并将每个图像文件中的所有数组堆叠到一个文件中。

我知道 h5py 允许我使用像 numPy 数组这样的数据集,但我不知道如何告诉 h5py 再次将数据保存到文件中。下面我有一个非常简单的例子。

我的问题是如何将 HDF5 文件中的数据与第二个数组 (arr2) 进行列堆叠,以便将 arr2 保存到文件中?

(注意:在我的实际应用中,文件中的数据会比示例中的大很多。因此,将数据导入内存,列堆叠,然后重写到文件中是不可能的。 )

import h5py
import numpy

arr1 = numpy.random.random((2000,2000))

with h5py.File("Plot0.h5", "w") as f:
    dset = f.create_dataset("Plot", data = arr1)

arr2 = numpy.random.random((2000,2000))

with h5py.File("Plot0.h5", "r+") as f:
    dset = f["Plot"]
    dset = numpy.column_stack((dset, arr2))

这似乎是一个微不足道的问题,但我所有的搜索都没有成功。提前致谢。

【问题讨论】:

    标签: python numpy h5py


    【解决方案1】:

    在重读了一些关于 H5py 的文档后,我意识到我的错误。这是我的新脚本结构,它允许我在 HDF5 文件中堆叠数组:

    import h5py
    import numpy
    
    arr1 = numpy.random.random((2000,2000))
    
    with h5py.File("Plot0.h5", "w") as f:
        dset = f.create_dataset("Plot", data = arr1, maxshape=(None,None))
    
    dsetX, dsetY = 2000,2000
    go = ""
    while go == "":
        go = raw_input("Current Size: " + str(dsetX) + "  " + str(dsetY) + "  Continue?")
        arr2 = numpy.random.random((2000,2000))
    
        with h5py.File("Plot0.h5", "r+") as f:
            dset = f["Plot"]
            print len(arr2[:])
            print len(arr2[0][:])
            change = "column"
    
            dsetX, dsetY = dset.shape
    
            if change == "column":
    
                x1 = dsetX
                x2 = len(arr2[:]) + dsetX
    
                y1 = 0
                y2 = len(arr2[0][:])
    
                dset.shape = (x2, y2)
            else:
                x1 = 0
                x2 = len(arr2[:])
    
                y1 = dsetY
                y2 = len(arr2[0][:]) + dsetY
    
                dset.shape = (x2, y2)
            print "x1", x1
            print "x2", x2
            print "y1", y1
            print "y2", y2
    
            print dset.shape
    
            dset[x1:x2,y1:y2] = arr2
    
            print arr2
            print "\n"
            print dset[x1:x2,y1:y2]
    
            dsetX, dsetY = dset.shape
    

    我希望这可以帮助其他人。当然,我们欢迎更好地解决这个问题。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 2019-11-22
      • 2013-06-10
      • 2016-08-13
      • 2011-01-26
      • 2021-05-10
      • 2021-07-08
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多