[python-hdf5storage] 81/152: Added write readback tests to file with no python type information stored (either no information or just matlab information).

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:36 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 d46aae8df14a619a6510812816751cfa3987118f
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Feb 2 05:22:06 2014 -0500

    Added write readback tests to file with no python type information stored (either no information or just matlab information).
---
 tests/test_write_readback.py | 166 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 165 insertions(+), 1 deletion(-)

diff --git a/tests/test_write_readback.py b/tests/test_write_readback.py
index 4b9173c..3ad5956 100644
--- a/tests/test_write_readback.py
+++ b/tests/test_write_readback.py
@@ -160,7 +160,7 @@ class TestPythonMatlabFormat(object):
         else:
             assert a.dtype == b.dtype
             assert a.shape == b.shape
-            if a.dtype.name != 'object':
+            if b.dtype.name != 'object':
                 npt.assert_equal(a, b)
             else:
                 for index, x in np.ndenumerate(a):
@@ -345,3 +345,167 @@ class TestPythonFormat(TestPythonMatlabFormat):
         TestPythonMatlabFormat.__init__(self)
         self.options = hdf5storage.Options(matlab_compatible=False)
         self.filename = 'data.h5'
+
+
+class TestNoneFormat(TestPythonMatlabFormat):
+    def __init__(self):
+        # The parent does most of the setup. All that has to be changed
+        # is turning off the storage of type information as well as
+        # MATLAB compatibility.
+        TestPythonMatlabFormat.__init__(self)
+        self.options = hdf5storage.Options(store_type_information=False,
+                                           matlab_compatible=False)
+
+    def assert_equal(self, a, b):
+        # Compares a and b for equality. b is always the original. If
+        # the original is not a numpy type (isn't or doesn't inherit
+        # from np.generic or np.ndarray), then it is a matter of
+        # converting it to the appropriate numpy type. Otherwise, both
+        # are supposed to be numpy types. For object arrays, each
+        # element must be iterated over to be compared. Then, if it
+        # isn't a string type, then they must have the same dtype,
+        # shape, and all elements. If it is an empty string, then it
+        # would have been stored as just a null byte (recurse to do that
+        # comparison). If it is a bytes_ type, the dtype, shape, and
+        # elements must all be the same. If it is string_ type, we must
+        # convert to uint32 and then everything can be compared.
+        if not isinstance(b, (np.generic, np.ndarray)):
+            if b is None:
+                # It should be np.float64([])
+                assert type(a) == np.ndarray
+                assert a.dtype == np.float64([]).dtype
+                assert a.shape == (0, )
+            elif isinstance(b, (bytes, str, bytearray)):
+                assert a == np.bytes_(b)
+            else:
+                self.assert_equal(a, \
+                    np.array(b)[()])
+        else:
+            if b.dtype.name != 'object':
+                if b.dtype.char in ('U', 'S'):
+                    if b.shape == tuple() and len(b) == 0:
+                        self.assert_equal(a, \
+                            np.zeros(shape=tuple(), dtype=b.dtype.char))
+                    elif b.dtype.char == 'U':
+                        c = np.atleast_1d(b).view(np.uint32)
+                        assert a.dtype == c.dtype
+                        assert a.shape == c.shape
+                        npt.assert_equal(a, c)
+                    else:
+                        assert a.dtype == b.dtype
+                        assert a.shape == b.shape
+                        npt.assert_equal(a, b)
+                else:
+                    assert a.dtype == b.dtype
+                    assert a.shape == b.shape
+                    npt.assert_equal(a, b)
+            else:
+                assert a.dtype == b.dtype
+                assert a.shape == b.shape
+                for index, x in np.ndenumerate(a):
+                    self.assert_equal(a[index], b[index])
+
+    def assert_equal_python_collection(self, a, b, tp):
+        # Compares two python collections that are supposed to be the
+        # specified type tp. As every collection is just getting turned
+        # into a list and then a numpy object array, b must be converted
+        # first before the comparison.
+        c = np.object_(list(b))
+        self.assert_equal(a, c)
+
+
+class TestMatlabFormat(TestNoneFormat):
+    def __init__(self):
+        # The parent does most of the setup. All that has to be changed
+        # is turning on the matlab compatibility, changing the filename,
+        # and removing 'float16' from the dtype list (its not supported
+        # by matlab).
+        TestNoneFormat.__init__(self)
+        self.options = hdf5storage.Options(store_type_information=False,
+                                           matlab_compatible=True)
+        self.filename = 'data.mat'
+        self.dtypes.remove('float16')
+
+    def assert_equal(self, a, b):
+        # Compares a and b for equality. b is always the original. If
+        # the original is not a numpy type (isn't or doesn't inherit
+        # from np.generic or np.ndarray), then it is a matter of
+        # converting it to the appropriate numpy type. Otherwise, both
+        # are supposed to be numpy types. For object arrays, each
+        # element must be iterated over to be compared. Then, if it
+        # isn't a string type, then they must have the same dtype,
+        # shape, and all elements. All strings are converted to
+        # numpy.str_ on read. If it is empty, it has shape (1, 0). A
+        # numpy.str_ has all of its strings per row compacted
+        # together. A numpy.bytes_ string has to have the same thing
+        # done, but then it needs to be converted up to UTF-32 and to
+        # numpy.str_ through uint32.
+        #
+        # In all cases, we expect things to be at least two dimensional
+        # arrays.
+        if not isinstance(b, (np.generic, np.ndarray)):
+            if b is None:
+                # It should be np.zeros(shape=(0, 1), dtype='float64'))
+                assert type(a) == np.ndarray
+                assert a.dtype == np.dtype('float64')
+                assert a.shape == (1, 0)
+            elif isinstance(b, (bytes, str, bytearray)):
+                if len(b) == 0:
+                    TestPythonMatlabFormat.assert_equal(self, a, \
+                        np.zeros(shape=(1, 0), dtype='U'))
+                elif isinstance(b, (bytes, bytearray)):
+                    TestPythonMatlabFormat.assert_equal(self, a, \
+                        np.atleast_2d(np.str_(b.decode())))
+                else:
+                    TestPythonMatlabFormat.assert_equal(self, a, \
+                        np.atleast_2d(np.str_(b)))
+            else:
+                TestPythonMatlabFormat.assert_equal(self, a, \
+                    np.atleast_2d(np.array(b)))
+        else:
+            if b.dtype.name != 'object':
+                if b.dtype.char in ('U', 'S'):
+                    if len(b) == 0 and (b.shape == tuple() \
+                            or b.shape == (0, )):
+                        TestPythonMatlabFormat.assert_equal(self, a, \
+                            np.zeros(shape=(1, 0), \
+                            dtype='U'))
+                    elif b.dtype.char == 'U':
+                        c = np.atleast_1d(b)
+                        c = np.atleast_2d(c.view(np.dtype('U' \
+                            + str(c.shape[-1]*c.dtype.itemsize//4))))
+                        assert a.dtype == c.dtype
+                        assert a.shape == c.shape
+                        npt.assert_equal(a, c)
+                    elif b.dtype.char == 'S':
+                        c = np.atleast_1d(b)
+                        c = c.view(np.dtype('S' \
+                            + str(c.shape[-1]*c.dtype.itemsize)))
+                        c = np.uint32(c.view(np.dtype('uint8')))
+                        c = c.view(np.dtype('U' + str(c.shape[-1])))
+                        c = np.atleast_2d(c)
+                        assert a.dtype == c.dtype
+                        assert a.shape == c.shape
+                        npt.assert_equal(a, c)
+                        pass
+                    else:
+                        c = np.atleast_2d(b)
+                        assert a.dtype == c.dtype
+                        assert a.shape == c.shape
+                        npt.assert_equal(a, c)
+                else:
+                    c = np.atleast_2d(b)
+                    # An empty complex number gets turned into a real
+                    # number when it is stored.
+                    if np.prod(c.shape) == 0 \
+                            and b.dtype.name.startswith('complex'):
+                        c = np.real(c)
+                    assert a.dtype == c.dtype
+                    assert a.shape == c.shape
+                    npt.assert_equal(a, c)
+            else:
+                c = np.atleast_2d(b)
+                assert a.dtype == c.dtype
+                assert a.shape == c.shape
+                for index, x in np.ndenumerate(a):
+                    self.assert_equal(a[index], c[index])

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