Skip to content

Commit

Permalink
Fix empty dimensions (#60)
Browse files Browse the repository at this point in the history
* only squeeze singular dimensions if array is 2D

* increment version
  • Loading branch information
skjerns authored Jul 24, 2024
1 parent 59fc821 commit 558a37f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 22 deletions.
27 changes: 8 additions & 19 deletions mat73/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion mat73/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.64'
__version__ = '0.65'
20 changes: 19 additions & 1 deletion tests/create_mat.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@
A = sparse([0 0 0; 0 0 0]);
save('testfile13.mat','-v7.3')
clear all
%%
%% 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


36 changes: 35 additions & 1 deletion tests/test_mat73.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
version = pkg_resources.get_distribution('mat73').version
except:
version = '0.00'

try:
repo = Repository('.')
head = repo.head
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Binary file modified tests/testfile1.mat
Binary file not shown.
Binary file modified tests/testfile13.mat
Binary file not shown.
Binary file added tests/testfile15.mat
Binary file not shown.

0 comments on commit 558a37f

Please sign in to comment.