0

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')])
lighttn
  • 3
  • 1
  • 4
  • `diff` is a structured array with a compound dtype. The real and imag parts are in separate `fields`. I don't recall what `h5py` says about complex dtypes, if anything. `diff.astype(complex)` might work, but I'd have to test it to be sure. – hpaulj Apr 21 '20 at 15:49
  • It would be helpful to see the output of `print(ls)`. I assume `dataset` the names when you list the root level keys. If so, you can get a numpy array directly with `diff = hdf['dataset'][:]`. Also, it would be helpful to see the data type of the array: `print (hdf['dataset'][:].dtype)` to see the array's field names and datatypes. – kcw78 Apr 21 '20 at 17:47
  • @kcw78, the error says the dtype is `dtype([('real', ' – hpaulj Apr 21 '20 at 20:13
  • List of datasets in this file: ['dataset'] / Shape of dataset: (1222, 1222) – lighttn Apr 22 '20 at 04:48
  • In: diff.astype(complex), Out: TypeError: Cannot cast array from dtype([('real', ' – lighttn Apr 22 '20 at 04:59
  • In: diff.dtype /// Out: dtype([('real', ' – lighttn Apr 22 '20 at 05:04

1 Answers1

0

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)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • In: diff Out: array([[(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., 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.), (0., 0.)]], dtype=[('real', ' – lighttn Apr 22 '20 at 05:15
  • Thank you! it works by conversion to type complex. I though the data was complex. Becasue it includes two data in one pixel. – lighttn Apr 22 '20 at 05:25