Skip to content

Commit

Permalink
Merge pull request #34 from skjerns/add_sparse_csc
Browse files Browse the repository at this point in the history
add sparse matrix support
  • Loading branch information
skjerns authored Apr 18, 2022
2 parents 13b1ebe + 25c0bd4 commit c7dc58c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_distribute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
pip install setuptools wheel twine
- name: Run tests once
run: |
pip install flake8 pytest
pip install flake8 pytest scipy
pip install -e .
pip install numpy h5py
- name: Test with pytest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install flake8 pytest scipy
pip install -e .
pip install numpy h5py
- name: Lint with flake8
Expand Down
34 changes: 29 additions & 5 deletions mat73/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,31 @@ def unpack_mat(self, hdf5, depth=0, MATLAB_class=None):
MATLAB_class = MATLAB_class.decode()
unpacked = self.unpack_mat(elem, depth=depth+1,
MATLAB_class=MATLAB_class)
if MATLAB_class=='struct' and len(elem)>1 and \
# 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')


elif MATLAB_class=='struct' and len(elem)>1 and \
isinstance(unpacked, dict):
values = unpacked.values()
# we can only pack them together in MATLAB style if
Expand Down Expand Up @@ -184,7 +208,6 @@ def convert_mat(self, dataset, depth, MATLAB_class=None):
logging.error(message)
return None

# if has
known_cls = ['cell', 'char', 'bool', 'logical', 'double', 'single',
'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16',
'uint32', 'uint64']
Expand Down Expand Up @@ -303,8 +326,9 @@ def savemat(filename, verbose=True):


if __name__=='__main__':
# d = loadmat('../tests/testfile5.mat', use_attrdict=True)
# for testing / debugging
d = loadmat('../tests/testfile1.mat')


file = '../tests/testfile8.mat'
data = loadmat(file)
# file = '../tests/testfile8.mat'
# data = loadmat(file)
2 changes: 1 addition & 1 deletion mat73/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.58'
__version__ = '0.59'
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
url="https://github.com/skjerns/mat7.3",
download_url=f"https://github.com/skjerns/mat7.3/archive/v{__version__}.tar.gz",
install_requires=['h5py', 'numpy'],
tests_require=['scipy'],
license='MIT',
packages=['mat73'],
classifiers=[
Expand Down
1 change: 1 addition & 0 deletions tests/create_mat.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
data.struct_ = struct('test', [1,2,3,4])
data.struct2_ = struct('type',{'big','little'},'color','red','x',{single([1.1,1.2,0.3;2,3,4]), double([1.1,1.2,0.3])})
data.structarr_ = struct('f1', {'some text'; [10,20,30]; magic(5)}, 'f2', {'v1'; 'v2'; 'v3';});
data.sparse_ = sparse([2, 4], [5, 8], [6, 7], 10, 8);

secondvar = [1,2,3,4]

Expand Down
15 changes: 11 additions & 4 deletions tests/test_mat73.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
name = 'unknown'
message = 'no msg'

EXPECTED_VARS_FILE1 = 30

print(f'#### Installed version: mat73-{version} on {branch}({name}) "{message}" ####')

class Testing(unittest.TestCase):
Expand All @@ -51,7 +53,7 @@ def test_file1_noattr(self):

assert len(d)==3
assert len(d.keys())==3
assert len(data)==29
assert len(data)==EXPECTED_VARS_FILE1
assert data['arr_two_three'].shape==(3,2)
np.testing.assert_allclose(d['secondvar'], [1,2,3,4])
np.testing.assert_array_equal(data['arr_bool'], np.array([True,True,False]))
Expand Down Expand Up @@ -108,6 +110,11 @@ def test_file1_noattr(self):
assert data['structarr_'][1]['f2'] == ['v2']
assert data['structarr_'][2]['f2'] == ['v3']
assert d['keys'] == 'must_not_overwrite'
sparse = data['sparse_'].toarray()
assert sparse[1,4] == 6
assert sparse[3,7] == 7
assert (sparse!=0).sum()==2


with self.assertRaises(AttributeError):
d.structarr_
Expand All @@ -122,7 +129,7 @@ def test_file1_withattr(self):

assert len(d)==3
assert len(d.keys())==3
assert len(data)==29
assert len(data)==EXPECTED_VARS_FILE1
assert data['arr_two_three'].shape==(3,2)
np.testing.assert_allclose(d['secondvar'], [1,2,3,4])
np.testing.assert_array_equal(data['arr_bool'], np.array([True,True,False]))
Expand Down Expand Up @@ -183,7 +190,7 @@ def test_file1_withattr(self):
data = d.data

assert len(d)==3
assert len(data)==29
assert len(data)==EXPECTED_VARS_FILE1
assert data.arr_two_three.shape==(3,2)
np.testing.assert_allclose(d.secondvar, [1,2,3,4])
np.testing.assert_array_equal(data.arr_bool, np.array([True,True,False]))
Expand Down Expand Up @@ -358,7 +365,7 @@ def test_load_specific_vars(self):

data = mat73.loadmat(self.testfile1, only_include=['data', 'keys'])
assert len(data)==2
assert len(data['data'])==29
assert len(data['data'])==EXPECTED_VARS_FILE1
assert len(data['data']['cell_'])==7

# check if loading times are faster, should be the case.
Expand Down
Binary file modified tests/testfile1.mat
Binary file not shown.

0 comments on commit c7dc58c

Please sign in to comment.