From b8dc56c6825fbc2386dcd5716d18fea9ef86fbaf Mon Sep 17 00:00:00 2001 From: Simon Kern <14980558+skjerns@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:47:19 +0200 Subject: [PATCH] Add unittests for empty sparse matrix loading (#58) * 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 --- tests/create_mat.m | 8 +++++--- tests/test_mat73.py | 15 ++++++++++++++- tests/testfile13.mat | Bin 0 -> 3120 bytes 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 tests/testfile13.mat diff --git a/tests/create_mat.m b/tests/create_mat.m index 99a719b..c30dd17 100644 --- a/tests/create_mat.m +++ b/tests/create_mat.m @@ -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}}; \ No newline at end of file +A = sparse([0 0 0; 0 0 0]); +save('testfile13.mat','-v7.3') +clear all +%% \ No newline at end of file diff --git a/tests/test_mat73.py b/tests/test_mat73.py index 3ee9d2b..0689dd9 100644 --- a/tests/test_mat73.py +++ b/tests/test_mat73.py @@ -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) @@ -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__': diff --git a/tests/testfile13.mat b/tests/testfile13.mat new file mode 100644 index 0000000000000000000000000000000000000000..3a6425201556844ff4a402102799a29f0033fc25 GIT binary patch literal 3120 zcmeHJ%}&BV5T4~n8-oV*Vm!^!1Cka|Ne{*f7%+lFN%W=_OVAWC6y5=E9(@EK!AI~h zOuYCOy4#&4*l0+M!5F(uJF_$U&F;>Aowj1t_pKe=Os`?Zs;@RLT_=S#*X}ht-L{Ff z{BgOukxgN~>)1W#0z20x)?0nt>$_;=&@jxbVR9v-XR=r-?5?BNXgO^gjkK;~8b52G zTBKZ2YAAmv1GoIlW@naX{Mj`PVvv*ziAeEAO;3F6%O1cPDU9TRlsvZO3zx{d3m=NI*`wxqXi7 zL!5VGcRlX*tBgFeZ82&-M;(S8|sC literal 0 HcmV?d00001