如果dtype为“

问题描述 投票:0回答:2
在python 3.6中如何从hdf5文件获取数据数组

我想从hdf5文件中获取格式为{N,16,512,128}的数据集作为4D numpy数组。N是具有{16,512,128}格式的3D数组的数量。我尝试这样做:

import os
import sys
import h5py as h5
import numpy as np
import subprocess
import re

file_name = sys.argv[1]
path = sys.argv[2]

f = h5.File(file_name, 'r')
data = f[path]
print(data.shape) #{27270, 16, 512, 128}
print(data.dtype) #"<u4"

data = np.array(data, dtype=np.uint32)
print(data.shape)

不幸的是,在执行data = np.array(data, dtype=np.uint32)命令后,代码似乎崩溃了,因为之后什么也没发生。

我需要以numpy数组或类似的形式检索此数据集以进行进一步的计算。如果您有任何建议,请告诉我。

python arrays numpy hdf5 h5py
2个回答
0
投票

我在写/获取<u4np.uint32时没有问题:

In [14]: import h5py                                                                                   
In [15]: f=h5py.File('u4.h5','w')                                                                      
In [16]: ds = f.create_dataset('data', dtype='<u4', shape=(10,))                                       
In [17]: ds                                                                                            
Out[17]: <HDF5 dataset "data": shape (10,), type "<u4">
In [18]: ds[:]                                                                                         
Out[18]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint32)
In [19]: ds[:] = np.arange(-5,5)                                                                       
In [20]: ds                                                                                            
Out[20]: <HDF5 dataset "data": shape (10,), type "<u4">
In [21]: ds[:]                                                                                         
Out[21]: array([0, 0, 0, 0, 0, 0, 1, 2, 3, 4], dtype=uint32)
In [22]: np.array(ds, dtype='uint32')                                                                  
Out[22]: array([0, 0, 0, 0, 0, 0, 1, 2, 3, 4], dtype=uint32)
In [23]: f.close()     

您可能会达到内存限制。尝试创建该大小的数组时出现内存错误:

In [24]: np.zeros((27270, 16, 512, 128),np.uint32);                                                    
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-24-2cfe704044b6> in <module>
----> 1 np.zeros((27270, 16, 512, 128),np.uint32);

MemoryError: Unable to allocate 107. GiB for an array with shape (27270, 16, 512, 128) and data type uint32

您可能仍然可以加载data的切片,例如data[0:100]


0
投票

事实证明,您甚至不需要重塑。这是访问数据集然后切片以获得数组的示例。我认为这正是您想要的。

编辑2020年4月30日OP关于uint32。我最初的答案是使用浮点数组。它展示了所需的行为。为了完整起见,我做了一些修改,以从uint32整数数组创建数据集。注意:我使用了a0=100。它创建的HDF5文件对于浮点数是840 MB,对于uint32是416 MB。将a0=27270乘以273。我没有足够的RAM一次创建该RAM。下面的代码显示了该过程。

((注:该数据集是使用maxshape=None为轴= 0创建的,以便进行扩展。如果您有兴趣测试更大的数据集,则可以通过添加一个循环以创建更多数据并将其添加到末尾来修改此示例。数据集。)

import numpy as np
import h5py

a0 = 27270
a0 = 100
a1= 16
a2 = 512
a3 = 128

f_arr = np.random.rand(a0*a1*a2*a3).reshape(a0, a1, a2, a3)
i_arr = np.random.randint(0,254, (a0, a1, a2, a3), dtype=np.uint32 )

with h5py.File('SO_61508870.h5', mode='w') as h5w:
     h5f.create_dataset('array1', data=i_arr, maxshape=(None, a1, a2, a3) )

with h5py.File('SO_61508870.h5', mode='r') as h5r:
     data_ds = h5r['array1']
     print ('dataset shape:', data_ds.shape)
     for i in range(5):
         sliced_arr = data_ds[i,:,:,:]
         print ('array shape:', sliced_arr.shape)
© www.soinside.com 2019 - 2024. All rights reserved.