[python-hdf5storage] 22/84: Fixed bugs in tests where structured numpy.ndarrays which were read right were found to be inequal.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:59 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 506fbf7424d70529280976a046d4b8fd9f913fb2
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Sep 14 20:13:48 2014 -0400

    Fixed bugs in tests where structured numpy.ndarrays which were read right were found to be inequal.
---
 tests/asserts.py             | 22 +++++++++++++++++++---
 tests/test_write_readback.py | 24 ++++++++++++++++--------
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/tests/asserts.py b/tests/asserts.py
index c8d945a..eba40e1 100644
--- a/tests/asserts.py
+++ b/tests/asserts.py
@@ -227,9 +227,25 @@ def assert_equal_matlab_format(a, b):
                 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)
+                # If it is structured, check that the field names are
+                # the same, in the same order, and then go through them
+                # one by one. Otherwise, make sure the dtypes and shapes
+                # are the same before comparing all values.
+                if b.dtype.names is None and a.dtype.names is None:
+                    assert a.dtype == c.dtype
+                    assert a.shape == c.shape
+                    npt.assert_equal(a, c)
+                else:
+                    assert a.dtype.names is not None
+                    assert b.dtype.names is not None
+                    assert set(a.dtype.names) == set(b.dtype.names)
+                    assert a.dtype.names == b.dtype.names
+                    a = a.flatten()
+                    b = b.flatten()
+                    for k in b.dtype.names:
+                        for index, x in np.ndenumerate(a):
+                            assert_equal_from_matlab(a[k][index],
+                                                     b[k][index])
         else:
             c = np.atleast_2d(b)
             assert a.dtype == c.dtype
diff --git a/tests/test_write_readback.py b/tests/test_write_readback.py
index 4443904..d610388 100644
--- a/tests/test_write_readback.py
+++ b/tests/test_write_readback.py
@@ -80,6 +80,7 @@ class TestPythonMatlabFormat(object):
         self.min_structured_ndarray_fields = 2
         self.max_structured_ndarray_fields = 5
         self.max_structured_ndarray_field_lengths = 10
+        self.max_structured_ndarray_axis_length = 2
         self.structured_ndarray_subarray_dimensions = 2
         self.max_structured_ndarray_subarray_axis_length = 4
         
@@ -120,7 +121,7 @@ class TestPythonMatlabFormat(object):
         return random.uniform(-1.0, 1.0) \
             * 10.0**random.randint(-300, 300)
 
-    def random_numpy(self, shape, dtype):
+    def random_numpy(self, shape, dtype, allow_nan=True):
         # Makes a random numpy array of the specified shape and dtype
         # string. The method is slightly different depending on the
         # type. For 'bytes', 'str', and 'object'; an array of the
@@ -157,7 +158,14 @@ class TestPythonMatlabFormat(object):
             if dtype == 'bool':
                 bts = b''.join([{True: b'\x01', False: b'\x00'}[ \
                     ch > 127] for ch in bts])
-            return np.ndarray(shape=shape, dtype=dtype, buffer=bts)
+            data = np.ndarray(shape=shape, dtype=dtype, buffer=bts)
+            # If it is a floating point type and we are supposed to
+            # remove NaN's, then turn them to zeros.
+            if not allow_nan and data.dtype.kind in ('f', 'c') \
+                and np.any(np.isnan(data)):
+                data = data.copy()
+                data[np.isnan(data)] = 0.0
+            return data
 
     def random_numpy_scalar(self, dtype):
         # How a random scalar is made depends on th type. For must, it
@@ -243,8 +251,8 @@ class TestPythonMatlabFormat(object):
             data = np.empty(shape=shape, dtype=dt)
             for index, x in np.ndenumerate(data):
                 for i, name in enumerate(names):
-                    data[name][index] = self.random_numpy(shapes[i],
-                                                          dtypes[i])
+                    data[name][index] = self.random_numpy(shapes[i], \
+                        dtypes[i], allow_nan=False)
             return data
 
     def random_name(self):
@@ -307,8 +315,8 @@ class TestPythonMatlabFormat(object):
     def check_numpy_structured_array(self, dimensions):
         # Makes a random structured ndarray of the given type, writes it
         # and reads it back, and then compares it.
-        shape = self.random_numpy_shape(dimensions,
-                                        self.max_array_axis_length)
+        shape = self.random_numpy_shape(dimensions, \
+            self.max_structured_ndarray_axis_length)
         data = self.random_structured_numpy_array(shape)
         out = self.write_readback(data, self.random_name(),
                                   self.options)
@@ -317,8 +325,8 @@ class TestPythonMatlabFormat(object):
     def check_numpy_structured_array_empty(self, dimensions):
         # Makes a random structured ndarray of the given type, writes it
         # and reads it back, and then compares it.
-        shape = self.random_numpy_shape(dimensions,
-                                        self.max_array_axis_length)
+        shape = self.random_numpy_shape(dimensions, \
+            self.max_structured_ndarray_axis_length)
         data = self.random_structured_numpy_array(shape, (1, 0))
         out = self.write_readback(data, self.random_name(),
                                   self.options)

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