From fa8968d42097b4862e8454b199526530721b7800 Mon Sep 17 00:00:00 2001 From: Ion Iturbe Date: Wed, 14 Aug 2019 19:02:03 +0200 Subject: [PATCH 1/7] Able to load complex data arrays. --- mat4py/loadmat.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mat4py/loadmat.py b/mat4py/loadmat.py index c98fd9a..d4a31a7 100644 --- a/mat4py/loadmat.py +++ b/mat4py/loadmat.py @@ -266,10 +266,16 @@ def read_numeric_array(fd, endian, header, data_etypes): """Read a numeric matrix. Returns an array with rows of the numeric matrix. """ - if header['is_complex']: - raise ParseError('Complex arrays are not supported') # read array data (stored as column-major) - data = read_elements(fd, endian, data_etypes) + if(header["is_complex"]): + realData = read_elements(fd, endian, data_etypes) + imagData = read_elements(fd, endian, data_etypes) + data = list() + for dataIndex in range(0,len(realData)): + data.append(complex(realData[dataIndex],imagData[dataIndex])) + data = tuple(data) + else: + data = read_elements(fd, endian, data_etypes) if not isinstance(data, Sequence): # not an array, just a value return data From 37f8c572bed8010dfe1aacfcac95be93e5c86ca8 Mon Sep 17 00:00:00 2001 From: Ion Iturbe Date: Fri, 16 Aug 2019 08:49:21 +0200 Subject: [PATCH 2/7] It passes the loaddata tests, but not the savedata tests. --- data/test_data.json | 1 + tests.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/data/test_data.json b/data/test_data.json index 85f3165..c1cba6d 100644 --- a/data/test_data.json +++ b/data/test_data.json @@ -1,5 +1,6 @@ { "loadmat": { + "complex_array.mat": {"a": [[{"__complex__":[0,0]},{"__complex__":[0,1]}],[{"__complex__":[1,0]},{"__complex__":[0,2]}]]}, "cell_array.mat": {"c": [["big", "little"], [1, 2, 3]]}, "char_array.mat": {"a": ["123", "456"]}, "struct_array.mat": {"s": {"color": ["red", "red"], "x": [3, 4], "type": ["big", "little"]}}, diff --git a/tests.py b/tests.py index e95ff0f..11592bc 100644 --- a/tests.py +++ b/tests.py @@ -5,13 +5,12 @@ import unittest2 as unittest else: import unittest -import json +from json_tricks import dumps, load # https://json-tricks.readthedocs.io/en/latest/ import os import mat4py - -test_data = json.load(open('data/test_data.json')) +test_data = load(open('data/test_data.json')) class TestSequenceFunctions(unittest.TestCase): @@ -59,4 +58,6 @@ def test_save_load_mat2(self): if __name__ == '__main__': + # a = (complex(0,1),complex(2,0)) + # print(dumps(a)) unittest.main() From 3f498cd9d718d8cbc3ebef9b50a52d786baefd6f Mon Sep 17 00:00:00 2001 From: Ion Iturbe Date: Fri, 16 Aug 2019 09:09:27 +0200 Subject: [PATCH 3/7] It passes loading tests but not saving tests as saving data is not implemented. --- data/complex_array.mat | Bin 0 -> 184 bytes data/test_data.json | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 data/complex_array.mat diff --git a/data/complex_array.mat b/data/complex_array.mat new file mode 100644 index 0000000000000000000000000000000000000000..0a9d34edf26be2a871f7e16af99fc324ae501d81 GIT binary patch literal 184 zcmeZu4DoSvQZUssQ1EpO(M`+DN!3vZ$Vn_o%P-2cQgHY2i*PhE(NS}aZCnS7eNHSA+ q#&A)Ai6dfRO`1X)Q<9s4Ps=99EJ=x(2VOm>VN=_{!C*3tkpTeL%q&L$ literal 0 HcmV?d00001 diff --git a/data/test_data.json b/data/test_data.json index c1cba6d..34e81df 100644 --- a/data/test_data.json +++ b/data/test_data.json @@ -1,6 +1,6 @@ { "loadmat": { - "complex_array.mat": {"a": [[{"__complex__":[0,0]},{"__complex__":[0,1]}],[{"__complex__":[1,0]},{"__complex__":[0,2]}]]}, + "complex_array.mat": {"a": [[{"__complex__":[0,0]},{"__complex__":[0,1]}],[{"__complex__":[1,0]},{"__complex__":[0,2]}],[{"__complex__":[2,2]},{"__complex__":[0,0]}]]}, "cell_array.mat": {"c": [["big", "little"], [1, 2, 3]]}, "char_array.mat": {"a": ["123", "456"]}, "struct_array.mat": {"s": {"color": ["red", "red"], "x": [3, 4], "type": ["big", "little"]}}, From a2fbccdb76ef7b6b613016aae8ae690ed4c185d2 Mon Sep 17 00:00:00 2001 From: Ion Iturbe Date: Fri, 16 Aug 2019 11:10:53 +0200 Subject: [PATCH 4/7] It saves complex value arrays and passes all the tests. The code about the flags introducing at the headers of variables is not clear. Is much clearer when reading header of variables (in loadmat function), and something similar should be done for code clarity. --- mat4py/savemat.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/mat4py/savemat.py b/mat4py/savemat.py index 9448223..bc0bfc8 100644 --- a/mat4py/savemat.py +++ b/mat4py/savemat.py @@ -172,8 +172,13 @@ def write_var_header(fd, header): # write tag bytes, # and array flags + class and nzmax (null bytes) + if(not ('is_complex' in header.keys())): + header['is_complex'] = False fd.write(struct.pack('b3xI', etypes['miUINT32']['n'], 8)) - fd.write(struct.pack('b3x4x', mclasses[header['mclass']])) + if(header['is_complex']): + fd.write(struct.pack('bb2x4x', mclasses[header['mclass']],8)) + else: + fd.write(struct.pack('b3x4x', mclasses[header['mclass']])) # write dimensions array write_elements(fd, 'miINT32', header['dims']) @@ -217,7 +222,19 @@ def write_numeric_array(fd, header, array): array = list(chain.from_iterable(izip(*array))) # write matrix data to memory file - write_elements(bd, header['mtp'], array) + if(not ('is_complex' in header.keys())): + header['is_complex'] = False + + if(header['is_complex']): + arrayReal = list() + arrayImag = list() + for valueIndex in range(0,len(array)): + arrayReal.append(array[valueIndex].real) + arrayImag.append(array[valueIndex].imag) + write_elements(bd, header['mtp'], arrayReal) + write_elements(bd, header['mtp'], arrayImag) + else: + write_elements(bd, header['mtp'], array) # write the variable to disk file data = bd.getvalue() @@ -384,6 +401,13 @@ def guess_header(array, name=''): 'mclass': 'mxDOUBLE_CLASS', 'mtp': 'miDOUBLE', 'dims': (1, len(array))}) + elif isarray(array, lambda i: isinstance(i, complex), 1): + # 1D double array + header.update({ + 'mclass': 'mxDOUBLE_CLASS', 'mtp': 'miDOUBLE', + 'dims': (1, len(array)), + 'is_complex' : True}) + elif (isarray(array, lambda i: isinstance(i, Sequence), 1) and any(diff(len(s) for s in array))): # sequence of unequal length, assume cell array @@ -420,6 +444,14 @@ def guess_header(array, name=''): 'mtp': 'miDOUBLE', 'dims': (len(array), len(array[0]))}) + elif isarray(array, lambda i: isinstance(i, complex)): + # 2D double array + header.update({ + 'mclass': 'mxDOUBLE_CLASS', + 'mtp': 'miDOUBLE', + 'is_complex' : True, + 'dims': (len(array), len(array[0]))}) + elif isarray(array, lambda i: isinstance( i, (int, float, basestring, Sequence, Mapping))): # mixed contents, make it a cell array From 0a4372d4e4462f2e6add00b905c3e7346440c2d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1igo=20Etxaniz?= Date: Thu, 28 Nov 2019 11:40:51 +0100 Subject: [PATCH 5/7] __cache__ folder ignored --- mat4py/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 mat4py/.gitignore diff --git a/mat4py/.gitignore b/mat4py/.gitignore new file mode 100644 index 0000000..a348e50 --- /dev/null +++ b/mat4py/.gitignore @@ -0,0 +1 @@ +/__pycache__/ From a4e283aaac3845479018748dfa5ca49915bdc9bd Mon Sep 17 00:00:00 2001 From: iiturbe Date: Wed, 18 Dec 2019 09:41:19 +0100 Subject: [PATCH 6/7] ignore cached files --- mat4py/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 mat4py/.gitignore diff --git a/mat4py/.gitignore b/mat4py/.gitignore new file mode 100644 index 0000000..a348e50 --- /dev/null +++ b/mat4py/.gitignore @@ -0,0 +1 @@ +/__pycache__/ From 6ca9a9f967a0596efbecb6a31d19175745ee8cc9 Mon Sep 17 00:00:00 2001 From: Ion Iturbe Date: Tue, 4 Oct 2022 12:09:57 +0200 Subject: [PATCH 7/7] Hutsunea soilik kendu --- tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests.py b/tests.py index e95ff0f..3bcf89b 100644 --- a/tests.py +++ b/tests.py @@ -1,4 +1,3 @@ - import sys if sys.version_info[0] == 2: # unittest2 required with python2 for subTest() functionality