From 802b82a46f1dd5363f1268342266c680d2b29b52 Mon Sep 17 00:00:00 2001 From: Felix Benning Date: Tue, 19 Mar 2024 15:52:03 +0100 Subject: [PATCH 1/2] fix bug #51 --- mat73/core.py | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/mat73/core.py b/mat73/core.py index c44de97..5a5bb61 100644 --- a/mat73/core.py +++ b/mat73/core.py @@ -112,6 +112,32 @@ def unpack_mat(self, hdf5, depth=0, MATLAB_class=None, force=False): """ if depth==99: raise RecursionError("Maximum number of 99 recursions reached.") + # sparse elements need to be loaded separately + # see https://github.com/skjerns/mat7.3/issues/28 + if 'MATLAB_sparse' in hdf5.attrs: + try: + from scipy.sparse import csc_matrix + data = unpacked['data'] + row_ind = unpacked['ir'] + col_ind = unpacked['jc'] + n_rows = hdf5.attrs['MATLAB_sparse'] + n_cols = len(col_ind) - 1 + unpacked = csc_matrix((data, row_ind, col_ind), shape=(n_rows, n_cols)) + except ModuleNotFoundError: + logging.error(f'`scipy` not installed. To load the sparse matrix' + f' `{hdf5.name}`,' + ' you need to have scipy installed. Please install' + ' via `pip install scipy`') + except DeprecationWarning: + logging.error(f'Tried loading the sparse matrix `{hdf5.name}`' + ' with scipy, but' + ' the interface has been deprecated. Please' + ' raise this error as an issue on GitHub:' + ' https://github.com/skjerns/mat7.3/issues') + except KeyError as e: + logging.error(f'Tried loading the sparse matrix `{hdf5.name}`' + ' but something went wrong: {e}\n{e.__traceback__}') + raise e if isinstance(hdf5, (h5py._hl.group.Group)): d = self._dict_class() @@ -125,35 +151,9 @@ def unpack_mat(self, hdf5, depth=0, MATLAB_class=None, force=False): MATLAB_class = MATLAB_class.decode() unpacked = self.unpack_mat(elem, depth=depth+1, MATLAB_class=MATLAB_class) - # sparse elements need to be loaded separately - # see https://github.com/skjerns/mat7.3/issues/28 - if 'MATLAB_sparse' in elem.attrs: - try: - from scipy.sparse import csc_matrix - data = unpacked['data'] - row_ind = unpacked['ir'] - col_ind = unpacked['jc'] - n_rows = elem.attrs['MATLAB_sparse'] - n_cols = len(col_ind) - 1 - unpacked = csc_matrix((data, row_ind, col_ind), shape=(n_rows, n_cols)) - except ModuleNotFoundError: - logging.error(f'`scipy` not installed. To load the sparse matrix' - f' `{elem.name}`,' - ' you need to have scipy installed. Please install' - ' via `pip install scipy`') - except DeprecationWarning: - logging.error(f'Tried loading the sparse matrix `{elem.name}`' - ' with scipy, but' - ' the interface has been deprecated. Please' - ' raise this error as an issue on GitHub:' - ' https://github.com/skjerns/mat7.3/issues') - except KeyError as e: - logging.error(f'Tried loading the sparse matrix `{elem.name}`' - ' but something went wrong: {e}\n{e.__traceback__}') - raise e - - - elif MATLAB_class=='struct' and len(elem)>1 and \ + + + if MATLAB_class=='struct' and len(elem)>1 and \ isinstance(unpacked, dict): values = unpacked.values() # we can only pack them together in MATLAB style if From 99aa844c1b00ff2148774f4dc3f9d40f0a7dcab2 Mon Sep 17 00:00:00 2001 From: Felix Benning Date: Tue, 19 Mar 2024 16:06:11 +0100 Subject: [PATCH 2/2] fix dangling variable --- mat73/core.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mat73/core.py b/mat73/core.py index 5a5bb61..b1c6e11 100644 --- a/mat73/core.py +++ b/mat73/core.py @@ -112,17 +112,18 @@ def unpack_mat(self, hdf5, depth=0, MATLAB_class=None, force=False): """ if depth==99: raise RecursionError("Maximum number of 99 recursions reached.") - # sparse elements need to be loaded separately + + # sparse elements need to be loaded separately (recursion end) # see https://github.com/skjerns/mat7.3/issues/28 if 'MATLAB_sparse' in hdf5.attrs: try: from scipy.sparse import csc_matrix - data = unpacked['data'] - row_ind = unpacked['ir'] - col_ind = unpacked['jc'] + data = hdf5['data'] + row_ind = hdf5['ir'] + col_ind = hdf5['jc'] n_rows = hdf5.attrs['MATLAB_sparse'] n_cols = len(col_ind) - 1 - unpacked = csc_matrix((data, row_ind, col_ind), shape=(n_rows, n_cols)) + return csc_matrix((data, row_ind, col_ind), shape=(n_rows, n_cols)) except ModuleNotFoundError: logging.error(f'`scipy` not installed. To load the sparse matrix' f' `{hdf5.name}`,' @@ -138,6 +139,7 @@ def unpack_mat(self, hdf5, depth=0, MATLAB_class=None, force=False): logging.error(f'Tried loading the sparse matrix `{hdf5.name}`' ' but something went wrong: {e}\n{e.__traceback__}') raise e + if isinstance(hdf5, (h5py._hl.group.Group)): d = self._dict_class()