Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array compression fails for non-native byte order #1009

Closed
eslavich opened this issue Jul 21, 2021 · 1 comment · Fixed by #1010
Closed

Array compression fails for non-native byte order #1009

eslavich opened this issue Jul 21, 2021 · 1 comment · Fixed by #1010
Labels
Milestone

Comments

@eslavich
Copy link
Contributor

Array compression fails for non-native byte order, for example:

import asdf
import numpy as np

with asdf.AsdfFile() as af:
  af["array"] = np.arange(1024, dtype='>i2')
  af.write_to("test.asdf", all_array_compression='zlib')

results in this error:

ValueError: memoryview: destination format must be a native single character format prefixed with an optional '@'
@eslavich eslavich added the bug label Jul 21, 2021
@eslavich eslavich added this to the 2.8.2 milestone Jul 21, 2021
@lgarrison
Copy link
Contributor

The weird thing is that memoryview support non-native endian formats, just not casting to them:

import numpy as np
a = np.ones(10, dtype='>i2')
m = memoryview(a)
print(m.format)
m.cast(m.format)
>h

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-4c86e9d5451d> in <module>
      3 m = memoryview(a)
      4 print(m.format)
----> 5 m.cast(m.format)

ValueError: memoryview: destination format must be a native single character format prefixed with an optional '@'

So... I think the best solution is probably just to do a quick round-trip to Numpy to coerce the right byte order and shape:

a = np.ones((3,5), dtype='>i2')
m = memoryview(a)
m = memoryview(np.frombuffer(m, dtype=m.format))
print(m.format, m.shape)
>h (15,)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants