Search code examples
complex-numbersh5pyabsolute-value

complex h5 File to show


I have a complex type h5 file. 2d array. I want to imshow it. But I have following error. What is wrong?

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'), ('imag', '<f4')])

Solution

  • According to http://docs.h5py.org/en/stable/faq.html#what-datatypes-are-supported

    h5py supports complex dtype, representing a HDF5 struc.

    The error indicates diff.dtype is dtype([('real', '<f4'), ('imag', '<f4')]). I don't know if that is the result of your np.array(data) conversion or there's something different about how the data is stored on the file.

    diff = data[:]
    

    might be worth trying, since that's the preferred syntax for loading an array from a dataset.

    But if diff is that structured array, you can make a complex dtype with:

    In [303]: arr2 = np.ones((3,), np.dtype([('real','f'),('imag','f')]))                                  
    In [304]: arr2                                                                                         
    Out[304]: 
    array([(1., 1.), (1., 1.), (1., 1.)],
          dtype=[('real', '<f4'), ('imag', '<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)
    

    testing in 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'), ('imag', '<f4')]) -> dtype([('real', '<f4'), ('imag', '<f4')])
    In [308]: np.abs(arr3)                                                                                 
    Out[308]: array([1.4142135, 1.4142135, 1.4142135], dtype=float32)