Skip to content
Snippets Groups Projects
Commit 1ef5b1b5 authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

New real <-> custom dtype IO functions

New helper functions that transform data from original type to real and vice versa added. Note - these functions have NOT been tested
parent aacbedb5
No related branches found
No related tags found
1 merge request!9fixed lots of stuff
......@@ -106,7 +106,7 @@ def recommendCores(num_jobs, requested_cores=None):
def complex_to_float(ds_main):
"""
Function to convert a complex ND numpy array or HDF5 dataset into a scalar dataset
Parameters
----------
ds_main : complex ND numpy array or ND HDF5 dataset
......@@ -121,7 +121,7 @@ def complex_to_float(ds_main):
def compound_to_scalar(ds_main):
"""
Converts a compound ND numpy array or HDF5 dataset into a real scalar dataset
Parameters
----------
ds_main : ND numpy array or ND HDF5 dataset object of compound datatype
......@@ -130,7 +130,7 @@ def compound_to_scalar(ds_main):
Returns
--------
retval : ND real numpy array
"""
if isinstance(ds_main, h5py.Dataset):
return np.hstack([np.float32(ds_main[name]) for name in ds_main.dtype.names])
......@@ -198,3 +198,105 @@ def check_dtype(ds_main):
func = new_dtype
return func, is_complex, is_compound, n_features, n_samples, type_mult
def realToComplex(ds_real):
"""
Puts the real and imaginary sections together to make complex dataset
Parameters
------------
ds_real : 2D real numpy array or HDF5 dataset
Data arranged as [instance, 2 x features]
where the first half of the features are the real component and the
second half contains the imaginary components
Returns
----------
ds_compound : 2D complex numpy array
Data arranged as [sample, features]
"""
return ds_real[:, :int(0.5 * ds_real.shape[1])] + 1j * ds_real[:, int(0.5 * ds_real.shape[1]):]
def realToCompound(ds_real, compound_type):
"""
Converts a real dataset to a compound dataset of the provided compound d-type
Parameters
------------
ds_real : 2D real numpy array or HDF5 dataset
Data arranged as [instance, 2 x features]
where the first half of the features are the real component and the
second half contains the imaginary components
compound_type : dtype
Target complex datatype
Returns
----------
ds_compound : 2D complex numpy array
Data arranged as [sample, features]
"""
new_spec_length = ds_real.shape[1]/len(compound_type)
if new_spec_length % 1:
raise TypeError('Provided compound type was not compatible by numbr of elements')
new_spec_length = int(new_spec_length)
ds_compound = np.empty([ds_real.shape[0], new_spec_length], dtype=compound_type)
for iname, name in enumerate(compound_type.names):
istart = iname * ds_compound.shape[1]
iend = (iname + 1) * ds_compound.shape[1]
ds_compound[name] = ds_real[:, istart:iend]
return ds_compound
def transformToTargetType(ds_real, new_dtype):
"""
Transforms real data into the target dtype
Parameters
----------
ds_real : 2D real numpy array or HDF5 dataset
Data arranged as [instance, 2 x features]
where the first half of the features are the real component and the
second half contains the imaginary components
new_dtype : dtype
Target datatype
Returns
----------
retval : 2D numpy array
Data of the target data type
"""
if new_dtype in [np.complex64, np.complex128, np.complex]:
return realToComplex(new_dtype)
elif len(new_dtype) > 1:
return realToCompound(ds_real, new_dtype)
else:
return new_dtype(ds_real)
def transformToReal(ds_main):
"""
Transforms real data into the target dtype
Parameters
----------
ds_real : 2D real numpy array or HDF5 dataset
Data arranged as [instance, 2 x features]
where the first half of the features are the real component and the
second half contains the imaginary components
new_dtype : dtype
Target datatype
Returns
----------
retval : 2D numpy array
Data of the target data type
"""
if ds_main.dtype in [np.complex64, np.complex128, np.complex]:
return complex_to_float(ds_main)
elif len(ds_main.dtype) > 1:
return compound_to_scalar(ds_main)
else:
return ds_main
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment