sidpy.hdf.dtype_utils.flatten_compound_to_real

sidpy.hdf.dtype_utils.flatten_compound_to_real(dataset, lazy=False)[source]

Flattens the individual components in a structured array or compound valued hdf5 dataset along the last axis to form a real valued array. Thus a compound h5py.Dataset or structured numpy matrix of shape (2, 3, 5) having 3 components will turn into a real valued matrix of shape (2, 3, 15), assuming that all the sub-dtypes of the matrix are real valued. ie - this function does not handle structured dtypes having complex values

Parameters:
Returns:

retval – real valued dataset

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_compound_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 ]