我正在尝试弄清楚如何使用 h5py 将一个多维结构化 numpy 数组写入一个 hdf5 文件,一次一个字段,但我收到有关广播不同形状数组的错误。我确实需要将创建数据集和将数据写入数据集的步骤分开,如示例中所示。
这是我的最小工作示例。
writehdf5.py
#!/bin/env python
import h5py
from numpy.random import randn
print 'Creating Test Data'
mass = randn(10)
altitude = randn(10)
position = randn(10, 3)
velocity = randn(10, 3)
print 'Write 1 dimensional arrays'
hdf5 = h5py.File('test1.hdf', 'w')
dataset = hdf5.create_dataset('test dataset', (10,),
dtype=[('mass', '<f8'),
('altitude', '<f8')])
dataset['mass'] = mass
dataset['altitude'] = altitude
hdf5.close()
print 'Write 2 dimensional arrays'
hdf5 = h5py.File('test2.hdf', 'w')
dataset = hdf5.create_dataset('test dataset', (10,),
dtype=[('position', '<f8', 3),
('velocity', '<f8', 3)])
print dataset['position'].shape
print position.shape
dataset['position'] = position # <-- Error Occurs Here
dataset['velocity'] = velocity
hdf5.close()
运行时会得到以下输出。
>> python writehdf5.py
Creating Test Data
Write 1 dimensional arrays
Write 2 dimensional arrays
(10, 3)
(10, 3)
Traceback (most recent call last):
File "mwe.py", line 27, in <module>
dataset['position'] = position # <-- Error Occurs Here
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/builddir/build/BUILD/h5py-2.5.0/h5py/_objects.c:2450)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/builddir/build/BUILD/h5py-2.5.0/h5py/_objects.c:2407)
File "/usr/lib64/python2.7/site-packages/h5py/_hl/dataset.py", line 514, in __setitem__
val = numpy.asarray(val, dtype=dtype, order='C')
File "/usr/lib64/python2.7/site-packages/numpy/core/numeric.py", line 462, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (10,3) into shape (10,3,3)
>> h5dump test1.hdf
HDF5 "test1.hdf" {
GROUP "/" {
DATASET "test dataset" {
DATATYPE H5T_COMPOUND {
H5T_IEEE_F64LE "mass";
H5T_IEEE_F64LE "altitude";
}
DATASPACE SIMPLE { ( 10 ) / ( 10 ) }
DATA {
(0): {
0.584402,
1.50107
},
(1): {
-0.284148,
-0.521783
},
(2): {
-0.461751,
0.53352
},
(3): {
2.06525,
-0.0364377
},
(4): {
-0.835377,
1.35912
},
(5): {
-1.31011,
1.21051
},
(6): {
0.103971,
-0.669617
},
(7): {
0.244425,
-0.654791
},
(8): {
0.468478,
2.60204
},
(9): {
0.837614,
1.21362
}
}
}
}
}
>> h5dump test2.hdf
HDF5 "test2.hdf" {
GROUP "/" {
DATASET "test dataset" {
DATATYPE H5T_COMPOUND {
H5T_ARRAY { [3] H5T_IEEE_F64LE } "position";
H5T_ARRAY { [3] H5T_IEEE_F64LE } "velocity";
}
DATASPACE SIMPLE { ( 10 ) / ( 10 ) }
DATA {
(0): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(1): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(2): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(3): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(4): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(5): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(6): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(7): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(8): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
},
(9): {
[ 0, 0, 0 ],
[ 0, 0, 0 ]
}
}
}
}
}
我可以看到我的数据集正在正确初始化,但我不明白我在尝试用数据填充字段时遇到的错误。数据集的形状和要写入的数据显然是相同的。
非常感谢任何关于我遗漏的帮助(很可能是简单的帮助)!
最佳答案
您的脚本在我的机器 (PY3) 上运行良好,h5py 版本 2.6.0。看起来你有 2.5。
It must be a bug in v2.5.0 that was fixed in v2.6.0.
关于python - 使用 h5py 一次将多维结构化 numpy 数组写入 hdf5 一个字段会引发 numpy 广播错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38204212/