Skip to content

Commit

Permalink
BUG: Store numeric and character archives transposed for MATLAB compa…
Browse files Browse the repository at this point in the history
…tibility. (#39)
  • Loading branch information
cfarrow committed Sep 21, 2017
1 parent cbf70c5 commit ee1381a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
13 changes: 8 additions & 5 deletions scripts/make_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sdafile import SDAFile


EXAMPLE_A1 = np.zeros(5, dtype=np.float64)
EXAMPLE_A1 = np.zeros((5, 1), dtype=np.float64)

EXAMPLE_A2 = np.empty((4, 3), dtype=np.complex128)
EXAMPLE_A2.real = 0
Expand All @@ -16,6 +16,10 @@

EXAMPLE_A4 = np.nan

EXAMPLE_B = True

EXAMPLE_C = 'Here is some text'


def make_example_data(filename):

Expand All @@ -27,12 +31,11 @@ def make_example_data(filename):

sda_file.insert("example A3", EXAMPLE_A3, "5x5 sparse matrix")

sda_file.insert("example A4", np.nan, "Empty array")
sda_file.insert("example A4", EXAMPLE_A4, "Empty array")

sda_file.insert("example B", True, "Logical scalar")
sda_file.insert("example B", EXAMPLE_B, "Logical scalar")

data = np.array(list('Here is some text'), 'S1').reshape(-1, 1)
sda_file.insert("example C", data, "Some text")
sda_file.insert("example C", EXAMPLE_C, "Some text")

desc = "Cell array combining examples A1 and A2"
sda_file.insert("example E", [EXAMPLE_A1, EXAMPLE_A2], desc)
Expand Down
5 changes: 4 additions & 1 deletion sdafile/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def extract_character(data):
'S1' dtype.
"""
# Matlab stores the transpose of 2D arrays. This must be unapplied here.
data = data.T
if data.ndim == 2 and data.shape[0] == 1:
data = data.tobytes().decode('ascii')
else:
Expand Down Expand Up @@ -163,7 +165,8 @@ def extract_numeric(data):
The input data
"""
return reduce_array(data)
# Matlab stores the transpose of 2D arrays. This must be unapplied here.
return reduce_array(data.T)


def extract_sparse(data):
Expand Down
6 changes: 3 additions & 3 deletions sdafile/tests/test_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sdafile.testing import data_path


EXAMPLE_A1 = np.zeros(5, dtype=np.float64)
EXAMPLE_A1 = np.zeros((5, 1), dtype=np.float64)

EXAMPLE_A2 = np.empty((4, 3), dtype=np.complex128)
EXAMPLE_A2.real = 0
Expand Down Expand Up @@ -71,8 +71,8 @@ def test_example_B(self):
def test_example_C(self):
""" Some text """
label = 'example C'
expected = np.array(list('Here is some text'), 'S1').reshape(-1, 1)
self.assertArray(label, expected)
expected = 'Here is some text'
self.assertScalar(label, expected)

def test_example_D(self):
""" Handle to the sine function """
Expand Down
13 changes: 6 additions & 7 deletions sdafile/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def test_unnest(self):
def test_coerce_character(self):
coerced = coerce_character(string.printable)
self.assertEqual(coerced.dtype, np.dtype(np.uint8))
expected = np.array([[ord(c) for c in string.printable]], np.uint8)
expected = np.array(
[ord(c) for c in string.printable],
np.uint8
).reshape(-1, 1)
assert_array_equal(coerced, expected)

def test_coerce_simple(self):
Expand Down Expand Up @@ -119,14 +122,10 @@ def test_coerce_logical(self):
assert_array_equal(coerced, [[1, 0, 1, 1]])

def test_coerce_numeric(self):
for data, typ in TEST_SCALARS:
for data, typ in TEST_SCALARS + TEST_ARRAYS:
if typ == 'numeric':
self.assertEqual(coerce_numeric(data), data)

for data, typ in TEST_ARRAYS:
if typ == 'numeric' and isinstance(data, np.ndarray):
coerced = coerce_numeric(data)
assert_array_equal(coerced, np.atleast_2d(data))
assert_array_equal(coerced, np.atleast_2d(data).T)

def test_coerce_sparse(self):
row = np.array([3, 4, 5, 6])
Expand Down
7 changes: 5 additions & 2 deletions sdafile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def coerce_character(data):
data = data.view(np.uint8)
else:
data = np.frombuffer(data.encode('ascii'), np.uint8)
return np.atleast_2d(data)

# Matlab stores the transpose of 2D arrays. This must be applied here.
return np.atleast_2d(data).T


def coerce_complex(data):
Expand Down Expand Up @@ -214,7 +216,8 @@ def coerce_numeric(data):
The data with at least 2 dimensions
"""
return np.atleast_2d(data)
# Matlab stores the transpose of 2D arrays. This must be unapplied here.
return np.atleast_2d(data).T


def coerce_sparse(data):
Expand Down

0 comments on commit ee1381a

Please sign in to comment.