要显示的复杂h5文件

我有一个复杂的h5文件。 2D阵列。我想展示它。但是我有以下错误。怎么了?

import h5py 
import numpy as np 
import matplotlib.pyplot as plt

with h5py.File('obj_0001.h5','r') as hdf:
    ls = list(hdf.keys())
    print('List of datasets in thies file: \n',ls)
    data = hdf.get('dataset')

    diff = np.array(data)
    print('Shape of dataset: \n',diff.shape)

plt.figure(1)
plt.imshow(np.abs(diff))
plt.savefig('diff_test.png')
plt.show()
UFuncTypeError: ufunc 'absolute' did not contain a loop with signature matching types dtype([('real','<f4'),('imag','<f4')]) -> dtype([('real','<f4')])
zj7887912 回答:要显示的复杂h5文件

根据http://docs.h5py.org/en/stable/faq.html#what-datatypes-are-supported

h5py支持复杂的dtype,代表HDF5 struc

错误表明diff.dtypedtype([('real','<f4'),('imag','<f4')])。我不知道这是您的np.array(data)转换的结果,还是数据在文件中的存储方式有所不同。

diff = data[:]

可能值得尝试,因为这是从数据集中加载数组的首选语法。

但是如果diff是该结构化数组,则可以使用以下方法创建复杂的dtype:

In [303]: arr2 = np.ones((3,),np.dtype([('real','f'),'f')]))                                  
In [304]: arr2                                                                                         
Out[304]: 
array([(1.,1.),(1.,1.)],dtype=[('real','<f4')])
In [305]: arr3 = arr2['real']+1j*arr2['imag']                                                          
In [306]: arr3                                                                                         
Out[306]: array([1.+1.j,1.+1.j,1.+1.j],dtype=complex64)

abs中进行测试:

In [307]: np.abs(arr2)                                                                                 
---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-307-333e28818b26> in <module>
----> 1 np.abs(arr2)

UFuncTypeError: ufunc 'absolute' did not contain a loop with signature matching types dtype([('real','<f4')]) -> dtype([('real','<f4')])
In [308]: np.abs(arr3)                                                                                 
Out[308]: array([1.4142135,1.4142135,1.4142135],dtype=float32)
本文链接:https://www.f2er.com/2391874.html

大家都在问