[python-hdf5storage] 18/152: Added utility function to encode complex types to be written to HDF5 file with the right complex field names.
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:30 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to annotated tag 0.1
in repository python-hdf5storage.
commit 33124c8d61b0d8e8139b5ef648cb8f285658e870
Author: Freja Nordsiek <fnordsie at gmail.com>
Date: Tue Jan 21 19:38:12 2014 -0500
Added utility function to encode complex types to be written to HDF5 file with the right complex field names.
---
hdf5storage/utilities.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/hdf5storage/utilities.py b/hdf5storage/utilities.py
index c8f1668..4fc73b3 100644
--- a/hdf5storage/utilities.py
+++ b/hdf5storage/utilities.py
@@ -147,6 +147,10 @@ def decode_complex(data):
complex version is returned. Otherwise, `data` is returned
unchanged.
+ See Also
+ --------
+ encode_complex
+
"""
# Now, complex types are stored in HDF5 files as an H5T_COMPOUND type
# with fields along the lines of ('r', 're', 'real') and ('i', 'im',
@@ -192,6 +196,53 @@ def decode_complex(data):
return data
+def encode_complex(data, complex_names):
+ """ Encodes complex data to having arbitrary complex field names.
+
+ Encodes complex `data` to have the real and imaginary field names
+ given in `complex_numbers`. This is needed because the field names
+ have to be set so that it can be written to an HDF5 file with the
+ right field names (HDF5 doesn't have a native complex type, so
+ H5T_COMPOUND have to be used).
+
+ Parameters
+ ----------
+ data : arraylike
+ The data to encode as a complex type with the desired real and
+ imaginary part field names.
+ complex_names : tuple of 2 str
+ ``tuple`` of the names to use (in order) for the real and
+ imaginary fields.
+
+ Returns
+ -------
+ encoded data
+ `data` encoded into having the specified field names for the
+ real and imaginary parts.
+
+ See Also
+ --------
+ decode_complex
+
+ """
+ # Grab the dtype name, and convert it to the right non-complex type
+ # if it isn't already one.
+ dtype_name = data.dtype.name
+ if dtype_name[0:7] == 'complex':
+ dtype_name = 'float' + str(int(dtype_name[7:]))
+
+ # Create the new version of the data with the right field names for
+ # the real and complex parts and the right shape.
+ new_data = np.ndarray(shape=data.shape,
+ dtype=[(complex_names[0], dtype_name),
+ (complex_names[1], dtype_name)])
+
+ # Set the real and complex parts and return it.
+ new_data[complex_names[0]] = np.real(data)
+ new_data[complex_names[1]] = np.imag(data)
+ return new_data
+
+
def get_attribute(target, name):
""" Gets an attribute from a Dataset or Group.
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-hdf5storage.git
More information about the debian-science-commits
mailing list