diff --git a/mat73/core.py b/mat73/core.py index 219df75..3db30a9 100644 --- a/mat73/core.py +++ b/mat73/core.py @@ -305,25 +305,14 @@ def convert_mat(self, dataset, depth, MATLAB_class=None): def squeeze(arr): - # Assumption: "superfluous" singular dimensions come at the beginning or the end - - shape = arr.shape - if len(shape) == 0: - return arr - - # Remove leading dimensions of size 1 - while len(shape) > 1 and shape[0] == 1: - shape = shape[1:] - - # Remove trailing dimensions of size 1 - while len(shape) > 1 and shape[-1] == 1: - shape = shape[:-1] - - # If all remaining dimensions are of size 1, reduce to 0-dimensional - if len(shape) == 1 and shape[0] == 1: - shape = () - - return arr.reshape(shape) + """Vectors are saved as 2D matrices in MATLAB, however in numpy + there is no distinction between a column and a row vector. + Therefore, remove superfluous dimensions if the array is 2D and + one of the dimensions is singular""" + if arr.ndim==2 and 1 in arr.shape: + return arr.reshape([x for x in arr.shape if x>1]) + return arr + def loadmat(file, use_attrdict=False, only_include=None, verbose=True): """ diff --git a/mat73/version.py b/mat73/version.py index e7b3f51..0dd5908 100644 --- a/mat73/version.py +++ b/mat73/version.py @@ -1 +1 @@ -__version__ = '0.64' +__version__ = '0.65' diff --git a/tests/create_mat.m b/tests/create_mat.m index c30dd17..ab8cc23 100644 --- a/tests/create_mat.m +++ b/tests/create_mat.m @@ -46,4 +46,22 @@ A = sparse([0 0 0; 0 0 0]); save('testfile13.mat','-v7.3') clear all -%% \ No newline at end of file +%% third file: create a file to test all kind of empty dimensions that migh pop up + +x_0 = rand(0) +x_1 = rand(1) +x_10 = 1:10 +x_1_0 = rand(1, 0) +x_0_1 = rand(0, 1) +x_1_1 = rand(1, 1) +x_0_10 = rand(0, 10) +x_1_10 = rand(1, 10) +x_10_0 = rand(10, 0) +x_10_1 = rand(10, 1) +x_10_10 = rand(10, 10) +x_1_1_10_1_1 = rand(1, 1, 10, 1, 1) +x_10_1_1_10 = rand(10, 1, 1, 10) +save('testfile15.mat','-v7.3') +clear all + + diff --git a/tests/test_mat73.py b/tests/test_mat73.py index e032f38..5302102 100644 --- a/tests/test_mat73.py +++ b/tests/test_mat73.py @@ -15,6 +15,7 @@ version = pkg_resources.get_distribution('mat73').version except: version = '0.00' + try: repo = Repository('.') head = repo.head @@ -34,7 +35,7 @@ class Testing(unittest.TestCase): def setUp(self): """make links to test files and make sure they are present""" - for i in range(1, 15): + for i in range(1, 16): file = 'testfile{}.mat'.format(i) if not os.path.exists(file): file = os.path.join('./tests', file) @@ -476,6 +477,39 @@ def test_file14_n_d_array_with_singular_axis(self): # Test that the singular dimension is preserved self.assertEqual(arr.shape[1], 1) + + + def test_file15_strip(self): + """Test loading of n-D array with one dimension of size 1""" + + # the array was created in Matlab like so: + # data = reshape(1:24, [3, 1, 4, 2]); + + data = mat73.loadmat(self.testfile15) + + + self.assertEqual(data['x_0'], None) + self.assertEqual(data['x_1_0'], None) + self.assertEqual(data['x_0_1'], None) + self.assertEqual(data['x_0_10'], None) + self.assertEqual(data['x_10_0'], None) + + expected = {'x_1' : (), + 'x_10' : (10,), + 'x_1_1': (), + 'x_1_10': (10,), + 'x_10_1': (10,), + 'x_10_10': (10, 10), + 'x_1_1_10_1_1': (1, 1, 10), + 'x_10_1_1_10': (10, 1, 1, 10), + } + + + for var, shape in expected.items(): + self.assertEqual(data[var].shape, shape) + self.assertEqual(data[var].ndim, len(shape)) + + if __name__ == '__main__': unittest.main() diff --git a/tests/testfile1.mat b/tests/testfile1.mat index 6f674a7..f0c33cb 100644 Binary files a/tests/testfile1.mat and b/tests/testfile1.mat differ diff --git a/tests/testfile13.mat b/tests/testfile13.mat index 3a64252..51c70cb 100644 Binary files a/tests/testfile13.mat and b/tests/testfile13.mat differ diff --git a/tests/testfile15.mat b/tests/testfile15.mat new file mode 100644 index 0000000..a306375 Binary files /dev/null and b/tests/testfile15.mat differ