[python-hdf5storage] 109/152: Added numpy.chararray support.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:39 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 9b5c74cf81937afbaf3de2e99aa84ef7469379b1
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Wed Feb 12 02:22:45 2014 -0500

    Added numpy.chararray support.
---
 README.rst                             | 87 ++++++++++++++++---------------
 doc/source/hdf5storage.Marshallers.rst |  2 +
 doc/source/storage_format.rst          | 93 ++++++++++++++++++----------------
 hdf5storage/Marshallers.py             | 17 +++++--
 4 files changed, 110 insertions(+), 89 deletions(-)

diff --git a/README.rst b/README.rst
index c28deee..44a774e 100644
--- a/README.rst
+++ b/README.rst
@@ -69,43 +69,46 @@ will be what it is read back as) the MATLAB class it becomes if
 targetting a MAT file, and the first version of this package to
 support writing it so MATlAB can read it.
 
-=============  =======  =======================  ===========  ========
-Python                                           MATLAB
------------------------------------------------  ---------------------
-Type           Version  Converted to             Class        Version
-=============  =======  =======================  ===========  ========
-bool           0.1      np.bool\_ or np.uint8    logical      0.1 [1]_
-None           0.1      ``np.float64([])``       ``[]``       0.1
-int            0.1      np.int64                 int64        0.1
-float          0.1      np.float64               double       0.1
-complex        0.1      np.complex128            double       0.1
-str            0.1      np.uint32/16             char         0.1 [2]_
-bytes          0.1      np.bytes\_ or np.uint16  char         0.1 [3]_
-bytearray      0.1      np.bytes\_ or np.uint16  char         0.1 [3]_
-list           0.1      np.object\_              cell         0.1
-tuple          0.1      np.object\_              cell         0.1
-set            0.1      np.object\_              cell         0.1
-frozenset      0.1      np.object\_              cell         0.1
-cl.deque       0.1      np.object\_              cell         0.1
-dict           0.1                               struct       0.1 [4]_
-np.bool\_      0.1                               logical      0.1
-np.uint8       0.1                               uint8        0.1
-np.uint16      0.1                               uint16       0.1
-np.uint32      0.1                               uint32       0.1
-np.uint64      0.1                               uint64       0.1
-np.uint8       0.1                               int8         0.1
-np.int16       0.1                               int16        0.1
-np.int32       0.1                               int32        0.1
-np.int64       0.1                               int64        0.1
+=============  =======  ==========================  ===========  ==========
+Python                                              MATLAB
+--------------------------------------------------  -----------------------
+Type           Version  Converted to                Class        Version
+=============  =======  ==========================  ===========  ==========
+bool           0.1      np.bool\_ or np.uint8       logical      0.1 [1]_
+None           0.1      ``np.float64([])``          ``[]``       0.1
+int            0.1      np.int64                    int64        0.1
+float          0.1      np.float64                  double       0.1
+complex        0.1      np.complex128               double       0.1
+str            0.1      np.uint32/16                char         0.1 [2]_
+bytes          0.1      np.bytes\_ or np.uint16     char         0.1 [3]_
+bytearray      0.1      np.bytes\_ or np.uint16     char         0.1 [3]_
+list           0.1      np.object\_                 cell         0.1
+tuple          0.1      np.object\_                 cell         0.1
+set            0.1      np.object\_                 cell         0.1
+frozenset      0.1      np.object\_                 cell         0.1
+cl.deque       0.1      np.object\_                 cell         0.1
+dict           0.1                                  struct       0.1 [4]_
+np.bool\_      0.1                                  logical      0.1
+np.uint8       0.1                                  uint8        0.1
+np.uint16      0.1                                  uint16       0.1
+np.uint32      0.1                                  uint32       0.1
+np.uint64      0.1                                  uint64       0.1
+np.uint8       0.1                                  int8         0.1
+np.int16       0.1                                  int16        0.1
+np.int32       0.1                                  int32        0.1
+np.int64       0.1                                  int64        0.1
 np.float16     0.1
-np.float32     0.1                               single       0.1
-np.float64     0.1                               double       0.1
-np.complex64   0.1                               single       0.1
-np.complex128  0.1                               double       0.1
-np.str\_       0.1      np.uint32/16             char/uint32  0.1 [2]_
-np.bytes\_     0.1      np.bytes\_ or np.uint16  char         0.1 [3]_
-np.object\_    0.1                               cell         0.1
-=============  =======  =======================  ===========  ========
+np.float32     0.1                                  single       0.1
+np.float64     0.1                                  double       0.1
+np.complex64   0.1                                  single       0.1
+np.complex128  0.1                                  double       0.1
+np.str\_       0.1      np.uint32/16                char/uint32  0.1 [2]_
+np.bytes\_     0.1      np.bytes\_ or np.uint16     char         0.1 [3]_
+np.object\_    0.1                                  cell         0.1
+np.ndarray     0.1      [5]_                        [5]_         0.1 [5]_
+np.matrix      0.1      [5]_                        [5]_         0.1 [5]_
+np.chararray   0.1      [5]_                        [5]_         0.1 [5]_
+=============  =======  ==========================  ===========  ==========
 
 .. [1] Depends on the selected options. Always ``np.uint8`` when doing
        MATLAB compatiblity, or if the option is explicitly set.
@@ -122,6 +125,8 @@ np.object\_    0.1                               cell         0.1
        stored as ``np.uint16`` in UTF-16 encoding. Otherwise, it is just
        written as ``np.bytes_``.
 .. [4] All keys must be ``str``.
+.. [5] Container types are only supported if their underlying dtype is
+       supported. Data conversions are done based on its dtype.
 
 This table gives the MATLAB classes that can be read from a MAT file,
 the first version of this package that can read them, and the Python
@@ -131,8 +136,8 @@ type they are read as.
 MATLAB Class  Version  Python Type
 ============  =======  ================================
 logical       0.1      np.bool\_
-single        0.1      np.float32 or np.complex64 [5]_
-double        0.1      np.float64 or np.complex128 [5]_
+single        0.1      np.float32 or np.complex64 [6]_
+double        0.1      np.float64 or np.complex128 [6]_
 uint8         0.1      np.uint8
 uint16        0.1      np.uint16
 uint32        0.1      np.uint32
@@ -141,9 +146,9 @@ int8          0.1      np.int8
 int16         0.1      np.int16
 int32         0.1      np.int32
 int64         0.1      np.int64
-struct        0.1      dict [6]_
+struct        0.1      dict [7]_
 cell          0.1      np.object\_
 ============  =======  ================================
 
-.. [5] Depends on whether there is a complex part or not.
-.. [6] Structure arrays are not supported.
+.. [6] Depends on whether there is a complex part or not.
+.. [7] Structure arrays are not supported.
diff --git a/doc/source/hdf5storage.Marshallers.rst b/doc/source/hdf5storage.Marshallers.rst
index feca8ee..1073b65 100644
--- a/doc/source/hdf5storage.Marshallers.rst
+++ b/doc/source/hdf5storage.Marshallers.rst
@@ -47,6 +47,7 @@ NumpyScalarArrayMarshaller
 
    .. autoinstanceattribute:: NumpyScalarArrayMarshaller.types
       :annotation: = [np.ndarray, np.matrix,
+                      np.chararray,
                       np.bool_,
                       np.uint8, np.uint16, np.uint32, np.uint64,
                       np.int8, np.int16, np.int32, np.int64,
@@ -56,6 +57,7 @@ NumpyScalarArrayMarshaller
 
    .. autoinstanceattribute:: NumpyScalarArrayMarshaller.python_type_strings
       :annotation: = ['numpy.ndarray', 'numpy.matrix',
+                      'numpy.chararray',
                       'numpy.bool_', 'numpy.uint8', 'numpy.uint16',
                       'numpy.uint32', 'numpy.uint64', 'numpy.int8',
 		      'numpy.int16', 'numpy.int32', 'numpy.int64',
diff --git a/doc/source/storage_format.rst b/doc/source/storage_format.rst
index 1488251..e63cbaa 100644
--- a/doc/source/storage_format.rst
+++ b/doc/source/storage_format.rst
@@ -47,41 +47,42 @@ stored (Group or Dataset), what type/s it is converted to (no conversion
 if none are listed), as well as the first version of this package to
 support the datatype.
 
-=============  =======  ============================  ================
-Type           Version  Converted to                  Group or Dataset
-=============  =======  ============================  ================
-bool           0.1      np.bool\_ or np.uint8 [1]_    Dataset
-None           0.1      ``np.float64([])``            Dataset
-int            0.1      np.int64                      Dataset
-float          0.1      np.float64                    Dataset
-complex        0.1      np.complex128                 Dataset
-str            0.1      np.uint32/16 [2]_             Dataset
-bytes          0.1      np.bytes\_ or np.uint16 [3]_  Dataset
-bytearray      0.1      np.bytes\_ or np.uint16 [3]_  Dataset
-list           0.1      np.object\_                   Dataset
-tuple          0.1      np.object\_                   Dataset
-set            0.1      np.object\_                   Dataset
-frozenset      0.1      np.object\_                   Dataset
-cl.deque       0.1      np.object\_                   Dataset
-dict [4]_      0.1                                    Group
-np.bool\_      0.1      not or np.uint8 [1]_          Dataset
-np.uint8       0.1                                    Dataset
-np.uint16      0.1                                    Dataset
-np.uint32      0.1                                    Dataset
-np.uint64      0.1                                    Dataset
-np.uint8       0.1                                    Dataset
-np.int16       0.1                                    Dataset
-np.int32       0.1                                    Dataset
-np.int64       0.1                                    Dataset
-np.float16     0.1                                    Dataset
-np.float32     0.1                                    Dataset
-np.float64     0.1                                    Dataset
-np.complex64   0.1                                    Dataset
-np.complex128  0.1                                    Dataset
-np.str\_       0.1      np.uint32/16 [2]_             Dataset
-np.bytes\_     0.1      np.bytes\_ or np.uint16 [3]_  Dataset
-np.object\_    0.1                                    Dataset
-=============  =======  ============================  ================
+=============  =======  =================================  ================
+Type           Version  Converted to                       Group or Dataset
+=============  =======  =================================  ================
+bool           0.1      np.bool\_ or np.uint8 [1]_         Dataset
+None           0.1      ``np.float64([])``                 Dataset
+int            0.1      np.int64                           Dataset
+float          0.1      np.float64                         Dataset
+complex        0.1      np.complex128                      Dataset
+str            0.1      np.uint32/16 [2]_                  Dataset
+bytes          0.1      np.bytes\_ or np.uint16 [3]_       Dataset
+bytearray      0.1      np.bytes\_ or np.uint16 [3]_       Dataset
+list           0.1      np.object\_                        Dataset
+tuple          0.1      np.object\_                        Dataset
+set            0.1      np.object\_                        Dataset
+frozenset      0.1      np.object\_                        Dataset
+cl.deque       0.1      np.object\_                        Dataset
+dict [4]_      0.1                                         Group
+np.bool\_      0.1      not or np.uint8 [1]_               Dataset
+np.uint8       0.1                                         Dataset
+np.uint16      0.1                                         Dataset
+np.uint32      0.1                                         Dataset
+np.uint64      0.1                                         Dataset
+np.uint8       0.1                                         Dataset
+np.int16       0.1                                         Dataset
+np.int32       0.1                                         Dataset
+np.int64       0.1                                         Dataset
+np.float16     0.1                                         Dataset
+np.float32     0.1                                         Dataset
+np.float64     0.1                                         Dataset
+np.complex64   0.1                                         Dataset
+np.complex128  0.1                                         Dataset
+np.str\_       0.1      np.uint32/16 [2]_                  Dataset
+np.bytes\_     0.1      np.bytes\_ or np.uint16 [3]_       Dataset
+np.object\_    0.1                                         Dataset
+np.chararray   0.1      np.bytes\_ or np.uint16/32 [2,3]_  Dataset
+=============  =======  =================================  ================
 
 .. [1] Depends on the selected options. Always ``np.uint8`` when
        ``convert_bools_to_uint8 == True`` (set implicitly when
@@ -155,6 +156,9 @@ np.complex128  'numpy.complex128'   'complex128'                 'double'
 np.str\_       'numpy.str\_'        'str#' [5]_                  'char' or 'uint32'  2 or 4 [6]_
 np.bytes\_     'numpy.bytes\_'      'bytes#' [5]_                'char'              2
 np.object\_    'numpy.object\_'     'object'                     'cell'
+np.ndarray     'numpy.ndarray'      [7]_                         [7]_
+np.matrix      'numpy.matrix'       [7]_                         [7]_
+np.chararray   'numpy.chararray'    [7]_                         'char' [7]_
 =============  ===================  ===========================  ==================  =================
 
 .. [5] '#' is replaced by the number of bits taken up by the string, or
@@ -163,6 +167,9 @@ np.object\_    'numpy.object\_'     'object'                     'cell'
        respectively.
 .. [6] ``2`` if it is stored as ``np.uint16`` or ``4`` if ``np.uint32``.
 
+.. [7] The value that would be put in for a scalar of the same dtype is
+       used.
+
 
 Python.Shape
 ------------
@@ -181,12 +188,12 @@ Python.numpy.Container
 
 Python Attribute
 
-{'scalar', 'ndarray', 'matrix'}
+{'scalar', 'ndarray', 'matrix', 'chararray'}
 
 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``), or a matrix (type
-is ``np.matrix``) is stored in this Attribute.
+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.
 
 Python.Empty and MATLAB_empty
 -----------------------------
@@ -368,8 +375,8 @@ type they are read as if there is no Python metadata attached to them.
 MATLAB Class  Version  Python Type
 ============  =======  ================================
 logical       0.1      np.bool\_
-single        0.1      np.float32 or np.complex64 [7]_
-double        0.1      np.float64 or np.complex128 [7]_
+single        0.1      np.float32 or np.complex64 [8]_
+double        0.1      np.float64 or np.complex128 [8]_
 uint8         0.1      np.uint8
 uint16        0.1      np.uint16
 uint32        0.1      np.uint32
@@ -378,9 +385,9 @@ int8          0.1      np.int8
 int16         0.1      np.int16
 int32         0.1      np.int32
 int64         0.1      np.int64
-struct        0.1      dict [8]_
+struct        0.1      dict [9]_
 cell          0.1      np.object\_
 ============  =======  ================================
 
-.. [7] Depends on whether there is a complex part or not.
-.. [8] Structure arrays are not supported.
+.. [8] Depends on whether there is a complex part or not.
+.. [9] Structure arrays are not supported.
diff --git a/hdf5storage/Marshallers.py b/hdf5storage/Marshallers.py
index a302c97..0ffbd7f 100644
--- a/hdf5storage/Marshallers.py
+++ b/hdf5storage/Marshallers.py
@@ -310,6 +310,7 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
         self.matlab_attributes |= {'MATLAB_class', 'MATLAB_empty',
                                    'MATLAB_int_decode'}
         self.types = [np.ndarray, np.matrix,
+                      np.chararray,
                       np.bool_,
                       np.uint8, np.uint16, np.uint32, np.uint64,
                       np.int8, np.int16, np.int32, np.int64,
@@ -317,6 +318,7 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                       np.complex64, np.complex128,
                       np.bytes_, np.str_, np.object_]
         self.python_type_strings = ['numpy.ndarray', 'numpy.matrix',
+                                    'numpy.chararray',
                                     'numpy.bool_',
                                     'numpy.uint8', 'numpy.uint16',
                                     'numpy.uint32', 'numpy.uint64',
@@ -528,8 +530,8 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
         # information.
 
         # If we are storing python information; the shape, underlying
-        # numpy type, and its type of container ('scalar', 'ndarray', or
-        # 'matrix') need to be stored.
+        # numpy type, and its type of container ('scalar', 'ndarray',
+        # 'matrix', or 'chararray') need to be stored.
 
         if options.store_python_metadata:
             set_attribute(grp[name], 'Python.Shape',
@@ -539,6 +541,8 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                                  data.dtype.name)
             if isinstance(data, np.matrix):
                 container = 'matrix'
+            elif isinstance(data, np.chararray):
+                container = 'chararray'
             elif isinstance(data, np.ndarray):
                 container = 'ndarray'
             else:
@@ -711,9 +715,10 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                     and np.prod(shape) == np.prod(data.shape):
                 data = data.reshape(tuple(shape))
 
-            # Convert to scalar, matrix, 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 problems.
+            # 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
+            # problems.
             if container == 'scalar':
                 if underlying_type.startswith('bytes'):
                     if python_empty == 1:
@@ -729,6 +734,8 @@ class NumpyScalarArrayMarshaller(TypeMarshaller):
                     data = data.flatten()[0]
             elif container == 'matrix':
                 data = np.asmatrix(data)
+            elif container == 'chararray':
+                data = data.view(np.chararray)
             elif container == 'ndarray':
                 data = np.asarray(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