Skip to content

Commit

Permalink
Add unittests for empty sparse matrix loading (#58)
Browse files Browse the repository at this point in the history
* Fixed issue with loading all-zero sparse matrices, as the data and ir pointers are not stored in the .mat file.

* add unittest

---------

Co-authored-by: S. Breedveld <[email protected]>
  • Loading branch information
skjerns and S. Breedveld authored Jul 18, 2024
1 parent ac31ea7 commit b8dc56c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 5 additions & 3 deletions tests/create_mat.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@

clear all

%% second created file
%% second created file for https://github.com/skjerns/mat7.3/pull/57

data.x1 = {'test', 'asd'};
data.x2 = {{1,2,3}, {1,2,3}};
A = sparse([0 0 0; 0 0 0]);
save('testfile13.mat','-v7.3')
clear all
%%
15 changes: 14 additions & 1 deletion tests/test_mat73.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Testing(unittest.TestCase):

def setUp(self):
"""make links to test files and make sure they are present"""
for i in range(1, 13):
for i in range(1, 14):
file = 'testfile{}.mat'.format(i)
if not os.path.exists(file):
file = os.path.join('./tests', file)
Expand Down Expand Up @@ -425,6 +425,19 @@ def test_file12_sparse_matrix_fix(self):
assert isinstance(struct_sparse[0]['stim_pattern'], sparse_type)
assert isinstance(struct_sparse[0]['stimulation'], str)

def test_file13_empty_sparse_matrix(self):
"""some sparse matrices could not be loaded. check if it now works"""
# check regular loading works
import scipy
data = mat73.loadmat(self.testfile13)
struct_sparse = data['A']
sparse_type = scipy.sparse.csc_matrix
assert isinstance(struct_sparse, sparse_type)
assert struct_sparse.getnnz()==0
assert struct_sparse.sum()==0
assert (struct_sparse.toarray()==np.zeros([2,3])).all()
assert struct_sparse.shape==(2, 3)


if __name__ == '__main__':

Expand Down
Binary file added tests/testfile13.mat
Binary file not shown.

0 comments on commit b8dc56c

Please sign in to comment.