Skip to content

Commit

Permalink
fix complex number loading
Browse files Browse the repository at this point in the history
  • Loading branch information
skjerns committed Apr 30, 2020
1 parent 3c6a073 commit c2428c0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion mat73/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ def convert_mat(self, dataset):
return None
# complex numbers need to be filtered out separately
elif 'imag' in str(dataset.dtype):
return np.array(dataset, np.complex).T.squeeze()
if dataset.attrs['MATLAB_class']==b'single':
dtype = np.complex64
else:
dtype = np.complex128
arr = np.array(dataset)
arr = (arr['real'] + arr['imag']*1j).astype(dtype)
return arr.T.squeeze()
# if it is none of the above, we can convert to numpy array
elif mtype in ('double', 'single', 'int8', 'int16', 'int32', 'int64',
'uint8', 'uint16', 'uint32', 'uint64'):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

setuptools.setup(
name='mat73',
version='0.39',
version='0.40',
author="skjerns",
author_email="[email protected]",
description="Load MATLAB .mat 7.3 into Python native data types",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/skjerns/mat7.3",
download_url="https://github.com/skjerns/mat7.3/archive/v.039.tar.gz",
download_url="https://github.com/skjerns/mat7.3/archive/v.040.tar.gz",
install_requires=['h5py', 'numpy'],
license='GNU 2.0',
packages=['mat73'],
Expand Down
14 changes: 13 additions & 1 deletion tests/test_mat73.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,26 @@ def test_file1(self):
np.testing.assert_array_equal(data['struct_']['test'],[1,2,3,4])
assert len(data['struct2_'])==3


def test_file2(self):
d = mat73.loadmat('testfile2.mat')
raw1 = d['raw1']
assert raw1.label == ['']*5
assert raw1.speakerType == ['main']*5
np.testing.assert_array_equal(raw1.channel,[1,2,3,4,5])
np.testing.assert_allclose(raw1.measGain,[-1.0160217,-0.70729065,-1.2158508,0.68839645,2.464653])
for i in range(5):
assert np.isclose(np.sum(raw1.h[i]),-0.0007341067459898744)

np.testing.assert_array_almost_equal(raw1.HSmooth[0][2], [ 0.001139-4.233492e-04j, 0.00068 +8.927040e-06j,
0.002382-7.647651e-04j, -0.012677+3.767829e-03j])

def test_file3(self):
d = mat73.loadmat('testfile3.mat')
raw1 = d['raw1']
assert raw1.label == ['']*5
assert raw1.speakerType == ['main']*5
np.testing.assert_array_equal(raw1.channel,[1,2,3,4,5])
np.testing.assert_allclose(raw1.measGain,[-1.0160217,-0.70729065,-1.2158508,0.68839645,2.464653])
for i in range(5):
assert np.isclose(np.sum(raw1.h[i]),-0.019355850366449)
for i in range(5):
Expand Down
Binary file modified tests/testfile2.mat
Binary file not shown.
Binary file added tests/testfile3.mat
Binary file not shown.

0 comments on commit c2428c0

Please sign in to comment.