[python-hdf5storage] 03/84: Added MATLAB_fields Attribute write support to the NumpyScalarArrayMarshaller.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:57 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 0.1.10
in repository python-hdf5storage.

commit 29fcef778442db7b2098334c07c9dfc845521956
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Aug 10 17:21:39 2014 -0400

    Added MATLAB_fields Attribute write support to the NumpyScalarArrayMarshaller.
---
 hdf5storage/Marshallers.py | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 1ae918a..1eb034e 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -464,7 +464,8 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                                    'Python.numpy.Container',
                                    'Python.Fields'])
         self.matlab_attributes |= set(['MATLAB_class', 'MATLAB_empty',
-                                   'MATLAB_int_decode'])
+                                      'MATLAB_int_decode',
+                                      'MATLAB_fields'])
         # As np.str_ is the unicode type string in Python 3 and the bare
         # bytes string in Python 2, we have to use np.unicode_ which is
         # or points to the unicode one in both versions.
@@ -695,6 +696,29 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                         set(field_names)):
                     del grp2[field]
 
+            # If we are making it MATLAB compatible and have h5py
+            # version >= 2.3, then we can set the MATLAB_fields
+            # Attribute as long as all keys are mappable to
+            # ASCII. Otherwise, the attribute should be deleted. It is
+            # written as a vlen='S1' array of bytes_ arrays of the
+            # individual characters.
+            if options.matlab_compatible \
+                    and distutils.version.LooseVersion( \
+                    h5py.__version__) \
+                    >= distutils.version.LooseVersion('2.3'):
+                try:
+                    dt = h5py.special_dtype(vlen=np.dtype('S1'))
+                    fs = np.empty(shape=(len(fields),), dtype=dt)
+                    for i, s in enumerate(fields):
+                        fs[i] = np.array([c.encode('ascii') for c in s],
+                                         dtype='S1')
+                except UnicodeDecodeError:
+                    del_attribute(grp[name], 'MATLAB_fields')
+                else:
+                    set_attribute(grp[name], 'MATLAB_fields', fs)
+            else:
+                del_attribute(grp[name], 'MATLAB_fields')
+            
             # Go field by field making an object array (make an empty
             # object array and assign element wise) and write it inside
             # the Group. If it only has a single element, write that

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