python-2.7 - 使用 h5py 沿新轴将数据添加到现有 h5py 文件

标签 python-2.7 numpy hdf5 h5py

我有一些生成 3d Numpy 数组的示例代码 - 然后我使用 h5 文件将此数据保存到 h5py 文件中。然后我如何沿着第四维“附加”第二个数据集?或者,如何沿着现有 .h5 文件的第四维(或新轴)编写另一个 3D 数据集?我已经阅读了我能找到的文档,但没有一个示例似乎解决了这个问题。我的代码如下所示:

import h5py
import numpy as np

dataset1 = np.random.rand(240,240,250);
dataset2 = np.random.rand(240,240,250);

with h5py.File('data.h5', 'w') as hf:
    dset = hf.create_dataset('dataset_1', data=dataset1)

最佳答案

使用http://docs.h5py.org/en/latest/high/dataset.html我尝试了一下:

In [504]: import h5py
In [505]: f=h5py.File('data.h5','w')
In [506]: data=np.ones((3,5))

制作一个普通的数据集:

In [509]: dset=f.create_dataset('dset', data=data)
In [510]: dset.shape
Out[510]: (3, 5)
In [511]: dset.maxshape
Out[511]: (3, 5)

调整大小的帮助:

In [512]: dset.resize?
Signature: dset.resize(size, axis=None)
Docstring:
Resize the dataset, or the specified axis.

The dataset must be stored in chunked format; it can be resized up to
the "maximum shape" (keyword maxshape) specified at creation time.
The rank of the dataset cannot be changed.

由于我没有指定 maxshape,所以看起来我无法更改或添加到此数据集。

In [513]: dset1=f.create_dataset('dset1', data=data, maxshape=(2,10,10))
...
ValueError: "maxshape" must have same rank as dataset shape

所以我无法定义 3d“空间”并将 2d 数组放入其中 - 至少不能这样。

但我可以向数据添加维度(排名):

In [514]: dset1=f.create_dataset('dset1', data=data[None,...], maxshape=(2,10,10))
In [515]: dset1
Out[515]: <HDF5 dataset "dset1": shape (1, 3, 5), type "<f8">

现在我可以调整数据集的大小 - 在 1 个或多个维度中,直到定义的最大值。

In [517]: dset1.resize((2,3,10))
In [518]: dset1
Out[518]: <HDF5 dataset "dset1": shape (2, 3, 10), type "<f8">
In [519]: dset1[:]
Out[519]: 
array([[[ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.],
        [ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.]],

       [[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]])

原始数据占据扩展数据集的一角

现在填写一些零:

In [521]: dset1[1,:,:]=10
In [523]: dset1[0,:,5:]=2

In [524]: dset1[:]
Out[524]: 
array([[[  1.,   1.,   1.,   1.,   1.,   2.,   2.,   2.,   2.,   2.],
        [  1.,   1.,   1.,   1.,   1.,   2.,   2.,   2.,   2.,   2.],
        [  1.,   1.,   1.,   1.,   1.,   2.,   2.,   2.,   2.,   2.]],

       [[ 10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.],
        [ 10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.],
        [ 10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.]]])

所以,是的,您可以将两个数据集放入一个h5数据集中,前提是您一开始就指定了足够大的maxshape,例如(2,240,240,250) 或 (240,240,500) 或 (240,240,250,2) 等

或者无限调整大小maxshape=(None, 240, 240, 250))

看起来主要的限制是创建后无法添加维度。

另一种方法是在存储之前连接数据,例如

dataset12 = np.stack((dataset1, dataset2), axis=0)

关于python-2.7 - 使用 h5py 沿新轴将数据添加到现有 h5py 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40062770/

相关文章:

python - Matplotlib 具有不同颜色条和显示值的条形图

java - 如何从HDF5文件读取属性?

c++ - HDF5 重新缩放数据集最大维度

python - IndexError : list index out of range. 谁能帮我解决这个Python代码吗?包括 numpy 和 pandas 的概念

python - 创建具有固定颜色的三 channel 图像的最佳方法?

python - 将 HDF5 子集存储为数据集(在 Python 中)

python - 使用 xlsxwriter 中的 Workbook 对象时,Workbook 对象没有属性 'add_sheet'

python - 如何在更正 if 语句之前打印 X 行

python - Pandas groupby 查找真假百分比

python - 检查 numpy 数组中步幅的非歧义性