[python-hdf5storage] 145/152: Added tests for reading data written by MATLAB.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:24:43 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 b338a43ae8ebd9a08d8acdd1144b5c59efd0895c
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sun Feb 16 04:04:21 2014 -0500

    Added tests for reading data written by MATLAB.
---
 tests/asserts.py                |  34 ++++++++++++
 tests/make_mat_with_all_types.m | 113 ++++++++++++++++++++++++++++++++++++++++
 tests/test_read_from_matlab.py  |  70 +++++++++++++++++++++++++
 3 files changed, 217 insertions(+)

diff --git a/tests/asserts.py b/tests/asserts.py
index 81e6725..cb414c0 100644
--- a/tests/asserts.py
+++ b/tests/asserts.py
@@ -221,3 +221,37 @@ def assert_equal_matlab_format(a, b):
             assert a.shape == c.shape
             for index, x in np.ndenumerate(a):
                 assert_equal_matlab_format(a[index], c[index])
+
+
+def assert_equal_from_matlab(a, b):
+    # Compares a and b for equality. They are all going to be numpy
+    # types. hdf5storage and scipy behave differently when importing
+    # arrays as to whether they are 2D or not, so we will make them all
+    # at least 2D regardless. For strings, the two packages produce
+    # transposed results of each other, so one just needs to be
+    # transposed. For object arrays, each element must be iterated over
+    # to be compared. For structured ndarrays, their fields need to be
+    # compared and then they can be compared element and field
+    # wise. Otherwise, they can be directly compared. Note, the type is
+    # often converted by scipy (or on route to the file before scipy
+    # gets it), so comparisons are done by value, which is not perfect.
+    a = np.atleast_2d(a)
+    b = np.atleast_2d(b)
+    if a.dtype.char == 'U':
+        a = a.T
+    if b.dtype.name == 'object':
+        a = a.flatten()
+        b = b.flatten()
+        for index, x in np.ndenumerate(a):
+            assert_equal_from_matlab(a[index], b[index])
+    elif b.dtype.names is not None or a.dtype.names is not None:
+        assert a.dtype.names is not None
+        assert b.dtype.names is not None
+        assert set(a.dtype.names) == set(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:
+        npt.assert_equal(a, b)
diff --git a/tests/make_mat_with_all_types.m b/tests/make_mat_with_all_types.m
new file mode 100644
index 0000000..588e1a4
--- /dev/null
+++ b/tests/make_mat_with_all_types.m
@@ -0,0 +1,113 @@
+
+clear a
+
+% Main types as scalars and arrays.
+
+a.logical = true;
+
+a.uint8 = uint8(2);
+a.uint16 = uint16(28);
+a.uint32 = uint32(28347394);
+a.uint64 = uint64(234392);
+
+a.int8 = int8(-32);
+a.int16 = int16(284);
+a.int32 = int32(-7394);
+a.int64 = int64(2334322);
+
+a.single = single(4.2134e-2);
+a.single_complex = single(33.4 + 3i);
+a.single_nan = single(NaN);
+a.single_inf = single(inf);
+
+a.double = 14.2134e200;
+a.double_complex = 8e-30 - 3.2e40i;
+a.double_nan = NaN;
+a.double_inf = -inf;
+
+a.char = 'p';
+
+a.logical_array = logical([1 0 0 0; 0 1 1 0]);
+
+a.uint8_array = uint8([0 1 3 4; 92 3 2 8]);
+a.uint16_array = uint16([0 1; 3 4; 92 3; 2 8]);
+a.uint32_array = uint32([0 1 3 4 92 3 2 8]);
+a.uint64_array = uint64([0; 1; 3; 4; 92; 3; 2; 8]);
+
+a.int8_array = int8([0 1 3 4; 92 3 2 8]);
+a.int16_array = int16([0 1; 3 4; 92 3; 2 8]);
+a.int32_array = int32([0 1 3 4 92 3 2 8]);
+a.int64_array = int64([0; 1; 3; 4; 92; 3; 2; 8]);
+
+a.single_array = single(rand(4, 9));
+a.single_array_complex = single(rand(2,7) + 1i*rand(2,7));
+
+a.double_array = rand(3, 2);
+a.double_array_complex = rand(5,2) + 1i*rand(5,2);
+
+a.char_array = ['ivkea'; 'avvai'];
+a.char_cell_array = {'v83nv', 'aADvai98v3'};
+
+% Empties of main types.
+
+a.logical_empty = logical([]);
+a.uint8_empty = uint8([]);
+a.uint16_empty = uint16([]);
+a.uint32_empty = uint32([]);
+a.uint64_empty = uint64([]);
+a.int8_empty = int8([]);
+a.int16_empty = int16([]);
+a.int32_empty = int32([]);
+a.int64_empty = int64([]);
+a.single_empty = single([]);
+a.double_empty = [];
+
+% Main container types.
+
+a.cell = {5.34+9i};
+a.cell_array = {1, [2 3]; 8.3, -[3; 3]; [], 20};
+a.cell_empty = {};
+
+a.struct = struct('a', {3.3}, 'bc', {[1 4 5]});
+a.struct_empty = struct('vea', {}, 'b', {});
+a.struct_array = struct('a', {3.3; 3}, 'avav_Ab', {[1 4 5]; []});
+
+% % Function handles.
+% 
+% ab = 1:6;
+% a.fhandle = @sin;
+% a.fhandle_args = @(x, y) x .* cos(y);
+% a.fhandle_args_environment = @(m, n) m*(b.*rand(size(b))) + n;
+% 
+% % Map type.
+% 
+% a.map_char = containers.Map({'4v', 'u', '2vn'}, {4, uint8(9), 'bafd'});
+% a.map_single = containers.Map({single(3), single(38.3), single(2e-3)}, {4, uint8(9), 'bafd'});
+% a.map_empty = containers.Map;
+% 
+% % The categorical type.
+% 
+% b = {'small', 'medium', 'small', 'medium', 'medium', 'large', 'medium'};
+% c = {'small', 'medium', 'large'};
+% d = round(2*rand(10,3));
+% 
+% a.categorical = categorical(b);
+% a.categorical_ordinal = categorical(b, c, 'Ordinal', true);
+% a.categorical_ordinal_int = categorical(d, 0:2, c, 'Ordinal', true);
+% 
+% a.categorical_empty = categorical({});
+% a.categorical_ordinal_empty = categorical({}, c, 'Ordinal', true);
+% a.categorical_ordinal_int_empty = categorical([], 0:2, c, 'Ordinal', true);
+% 
+% % Tables.
+% 
+% a.table = readtable('patients.dat');
+% a.table_oneentry = a.table(1,:);
+% a.table_empty = a.table([], :);
+% 
+% % Not doing time series yet.
+
+save('types_v7p3.mat','-struct','a','-v7.3')
+save('types_v7.mat','-struct','a','-v7')
+
+exit
diff --git a/tests/test_read_from_matlab.py b/tests/test_read_from_matlab.py
new file mode 100644
index 0000000..66be367
--- /dev/null
+++ b/tests/test_read_from_matlab.py
@@ -0,0 +1,70 @@
+# Copyright (c) 2014, Freja Nordsiek
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import os
+import os.path
+import subprocess
+
+import scipy.io
+
+import hdf5storage
+
+from asserts import *
+
+mat_files = ['types_v7p3.mat', 'types_v7.mat']
+for i in range(0, len(mat_files)):
+    mat_files[i] = os.path.join(os.path.dirname(__file__), mat_files[i])
+
+script_name = os.path.join(os.path.dirname(__file__),
+                           'make_mat_with_all_types.m')
+
+data_v7 = dict()
+data_v7p3 = dict()
+
+
+def setup_module():
+    teardown_module()
+    matlab_command = "run('" + script_name + "')"
+    subprocess.check_output(['matlab', '-nosplash', '-nodesktop',
+                            '-nojvm', '-r', matlab_command])
+    scipy.io.loadmat(file_name=mat_files[1], mdict=data_v7)
+    hdf5storage.loadmat(file_name=mat_files[0], mdict=data_v7p3)
+
+
+def teardown_module():
+    for name in mat_files:
+        if os.path.exists(name):
+            os.remove(name)
+
+
+def test_read():
+    for k in (set(data_v7.keys()) - set(['__version__', '__header__', \
+            '__globals__'])):
+        yield check_variable, k
+
+
+def check_variable(name):
+    assert_equal_from_matlab(data_v7p3[name], data_v7[name])

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