Skip to content

Commit 7a22e59

Browse files
Fix encoding/decoding of F-ordered numpy arrays
Closes #63
1 parent 2fa5f7d commit 7a22e59

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

mcbackend/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
pass
2121

2222

23-
__version__ = "0.2.3"
23+
__version__ = "0.2.4"

mcbackend/npproto/__init__.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mcbackend/npproto/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def ndarray_from_numpy(arr: numpy.ndarray) -> Ndarray:
1818
dtype=dt,
1919
data=bytes(arr.data),
2020
strides=list(arr.strides),
21+
order="CF"[arr.flags.f_contiguous],
2122
)
2223

2324

@@ -38,4 +39,12 @@ def ndarray_to_numpy(nda: Ndarray) -> numpy.ndarray:
3839
dtype=numpy.dtype(nda.dtype),
3940
strides=nda.strides,
4041
)
42+
# The code that reads the bytes(arr.data) always results in C-ordered data.
43+
# Passing `to the `np.ndarray(order=...)` call has no effect.
44+
# This is where below workaround comes into play:
45+
# It reshapes arrays that were originally in Fortran order.
46+
# F-ordered arrays that were encoded with mcbackend < 0.2.4 default
47+
# to `nda.order == ""` and there is nothing we can do for these.
48+
if nda.order == "F":
49+
arr = arr.T.reshape(arr.shape)
4150
return arr

mcbackend/test_npproto.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ def test_conversion(self, arr: numpy.ndarray):
3131
result = utils.ndarray_to_numpy(dec)
3232
numpy.testing.assert_array_equal(result, arr)
3333
pass
34+
35+
@pytest.mark.parametrize("shape", [(5,), (2, 3), (2, 3, 5), (5, 2, 1, 7)])
36+
@pytest.mark.parametrize("order", "CF")
37+
def test_byteorders(self, shape, order):
38+
arr = numpy.arange(numpy.prod(shape)).reshape(shape, order=order)
39+
40+
nda = utils.ndarray_from_numpy(arr)
41+
assert nda.order == "CF"[arr.flags.f_contiguous]
42+
43+
dec = utils.ndarray_to_numpy(nda)
44+
numpy.testing.assert_array_equal(arr, dec)
45+
pass

protobufs/npproto/ndarray.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ message ndarray {
99
string dtype = 2;
1010
repeated int64 shape = 3;
1111
repeated int64 strides = 4;
12+
string order = 5;
1213
}

0 commit comments

Comments
 (0)