[python-hdf5storage] 02/84: Added MATLAB_fields Attribute support to the dict marshaller.

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 5a1bf8a9b9290eb1138adff3cbd4d2a0f31a07a7
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Aug 10 16:56:02 2014 -0400

    Added MATLAB_fields Attribute support to the dict marshaller.
---
 hdf5storage/Marshallers.py | 42 +++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index af623fa..1ae918a 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -31,6 +31,7 @@
 import sys
 import posixpath
 import collections
+import distutils.version
 
 import numpy as np
 import h5py
@@ -1287,7 +1288,7 @@ class PythonDictMarshaller(TypeMarshaller):
     def __init__(self):
         TypeMarshaller.__init__(self)
         self.python_attributes |= set(['Python.Fields'])
-        self.matlab_attributes |= set(['MATLAB_class'])
+        self.matlab_attributes |= set(['MATLAB_class', 'MATLAB_fields'])
         self.types = [dict]
         self.python_type_strings = ['dict']
         self.__MATLAB_classes = {dict: 'struct'}
@@ -1356,15 +1357,36 @@ class PythonDictMarshaller(TypeMarshaller):
         TypeMarshaller.write_metadata(self, f, grp, name, data,
                                       type_string, options)
 
+        # Grab all the keys and sort the list.
+        fields = sorted(list(data.keys()))
+
         # If we are storing python metadata, we need to set the
-        # 'Python.Fields' Attribute to be all the keys. They will be
-        # sorted for convenience
+        # 'Python.Fields' Attribute to be all the keys.
         if options.store_python_metadata:
-            fields = list(data.keys())
-            fields.sort()
             set_attribute_string_array(grp[name], 'Python.Fields',
                                        fields)
 
+        # 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')
+
         # If we are making it MATLAB compatible, the MATLAB_class
         # attribute needs to be set for the data type. If the type
         # cannot be found or if we are not doing MATLAB compatibility,
@@ -1378,16 +1400,6 @@ class PythonDictMarshaller(TypeMarshaller):
         else:
             del_attribute(grp[name], 'MATLAB_class')
 
-        # Write an array of all the fields to the attribute that lists
-        # them.
-        #
-        # NOTE: Can't make it do a variable length set of strings like
-        # MATLAB likes. However, not including them seems to cause no
-        # problem.
-        #
-        # set_attribute_string_array(grp[name], \
-        #     'MATLAB_fields', [k for k in data])
-
     def read(self, f, grp, name, options):
         # If name is not present or is not a Group, then we can't read
         # it and have to throw an error.

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