[python-hdf5storage] 21/152: Made utilities.decode_complex more general (can set one or both field names manually).

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 3f75a8d72078566f14c2c2183bb0123700412ad8
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Tue Jan 21 20:16:19 2014 -0500

    Made utilities.decode_complex more general (can set one or both field names manually).
---
 hdf5storage/utilities.py | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/hdf5storage/utilities.py b/hdf5storage/utilities.py
index 21f0d33..47075d5 100644
--- a/hdf5storage/utilities.py
+++ b/hdf5storage/utilities.py
@@ -122,14 +122,15 @@ def decode_to_numpy_ascii(data):
         return data
 
 
-def decode_complex(data):
+def decode_complex(data, complex_names=(None, None)):
     """ Decodes possibly complex data read from an HDF5 file.
 
     Decodes possibly complex datasets read from an HDF5 file. HDF5
     doesn't have a native complex type, so they are stored as
     H5T_COMPOUND types with fields such as 'r' and 'i' for the real and
     imaginary parts. As there is no standardization for field names, the
-    field names have to be analyzed for proper decoding. A variety of
+    field names have to be given explicitly, or the fieldnames in `data`
+    analyzed for proper decoding to figure out the names. A variety of
     reasonably expected combinations of field names are checked and used
     if available to decode. If decoding is not possible, it is returned
     as is.
@@ -139,6 +140,10 @@ def decode_complex(data):
     data : arraylike
         The data read from an HDF5 file, that might be complex, to
         decode into the proper Numpy complex type.
+    complex_names : tuple of 2 str and/or Nones, optional
+        ``tuple`` of the names to use (in order) for the real and
+        imaginary fields. A ``None`` indicates that various common
+        field names should be tried.
 
     Returns
     -------
@@ -151,6 +156,12 @@ def decode_complex(data):
     --------
     encode_complex
 
+    Notes
+    -----
+    Currently looks for real field names of ``('r', 're', 'real')`` and
+    imaginary field names of ``('i', 'im', 'imag', 'imaginary')``
+    ignoring case.
+
     """
     # 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',
@@ -175,23 +186,20 @@ def decode_complex(data):
     # is and setting variables to the proper name if it is in it (they
     # are initialized to None so that we know if one isn't found).
 
-    real_name = None
-    imag_name = None
-
     real_fields = ['r', 're', 'real']
     imag_fields = ['i', 'im', 'imag', 'imaginary']
 
     for s in fields:
         if s.lower() in real_fields:
-            real_name = s
+            complex_names[0] = s
         elif s.lower() in imag_fields:
-            imag_name = s
+            complex_names = s
 
     # If the real and imaginary fields were found, construct the complex
     # form from the fields. Otherwise, return what we were given because
     # it isn't in the right form.
-    if real_name is not None and imag_name is not None:
-        return data[real_name] + 1j*data[imag_name]
+    if complex_names[0] is not None and complex_names[1] is not None:
+        return data[complex_names[0]] + 1j*data[complex_names[1]]
     else:
         return data
 

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