sidpy.hdf.dtype_utils.flatten_to_real

sidpy.hdf.dtype_utils.flatten_to_real(ds_main, lazy=False)[source]

Flattens complex / compound / real valued arrays to real valued arrays

Parameters:
  • ds_main (numpy.ndarray, or h5py.Dataset, or dask.array.core.Array) – Compound, complex or real valued numpy array or HDF5 dataset

  • lazy (bool, optional. Default = False) – If set to True, will use lazy Dask arrays instead of in-memory numpy arrays

Returns:

ds_main – Array raveled to a float data type

Return type:

numpy.ndarray, or dask.array.core.Array

Examples

>>> import numpy as np
>>> import sidpy
>>> num_elems = 5
>>> struct_dtype = np.dtype({'names': ['r', 'g', 'b'],
>>>                          'formats': [np.float32, np.uint16, np.float64]})
>>> structured_array = np.zeros(shape=num_elems, dtype=struct_dtype)
>>> structured_array['r'] = np.random.random(size=num_elems) * 1024
>>> structured_array['g'] = np.random.randint(0, high=1024, size=num_elems)
>>> structured_array['b'] = np.random.random(size=num_elems) * 1024
>>> print('Structured array is of shape {} and have values:'.format(structured_array.shape))
>>> print(structured_array)
Structured array is of shape (5,) and have values:
[(859.62445,  54, 1012.22256219) (959.5565 , 678,  296.19788769)
 (383.20737, 689,  192.45427816) (201.56635, 889,  939.01082338)
 (334.22015, 467,  980.9081472 )]
>>> real_array = sidpy.dtype_utils.flatten_to_real(structured_array)
>>> print('This array converted to regular scalar matrix has shape: {} and values:'.format(real_array.shape))
>>> print(real_array)
This array converted to regular scalar matrix has shape: (15,) and values:
[ 859.62445068  959.55651855  383.20736694  201.56634521  334.22015381
   54.          678.          689.          889.          467.
 1012.22256219  296.19788769  192.45427816  939.01082338  980.9081472 ]