[python-hdf5storage] 32/152: Fixed bugs in writing boolean types in a Matlab compatible way.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:31 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 3b2e6024a162ac7d5878d8c44cf4cb31c9371a78
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Jan 26 16:22:47 2014 -0500

    Fixed bugs in writing boolean types in a Matlab compatible way.
---
 hdf5storage/Marshallers.py | 27 ++++++++++++++++++++++-----
 hdf5storage/core.py        | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 66aa71e..3337554 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -388,6 +388,11 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
         if options.reverse_dimension_order:
             data_to_store = data_to_store.T
 
+        # Bools need to be converted to uint8 if the option is given.
+        if data_to_store.dtype.name == 'bool' \
+                and options.convert_bools_to_uint8:
+            data_to_store = np.uint8(data_to_store)
+
         # If data is empty, we instead need to store the shape of the
         # array if the appropriate option is set.
 
@@ -475,9 +480,9 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
 
         # If we are making it MATLAB compatible, the MATLAB_class
         # attribute needs to be set looking up the data type (gotten
-        # using np.dtype.type) and if it is a string type, then the
-        # MATLAB_int_decode attribute must be set properly. Otherwise,
-        # the attributes must be deleted.
+        # using np.dtype.type) and if it is a string or bool type, then
+        # the MATLAB_int_decode attribute must be set
+        # properly. Otherwise, the attributes must be deleted.
 
         if options.MATLAB_compatible:
             tp = data.dtype.type
@@ -487,9 +492,10 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
             else:
                 set_attribute_string(grp[name], 'MATLAB_class', '')
 
-            if tp in (np.string_, np.unicode):
+            if tp in (np.string_, np.unicode, np.bool8):
                 set_attribute(grp[name], 'MATLAB_int_decode', np.int64(
-                              {np.string_: 2, np.unicode: 4}[tp]))
+                              {np.bool8: 1, np.string_: 2,
+                              np.unicode: 4}[tp]))
             else:
                 del_attribute(grp[name], 'MATLAB_int_decode')
 
@@ -556,6 +562,12 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
             if underlying_type.startswith('complex'):
                 data = decode_complex(data)
 
+            # If its underlying type is 'bool' but it is something else,
+            # then it needs to be converted (means it was written with
+            # the convert_bools_to_uint8 option).
+            if underlying_type == 'bool8' and data.dtype.name != 'bool':
+                data = np.bool8(data)
+
             # Convert to scalar, matrix, or ndarray depending on the
             # container type.
             if container == 'scalar':
@@ -595,6 +607,11 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
             if matlab_class in ['single', 'double']:
                 data = decode_complex(data)
 
+            # If it is a logical, then it must be converted to
+            # numpy.bool8.
+            if matlab_class == 'logical':
+                data = np.bool8(data)
+
             # If it is a 'char' type, the proper conversion to
             # numpy.unicode needs to be done.
             if matlab_class == 'char':
diff --git a/hdf5storage/core.py b/hdf5storage/core.py
index 75f5e7a..768c2fa 100644
--- a/hdf5storage/core.py
+++ b/hdf5storage/core.py
@@ -58,6 +58,7 @@ class Options(object):
     delete_unused_variables    ``True``
     convert_scalars_to_arrays  ``True``
     convert_strings_to_utf16   ``True``
+    convert_bools_to_uint8     ``True``
     reverse_dimension_order    ``True``
     store_shape_for_empty      ``True``
     complex_names              ``('real', 'imag')``
@@ -79,6 +80,8 @@ class Options(object):
         See Attributes.
     convert_strings_to_utf16 : bool, optional
         See Attributes.
+    convert_bools_to_uint8 : bool, optional
+        See Attributes.
     reverse_dimension_order : bool, optional
         See Attributes.
     store_shape_for_empty : bool, optional
@@ -93,6 +96,7 @@ class Options(object):
     delete_unused_variables : bool
     convert_scalars_to_arrays : bool
     convert_strings_to_utf16 : bool
+    convert_bools_to_uint8 : bool
     reverse_dimension_order : bool
     store_shape_for_empty : bool
     complex_names : tuple of two str
@@ -109,6 +113,7 @@ class Options(object):
                  delete_unused_variables=False,
                  convert_scalars_to_arrays=False,
                  convert_strings_to_utf16=False,
+                 convert_bools_to_uint8=False,
                  reverse_dimension_order=False,
                  store_shape_for_empty=False,
                  complex_names=('r', 'i')):
@@ -118,6 +123,7 @@ class Options(object):
         self._delete_unused_variables = False
         self._convert_scalars_to_arrays = False
         self._convert_strings_to_utf16 = False
+        self._convert_bools_to_uint8 = False
         self._reverse_dimension_order = False
         self._store_shape_for_empty = False
         self._complex_names = ('r', 'i')
@@ -131,6 +137,7 @@ class Options(object):
         self.delete_unused_variables = delete_unused_variables
         self.convert_scalars_to_arrays = convert_scalars_to_arrays
         self.convert_strings_to_utf16 = convert_strings_to_utf16
+        self.convert_bools_to_uint8 = convert_bools_to_uint8
         self.reverse_dimension_order = reverse_dimension_order
         self.store_shape_for_empty = store_shape_for_empty
         self.complex_names = complex_names
@@ -183,6 +190,7 @@ class Options(object):
         delete_unused_variables    ``True``
         convert_scalars_to_arrays  ``True``
         convert_strings_to_utf16   ``True``
+        convert_bools_to_uint8     ``True``
         reverse_dimension_order    ``True``
         store_shape_for_empty      ``True``
         complex_names              ``('real', 'imag')``
@@ -205,6 +213,7 @@ class Options(object):
                 self._delete_unused_variables = True
                 self._convert_scalars_to_arrays = True
                 self._convert_strings_to_utf16 = True
+                self._convert_bools_to_uint8 = True
                 self._reverse_dimension_order = True
                 self._store_shape_for_empty = True
                 self._complex_names = ('real', 'imag')
@@ -290,6 +299,32 @@ class Options(object):
             self._MATLAB_compatible = False
 
     @property
+    def convert_bools_to_uint8(self):
+        """ Whether or not to convert bools to ``numpy.uint8``.
+
+        bool
+
+        If ``True`` (defaults to ``False`` unless MATLAB compatibility
+        is being done), bool types are converted to ``numpy.uint8``
+        before being written to file.
+
+        Must be ``True`` if doing MATLAB compatibility. MATLAB doesn't
+        use the enums that ``h5py`` wants to use by default and also
+        uses uint8 intead of int8.
+
+        """
+        return self._convert_bools_to_uint8
+
+    @convert_bools_to_uint8.setter
+    def convert_bools_to_uint8(self, value):
+        # Check that it is a bool, and then set it. If it is false, we
+        # are not doing MATLAB compatible formatting.
+        if isinstance(value, bool):
+            self._convert_bools_to_uint8 = value
+        if not self._convert_bools_to_uint8:
+            self._MATLAB_compatible = False
+
+    @property
     def reverse_dimension_order(self):
         """ Whether or not to reverse the order of array dimensions.
 
@@ -601,6 +636,7 @@ def write(filename='data.h5', name='/data', data=None,
           convert_scalars_to_arrays=False,
           reverse_dimension_order=False,
           convert_strings_to_utf16=False,
+          convert_bools_to_uint8=False,
           store_shape_for_empty=False,
           complex_names=('r', 'i')):
     # Pack the different options into an Options class. The easiest way

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