Skip to content

Commit

Permalink
Merge pull request #539 from drdavella/fix-fortran-order
Browse files Browse the repository at this point in the history
Allow fortran-order arrays to be serialized
  • Loading branch information
drdavella authored Sep 6, 2018
2 parents e4bf6ef + c7487a1 commit 6705cd6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

- Fix bug with dangling file handle when using ASDF-in-FITS. [#533]

- Fix bug that prevented fortran-order arrays from being serialized properly.
[#539]

2.0.2 (2018-07-27)
------------------

Expand Down
7 changes: 5 additions & 2 deletions asdf/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,10 @@ class Block(object):

def __init__(self, data=None, uri=None, array_storage='internal', memmap=True):
if isinstance(data, np.ndarray) and not data.flags.c_contiguous:
self._data = np.ascontiguousarray(data)
if data.flags.f_contiguous:
self._data = np.asfortranarray(data)
else:
self._data = np.ascontiguousarray(data)
else:
self._data = data
self._uri = uri
Expand Down Expand Up @@ -867,7 +870,7 @@ def _set_checksum(self, checksum):

def _calculate_checksum(self, data):
m = hashlib.new('md5')
m.update(self.data)
m.update(self.data.flatten())
return m.digest()

def validate_checksum(self):
Expand Down
4 changes: 2 additions & 2 deletions asdf/generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def write(self, content):
"""

def write_array(self, array):
_array_tofile(None, self.write, np.ascontiguousarray(array))
_array_tofile(None, self.write, array.ravel(order='A'))

def seek(self, offset, whence=0):
"""
Expand Down Expand Up @@ -755,7 +755,7 @@ def write_array(self, arr):
arr.flush()
self.fast_forward(len(arr.data))
else:
_array_tofile(self._fd, self._fd.write, np.ascontiguousarray(arr))
_array_tofile(self._fd, self._fd.write, arr.ravel(order='A'))

def can_memmap(self):
return True
Expand Down
2 changes: 1 addition & 1 deletion asdf/tags/core/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def from_tree(cls, node, ctx):
strides = node.get('strides', None)
mask = node.get('mask', None)

return cls(source, shape, dtype, offset, strides, 'C', mask, ctx)
return cls(source, shape, dtype, offset, strides, 'A', mask, ctx)

raise TypeError("Invalid ndarray description.")

Expand Down
6 changes: 6 additions & 0 deletions asdf/tags/core/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,9 @@ def test_broadcasted_array(tmpdir):
attrs = np.broadcast_arrays(np.array([10,20]), np.array(10), np.array(10))
tree = {'one': attrs[1] }#, 'two': attrs[1], 'three': attrs[2]}
helpers.assert_roundtrip_tree(tree, tmpdir)


def test_fortran_order(tmpdir):
array = np.array([[11,12,13], [21,22,23]], order='F')
tree = dict(data=array)
helpers.assert_roundtrip_tree(tree, tmpdir)

0 comments on commit 6705cd6

Please sign in to comment.