[python-hdf5storage] 130/152: Added read/write numpy.recarray support.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:41 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 7cebd7234e0fa310d751a9a32132030f65326f5c
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Fri Feb 14 20:18:41 2014 -0500

    Added read/write numpy.recarray support.
---
 README.rst                             |  1 +
 doc/source/hdf5storage.Marshallers.rst |  4 ++--
 doc/source/storage_format.rst          |  7 +++++--
 hdf5storage/Marshallers.py             | 15 ++++++++++++++-
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/README.rst b/README.rst
index 2df4783..8c7ba07 100644
--- a/README.rst
+++ b/README.rst
@@ -109,6 +109,7 @@ np.object\_    0.1                                  cell         0.1
 np.ndarray     0.1      [5]_ [6]_                   [5]_ [6]_    0.1 [5]_
 np.matrix      0.1      [5]_                        [5]_         0.1 [5]_
 np.chararray   0.1      [5]_                        [5]_         0.1 [5]_
+np.recarray    0.1      structured np.ndarray       [5]_ [6]_    0.1 [5]_
 =============  =======  ==========================  ===========  ==========
 
 .. [1] Depends on the selected options. Always ``np.uint8`` when doing
diff --git a/doc/source/hdf5storage.Marshallers.rst b/doc/source/hdf5storage.Marshallers.rst
index db13142..6b6e997 100644
--- a/doc/source/hdf5storage.Marshallers.rst
+++ b/doc/source/hdf5storage.Marshallers.rst
@@ -72,7 +72,7 @@ NumpyScalarArrayMarshaller
 
    .. autoinstanceattribute:: NumpyScalarArrayMarshaller.types
       :annotation: = [np.ndarray, np.matrix,
-                      np.chararray,
+                      np.chararray, np.core.records.recarray,
                       np.bool_, np.void,
                       np.uint8, np.uint16, np.uint32, np.uint64,
                       np.int8, np.int16, np.int32, np.int64,
@@ -82,7 +82,7 @@ NumpyScalarArrayMarshaller
 
    .. autoinstanceattribute:: NumpyScalarArrayMarshaller.python_type_strings
       :annotation: = ['numpy.ndarray', 'numpy.matrix',
-                      'numpy.chararray',
+                      'numpy.chararray', 'numpy.recarray',
                       'numpy.bool_', 'numpy.void',
                       'numpy.uint8', 'numpy.uint16',
                       'numpy.uint32', 'numpy.uint64', 'numpy.int8',
diff --git a/doc/source/storage_format.rst b/doc/source/storage_format.rst
index a072ac7..8944298 100644
--- a/doc/source/storage_format.rst
+++ b/doc/source/storage_format.rst
@@ -85,6 +85,7 @@ np.object\_    0.1                                            Dataset
 np.ndarray     0.1      not or Group of contents [5]_         Dataset or Group [5]_
 np.matrix      0.1      np.ndarray                            Dataset
 np.chararray   0.1      np.bytes\_ or np.uint16/32 [2]_ [3]_  Dataset
+np.recarray    0.1      structued np.ndarray [5]_             Dataset or Group [5]_
 =============  =======  ====================================  =====================
 
 .. [1] Depends on the selected options. Always ``np.uint8`` when
@@ -170,6 +171,7 @@ np.object\_    'numpy.object\_'     'object'                     'cell'
 np.ndarray     'numpy.ndarray'      [8]_                         [8]_ [9]_
 np.matrix      'numpy.matrix'       [8]_                         [8]_
 np.chararray   'numpy.chararray'    [8]_                         'char' [8]_
+np.recarray    'numpy.recarray'     [8]_                         [8]_ [9]_
 =============  ===================  ===========================  ==================  =================
 
 .. [6] '#' is replaced by the number of bits taken up by the string, or
@@ -201,12 +203,13 @@ Python.numpy.Container
 
 Python Attribute
 
-{'scalar', 'ndarray', 'matrix', 'chararray'}
+{'scalar', 'ndarray', 'matrix', 'chararray', 'recarray'}
 
 For Numpy types (or types converted to them), whether the type is a
 scalar (its type is something such as ``np.uint16``, ``np.str_``, etc.),
 some form of array (its type is ``np.ndarray``), a matrix (type
-is ``np.matrix``), or is a ``np.chararray`` is stored in this Attribute.
+is ``np.matrix``), is a ``np.chararray``, or is a ``np.recarray`` is
+stored in this Attribute.
 
 Python.Fields
 -------------
diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index 254ca17..89c1b6b 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -464,7 +464,7 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
         self.matlab_attributes |= {'MATLAB_class', 'MATLAB_empty',
                                    'MATLAB_int_decode'}
         self.types = [np.ndarray, np.matrix,
-                      np.chararray,
+                      np.chararray, np.core.records.recarray,
                       np.bool_, np.void,
                       np.uint8, np.uint16, np.uint32, np.uint64,
                       np.int8, np.int16, np.int32, np.int64,
@@ -473,6 +473,7 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                       np.bytes_, np.str_, np.object_]
         self.python_type_strings = ['numpy.ndarray', 'numpy.matrix',
                                     'numpy.chararray',
+                                    'numpy.recarray',
                                     'numpy.bool_', 'numpy.void',
                                     'numpy.uint8', 'numpy.uint16',
                                     'numpy.uint32', 'numpy.uint64',
@@ -551,6 +552,11 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
 
         data_to_store = data.copy()
 
+        # recarrays must be converted to structured ndarrays in order
+        # for h5py to be able to write them.
+        if isinstance(data_to_store, np.core.records.recarray):
+            data_to_store = data_to_store.view(np.ndarray)
+
         # Optionally convert ASCII strings to UTF-16. This is done by
         # simply converting to uint16's. This will require making them
         # at least 1 dimensinal.
@@ -782,6 +788,8 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                 container = 'matrix'
             elif isinstance(data, np.chararray):
                 container = 'chararray'
+            elif isinstance(data, np.core.records.recarray):
+                container = 'recarray'
             elif isinstance(data, np.ndarray):
                 container = 'ndarray'
             else:
@@ -1039,6 +1047,11 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                     and np.prod(shape) == np.prod(data.shape):
                 data = data.reshape(tuple(shape))
 
+            # If data is a structured ndarray and the type string says
+            # it is a recarray, then turn it into one.
+            if type_string == 'numpy.recarray':
+                data = data.view(np.core.records.recarray)
+
             # Convert to scalar, matrix, chararray, or ndarray depending
             # on the container type. For an empty scalar string, it
             # needs to be manually set to '' and b'' or there will be

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